#!/usr/bin/perl
# Postinst for pdbv

use Debconf::Client::ConfModule ':all';
version('2.0');

# Files
my $conffile = "/etc/pdbv/pdbvrc";
my $tmpfile = "/etc/pdbv/pdbvrc2.tmp";

# Settings
my @settings = ("listing", "output_dir", "cron", "cron_lang", "force");

# Read conffile and replace old values with old ones
open(OUT, ">$tmpfile");
open(IN, "<$conffile");
while (<IN>) {
    my $found;
    foreach my $setting (@settings) {
	if (/^(.*)$setting\=\"(.*)\"\;/) {
	    $$setting = get("pdbv/$setting");
	    if ($setting eq 'force') {
		if ($$setting eq 'true') { $$setting = "1"; } else { $$setting = "0"; }
	    }
	    print OUT "\$$setting=\"$$setting\"\;\n";
	    $found = 1;
	}
    }
    next if $found;
    print OUT $_;
}
close(OUT);
close(IN);

`mv -f $tmpfile $conffile`;

# Make sure that no old crontab file remains
`rm -f /etc/cron.d/pdbv2* /etc/cron.daily/pdbv`;


# Create output dir. Nobody as no write access in /var/www usually
`mkdir -p $output_dir`;
`chown nobody $output_dir`;


exit;
