#!/usr/bin/perl
# This file was preprocessed, do not edit!


use warnings;
use strict;
use Debconf::Db;
use Debconf::Template;
use Debconf::AutoSelect qw(:all);
use Debconf::Log qw(:all);

Debconf::Db->load;

debug developer => "frontend started";

my $frontend=make_frontend();

shift @ARGV if $ARGV[0] eq '--';

my $package;
my $no_title=0;
my $root=$ENV{DPKG_ROOT} // '';
if ($ENV{DEBCONF_PACKAGE}) {
	$package=$ENV{DEBCONF_PACKAGE};
}
elsif ($ARGV[0]=~m!^.*/(.*?)\.(?:postinst|postrm|prerm)$!) {
	$package=$1;
	my $action=$ARGV[1];
	$no_title=1 if $action eq 'triggered';
}
elsif (-e "$root/var/lib/dpkg/tmp.ci/control") {
	open (my $control, "<", "$root/var/lib/dpkg/tmp.ci/control")
		|| die "Debconf: unable to open control file: $!";
	while (<$control>) {
		if (/^Package: (.*)/) {
			$package=$1;
			last;
		}
	}
	close $control;
	if (! exists $ENV{PERL_DL_NONLAZY} || ! $ENV{PERL_DL_NONLAZY}) {
		warn "PERL_DL_NONLAZY is not set, if debconf is running from a preinst script, this is not safe";
	}
}
else {
	$package='';

	debug developer => 'Trying to find a templates file..';
	sub trytemplate {
		my $fn=shift;
		debug developer => "Trying $fn";
		if (-e $fn) {
			debug developer => "I guess it is $fn";
			Debconf::Template->load($fn, $package);
			return 1;
		}
		else {
			return;
		}
	}

	unless (trytemplate("$ARGV[0].templates")) {
		unless ($ARGV[0]=~m/(.*)config$/ && trytemplate("${1}templates")) {
			unless ($ARGV[0]=~m!^(?:.*/)?(.*)! && trytemplate("/usr/share/debconf/templates/${1}.templates")) {
				debug developer => "Couldn't find a templates file."
			}
		}
	}
}
debug developer => "frontend running, package name is $package";
$frontend->default_title($package) if length $package and not $no_title;
$frontend->info(undef);

if ($ARGV[0] =~/^(.*[.\/])(?:preinst|postinst|prerm|postrm)$/) {
	my $base=$1;
	my $templates=$base."templates";
	Debconf::Template->load($templates, $package)
		if -e $templates;
}

if ($ARGV[0] =~/^(.*[.\/])(?:postinst|preinst)$/) {
	my $base=$1;

	my $config=$base."config";
	if (-e $config) {
		my $version=$ARGV[2];
		if (! defined($version)) {
			$version='';
		}
		my $confmodule=make_confmodule($config,
			"configure", $version);

		$confmodule->owner($package);

		1 while ($confmodule->communicate);

		exit $confmodule->exitcode if $confmodule->exitcode > 0;
	}
}

my $confmodule=make_confmodule(@ARGV);

$confmodule->owner($package);

1 while ($confmodule->communicate);

$frontend->shutdown;

Debconf::Db->save;

exit $confmodule->exitcode;

