#!/usr/bin/perl

=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)> (in the
debconf-doc package).

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.

The command to be run under debconf must be specified in a way that will
let your PATH find it.

This command is not the usual way that debconf is used. It's more typical
for debconf to be used via L<dpkg-preconfigure(8)> or L<dpkg-reconfigure(8)>.

=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.

=item B<--terse>

Enables terse output mode. This affects only some frontends.

=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 warnings;
use strict;
use Debconf::Db;
use Debconf::AutoSelect qw(:all);
use Debconf::Gettext;
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] if defined $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 = gettext("Usage: debconf [options] command [args]");
my $owner='';
Debconf::Config->getopt($usage.gettext(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
