#!/usr/opkg/bin/perl

#   official upload parameters
my $upload_url = "openpkg-upload\@download.openpkg.org:/";
my $upload_mode = "rsync";

#   list of positional arguments
my @spec = (
    "openpkg-prefix",
    "spec-dir",
    "source-dir",
    "binary-rpm-file",
    "source-rpm-file",
    "package-name",
    "package-version",
    "package-release",
    "package-version-old",
    "package-release-old",
    "commit-message",
    "vcs",
    "option-force"
);

#   convert positional arguments back into names
my $info = {};
for (my $i = 0; $i < @ARGV; $i++) {
    $info->{$spec[$i]} = $ARGV[$i];
    #printf(STDOUT "%s=\"%s\"\n", $spec[$i], $ARGV[$i]);
}

#   sanity check situation
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time());
my $today = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
if ($info->{"package-release"} ne $today) {
    print STDERR "openpkg-dev-release: ERROR: package release not matching current date\n";
    exit(1);
}
if ($info->{"package-name"} eq "") {
    print STDERR "openpkg-dev-release: ERROR: package name not set\n";
    exit(1);
}

#   commit changeset to Monotone repository
my $stat = `git stat . 2>/dev/null`;
if ($stat !~ m/nothing\s+to\s+commit,\s+working\s+tree\s+clean/s) {
    print STDOUT "\033[31m++ committing changeset to OpenPKG VCS repository\033[0m\n";
    if ($info->{"vcs"} ne "git") {
        print STDERR "openpkg-dev-release: ERROR: Ops, not Git VCS?!\n";
        exit(1);
    }
    if (! -f $info->{"spec-dir"} . "/" . $info->{"package-name"} . ".spec") {
        print STDERR "openpkg-dev-release: ERROR: package specification not known\n";
        exit(1);
    }
    print STDOUT "-- local:  " . $info->{"spec-dir"} . "\n";
    my $cmd = "cd " . $info->{"spec-dir"} . " && ";
    $info->{"commit-message"} =~ s/'/\\'/sg;
    $cmd .= "git commit -m '" . $info->{"commit-message"} . "' .";
    print STDOUT "\$ $cmd\n";
    my $rc;
    for (my $i = 0; $i < 10; $i++) {
        $rc = system($cmd);
        last if ($rc == 0);
        print STDERR "openpkg-dev-release: WARNING: failed to commit to VCS repository (retrying in 2 seconds)\n";
        sleep(2);
    }
    if ($info->{"option-force"} eq "no") {
        if ($rc != 0) {
            print STDERR "openpkg-dev-release: ERROR: failed to commit to VCS repository\n";
            exit(1);
        }
    }
}

#   upload file to download.openpkg.org
print STDOUT "\033[31m++ uploading source RPM package to OpenPKG download repository\033[0m\n";
print STDOUT "-- local:  " . $info->{"source-rpm-file"} . "\n";
print STDOUT "-- remote: " . $upload_url . "\n";
my $cmd;
if ($upload_mode eq "rsync") {
    $cmd = "rsync --rsh=\"ssh -o 'Compression no'\" --partial --progress";
    $cmd .= " " . $info->{"source-rpm-file"};
    $cmd .= " " . $upload_url;
}
elsif ($upload_mode eq "scp") {
    $cmd = "scp -o 'Compression no'";
    $cmd .= " " . $info->{"source-rpm-file"};
    $cmd .= " " . $upload_url;
}
print STDOUT "\$ $cmd\n";
system($cmd);

#   schedule VCS synchronization
if (`uname -s` =~ m/^FreeBSD\n?$/s) {
    my $jobs = `at -qZ -l 2>/dev/null`;
    $jobs =~ s/^[^\n]+\n//s;
    foreach my $job ($jobs =~ m/\s+(\d+)[^ \t]*$/mg) {
        system("at -r $job >/dev/null 2>&1 || true");
    }
    system("echo 'git push --quiet >/dev/null 2>&1' | at -qZ '+ 15 minutes'");
}

#   final hint
print STDOUT "\033[32m++ PACKAGE RELEASED\033[0m\n";

