#!/usr/bin/perl -I/home/pgl/public_html/pisg/src/pisg/modules

### small script to check whether pisg has been updated. the -I
### to use Pisg; is necessary becuase as of version 0.34, pisg
### doesn't have a --version or -v switch to find out what version
### you're running.
###

use warnings;
use strict;

use LWP::Simple qw/get getstore/;
use Pisg;

my $dlpage	= 'http://pisg.sourceforge.net/index.php?page=download';
my $dldir	= '/home/pgl/public_html/pisg/';
my $regex	= qr/href="(.*?pisg-([0-9]+\.[0-9]+).tar.gz)/;

my $page = get $dlpage
	or die "Couldn't fetch download page: $!\n";
my ($dlurl, $latestver) = $page =~ /$regex/
	or die "Couldn't find pisg version number in $dlpage, giving up\n";

my $pisg = new Pisg;
$pisg->get_default_config_settings();			# ick
my ($ourver) = $pisg->{cfg}->{version} =~ m/^v(.*)$/;	# nasty

die "Couldn't get pisg version number!" unless $ourver;

if ($ourver > $latestver) {
	print	"Something fishy going on: pisg v$latestver is on the download page,\n",
		"which is lower than your the installed version (v$ourver).\n";
	
	}
elsif ($ourver < $latestver) {
	my $dlto	= "$dldir/pisg-$latestver.tar.gz";

	print	"New pisg out: v$latestver (v$ourver installed)\n",
		"Downloading it to $dlto\n";


	my $httpstatus = getstore $dlurl, $dlto;

	if ($httpstatus != 200) {
		warn	"Warning: received HTTP status code $httpstatus whilst trying to download\n",
			"pisg v$latestver from $dlurl\n";
		}
	else {
		# maybe do the untar bit here?
		}
	}
