#!/usr/bin/perl -w
#=============================================================================#
#                      Send package to incoming/                              #
#=============================================================================#
# (c) Denis Smirnov <ds@seiros.ru>                               10 Apr 2008  #
#=============================================================================#
use strict;
use Term::ANSIColor;

sub rpm_checksign($) {
    my $fn = shift;
    my $r  = `LANG=C rpm --checksig "$fn" 2> /dev/null`;
    return $r =~ /gpg OK/;
}

sub rpm_sign($) {
    my $fn = shift;
    unless ( rpm_checksign($fn) ) {
        `rpm --resign "$fn"`;
    }
}

# sign all RPM packages
my @for_sign;
foreach my $fn (@ARGV) {
    next unless -f $fn;
    my $tmp = $fn;
    unless ( $tmp =~ s/\.src\.rpm$// ) {
        print "$fn -- not src.rpm, ignoring\n";
        next;
    }
	push @for_sign, $fn unless rpm_checksign($fn);
};

{
	my $tmp = join(' ', @for_sign);
	`rpm --resign $tmp` if $tmp ne '';
}

foreach my $fn (@ARGV) {
    next unless -f $fn;
    my $tmp = $fn;
    unless ( $tmp =~ s/\.src\.rpm$// ) {
        print "$fn -- not src.rpm, ignoring\n";
        next;
    }
    rpm_sign($fn);
    unless ( rpm_checksign($fn) ) {
        print color "red";
        print "$fn -- hasn't correct gpg sign, ignoring\n";
        print color "reset";
        next;
    }
    $tmp =~ s/^.*alt(.+)$/$1/;
    my $branch = 'Sisyphus';
    my $dir    = "/incoming/Sisyphus/";
    if ( $tmp =~ /M40/ ) {
        $branch = "4.0 branch";
        $dir    = "/incoming/updates/4.0/";
    }
    elsif ( $tmp =~ /M41/ ) {
        $branch = "4.1 branch";
        $dir    = "/incoming/updates/4.1/";
    }
    print color "green";
    print $fn. " -- " . $branch . "\n";
    print color "reset";
    print `rsync $fn devel:$dir 2>&1`;
	if( $? == 0 )
	{
		`mv $fn ~/RPM/SRPMS/sent/`;
	}
}

