#!/usr/athena/bin/perl

# usage: compilerun <progname> <flags> <asm-ext> <projphase> <language> 
# authors: andre, maxp

# runs the tests for project phase <projphase> and language <language> on
# program <progname> invoked with flags <flags>.  Creates a file
# 00public_tests.<projphase>

$usagestring = "Usage: compilerun <progname> <flags> <asm-ext> <projphase> <language>\n";

if ($#ARGV != 4) {
    die $usagestring;
}

$progname = $ARGV[0];
$flags = $ARGV[1];
$asmext = $ARGV[2];
$projphase = $ARGV[3];
$language = $ARGV[4];

$publictestpath = '/mit/6.035/tests/'.$projphase.'/'.$language;
$inputtestpath = '/mit/6.035/tests/'.$projphase.'/input';

if (! -e $progname) {
    die '['.$progname."]\n[".$testpath."]\n".$usagestring;
}

opendir (TPH,$publictestpath);
@publictestfiles = grep (!/^\./, readdir (TPH));
closedir (TPH);
@publictestfiles = sort (@publictestfiles);

open (testoutput,">00public_tests.".$projphase);
while (@publictestfiles) {
    $tmpfile = shift @publictestfiles;
    $curfile = $publictestpath.'/'.$tmpfile;
    print stdout "::::::::::::::::::::::".$tmpfile."\n";
    open(tmp,"cp $curfile $tmpfile|");
    close(tmp);
    open (testinput, "$progname $flags $tmpfile 2>&1 |");
    print testoutput "\n::::::::::::::::::::::::::::::::::::::::::::::::: ".$tmpfile."\n";
    print testoutput <testinput>;
    close (testinput);
    ($lstfile = $tmpfile) =~ s/\.$language$/.$asmext/;
    ($inffile = $tmpfile) =~ s/\.$language$/.inf/;
    ($spimfile = $tmpfile) =~ s/\.$language$/.spim/;
    $spimfile = $inputtestpath.'/'.$spimfile;

    print stdout $spimfile."\n";

    if (-e $inffile)
        {
         print testoutput "Ignoring ".$lstfile." since ".$inffile." present.\n";
         print stdout "Ignoring ".$lstfile." since ".$inffile." present.\n";
        }
    else {
     if ( -e $lstfile) {
       if ( -e $spimfile) {
               open (testinput, "cat $spimfile | spim -file $lstfile 2>&1 | ghead -c 2k|");          }
       else {
               open (testinput, "spim -file $lstfile 2>&1 | ghead -c 2k|");          }
       print testoutput "\n..........running..........: ".$lstfile."\n\n";
       print testoutput <testinput>;
       close (testinput);
       }
     else
      {
         print testoutput "\n****No assembly file: ".$lstfile."***\n";
         print stdout "****No assembly file: ".$lstfile."***\n";

      }
     }
}
close (testoutput);
