Codebase list debconf / 37c0d637-bb1a-489e-be1c-76e88cb5800e/main frontend
37c0d637-bb1a-489e-be1c-76e88cb5800e/main

Tree @37c0d637-bb1a-489e-be1c-76e88cb5800e/main (Download .tar.gz)

frontend @37c0d637-bb1a-489e-be1c-76e88cb5800e/mainraw · history · blame

#!/usr/bin/perl -w

=head1 NAME

frontend - runs a debconf frontend

=head1 SYNOPSIS

 frontend confmodule [params]

=head1 DESCRIPTION

This is a helper program for confmodules. It expects to be passed
the name of the confmodule script to run, and any parameters for it.

This whole thing is really a hack; in an ideal world, dpkg would handle
all this.

=cut

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 '--';

# Set the default title.
my $package;
my $no_title=0;
if ($ENV{DEBCONF_PACKAGE}) {
	$package=$ENV{DEBCONF_PACKAGE};
}
elsif ($ARGV[0]=~m!^.*/(.*?)\.(?:postinst|postrm|prerm)$!) {
	$package=$1;
	my $action=$ARGV[1];
	# Avoid spurious title updates with triggered actions:
	$no_title=1 if $action eq 'triggered';
}
elsif (-e "/var/lib/dpkg/tmp.ci/control") {
	# The preinst is running, presumably. Now it gets really ugly, because
	# I have to parse the control file.
	open (CONTROL, "< /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 {
	# Being run some other way, not via a dpkg script.
	$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;
		}
	}

	# See if there is a templates file in the same directory as the script,
	# with the same name except .templates is appended.
	unless (trytemplate("$ARGV[0].templates")) {
		# Next try removing "config" from the end of the script name,
		# and putting in "templates".
		unless ($ARGV[0]=~m/(.*)config$/ && trytemplate("${1}templates")) {
			# Finally, look in debconf lib directory for the base
			# filename with .templates appended.
			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 !$no_title;
$frontend->info(undef);

# Load any associated templates if we're being run from a package maintainer
# script.
if ($ARGV[0] =~/^(.*[.\/])(?:preinst|postinst|prerm|postrm)$/) {
	my $base=$1;
	my $templates=$base."templates";
	Debconf::Template->load($templates, $package)
		if -e $templates;
}

# See if the preinst or postinst of the package is being run, and if there
# is a config script associated with this package. If so, run it first as a
# confmodule (also loading the templates). This is a bit of a nasty hack, that
# lets you dpkg -i somepackage.deb and have its config script be run first.
#
# If it is the preinst, everything is in this weird directory deep in
# /var/lib/dpkg.
if ($ARGV[0] =~/^(.*[.\/])(?:postinst|preinst)$/) {
	my $base=$1;

	# Run config script, if any.
	my $config=$base."config";
	if (-e $config) {
		# I assume that the third argument passed to this
		# program (which should be the second argument passed to the
		# preinst or postinst that ran it), is the package version.
		my $version=$ARGV[2];
		if (! defined($version)) {
			$version='';
		}
		my $confmodule=make_confmodule($config,
			"configure", $version);

		# Make sure any questions the confmodule generates
		# are owned by this package.
		$confmodule->owner($package);

		# Talk to it until it is done.
		1 while ($confmodule->communicate);
		
		exit $confmodule->exitcode if $confmodule->exitcode > 0;
	}
}

# Start up the confmodule we were asked to run.
my $confmodule=make_confmodule(@ARGV);

# Make sure any questions the confmodule generates are owned by this package.
$confmodule->owner($package);

# Talk to it until it is done.
1 while ($confmodule->communicate);

$frontend->shutdown;

# Save state.
Debconf::Db->save;

exit $confmodule->exitcode;

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut