#! /usr/bin/perl -w
#
# Copyright 2001 by Stefan Hornburg (Racke) <racke@linuxia.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

use strict;

# constants we use
my $lock_ex = 2;
my $lock_un = 8;

# configuration files which we modify
my $featconf = '/etc/interchange/features.cfg';
my $settingsconf = '/etc/interchange/settings.cfg';

# Interchange variables
my %setvars = (DEBUG => 0, UI_LOCALE => 'en_US');
my %featvars = (UI => '', USE_FOUNDATION => '');
my %skipvars;
	
# other variables
my (@tokens, $varref);

# parse commandline
for (@ARGV) {
	unless (/\S+=/) {
		die "$0: invalid parameter '$_'\n";
	}
	@tokens = split(/=/);
	if (exists($setvars{$tokens[0]})) {
		$varref = \%setvars;
	} elsif (exists($featvars{$tokens[0]})) {
		$varref = \%featvars;
	} else {
		die "$0: no such variable $tokens[0]\n";
	}
	if (defined $tokens[1]) {
		$varref->{$tokens[0]} = $tokens[1];
	} else {
		$varref->{$tokens[0]} = '';
	}
	# don't override these settings
	$skipvars{$tokens[0]} = 1;
}

# modify features configuration
&update_configfile($featconf, 0, \%featvars);

# modify settings configuration
&update_configfile($settingsconf, 1, \%setvars);

sub update_configfile {
	my ($configfile, $modsallowed, $varref) = @_;

	if (-f $configfile) {
		open (SCONF, "+<$configfile")
			|| die ("$0: Couldn't open $configfile: $!\n");
		flock (SCONF, $lock_ex);
		while (<SCONF>) {
			# strip leading/trailing whitespace
			s/^\s+//; s/^\s+$//;
			# skip comment lines/blank lines
			next if /^\#/; next unless /\S/;
			# check for "Variable"'s
			@tokens = split (/\s+/, $_, 3);
			next if $tokens[0] ne 'Variable';
			next if exists $skipvars{$tokens[1]};
			# record values found
			if (exists $varref->{$tokens[1]}) {
				if (defined $tokens[2]) {
					$varref->{$tokens[1]} = $tokens[2];
				} else {
					$varref->{$tokens[1]} = '';
				}
			}
		}
		truncate (SCONF, 0)
			|| die ("$0: Couldn't truncate $configfile: $!\n");
		seek (SCONF, 0, 0)
			|| die ("$0: Seek failed on $configfile: $!\n");
	} else {
		open (SCONF, ">$configfile")
			|| die ("$0: Couldn't open $configfile: $!\n");
	}

	if ($modsallowed) {
		print SCONF <<EOF;
# This file is automatically generated by $0.
# You may modify this file, but this is NOT recommended.

EOF
    } else {
		print SCONF <<EOF;
# This file is automatically generated by $0.
# You may NOT modify this file, because it reflects the
# installation state of the Interchange packages.

EOF
	}

	for (sort (keys %$varref)) {
		print SCONF "Variable $_ $varref->{$_}\n";
	}
	flock (SCONF, $lock_un);
	close (SCONF)
		|| die ("$0: Couldn't close $configfile: $!\n");
}


=head1 NAME

interchangeconfig - Updates Debian specific Interchange configuration files

=head1 DESCRIPTION

interchangeconfig reads and writes the Interchange configuration files
/etc/interchange/features.cfg and /etc/interchange/settings.cfg.

=head1 AUTHOR

Stefan Hornburg (Racke), racke@linuxia.de

=head1 SEE ALSO

interchange(1p)

=cut    
