#!/usr/bin/perl
#
# $Id: rpmbuildupdate.in 224097 2007-07-01 00:17:14Z guillomovitch $

use strict;
use warnings; 
use AppConfig qw/:argcount/;
use Pod::Usage;
use Youri::Package::RPM::Updater 0.002;

my $config = AppConfig->new(
    {
        CASE => 1,
        ERROR => \&pod2usage,
        GLOBAL => {
            DEFAULT  => '',
            ARGCOUNT => ARGCOUNT_ONE,
        }
    },
    'srpms=s'                => { DEFAULT => '' },
    'rpmoption=s'            => { DEFAULT => '' },
    'top=s'                  => { DEFAULT => '' },
    'changelog=s@'           => { DEFAULT => [] },
    'release=s'              => { DEFAULT => '' },
    'releasesuffix=s'        => { DEFAULT => '' },
    'update!'                => { DEFAULT => 1 },
    'update-revision!'       => { DEFAULT => 1 },
    'update-changelog!'      => { DEFAULT => 1 },
    'build!'                 => { DEFAULT => 1 },
    'build-source!'          => { DEFAULT => 1 },
    'build-binaries!'        => { DEFAULT => 1 },
    'download!'              => { DEFAULT => 1 },
    'verbose!'               => { DEFAULT => 0 },
    'execute=s'              => { DEFAULT => '' },
    'execafterbuild=s'       => { DEFAULT => '' },
    'installbuildrequires=s' => { DEFAULT => '' },
);

foreach my $f (
    '/etc/rpmbuildupdate.conf',
    "$ENV{HOME}/.rpmbuildupdaterc"
) {    
    $config->file($f) if -f $f;
}       
$config->args();

# shortcuts
if (!$config->get('build')) {
    $config->set('build-source', 0);
    $config->set('build-binaries', 0);
}

if (!$config->get('update')) {
    $config->set('update-revision', 0);
    $config->set('update-changelog', 0);
}

my $updater = Youri::Package::RPM::Updater->new(
    topdir            => $config->get('top'),
    options           => $config->get('rpmoption'),
    verbose           => $config->get('verbose') + 1,
    download          => $config->get('download'),
    update_revision   => $config->get('update-revision'),
    update_changelog  => $config->get('update-changelog'),
    build_source      => $config->get('build-source'),
    build_binaries    => $config->get('build-binaries'),
    release_suffix    => $config->get('releasesuffix'),
    changelog_entries => $config->get('changelog'),
    srpm_dirs         => [ split(/,/, $config->get('srpms')) ],
    build_requires_command => $config->get('installbuildrequires'),
    build_results_command  => $config->get('execafterbuild'),
    spec_line_expression   => $config->get('execute')
);

my ($name, $version, $release);
if ($ARGV[0]) {
    $name    = $ARGV[0];
    $version = $ARGV[1];
    $release = $ARGV[2];
    if (-f $name) {
        if ($name =~ /.spec$/) {
            $updater->build_from_spec($name, $version, $release);
        } elsif ($name =~ /.(?:no)?src.rpm$/) {
            $updater->build_from_source($name, $version, $release);
        } else {
            pod2usage('invalid file argument, exiting');
        }
    } else {
        $updater->build_from_repository($name, $version, $release);
    }
} else {
    pod2usage('missing argument, exiting');
}

__END__

=head1 NAME

rpmbuildupate - automatic package rebuilder

=head1 SYNOPSIS

rpmbuildupdate [options] <package> <version>

rpmbuildupdate [options] <file.spec> <version>

rpmbuildupdate [options] <file.src.rpm> <version>

Options:

    --srpms <path>                   specify SRPMS path
    --rpmoption <option>             pass this option to rpm during build
    --release <release>              release tag
    --releasesuffix <suffix>         release tag suffix
    --changelog <message>            use a alternate message
    --top <dir>                      specify rpm top dir
    --verbose                        be verbose
    --nodownload                     do not download any files
    --noupdate                       do not update anything
    --noupdate-revision              do not update package revision
    --noupdate-changelog             do not update package changelog
    --nobuild                        do not build anything
    --nobuild-binaries               do not build binary packages
    --nobuild-source                 do not build source package
    --execute <expression>           expression to evaluate during update
    --execafterbuild <command>       command to execute after the build
    --installbuildrequires <command> command to install build requires


=head1 OPTIONS

=over

=item B<--srpms>

Specify SRPMS path, separate folder with a comma.

=item B<--rpmoption>

Pass this option to rpm during build.

=item B<--release>

Complete release tag value.

=item B<--releasesuffix>

Release tag value suffix, remaining should be computed automatically.

=item B<--changelog>

Changelog entry message. Several can be used, and they will be automatically
formatted, with \%\%VERSION substituted by version. Defaults to 'New version
\%\%VERSION' when a new version is given, and 'Rebuild' otherwise.

=item B<--top>

Specify rpm tree top directory. Defaults to rpm configuration.

=item B<--verbose>

Be verbose.

=item B<--noupdate>

Do not update anything.

=item B<--noupdate-revision>

Do not update the package revision.

=item B<--noupdate-changelog>

Do not update the package changelog.

=item B<--nodownload>

Do not download any files.

=item B<--nobuild>

Do not build anything.

=item B<--nobuild-source>

Do not build the source package.

=item B<--nobuild-binaries>

Do not build the binary package.

=item B<--execute>

Evaluate a perl expression for each line of the spec file. $_ will hold the
value of the line. 

=item B<--execafterbuild>

Execute an external command after the build, with files created as arguments.

=item B<--installbuildrequires>

Execute an external command before the build, with build dependencies as
arguments.

=back

=cut

=head1 DESCRIPTION

This tool automatises rpm package building. When given an explicit new version,
it downloads new sources automatically, updates the spec file and builds
a new version. When not given a new version, it just updates the spec file a
builds a new release.

=head1 SEE ALSO

AppConfig, for additional details about configuration file format.

Youri::Package::RPM::Updater, for details about update algorithm.

=head1 AUTHORS

Julien Danjou <danjou@mandriva.com>

Michael Scherer <misc@mandriva.org>

Guillaume Rousse <guillomovitch@mandriva.org>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2003-2007 Mandriva.

Permission to use, copy, modify, and distribute this software and its
documentation under the terms of the GNU General Public License is hereby 
granted. No representations are made about the suitability of this software 
for any purpose. It is provided "as is" without express or implied warranty.
See the GNU General Public License for more details.
