use File::Basename;
my $sep = $^O eq "MSWin32" ? "\\" : "/";
do "$ENV{'TASK_HOME'}/lib.pl";

die "Could not determine your operating system"
	unless ( $^O =~ "linux|darwin|MSWin32" );

#arguments
our $action = "";

#intervening variables
our $prefix = "";
our $release_dir = "";
our $rk = "";
our $snapshot_id = "";
our $branch_path = "";
our $major_version = 0;

if ( exists $ARGV[0] ) {
	$action = $ARGV[0];
}

if ( exists $ARGV[1] ) {
	die "too many arguments to snapshot task\n";
}

#default data dir
if ( ! exists $ENV{'DATA_DIR'} ) {
	$ENV{'DATA_DIR'} = "$ENV{'HOME'}${sep}snapshots";
}

#default identity dir
if ( ! exists $ENV{'IDENTITY_DIR'} ) {
	$ENV{'IDENTITY_DIR'} = "$ENV{'HOME'}${sep}.ssh";
}

#choose the parameters of the build based on the task name
if ( $ENV{'TASK_NAME'} =~ "gs2-(caveat|stable)" ) {
	$major_version = 2;
	$prefix="2s";
	$rk="rk2";
} elsif ( $ENV{'TASK_NAME'} =~ "gs3-(caveat|stable)" ) {
	$major_version = 3;
	$prefix="3s";
	$rk="rk3";
} elsif ( $ENV{'TASK_NAME'} =~ "gs2-src-(caveat|stable)" ) {
	$major_version = 2;
	$prefix="2s";
	$rk="sork2";
} elsif ( $ENV{'TASK_NAME'} =~ "gs3-src-(caveat|stable)" ) {
	$major_version = 3;
	$prefix="3s";
	$rk="sork3";
} elsif ( $ENV{'TASK_NAME'} =~ "gs2-cd-(caveat|stable)" ) {
	$major_version = 2;
	$prefix="2s";
	$rk="cdrk2";
} elsif ( $ENV{'TASK_NAME'} =~ "dec-(caveat|stable)" ) {
	$prefix="";
	$rk="derk";
} else {
	die "unrecognised task name '$ENV{'TASK_NAME'}'\n";
}

#keep wget/curl base command in a variable
my $httpRetrieve = $^O eq "darwin" ? "curl" : "wget -O -";

#setup based on mode
if ( $ENV{'TASK_NAME'} =~ "caveat\$" ) {
	$ENV{'SNAPSHOT_MODE'} = "caveat";

	if ( $major_version == 2 ) {
		$ENV{'snapshot_id2'} = `$httpRetrieve http://www.greenstone.org/next-release.txt`;
		chomp($ENV{'snapshot_id2'});
	} else {
		$ENV{'snapshot_id3'} = `$httpRetrieve http://www.greenstone.org/next-release-greenstone3.txt`;
		chomp($ENV{'snapshot_id3'});
	}
	
	#change the filenames to have the date in them
	$ENV{'munges'} = "s/\D$ENV{'snapshot_id2'}/$ENV{'snapshot_id2'}-candidate-" . get_date() . "/g ";
	$ENV{'munges'} .= "s/\D$ENV{'snapshot_id3'}/$ENV{'snapshot_id3'}-candidate-" . get_date() . "/g";
	# the above generates a list of regex. Have a \D (non-digit) prefixed to the value to be substituted, else
	# Greenstone-documented-examples-2013.06.18.tar.gz gets changed to Greenstone-documented-examples-2013.06-candidate-<daterepeat of 2013.06.18>.18.tar.gz

	#set the path to server.exe
	$ENV{'SERVER_EXE_LOCATION'} = "http://www.greenstone.org/caveat-emptor/latest-server.exe";

} elsif ( $ENV{'TASK_NAME'} =~ "stable\$" ) {
	$ENV{'SNAPSHOT_MODE'} = "stable";
	$ENV{'BRANCH_PATH'} = "tags/stable";

	#dont proceed if main/stable is old
	#get last changed date from svn
	open( INFO, "svn info http://svn.greenstone.org/main/$ENV{'BRANCH_PATH'}|" )
		or die "Cant determine age of stable tag";
	my $changed_date;
	while ( my $line = <INFO>) {
		chomp($line);
		if ( $line =~ /^Last Changed Date:/ ) {
			$changed_date = $line;
			break;
		}
	}
	close(INFO);
	#change the format
	$changed_date =~ s/.*: ([^ ]+) .*/\1/g;
	if ( $changed_date !~ /^20[0-9]{2}-[0-9]{2}-[0-9]{2}$/ ) {
		die "Cant determine age of stable tag";
	}
	$changed_date =~ s/-/./g;
	#check if main/stable is new
	if ( $changed_date ne get_date() ) {
		print "main/stable is old, will not create snapshot\n";
		exit;
	} else {
		print "main/stable is fresh, will create snapshot\n";
	}

	#set the path to server.exe
	$ENV{'SERVER_EXE_LOCATION'} = "http://www.greenstone.org/release-snapshots/server-\$\\\{version\\\}.exe";
}

#use the  correct key for uploading
$ENV{'IDENTITY_FILE'} =
	"$ENV{'IDENTITY_DIR'}${sep}upload-" . $ENV{'SNAPSHOT_MODE'} . ($^O eq "MSWin32" ? ".ppk" : "");


#always rename the log not to clash with other files on puka
$ENV{'munges'} = $ENV{'munges'} . " s/\.out/-" . get_date() . "-" . $^O . "-log.txt/";

#choose a snapshot ID
if ( exists $ENV{'snapshot_id2'} && $major_version == 2 ) {
	$snapshot_id = $ENV{'snapshot_id2'};
} elsif ( exists $ENV{'snapshot_id3'} && $major_version == 3 ) {
	$snapshot_id = $ENV{'snapshot_id3'};
} else {
	$snapshot_id = gen_snapshot_id($prefix);
}

#set a release directory
$release_dir = "$ENV{'DATA_DIR'}${sep}$ENV{'TASK_NAME'}${sep}from-" . get_date();

print "creating a snapshot release\n";
print "release id : $snapshot_id\n";
print "release dir: $release_dir\n";

if ( $action eq "create" ) {
	create();
} elsif ( $action eq "upload" ) {
	upload();
} elsif ( $action eq "all" || !$action ) {
	create();
	upload();
} else {
	die "bad snapshot action\n";
}

