#! /usr/bin/perl
#
# Copyright 2001,2003,2004,2005,2009,2010 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., 51 Franklin St, Fifth Floor, Boston,
# MA  02110-1301  USA.

use strict;
use warnings;

# 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, FULL_URL => 0, ROBOTS => 1, SOAP => 0,
			   SWISH => 0, TRAFFIC => 'low', UI_LOCALE => 'en_US');
my %requires = (SOAP => ['SOAP::Lite']);
my %featvars = (UI => '', USE_FOUNDATION => '', USE_STANDARD => '',
				MV_GETPPID_BROKEN => 0);
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.

The following settings can be changed with C<interchangeconfig>:

=over 4

=item C<DEBUG>

Whether to enable debug mode or not.

=back

=over 4

=item C<FULL_URL>

Whether to enable FullUrl configuration directive or not.

=back

=over 4

=item C<ROBOTS>

The Interchange Debian package uses a separate configuration file
C</etc/interchange/robots.cfg> for the directives C<RobotUA>, C<RobotIP>
and C<RobotHost>. It is recommended to include these settings so
Interchange can distinguish between robots and ordinary users.

=back

=over 4

=item C<SWISH>

Whether to enable Swish search or not.

=back

=over 4

=item C<SOAP>

This setting determines if Interchange starts the SOAP server as well.
This is only available if the Perl module SOAP::Lite is installed.

=back

=over 4

=item C<TRAFFIC>

You can choose different sets of server parameters (C<low>, C<high> and
C<rpc>). Any store based on the foundation demo will change its behaviour too.
If C<rpc> is selected, the Interchange server will run in PreFork mode.

=back

=head1 AUTHOR

Stefan Hornburg (Racke), racke@linuxia.de

=head1 SEE ALSO

interchange(1p)

=cut
