#!/usr/bin/perl -w

use strict;
use File::Copy;

my $tar = "\"C:\\greenstone\\gs-release-builder\\release-kits\\bin\\tar.exe\"";

#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", "poo", "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;
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 -c 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 -c rk* Greenstone* | plink -T -i ".$keys_dir."upload-caveat-ed25519.ppk nzdl-gsorg\@www-internal.greenstone.org";
		print "About to run: $command\n";
		STDOUT->flush();
		system($command);
	}	
	}
}
print "finished\n";
