my $sep = $^O eq "MSWin32" ? "\\" : "/";

sub get_date {
	local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	local $day = $mday;
	local $month = $mon+1;
	local $year = $year+1900;
	if ( length $month == 1 ) {
		$month = join( "", "0", $month);
	}
	if ( length $day== 1 ) {
		$day = join( "", "0", $day );
	}
	local $date = join( ".", $year,$month,$day );
	return $date;
}

sub gen_snapshot_id {
	if ( exists $_[0] && exists $_[1] ) {
		return $_[0] . get_date() . $_[1];
	}

	if ( exists $_[0] ) {
		return $_[0] . get_date();
	}
	return get_date();
}

sub create {
	
	die "release_dir not set, cant create\n" unless $release_dir;

	print "about to clean up old snapshots (Ctrl-C to cancel)";
	local $| = 1;
	for ( my $i=0; $i<5; $i++ ) {
		print ".";
		sleep 1;
	}
	$| = 0;

	print "cleaning up previous snapshot\n";
	local $release_parent = dirname($release_dir);
	if ( $^O eq "MSWin32" ) {
		system("rd /q /s \"$release_parent\"");
	} else {
		system("rm -rf \"$release_parent\"");
	}

	print "creating the release dir\n";
	mkdir $release_parent or die "couldn't create release parent directory\n";
	mkdir $release_dir or die "couldn't create release directory\n";

	print "changing to the release dir\n";
	chdir $release_dir;

	#version property
 	print "setting up todays properties\n";
	`echo version:$snapshot_id> $rk-build.properties`;

	#processor propertylocal $| = 1;
	if ( $^O eq "darwin" ) {
		print "setting processor\n";
		if ( `uname -p` eq "i386" ) {
			`echo processor:intel>> $rk-build.properties`;
		} elsif ( `uname -p` eq "powerpc" ) {
			`echo processor:ppc>> $rk-build.properties`;
		} else {
			print "unable to determine processor type, using intel\n";
			`echo processor:intel>> $rk-build.properties`;
		}
	}

	#branch path property
	if ( $ENV{'branch_path'} ) {
		`echo branch.path:$ENV{'branch_path'}>> $rk-build.properties`;
	}

	#server.exe.location
	if ( exists $ENV{'SERVER_EXE_LOCATION'} ) {
		`echo server.exe.location:$ENV{'SERVER_EXE_LOCATION'}>> $rk-build.properties`;
	}

	print "creating the snapshot using $rk\n";
	system( $rk );

}

sub upload {
	print "uploading files\n";

	my @munges = ();
	if ( exists $ENV{'munges'} ) {
		@munges = split(' ', $ENV{'munges'});
	}

	#copy products to a temporary folder, giving them their new names
	if ( -d "$release_dir${sep}uploads" ) {
		system( "rm -rf '$release_dir${sep}uploads'" );
	}
	mkdir "$release_dir${sep}uploads";

	my @files;
	if ( -d "$release_dir${sep}products" ) {
		@files = <$release_dir${sep}products${sep}*>;
	}
	push( @files, "$release_dir${sep}$rk.out" );

	for my $file ( @files ) {
		if ( -e $file ) {
			my $filename = basename($file);
			#munge
			for my $m ( @munges ) {
				$doit="\$filename =~ $m"; eval "$doit";
			}
			#upload
			print "Will upload '" . basename($file) . "' to '$filename'\n";
			system("copy \"$file\" \"${release_dir}${sep}uploads${sep}$filename\"");
		}

	}

	my $command = "cd \"${release_dir}${sep}uploads\" && tar -c * | ";
	$command .= ($^O eq "MSWin32" ? "plink" : "ssh");
	$command .= " -T -i \"$ENV{'IDENTITY_FILE'}\" nzdl\@puka.cs.waikato.ac.nz";
	#print "$command\n";
	system("$command");
}
