#!/usr/bin/perl
 
use strict;
use warnings;
 
# small helper update script because I always forget the command and URL
# of Wordpress' SVN repository
# use at your own risk. Coded by Thomas Westfeld, www.gorillapatch.com
 
# base url to wordpress subversion repository
my $base_url="http://core.svn.wordpress.org/tags/";
 
# validate user input
die "usage: updateWordpress.pl [version]\n" unless scalar(@ARGV) == 1;
die "no subversion repository found. cd into the wordpress directory first\n" unless (-d ".svn");
my $newVersion = $ARGV[0];
die "$newVersion is no valid version number" unless $newVersion =~ /\d+(.\d+)+/;
 
my $currentVersion =  &getCurrentVersion;
 
# confirm update from user
print "Do you really want to upgrade from current version $currentVersion to $newVersion [y/n]? ";
chomp (my $confirm = <STDIN>);
if (uc($confirm) eq 'Y') {
    system("svn sw ${base_url}${newVersion} .");
} else {
    print "update cancelled\n";
    exit 1;
}
 
exit 0;
 
# use svn info to get the URL of the currently checked out tag
sub getCurrentVersion
{
    my $result;
    open my $version_fh, "-|", "svn info";
    while (my $line = <$version_fh>) {
        if ($line =~ /^URL: $base_url([0-9.]+)/) {
            $result = $1;
        }
    }
    close ($version_fh);
    return $result;
}

