#!/usr/bin/perl -w
# This file was preprocessed, do not edit!
BEGIN {
	eval qq{
		use strict;
		use FileHandle;
		use Debconf::Log qw(:all);
		use Debconf::Db;
		use Debconf::Template;
		use Debconf::Config;
		use Debconf::AutoSelect qw(:all);
		use Debconf::Gettext;
	};
	if ($@) {
		print STDERR "debconf: Perl may be unconfigured ($@) -- aborting\n";
		exit 0;
	}
}
Debconf::Db->load;
my $apt=0;
Debconf::Config->getopt(
qq{Usage: dpkg-preconfigure [options] [debs]
       --apt			Apt mode.},
	"apt"			=> \$apt,
);
$|=1;
my @debs=@ARGV;
@ARGV=();
if ($apt) {
	while (<>) {
		chomp;
		push @debs, $_ if length $_;
	}
	
	exit unless @debs;
	
	open (STDIN, "/dev/tty") ||
		(print STDERR sprintf("dpkg-preconfigure: ".gettext("unable to re-open stdin: %s"), $!)."\n", exit 0);
}
elsif (! @debs) {
	print STDERR sprintf("dpkg-preconfigure: ".gettext("must specify some debs to preconfigure")), "\n";
	exit(1);
}
if (! -x "/usr/bin/apt-extracttemplates") {
	warn gettext("delaying package configuration, since apt-utils is not installed");
	exit;
}
my $frontend=make_frontend();
my ($package, $version, $template, $config);
unless (open(INFO, "-|")) {
	my $command_max=20000; # LINUX SPECIFIC!!
	my $static_len=length("apt-extracttemplates");
	my $len=$static_len;
	my @collect;
	foreach my $deb (@debs) {
		$len += length($deb) + 1;
		if ($len < $command_max) {
			push @collect, $deb;
		}
		else {
			if (system("apt-extracttemplates", @collect) != 0) {
				print STDERR sprintf("debconf: ".gettext("apt-extracttemplates failed: %s"),$!);
			}
				
			@collect=($deb);
			$len=$static_len + length($deb) + 1;
		}
	}
	exec "apt-extracttemplates", @collect or
		print STDERR sprintf("debconf: ".gettext("apt-extracttemplates failed: %s"),$!);
}
my @buffer=<INFO>;
if ($apt && @buffer) {
	print gettext("Preconfiguring packages ...\n");
}
foreach my $line (@buffer) {
	($package, $version, $template, $config)=split /\s/, $line;
	
	if (defined $template && length $template) {
		eval q{
			Debconf::Template->load($template, $package)
		};
		unlink $template;
		if ($@) {
			print STDERR "$package ".sprintf(gettext("template parse error: %s"), $@)."\n";
			unlink $config;
			next;
		}
	}
}
foreach my $line (@buffer) {
	($package, $version, $template, $config)=split /\s/, $line;
	if (defined $config && length $config && -e $config) {
		debug user => sprintf("preconfiguring %s (%s)",$package,$version);
		chmod(0755, $config) or
			die sprintf(gettext("debconf: can't chmod: %s"), $!);
		$frontend->default_title($package);
		my $confmodule=make_confmodule($config, 'configure', $version);
		$confmodule->owner($package);
		1 while ($confmodule->communicate);
		if ($confmodule->exitcode > 0) {
			print STDERR sprintf(
				gettext("%s failed to preconfigure, with exit status %s"),
				$package, $confmodule->exitcode)."\n";
		}
		unlink $config;
	}
}
$frontend->shutdown;
Debconf::Db->save;
