#!/usr/bin/perl -w

use English;


sub execute_cmd {
    my($this_cmd) = pop(@_);
    my($pid);

    printf "Executing '$cmd' \n";
    $pid = open (LAST,"$cmd |");
    while (<LAST>) {
	print $_;
    }
    close(LAST);
}

$_=`/usr/ucb/whoami`;
chop;
$person = $_;


## Change these variables
$simdir = "abm_ss";
$assignment="Arch_sim3";
$target_dir = "/home/reese/vhdl_dev/sims/$person";
#@files = ("$simdir/");
@files = ();   # for file check

#####


$tarname="tmp_submit";
$uuname = $tarname . "\.uu";
$maxsize = 1000000;



$fail = 0;
if (!-e $simdir) {
    printf "Expecting a directory named $simdir\n. File not submitted.\n";
    exit(1);
}

foreach $file (@files) {
 
    
    printf "Checking existence of: $file \n";
    if (!(-e $file)) {
	printf STDERR "Can't find file: $file\n";
	$fail = 1;
    }

}

if ($fail) {
    printf STDERR "File Existence check failed, project not submitted \n";
    exit(-1);
}




printf STDERR "Creating uuencoded tar file...\n";

$cmd = "tar cf $tarname.tar $simdir";

&execute_cmd($cmd);

if (!(-e "$tarname.tar")) {
    printf STDERR "Tar file creation failed. Internal error, send email to reese\@erc.msstate.edu \n";
    printf STDERR "Homework not submitted.\n";
    exit(-1);
}

@fstats = stat("$tarname.tar");

$fsize =  $fstats[7];
if ($fsize > $maxsize) {
    print STDERR "Your TAR file ($tarname.tar) submission is too big (> $maxsize bytes) \n";
    print STDERR "Make sure only ESSENTIAL files are in the $simdir directory. \n";
    print STDERR "Homework not submitted.\n";
    exit(-1);
}
  

$cmd = "uuencode $tarname.tar $target_dir/$tarname.tar \> $uuname";
printf STDERR "Executing $cmd \n";

`$cmd`;



if (!(-e $uuname)) {
    printf STDERR "UUencoded tar file creation failed. Internal error, send email to reese\@erc.msstate.edu \n";
    printf STDERR "Project not submitted.\n";
    exit(-1);
}

printf STDERR "Mailing uuencoded tar file...\n";

$cmd = "mailx -s $assignment reese\@erc.msstate.edu $person < $uuname";


&execute_cmd($cmd);

`rm -f $tarname.tar $uuname`;


printf STDERR "Project submitted. Mailed to both reese\@erc.msstate.edu and $person\n";


<div align="center"><br /><script type="text/javascript"><!--
google_ad_client = "pub-7293844627074885";
//468x60, Created at 07. 11. 25
google_ad_slot = "8619794253";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />&nbsp;</div>