#!/usr/bin/perl

use Config::Ini;
use Debconf::Client::ConfModule ':all';
use strict;

my %autoentries = (
	'BBS|AutoStart'	 => 'synchronet/bbs_server_enable',
	'BBS|TelnetInterface'	 => 'synchronet/telnet_server_iface',
	'BBS|TelnetPort' => 'synchronet/telnet_server_port',
	'BBS|RLoginInterface'	 => 'synchronet/rlogin_server_iface',
	'BBS|RLoginPort'	 => 'synchronet/rlogin_server_port',
	'BBS|ExternalYield'	 => 'synchronet/bbs_server_externalyield',
	'BBS|HostName'	 => 'synchronet/bbs_server_hostname',
	'BBS|DOSemuPath'	 => 'synchronet/dosemu_path',
	'Mail|AutoStart'	 => 'synchronet/mail_server_enable',
	'Mail|SMTPPort'	 => 'synchronet/smtp_server_port',
	'Mail|POP3Port'	 => 'synchronet/pop3_server_port',
	'FTP|AutoStart'	 => 'synchronet/ftp_server_enable',
	'FTP|Port'	 => 'synchronet/ftp_server_port',
	'Services|AutoStart'	 => 'synchronet/services_enable',
	'Web|AutoStart' => 'synchronet/web_server_enable',
	'Web|Port' => 'synchronet/web_server_port',
);

my %multiselectentries = (
	'BBS|Options'	 => 'synchronet/bbs_server_options',
	'Web|Options'	 => 'synchronet/web_server_options',
	'FTP|Options'	 => 'synchronet/ftp_server_options',
	'Mail|Options'	 => 'synchronet/mail_server_options',
	'Services|Options'	 => 'synchronet/services_options',

);

version('2.0');
my $capb = capb('backup', 'multiselect');

if ($ARGV[0] eq 'preinst') {

	# Read the existing values into debconf so we don't wipe out the sysop's customizations
	if ( -e '/etc/sbbs.ini') {
		my $ini = new Config::Ini('/etc/sbbs.ini');
		foreach my $key (keys %autoentries) {
			my @key = split /\|/, $key;
			my $debconf = $autoentries{$key};
			my $value = $ini->get([$key[0], $key[1]]);
			if ($value) { set($debconf, $value); }
		}

		foreach my $key (keys %multiselectentries) {
			my @key = split /\|/, $key;
			my $debconf = $multiselectentries{$key};
			my $value = $ini->get([$key[0], $key[1]]);
			if ($value) {
				my @values = split /[\|\ ]+/, $value;
				$value = join (', ', @values);
				set($debconf, $value);
			}
		}
	}
	stop;
	`cp /etc/sbbs.ini /etc/sbbs.ini-bak`;
	exit 0;
}

elsif ($ARGV[0] eq 'postinst') {

	# save any non-debconf values that we use
	my $ini = new Config::Ini('/etc/sbbs.ini');
	my $logfacility = $ini->get(['UNIX', 'LogFacility']);

	$ini = new Config::Ini('/usr/share/synchronet/sbbs_ini_template');
	foreach my $key (keys %autoentries) {
		my @key = split /\|/, $key;
		my $value = get($autoentries{$key});
		if ($value) { $ini->put([$key[0], $key[1], $value]); }
	}

	foreach my $key (keys %multiselectentries) {
		my @key = split /\|/, $key;
		my $value = get($multiselectentries{$key});
		if ($value && $value !~ /.*doesn't exist.*/) {
#			print STDERR $value;
			my @values = split /[,\ ]+/, $value;
			$value = join (' | ', @values);
#			print STDERR $value;
			$ini->put([$key[0], $key[1], $value]);
		}
	}

	stop;

	$ini->put(['UNIX', 'LogFacility', $logfacility]);
	$ini->put(['UNIX', 'Daemonize', 'true']);
	$ini->save('/etc/sbbs.ini');
	exit 0;
}

elsif ($ARGV[0] eq 'setlog') {

	my $facility = $ARGV[1];
	my $ini = new Config::Ini('/etc/sbbs.ini');
	
	$ini->put(['UNIX', 'LogFacility', $facility]);
	$ini->put(['UNIX', 'Daemonize', 'true']);
	$ini->save('/etc/sbbs.ini');

	exit 0;
}

exit 1;

