#!/usr/bin/env perl

use strict;

use File::Basename;
use Getopt::Long;
use Term::ReadPassword;
use WWW::SourceforgeJP;

use constant PROJECT    => 'mod-uploader';
use constant USER       => 'kimata';

my $pass;
my %args = ();
my $sf;

GetOptions(\%args, "package=s", "mode=s", "version=s", "file=s");

$pass = $ENV{'PASS'} || read_password('sourceforge pass: ', 0, 1);

$sf = WWW::SourceforgeJP->new(PROJECT);
$sf->login(USER, $pass);

if ($args{'mode'} eq 'release') {
    die '--file が指定されていません．' unless (defined $args{'file'});
    die 'ファイル (' . $args{'file'} . ') が存在しません．' unless (-e $args{'file'});

    $sf->create_release($args{'package'}, $args{'version'}, $args{'file'});
} else {
    my @links = $sf->get_release_links();
    my $src_link = (grep($_->text() =~ /^mod_uploader-.*\.tgz$/, @links))[0];
    my $win32_bin_link = (grep($_->text() =~ /^mod_uploader-.*\.msi$/, @links))[0];

    while (<>) {
        if (/\* `mod_uploader-\d+\.\d+\.\d+\.tgz/) {
            print << "__HTML__";
* `@{[$src_link->text()]}[0] <@{[$src_link->url()]}[0]>`_
__HTML__
        } elsif (/\* `mod_uploader-\d+\.\d+\.\d+\.msi/) {
            print << "__HTML__";
* `@{[$win32_bin_link->text()]}[0] <@{[$win32_bin_link->url()]}[0]>`_
__HTML__
        } else {
            s/mod_uploader-.*\.msi/$win32_bin_link->text()/eg;
            print;
        }
    }
}

# Local Variables:
# mode: cperl
# buffer-file-coding-system: euc-japan-unix
# End:
