#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;

# Constants
my $VERSION = "1.0.0";
my $DOCSLIST = "pushdocs.files";
my $DEFEDITOR = "/usr/bin/vi";
my $EDITOR = "";
my $SourceDir = "/home/marisag/DropBox/MyTextFiles";
my $RemoteServer = "marisa-apps.retro-os.live";

if ($ENV{'EDITOR'})
{
        $EDITOR = $ENV{'EDITOR'};
}
else
{
        $EDITOR = $DEFEDITOR;
}


print "pushdocs v$VERSION\n";
print "=================\n";
if (! -f $DOCSLIST)
{
	print "Document list file ($DOCSLIST) does not exist. Please create it. One document per line.\n";
	sleep(3);
	system ("$EDITOR $DOCSLIST");
	print "Rerun this script to try again\n";
	exit 0;
};

print "Enter the password to use (or \"quit\"): ";
my $USEPASSWORD = <>;
chop($USEPASSWORD);
if ($USEPASSWORD eq "quit")
{
	exit 0;
}

# Open the host list
open( my $fh => $DOCSLIST) || die "Cannot open $DOCSLIST: $!";

chdir($SourceDir);

# Loop for each line
while(my $curline = <$fh>)
{
	chop($curline);
	print "Working on document '$curline'\n";
	my $ssh = Net::OpenSSH->new($RemoteServer, user => "root", password => $USEPASSWORD);
	$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
	$ssh->scp_put("$curline", "/var/www/html/jdownloads/My_Install_Scripts/") or die "scp failed: " . $ssh->error;
}
close($fh);

exit 0;

