use File::Basename;
do "$ENV{'TASK_HOME'}/lib.pl";

#check key environment vars are set
die "Please set a DATA_DIR in the nightly snapshot environment\n"
	if ! exists $ENV{'DATA_DIR'};
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";
}

#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";
}

#setup based on mode
if ( $ENV{'TASK_NAME'} =~ "caveat\$" ) {
	$ENV{'UPLOAD_DIR'}="nzdl\@puka.cs.waikato.ac.nz:/greenstone/greenstone.org/base/caveat-emptor";
	
	if ( $major_version == 2 ) {
		$ENV{'snapshot_id2'} = `wget -O - http://www.greenstone.org/next-release.txt 2>nul`;
		chomp($ENV{'snapshot_id2'});
	} else {
		$ENV{'snapshot_id3'} = `wget -O - http://www.greenstone.org/next-release-greenstone3.txt 2>nul`;
		chomp($ENV{'snapshot_id3'});
	}

	#change the filenames to have the date in them
	$ENV{'munges'} = "s/$ENV{'snapshot_id2'}/$ENV{'snapshot_id2'}-candidate-" . get_date() . "/g ";
	$ENV{'munges'} .= "s/$ENV{'snapshot_id3'}/$ENV{'snapshot_id3'}-candidate-" . get_date() . "/g";

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

} else {
	if ( $ENV{'TASK_NAME'} =~ "stable\$" ) {
		$ENV{'BRANCH_PATH'}="stable";
	}
	$ENV{'UPLOAD_DIR'}="nzdl\@puka.cs.waikato.ac.nz:/greenstone/greenstone.org/base/release-snapshots";

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

#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'}/$ENV{'TASK_NAME'}/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";
}

