Pablo Greco ceb317
#! /usr/bin/perl
Pablo Greco ceb317
Pablo Greco ceb317
my @args=@ARGV;
Pablo Greco ceb317
my %configvalues;
Pablo Greco ceb317
my @configoptions;
Pablo Greco ceb317
my $configcounter = 0;
Pablo Greco ceb317
Pablo Greco ceb317
# optionally print out the architecture as the first line of our output
Pablo Greco ceb317
my $arch = $args[2];
Pablo Greco ceb317
if (defined $arch) {
Pablo Greco ceb317
	print "# $arch\n";
Pablo Greco ceb317
}
Pablo Greco ceb317
Pablo Greco ceb317
# first, read the override file
Pablo Greco ceb317
Pablo Greco ceb317
open (FILE,"$args[0]") || die "Could not open $args[0]";
Pablo Greco ceb317
while (<FILE>) {
Pablo Greco ceb317
	my $str = $_;
Pablo Greco ceb317
	my $configname;
Pablo Greco ceb317
Pablo Greco ceb317
	if (/\# ([\w]+) is not set/) {
Pablo Greco ceb317
		$configname = $1;
Pablo Greco ceb317
	} elsif (/^\#/) {
Pablo Greco ceb317
		# fall through on comments like 'avoid CONFIG_FOO=y'
Pablo Greco ceb317
		;
Pablo Greco ceb317
	} elsif (/([\w]+)=/) {
Pablo Greco ceb317
		$configname = $1;
Pablo Greco ceb317
	}
Pablo Greco ceb317
Pablo Greco ceb317
	if (defined($configname) && !exists($configvalues{$configname})) {
Pablo Greco ceb317
		$configvalues{$configname} = $str;
Pablo Greco ceb317
		$configoptions[$configcounter] = $configname;
Pablo Greco ceb317
		$configcounter ++;
Pablo Greco ceb317
	}
Pablo Greco ceb317
};
Pablo Greco ceb317
Pablo Greco ceb317
# now, read and output the entire configfile, except for the overridden
Pablo Greco ceb317
# parts... for those the new value is printed.
Pablo Greco ceb317
Pablo Greco ceb317
open (FILE2,"$args[1]") || die "Could not open $args[1]";
Pablo Greco ceb317
while (<FILE2>) {
Pablo Greco ceb317
	my $configname;
Pablo Greco ceb317
Pablo Greco ceb317
	if (/\# ([\w]+) is not set/) {
Pablo Greco ceb317
		$configname = $1;
Pablo Greco ceb317
	} elsif (/^\#/) {
Pablo Greco ceb317
		# fall through on comments like 'avoid CONFIG_FOO=y'
Pablo Greco ceb317
		;
Pablo Greco ceb317
	} elsif (/([\w]+)=/) {
Pablo Greco ceb317
		$configname  = $1;
Pablo Greco ceb317
	}
Pablo Greco ceb317
Pablo Greco ceb317
	if (defined($configname) && exists($configvalues{$configname})) {
Pablo Greco ceb317
		print "$configvalues{$configname}";
Pablo Greco ceb317
		delete($configvalues{$configname});
Pablo Greco ceb317
	} else {
Pablo Greco ceb317
		print "$_";
Pablo Greco ceb317
	}
Pablo Greco ceb317
}
Pablo Greco ceb317
Pablo Greco ceb317
# now print the new values from the overridden configfile
Pablo Greco ceb317
my $counter = 0;
Pablo Greco ceb317
Pablo Greco ceb317
while ($counter < $configcounter) {
Pablo Greco ceb317
	my $configname = $configoptions[$counter];
Pablo Greco ceb317
	if (exists($configvalues{$configname})) {
Pablo Greco ceb317
		print "$configvalues{$configname}";
Pablo Greco ceb317
	}
Pablo Greco ceb317
	$counter++;
Pablo Greco ceb317
}
Pablo Greco ceb317
Pablo Greco ceb317
1;