#!C:\greenstone\gs-release-builder\packages\strawberry-perl-5.32.1.1\perl\bin\perl.exe -w

use strict;
use File::Copy;

# The following is GNU tar, and I found on the windows machine build machine it wasn't preserving capitalisation
#my $tar = "\"C:\\greenstone\\gs-release-builder\\release-kits\\bin\\tar.exe\" -c ";
# the following uses tar from C:\Windows\System32\tar.exe - this tar cannot output to standard out
# my $tar = "tar -c ";
# the following uses 7Zip and seems to work.
# a (create), -ttar (type of archive = tar) -an (disable archive_name field - otherwise it will try to use one of the input files as the field name) -so (output to stdout)
my $tar = "\"C:\\Program Files\\7-Zip\\7z\" a -an -ttar -so ";

#my $set = `set`;
#print $set;
#print "\n";
my $main_snapshot_dir = "C:\\greenstone\\gs-release-builder\\snapshots\\";
my $keys_dir = "C:\\greenstone\\gs-release-builder\\keys\\";

my @snap_dirs = ("gs2-caveat", "gs3-caveat"); # diffcoll?
#my $private_key_file = $keys_dir . "kjdon-linux-labs.ppk";
#die "Couldn't find jumphost key file $private_key_file\n" unless -e $private_key_file; - don't need to use the jumphost now
foreach my $dir (@snap_dirs) {
	opendir (DIR, "$main_snapshot_dir$dir") || next;
	print "Attempting to upload snapshots and logs for $dir\n";
	my @contents = readdir(DIR); close DIR;
	foreach my $from (@contents) {
	if ($from =~ /^from/) { # look for the 'from-2022-03-23'
		my $new_dir = "$main_snapshot_dir$dir\\$from\\uploads";
		if (!-d $new_dir) {
			print "directory $new_dir doesn't exist, skipping\n";
			next;
		}
	#	copy ($private_key_file, $new_dir);
#		my $command = "cd  $new_dir && $tar rk* Greenstone* | plink -proxycmd \"plink -i kjdon-linux-labs.ppk kjdon\@linux-labs.cms.waikato.ac.nz -nc %host:%port\" -T -i ".$keys_dir."upload-caveat-ed25519.ppk nzdl-gsorg\@www-internal.greenstone.org";
		my $command = "cd $new_dir && $tar rk* Greenstone* | plink -T -i ".$keys_dir."upload-caveat-ed25519.ppk nzdl\@www-internal.greenstone.org";
		print "About to run: $command\n";
		STDOUT->flush();
		system($command);
	}	
	}
}

my $diffcol_command = "$tar C:\\greenstone\\gs-release-builder\\diffcol\\diffcol-reports\\diffcol*.htm | plink -T -i ".$keys_dir."upload-caveat-ed25519.ppk nzdl\@www-internal.greenstone.org";
print "About to run: $diffcol_command\n";
STDOUT->flush();
system($diffcol_command);

print "finished\n";
