# Upload - upload new files to web site

use Win32::Internet;
use Win32::Console;

$webdir = "c:/home/web";
$filelist = "newfiles.txt";

sub getpasswd {
    use Win32::Console;
    
    $OUT = new Win32::Console(STD_OUTPUT_HANDLE);
    $IN = new Win32::Console(STD_INPUT_HANDLE);
    
    $OUT->Write("password: ");
    
    while(!$done) {
	@event = $IN->Input();
	
	if($event[0] == 1 and $event[1]) {
	    $key = chr($event[5]);
	    
	    # ENTER
	    if($event[5] == 13) {
		$done = 1;
		$OUT->Write("\n");
	    }
	    else {
		$OUT->Write("*");
		$passwd .= $key;
	    }
	}
    }
}

sub sendfiles {
    $use_ftp = 1;
    $webroot = "~/public_html";
    $host = "charlotte.neuro.brown.edu";
    $user = "sheinb";
    getpasswd();

    if ($use_ftp) {
	$I = new Win32::Internet();
	$I->FTP($FTP, $host, $user, $passwd);
	$FTP->Cd($webroot);
	$FTP->Binary();
    }
    
    open(NEW,$filelist);
    while(<NEW>) {
	chop;
	if ($_ ne "") {
	    ($dir, $file) = m@(\S+)/(\S+)@;
	    if (!$uploaded{"$dir/$file"}) {
		chdir "$webdir/$dir";
		print "Uploading $file to directory $dir\n";
		if ($use_ftp) {
		    $FTP->Cd("$webroot/$dir");
		    $FTP->Put($file,$file);
		}
		$uploaded{"$dir/$file"} = 1;
	    }
	}
    }
    close(NEW);
    
    if ($use_ftp) {
	$FTP->Close();
	$I->Close();
    }
}

sendfiles();

# Clear the file contents
chdir("$webdir/utils");
rename $filelist, "oldfiles.txt";
open (EMPTY,">$filelist");
close(EMPTY);
