#!/usr/bin/perl -w
#
# Author: Petter Reinholdtsen <pere@hungry.com>
# Date:   2002-04-23
#
# Insert new default values into the debconf database

# Check out autoinst-read-debconf from the autoinstall package.

use vars qw(%opts $filename $debug $error $checkonly);

sub usage {
    print <<EOF
Usage: debconf-load-defaults [-vc] <file(s)>
  -v   verbose output
  -c   only check the input file format
EOF
}

sub report_bug {
    print STDERR "error: Please report the bug and press enter to continue\n";
    sleep 3;
}

sub info {
    my ($msg) = @_;
    print STDERR "info: $msg\n" if $debug;
}

sub error {
    my ($msg) = @_;
    print STDERR "error: $msg\n";
    $error++
}

sub load_answer {
    my ($label, $type, $content) = @_;
    info "Loading answer for '$label'";

    # Avoid inserting dummy template if the question is already loaded
    if ( ! $Debconf::Db::config->exists($label) ) {
	$template = "
Template: $label
Type: $type
Default: $content
Description: Dummy template for '$label' to set the answer to '$content'.
 This is not the real content, but we fake it to get the answer into the
 database.  The template name is '$label', and the type is '$type'.
 The value set for this template is '$content'.
";
	my $tmpfile = "/tmp/template.input";
	open(TMPFILE, ">$tmpfile") || die "Unable to write to $tmpfile";
	print TMPFILE $template;
	close TMPFILE;

	Debconf::Template->load($tmpfile, "debian-edu-install") ||
	    error "Unable to load template";
	unlink $tmpfile || error "Unable to unlink tempfile";
    } else {
	info "Template for '$label' already exists, not loading dummy.";
    }
    $Debconf::Db::config->setfield($label, 'value', $content)
	|| error "Unable to set '$label' value to '$content' in DB";

    $Debconf::Db::config->setflag($label, 'seen', "true")
	|| error "Unable to set seen flag for '$label' in DB";

    # Check that the content is still present
    if ( $content ne $Debconf::Db::config->getfield($label, 'value') ) {
	error "Failed to set '$label' to '$content'.";
    }
    if ( "true" ne $Debconf::Db::config->getflag($label, 'seen') ) {
	error "Failed to set seen-flag for '$label'.";
    }
}

my @knowntypes = qw(select boolean string multiselect note);

sub ok_format {
    my ($label, $type, $content) = @_;
    if ( ! defined $label || ! defined $content
	 || ! grep /$type/, @knowntypes ) {
	return ""; # false
    }
    return 1; # true
}

# Handle options if the perl-modules package is installed
eval "use Getopt::Std;";
if ( $@ ) {
    # Direct printing, as $debug will be unset with Getopt::Std
    print STDERR "info: Unable to use Getopt::Std.  Ignoring options.\n"
} else {
    getopts("vc", \%opts);
}

$debug     = $opts{'v'} || "";
$checkonly = $opts{'c'} || "";

if ( !$checkonly ) {
    eval "use Debconf::Db; use Debconf::Template;";
    Debconf::Db->load();
}

$error = 0;

for $filename (@ARGV) {
    info "Loading defaults from $filename\n";
    if (open(DEFAULTS, "$filename")) {
	# Load default values

	my $line = 0;
	while (<DEFAULTS>) {
	    chomp;
	    $line++;
	    s/\#.+$//;
	    next if /^\s*$/;
	    my ($label, $type, $content) = split(/\s/, $_, 3);

	    info "Trying to set '$label' [$type] to '$content'";

	    $content = "" if ("[undef]" eq $content);

	    if ( $checkonly ) {
		if ( ! ok_format($label, $type, $content ) ) {
		    error "'$filename' has wrong format in line $line: '$_'";
		}
	    } else {
		load_answer($label, $type, $content);
	    }
	}
    } else {
	error "Unable to read defaults from $filename";
    }
}

if ( ! $checkonly ) {
    Debconf::Db->save();
}

if ($error) {
    report_bug
    exit 1;
}
