#!/usr/bin/perl -w

=head1 NAME

debconf - runs a program under debconf

=head1 SYNOPSIS

 debconf owner [options] program [params]

=head1 DESCRIPTION

This program runs a program that uses debconf, and talks to it via the
debconf protocol, accessing the database and displaying questions as
directed.

=head1 OPTIONS

=over 4

=item B<-f>I<type>, B<--frontend=>I<type>

Specify a debconf frontend to use.

=item B<-p>I<value>, B<--priority=>I<value>

Specify the minimum priority of question that will be displayed.

=item B<--package>=I<package>

Tell debconf what package the program is a part of. Recommended.

=item B<-t>I<file>, B<--templates>=I<file>

Load up the specified file as a templates file first.

=back

=head 1 RETURN VALUE

This program returns the return code of the program it runs.

=cut

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

Debconf::Db->load;

my $templates='';
my $package='';
Debconf::Config->getopt( # TODO: i18n this? What's the best way to break it up?
qq{Syntax: debconf [options] program [params]
  -t,  --templates		Load specified templates file.
       --package		Specifiy package the program is part of.},
	"templates|t=s",	\$templates,
	"package=s",		\$package,
);

if (! @ARGV) {
	warn gettext("you must specify a program to run");
	exit(1);
}

my $frontend=make_frontend();
$frontend->default_title($package);

# Load templates.
if (length $templates) {
	debug developer => "loading templates from $templates";
	Debconf::Template->load($templates, $package);
}

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

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

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

# End.
$frontend->shutdown;
Debconf::Db->save;
exit $confmodule->exitcode;

=head1 AUTHOR

Joey Hess <joey@kitenet.net>

=cut
