#!/usr/bin/perl -w
# This script modifies an existing v3 config file based on options
# set in a buildrom config file.
# Copyright (C) 2008, Jordan crouse <jordan@cosmicpenguin.net>
# Released under the terms of the GPLv2
#
# Usage: cat v3-config | ./mkconfig.pl <buildrom-config>

use strict;

my %options;

# FIXME:  This should probably be in a seperate rules file of
# some sort.

# For every given option in the buildrom config, 
# we will either set or clear options in the v3 config.

$options{"CONFIG_USE_LZMA"}->{enabled}->{set} = 
    "CONFIG_COMPRESSION_LZMA|CONFIG_DEFAULT_COMPRESSION_LZMA";

$options{"CONFIG_USE_LZMA"}->{enabled}->{clear} = 
    "CONFIG_DEFAULT_COMPRESSION_NONE";

$options{"CONFIG_USE_LZMA"}->{disabled}->{set} = 
    "CONFIG_COMPRESSION_NRV2B|CONFIG_DEFAULT_COMPRESSION_NRV2B";
	
$options{"CONFIG_USE_LZMA"}->{disabled}->{clear} = 
    "CONFIG_DEFAULT_COMPRESSION_NONE";

sub print_enabled {
    my (@list) = @_;
    print "$_=y\n" foreach(@list);
}

sub print_disabled {
    my (@list) = @_;
    print "# $_ is not set\n" foreach(@list);
}
    
my %set;
my @filter;

($ARGV[0] eq "") && die "I couldn't find the buildrom config\n";


open(CONFIG, $ARGV[0]) || die "I couldn't open the buildrom config\n";

# Read the buildrom config and look for options we care about
while(<CONFIG>) {

    chop($_);

    # Empty line or commented out 
    next if ($_ eq "" || /^\#/);
        
    /^(.*)=(.*)/;

    foreach my $i (keys %options) {
	if ($1 eq $i) {
	    $set{$i} = 1;
	    last;
	}
    }
}

close(CONFIG);

# Construct the list of options to filter out of the incoming 
# config.

foreach(keys %options) {

    foreach my $i (split(/\|/, $options{$_}->{enabled}->{set})) {
	push @filter, $i;
    }

    # Only clear {enabled}->{clear} if the item is set in the buildrom
    # config

    if (defined($set{$_}) && $set{$_} == 1) {
	foreach my $i (split(/\|/, $options{$_}->{enabled}->{clear})) {
	    push @filter, $i;
	}
    }

    foreach my $i (split(/\|/, $options{$_}->{disabled}->{set})) {
	push @filter, $i;
    }
    
    # Only clear {disabled}->{clear} if the item is not set in the buildrom
    # config

    if (!defined($set{$_}) || $set{$_} == 0) {
	foreach my $i (split(/\|/, $options{$_}->{disabled}->{clear})) {
	    push @filter, $i;
	}
    }
}

# Read the incoming config and filter out all of the options in the filter
# list

LOOP: while(<STDIN>) {
    
    # See if this is an option that we care about
    # if so, then don't print it out 

    if (/^\# (.*) is not set\n$/) {
	next LOOP if (grep(/^$1$/, @filter));
    }
    elsif (/^(.*)=/) {	
	next LOOP if (grep(/^$1$/, @filter));
    }

    print "$_";
}

print "\n#\n# The following options were set\n";
print "# by buildrom\n#\n";

# Now all the options should be filtered out - go back through the list
# options and print each one.  Note that kconfig can get angry if not
# all the options are accounted for - so we have to go out of our way
# to print everything we cleared above, set or not.

foreach(keys %options) {
    if (defined($set{$_}) && $set{$_} == 1) {
	print_enabled(split(/\|/, $options{$_}->{enabled}->{set}));
	print_disabled(split(/\|/, $options{$_}->{enabled}->{clear}));
	print_disabled(split(/\|/, $options{$_}->{disabled}->{set}));
    } else {
	print_enabled(split(/\|/, $options{$_}->{disabled}->{set}));
	print_disabled(split(/\|/, $options{$_}->{disabled}->{clear}));
	print_disabled(split(/\|/, $options{$_}->{enabled}->{set}));
    }
}


	
	
    
	
		

	    

