#!/usr/bin/perl
# $Id: rpmbuildupdate.in 216003 2007-04-25 20:21:49Z guillomovitch $

=head1 NAME

youri-update - package update agent

=head1 SYNOPSIS

youri-update [options] <package> <version>

youri-update [options] <file.spec> <version>

youri-update [options] <file.src.rpm> <version>

Options:

    --topdir <dir>         specify rpm top dir
    --options <options>    pass this option to rpm during build
    --no-download          do not download files
    --no-update-changelog  do not update changelog
    --no-update-revision   do not update revision
    --no-build-source      do not build source package
    --no-build-binaries    do not build binary packages

=head1 DESCRIPTION

B<youri-update> allows to update packages.

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.

It is a rewrite of rpmbuildupdate, using a configuration files and command line
options more consistent with other youri tools.

=head1 OPTIONS

=over

=item verbose $level

verbosity level (default: 0).

=item B<--topdir>

rpm tree top directory (default: rpm %_topdir macro).

=item B<--options>

rpm build options.

=item B<--no-build-binaries>

do not build binary packages.

=item B<--no-build-source>

do not build source package.

=item B<--no-update-revision>

do not modify package revision automatically.

=item B<--no-update-changelog>

do not modify package changelog automatically.

=item B<--no-download>

Do not download any files.

=cut

=head1 SEE ALSO

Youri::Config, for additional details about configuration file format.

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

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007, YOURI project

This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

=cut

use strict;
use warnings; 

use Youri::Config;
use Pod::Usage;
use Youri::Package::RPM::Updater;

my $config = Youri::Config->new(
    args => {
        'download'         => '!',
        'update-revision'  => '!',
        'update-changelog' => '!',
        'build-source'     => '!',
        'build-binaries'   => '!',
        'topdir'           => '=s',
        'options'          => '=s',
    },
    directories => [ "$ENV{HOME}/.youri", '/etc/youri' ],
    file        => 'update.conf',
    mandatory   => 0
);

my $updater = Youri::Package::RPM::Updater->new(
    topdir           => $config->get_arg('topdir') ||
                        $config->get_param('topdir'),
    options          => $config->get_arg('options') ||
                        $config->get_param('options'),
    download         => $config->get_arg('download'),
    update_revision  => $config->get_arg('update-revision'),
    update_changelog => $config->get_arg('update-changelog'),
    build_source     => $config->get_arg('build-source'),
    build_binaries   => $config->get_arg('build-binaries'),
);

pod2usage(
    -verbose => 0,
    -message => "No target specified, aborting\n"
) unless @ARGV;

my $name    = $ARGV[0];
my $version = $ARGV[1];
my $release = $ARGV[2];
if (-f $name) {
    if ($name =~ /.spec$/) {
        $updater->build_from_spec($name, $version, $release);
    } elsif ($name =~ /.(?:no)?src.rpm$/) {
        $updater->build_from_src($name, $version, $release);
    } else {
        pod2usage(
            -verbose => 0,
            -message => "Invalid target specified, aborting\n"
        );
    }
} else {
    $updater->build_from_repository($name, $version, $release);
}
