#! /usr/bin/env perl
use Getopt::Long;
use Cwd;
use Time::Local;
use strict;
use warnings;

my $compileargs = $ARGV[0];

my @alltargets = split /,/, $compileargs;
my $compileenv = "/home/gcc423/bin:.:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin";
my $compilecmd = "mcc -m -R -singleCompThread -R -nodisplay -R -nojvm -nocache ";
my $cmd = "";

my $unsetdef;
#$ENV{PATH} = $compileenv . ":" . $ENV{PATH};
$ENV{PATH} = $compileenv;
$ENV{USER} = "bt";
$ENV{TERM} = "xterm";
$ENV{SHELL} = "/bin/bash";
$ENV{LOGNAME} = "bt";
$ENV{HOME} = ".";
$ENV{PWD} = ".";
$ENV{DISPLAY} = $unsetdef;
$ENV{HOSTNAME} = "submit.chtc.wisc.edu";
$ENV{SSH_TTY} = "/dev/pts/4";
$ENV{LANG} = "en_US.UTF-8";
$ENV{PWD} = "/tmp";

print "MCC compiletime path = <<<<$ENV{PATH}>>>>\n";

my $matlabsources = "matlabsources.tar.gz";
if(-f "$matlabsources") {
	system("tar -zxvf $matlabsources");
}

print "Have these files handy.\n";
system("printenv");

foreach my $targ (@alltargets) {
	my @targetparts = split /\./, $targ;
	print "My base name = $targetparts[0]\n";
	print "Removing <$targetparts[0]>\n";
	$cmd = $compilecmd . $targ;
	print "About to do this compile: <<$cmd>>\n";
	my $ret = system("$cmd");
	print "Return from compile request is <<<$ret>>>\n";
	if(!(-f "$targetparts[0]")){
		print "Compile failed for <$targ>: Recover!\n";
		system("touch RETRYCOMPILE");
		exit(1);
	}
}


print "After, have these files.\n";
system("ls");
print "Bye\n";
system("touch COMPILEDONE");
exit(0);
