#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use English;
use utf8;

use Basicos::Config;

our $VERSION = '0.1';

my %config = _configure(
    merge           =>  1,
    getopt  =>  {
        'man'       =>  1,
        'verbose'   =>  0,
        },
    variables => {
        'output'    =>  [ 'output=s', undef, undef,
                        '/var/lib/bacula/dpkg-get-selections' ],
        'compress'  =>  [ 'compress', undef, undef, 1 ],                        
        },
    defaults    =>  {
        'dpkg'  =>  '/usr/bin/dpkg --get-selections',
        'gzip'  =>  '/bin/gzip --force -9',
        }
);

# Primero obtenemos la lista de paquetes instalados 
run(sprintf("%s > %s", $config{dpkg}, $config{output}));

# y si se nos ha indicado que debemos comprimir
if ($config{compress}) {
    run(sprintf("%s %s", $config{gzip}, $config{output}));
}
        
exit(0);

sub run {
    my  $cmd    =   shift;

    if ($config{verbose}) {
        print "executing ${cmd} ... \n";
    }        
    system ($cmd) == 0 or 
        die "execution of ${cmd} failed - ${ERRNO}";

    return;
}

__END__
=pod

=head1 NAME

dump_packages - Vuelca la selección de paquetes del sistema

=head1 USAGE

    dump_packages [--compress] [--output file]

=head1 OPTIONS

=over 

=item --output file

Define el directorio donde almacenar los datos. El valor predeterminado es
F</var/lib/bacula/dpkg-get-selections>.

=item --compress

Habilita la compresión del archivo resultante mediante L<gzip>.

=back

=head1 DESCRIPTION

Este programa obtiene una lista de los paquetes instalados en el sistema
mediante el uso de C</usr/bin/dpkg --get-selections> y, si se require,
comprime el resultado usando C</bin/gzip --force -9>.

El resultado es almacenado en F</var/lib/bacula/dpkg-get-selections> ó en
F</var/lib/bacula/dpkg-get-selections.gz> dependiendo de lo indicado en la
llamada. 

=head1 DIAGNOSTICS

=over

=item execution of XXX failed - ERRNO

Este mensaje de error aparece cuando no es posible llamar al programa externo.
El código de error es el que proporciona la llamada al sistema L<system>.

=back

=head1 DEPENDENCIES

=over

=item L<Basicos::Config>

=back

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to the author(s). Patches are welcome.

=head1 AUTHOR

Víctor Moral <victor@taquiones.net>, Angel Ortega <angel@triptico.com>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2005 <Venexma Europa, S.L.>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

