#!/usr/bin/perl -w

use Debconf::Client::ConfModule ':all';
use Debconf::Log ':all';
use Printbill::PTDB_File;

version ('2.0');

my (@ret, @ret1, @ret2);

if ($ARGV[0] eq "configure") {
# If we have an obsolete printbillrc... the user must fix this him/herself.

	if (-f "/etc/printbill/printbillrc") {
		$out = `/bin/grep definedprinter /etc/printbill/printbillrc`;

		if ($out =~ "definedprinter") {
			rename "/etc/printbill/printbillrc", "/etc/printbill/printbillrc.bak"
		}
	}

# Now we are ready.

	tie %confighash, "Printbill::PTDB_File", "/etc/printbill/printbillrc", "FALSE";

# Remove any obsolete "def_" entries - these will be replaced by debconf's
# supplied values anyway.
	
	foreach (keys %confighash) {
		if ($_ =~ /^def_.*$/) {
			delete $confighash{$_};
		}
	}

	@ret = get ("printbill/def_price_per_page");
	
	$confighash{'price_per_page'} = $ret [1];
	
	@ret = get ("printbill/def_colourspace");
	
	$confighash{'colourspace'} = $ret [1];
	
	if ($confighash{'colourspace'} eq "mono" || $confighash{'colourspace'} eq "cmyk") {
		@ret1 = get ("printbill/black_cartridge_cost");
		@ret2 = get ("printbill/black_cartridge_quoted_pages");
		$confighash{'estimated_total_percent_black'} = $ret2 [1] * 5;
		$confighash{'price_per_percent_black'} = $ret1 [1] / $confighash{'estimated_total_percent_black'};
	}
	
	if ($confighash{'colourspace'} eq "cmyk" || $confighash{'colourspace'} eq "cmy") {
		@ret1 = get ("printbill/colour_cartridge_cost");
		@ret2 = get ("printbill/colour_cartridge_quoted_pages");
		$confighash{'estimated_total_percent_colour'} = $ret2 [1] * 5;
		$confighash{'price_per_percent_colour'} = $ret1 [1] / $confighash{'estimated_total_percent_colour'};
	}
	
	@ret = get ("printbill/response_method");
	
	$confighash{'response_method'} = $ret [1];
	
	@ret = get ("printbill/dpi");
	
	$confighash{'dpi'} = $ret [1];
	
	@ret = get ("printbill/filter");
	
	$confighash{'filter'} = $ret [1];
	
	@ret = get ("printbill/admin_mail");
	
	$confighash{'admin_mail'} = $ret [1];
	
	@ret = get ("printbill/verbosity");
	
	$confighash{'verbosity'} = $ret [1];
	
	@ret = get ("printbill/bill_max_processes");
	
	$confighash{'bill_max_processes'} = $ret [1];
	
	@ret = get ("printbill/currency_symbol");
	
	$confighash{'currency_symbol'} = $ret [1];
	
	@ret = get ("printbill/stats_path");
	
	if ($ret [1] ne "") {
		$confighash{'stats_path'} = $ret [1];
	}
	
	@ret = get ("printbill/financelog");
	
	if ($ret [1] ne "") {
		$confighash{'financelog'} = $ret [1];
	}
	
# Standard things on Debian. If there are missing entries, or if the config
# file does not exist, we need to fill this stuff in.

	$confighash{'db_home'} = "/var/lib/printbill" if (!defined $confighash{'db_home'});
	$confighash{'prog_path'} = "/usr/bin" if (!defined $confighash{'prog_path'});
	$confighash{'admin_prog_path'} = "/usr/sbin" if (!defined $confighash{'admin_prog_path'});
	$confighash{'gs'} = "/usr/bin/gs" if (!defined $confighash{'gs'});
	$confighash{'lpr'} = "/usr/bin/lpr" if (!defined $confighash{'lpr'});
	$confighash{'mta'} = "/usr/sbin/sendmail" if (!defined $confighash{'mta'});
	$confighash{'retry_interval'} = "1" if (!defined $confighash{'retry_interval'});
	$confighash{'printbilld_user'} = "daemon" if (!defined $confighash{'printbilld_user'});
	$confighash{'printbilld_group'} = "printbil" if (!defined $confighash{'printbilld_group'});
	$confighash{'web_user'} = "www-data" if (!defined $confighash{'web_user'});
	$confighash{'printcap'} = "/etc/printcap" if (!defined $confighash{'printcap'});
	$confighash{'smbclient'} = "/usr/bin/smbclient" if (!defined $confighash{'smbclient'});
	$confighash{'gnuplot'} = "/usr/bin/gnuplot" if (!defined $confighash{'gnuplot'});
	$confighash{'nice'} = "/usr/bin/nice" if (!defined $confighash{'nice'});
	$confighash{'bill_nicethreshold'} = "1000000" if (!defined $confighash{'bill_nicethreshold'});
	$confighash{'small_bill_niceness'} = "0" if (!defined $confighash{'small_bill_niceness'});
	$confighash{'large_bill_niceness'} = "9" if (!defined $confighash{'large_bill_niceness'});
	$confighash{'quote_generation_niceness'} = "19" if (!defined $confighash{'quote_generation_niceness'});
	$confighash{'cookieroot'} = "/var/lib/printbill/cookies" if (!defined $confighash{'cookieroot'});
	$confighash{'admin_cgi'} = "/cgi-bin/webpqadmin.pl" if (!defined $confighash{'admin_cgi'});
	$confighash{'check_cgi'} = "/cgi-bin/webpqcheck.pl" if (!defined $confighash{'check_cgi'});
	$confighash{'web_header'} = "/etc/printbill/web_header" if (!defined $confighash{'web_header'});
	$confighash{'web_footer'} = "/etc/printbill/web_footer" if (!defined $confighash{'web_footer'});
	$confighash{'web_message_text'} = "/etc/printbill/web_message_text" if (!defined $confighash{'web_message_text'});
	$confighash{'extra_quota_message'} = "/etc/printbill/extra_quota_message" if (!defined $confighash{'extra_quota_message'});
		
# Sort out group membership - if the group does not exist, create it...
	
	my $members = (getgrnam $confighash{'printbilld_group'})[3];
	
	if (!defined $members) {
		system "groupadd $confighash{'printbilld_group'}";
	}

# Make sure www-data and daemon are members of the group, if appropriate.

	@ret = get ("printbill/use_admin_web_interface");

	if (!defined $members || ($members !~ "www-data" && $ret [1] eq "true")) {
		system "adduser www-data $confighash{'printbilld_group'}"
	}

	if (!defined $members || $members !~ "daemon") {	
		system "adduser daemon $confighash{'printbilld_group'}";
	}
	
	if ($ret [1] eq "true") {
		@ret = get ("printbill/admin_form_type");
		$confighash{'admin_form_type'} = $ret [1];
		$confighash{'graph_png_output_dir'} = "/var/www/printbill" if (!defined $confighash{'graph_png_output_dir'});
		$confighash{'png_url'} = "/printbill" if (!defined $confighash{'png_url'});

# Everything is running in the context of the web browser...

		if (! -d $confighash{'graph_png_output_dir'}) {
			system "install -d -m 0755 -o www-data -g www-data $confighash{'graph_png_output_dir'}";
		}
	} else {
		chmod 0644, "/usr/lib/cgi-bin/webpqadmin.pl";
	}
	
	@ret = get ("printbill/use_user_web_interface");

	if ($ret [1] ne "true") {
		chmod 0644, "/usr/lib/cgi-bin/webpqcheck.pl";
	}

# Make sure all the critical directories exist, with the correct permissions
# and ownership. We create the stats and cookies dirs even if they are not
# used... why not, after all.

# This is to fool lintian... in actual fact it's quite safe, we are just
# creating a tmp directory for printbilld and members of the group. It isn't
# world-read/writable.

	$td = "$confighash{'db_home'}/" . 'tmp';

	system "install -d -m 0755 -o daemon -g daemon $confighash{'db_home'}";
	system "install -d -m 0770 -o daemon -g $confighash{'printbilld_group'} $td";
	system "install -d -m 0775 -o daemon -g $confighash{'printbilld_group'} $confighash{'db_home'}/users";
	system "install -d -m 0775 -o daemon -g $confighash{'printbilld_group'} $confighash{'db_home'}/printers";
	system "install -d -m 0755 -o daemon -g daemon $confighash{'db_home'}/stats";
	system "install -d -m 0700 -o www-data -g www-data $confighash{'db_home'}/cookies";

# If there is no misc.db database, create an empty one...
	
	if (! -f "$confighash{'db_home'}/misc.db") {
		tie %mischash, "Printbill::PTDB_File", "$confighash{'db_home'}/misc.db", "FALSE";

		$mischash{'total pages'} = 0;
		$mischash{'total paid'} = 0;
		$mischash{'total spent'} = 0;

		untie %mischash;
	}

	untie %confighash;

# Needed to stop it blocking below...
	
	stop;

# This is what DEBHELPER would insert here, if it wasn't so retarded as to
# assume that all init scripts were written in sh.

	if ( -x "/etc/init.d/printbill" ) {
		system ("/usr/sbin/update-rc.d printbill defaults 20 20 >/dev/null") == 0 or die "update-rc.d failed: $!\n";

		if (-x "/usr/sbin/invoke-rc.d" ) {
			`invoke-rc.d printbill start`;
		} else {
			`/etc/init.d/printbill start`;
		}
	}

# Lintian doesn't grok perl. Consequently, we need to insert the following
# BS to allow this should-be-lintian-clean script to pass the test.

	`/bin/echo '

	update-rc.d printbill defaults 91 20 > /dev/null

	' > /dev/null`;
}
