genericize.pl revision 044539800adf94b9334ee9189188caa2d2e28afa
1#!/usr/bin/perl -w
2#-
3# Copyright (c) 2004 Dag-Erling Co�dan Sm�rgrav
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer
11#    in this position and unchanged.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. The name of the author may not be used to endorse or promote products
16#    derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# $FreeBSD$
30#
31
32use strict;
33
34MAIN:{
35    my %config;
36    my $machine;
37    my $ident;
38
39    while (<>) {
40	chomp();
41	s/\s*(\#.*)?$//;
42	next unless $_;
43	#print("$_\n");
44	my ($keyword, $values) = split(' ', $_, 2);
45	foreach my $value (split(/,\s*/, $values)) {
46	    if ($keyword eq 'machine') {
47		$machine = $value;
48	    } elsif ($keyword eq 'ident') {
49		$ident = $value;
50	    } else {
51		$config{$keyword}->{$value} = 1;
52	    }
53	    #print "$keyword $value\n";
54	}
55    }
56
57    my $generic = "/usr/src/sys/$machine/conf/GENERIC";
58    local *GENERIC;
59    open(GENERIC, "<", $generic)
60	or die("$generic: $!\n");
61    my $blank = 0;
62    while (<GENERIC>) {
63	my $line = $_;
64	chomp();
65	++$blank unless $_;
66	s/\s*(\#.*)?$//;
67	next unless $_;
68	my ($keyword, $value) = split(' ', $_);
69	if ($keyword eq 'machine') {
70	    die("$generic is for $value, not $machine\n")
71		unless ($value eq $machine);
72	    if ($blank) {
73		print "\n";
74		$blank = 0;
75	    }
76	} elsif ($keyword eq 'ident') {
77	    $line =~ s/$value/$ident/;
78	} elsif ($config{$keyword}->{$value}) {
79	    delete($config{$keyword}->{$value});
80	    delete($config{$keyword})
81		unless %{$config{$keyword}};
82	} else {
83	    next;
84	}
85	if ($blank) {
86	    print "\n";
87	    $blank = 0;
88	}
89	print $line;
90    }
91    close(GENERIC);
92
93    if (%config) {
94	print "\n# Addenda\n";
95	foreach my $keyword (sort(keys(%config))) {
96	    foreach my $value (sort(keys(%{$config{$keyword}}))) {
97		print "$keyword";
98		if (length($keyword) < 7) {
99		    print "\t";
100		} elsif (length($keyword) == 7) {
101		    print " ";
102		}
103		print "\t$value\n";
104	    }
105	}
106    }
107    exit(0);
108}
109