#!/usr/bin/perl -w

=head1 NAME

debconf - run a debconf-using program

=cut

=head1 SYNOPSIS

 debconf [options] command [args]

=head1 DESCRIPTION

Debconf is a configuration system for Debian packages. For a debconf
overview and documentation for sysadmins, see L<debconf(7)>.

The B<debconf> program runs a program under debconf's control, setting it up
to talk with debconf on stdio. The program's output is expected to be debconf
protocol commands, and it is expected to read result codes on stdin. See
L<debconf-devel(7)> for details about the debconf protocol.

=head1 OPTIONS

=over 4

=item B<-o>I<package>, B<--owner=>I<package>

Tell debconf what package the command it is running is a part of. This is
necessary to get ownership of registered questions right, and to support
unregister and purge commands properly.

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

Select the frontend to use.

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

Specify the minimum priority of question that will be displayed.

=back

=head1 EXAMPLES

To debug a shell script that uses debconf, you might use:

 DEBCONF_DEBUG=developer debconf my-shell-prog

Or, you might use this:

 debconf --frontend=readline sh -x my-shell-prog

=head1 SEE ALSO

L<debconf-devel(7)>, L<debconf(7)>

=cut

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

# FInd the end of the options for this command, and the beginning of the
# command to run, which may have arguments. Break those arguments out.
my (@argv, @command);
for (my $x=0; $x <= $#ARGV; $x++) {
	if ($ARGV[$x] =~ /^-(o|f|p|-(owner|frontend|priority))$/) {
		push @argv, $ARGV[$x++];
		push @argv, $ARGV[$x]; # skip option argument
		next;
	}
	elsif ($ARGV[$x] =~ /^-/) {
		push @argv, $ARGV[$x];
	}
	else {
		# end of arguments, start of command
		@command=@ARGV[$x..$#ARGV];
		last;
	}
}
@ARGV=@argv;
my $usage = "Usage: debconf [options] command [args]";
my $owner='';
Debconf::Config->getopt($usage.qq{
  -o,  --owner=package		Set the package that owns the command.},
        "o|owner=s"		=> \$owner,
);
die "$usage\n" unless @command;

Debconf::Db->load;
my $frontend=make_frontend();
my $confmodule=make_confmodule(@command);
$confmodule->owner($owner) if length $owner;

1 while ($confmodule->communicate);

my $code=$confmodule->exitcode;
$frontend->shutdown;
$confmodule->finish;
Debconf::Db->save;
exit $code;

=head1 AUTHOR

Joey Hess <joeyh@debian.org>

=cut
