#!/usr/bin/perl

# this script builds a new tarball of gtk123. it assumes a lot of things.
# ~/.buildrelease is a config file. it contains two items, username and password.
# these are my username and password for logging on to Apple's servers.
# the gtk123 software is in a subdirectory of my home directory named gtk123.
# the webpage on the Apple site is located in mac.com/gtk123, also relative to my home dir.
# given all these, it produces four files: GZipped and BZipped versions of the tarball, plus
# md5 checksums of those two files, uploads the tarballs to SourceForge, modifies my page on
# Apple's servers to insert the new release, and then uploads the new index.html plus the
# four generated files to Apple.
# It then sets me up to login to SourceForge.
# There's really no error-checking; if a version's already been created, this script may
# fail in most humourous ways. Also, it assumes that the current version of gtk123 has
# already been installed on my system (i.e. that I've run a make install).
# It depends on a lot of things, some of which gtk123 also depends on. Notable exceptions are
# the HTTP::DAV module, elinks and ncftp, although both elinks and ncftp could be pretty
# easily substituted.

use Cwd 'chdir';
use AppConfig;
use HTTP::DAV;

my $config = AppConfig->new('username' => { ARGCOUNT => 1 },
                            'password' => { ARGCOUNT => 1 });
$config->file($ENV{'HOME'} . '/.buildrelease');

my $version = '';
open PIPE, "/usr/local/bin/gtk123 --version |" or die "can't fork: $!";
while (<PIPE>) {
    if (/version/) {
        s/.*version //;
        $version = $_;
        chop ($version);
    }
}
close PIPE;
print "Building gtk123 release ", $version, "\n";
chdir "$ENV{'HOME'}/gtk123-$version" or die "Couldn't chdir to source directory: $!\n";

# can you tell this used to be a shell script? :)

system ('autoconf');
system ('./configure');
system ('make dist');
chdir '..';

system ("tar -cf mac.com/gtk123/gtk123_$version.tar gtk123-$version");
chdir 'mac.com/gtk123/';
system ('bzip2 -k *.tar');
system ('gzip *.tar');
system ("md5sum gtk123_$version.tar.gz > gtk123_$version.tar.gz.md5sum");
system ("md5sum gtk123_$version.tar.bz2 > gtk123_$version.tar.bz2.md5sum");
system ("ncftpput upload.sourceforge.net incoming gtk123_$version.tar.gz gtk123_$version.tar.bz2");

# update index.html
system ('rm index.backup.html') if (-w 'index.backup.html');
system ('mv index.html index.backup.html');
open OLDPAGE, '<index.backup.html';
open NEWPAGE, '>index.html';

while (<OLDPAGE>) {
    # copy everything
    print NEWPAGE $_;
    if (/<caption>Downloads available here<\/caption>/) {
        print NEWPAGE '<tr><td>Release ', $version, '</td><td><a href=gtk123_', $version, '.tar.gz>GZip; </a><a href=gtk123_', $version, '.tar.gz.md5sum>md5 checksum</a></td><td><a href=gtk123_', $version, '.tar.bz2>BZip2; </a><a href=gtk123_', $version, '.tar.bz2.md5sum>md5 checksum</a></td></tr>', "\n";
    }
}

close OLDPAGE;
close NEWPAGE;
# WebDAV scripts

$idisk = new HTTP::DAV;
$URL   = "http://idisk.mac.com/danflemming/Sites/gtk123/";
$idisk->credentials( -user=>$config->username,-pass =>$config->password,
    -url =>$URL);

$idisk->open( -url=>$URL) or die("Couldn't open $URL: $idisk->message\n");
$idisk->put( -local => "gtk123_$version.tar.gz");
$idisk->put( -local => "gtk123_$version.tar.gz.md5sum");
$idisk->put( -local => "gtk123_$version.tar.bz2");
$idisk->put( -local => "gtk123_$version.tar.bz2.md5sum");
$idisk->put( -local => "index.html");

# login to SourceForge
exec ('elinks https://sourceforge.net/projects/gtk123/');

Valid XHTML 1.0! Valid CSS!