#!D:\greenstone\gs-release-builder-64bit\ext-cli\selfcontained-perl-with-cpan\gsperl5-with-cpan-windows32\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_64 = "D:\\greenstone\\gs-release-builder-64bit\\snapshots\\";
my $main_snapshot_dir_32 = "D:\\greenstone\\gs-release-builder-32bit\\snapshots\\";
my $keys_dir = "D:\\greenstone\\gs-release-builder-64bit\\keys\\";

my @snap_dirs_32 = ("gs2-caveat", "gs3-caveat");
my @snap_dirs_64 = ("gs3-caveat-x64");

#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_32) {
        opendir (DIR, "$main_snapshot_dir_32$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_32$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);
	}	
	}
}


foreach my $dir (@snap_dirs_64) {
        opendir (DIR, "$main_snapshot_dir_64$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_64$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);
	}	
	}
}

if (-d "D:\\greenstone\\gs-release-builder-64bit\\diffcol") {
    my $diffcol_command = "$tar D:\\greenstone\\gs-release-builder-64bit\\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";
