#!/usr/bin/perl

# needs input in the form of
#fib 64 par 0 0 rws
#fib 68 sim 0.1 100000 rws
#fib 68 scenar 1 0 rws

$input = pop(@ARGV);

# ----------------------- first do a syntax check...----------------------------------------
open(INPUT, "$input") or die "Can't open input file ($input): $!\n";

while ($line = <INPUT>) {
    @words = split(" ", $line);

    if($words[5] eq "rws") {
    } elsif ($words[5] eq "rwp") {
    } elsif ($words[5] eq "chws") {
    } elsif ($words[5] eq "clws") {
    } elsif ($words[5] eq "cws2") {
    } else {
	printf "unknown sched type $words[5], line = $line\n";
	die;
    }

    if($words[2] eq "par") {
	if($words[1] != 64) {
	    printf("wrong size in <$line>?\n");
	}
    } elsif($words[2] eq "sim" || $words[2] eq "scenar") {
	if($words[1] != 68) {
	    printf("wrong size in <$line>?\n");
	}
    } else {
	printf("illegal type $words[1]\n");
	die;
    }
}


# ----------------------- now run it----------------------------------------

open(INPUT, "$input") or die "Can't open input file ($input): $!\n";

while ($line = <INPUT>) {
    @words = split(" ", $line);

    if($words[5] eq "rws") {
	$sched = "-satin_random_work_stealing";
    } elsif ($words[5] eq "rwp") {
	$sched = "-satin_random_work_pushing";
    } elsif ($words[5] eq "chws") {
	$sched = "-satin_cluster_hierarchical_work_stealing";
    } elsif ($words[5] eq "clws") {
	$sched = "-satin_cluster_random_loadbased_work_stealing";
    } elsif ($words[5] eq "cws2") {
	$sched = "-satin_cluster_random_work_stealing2";
    } else {
	printf "unknown sched type $words[5]\n";
	die;
    }

    if($words[2] eq "par") {
	$command = "make parrun LISTRUN=1 LIST_APPS=$words[0] LIST_SIZES=$words[1] LIST_PAR_SCHEDULING=$sched";
	printf("running $command\n");
	system($command);
    } elsif($words[2] eq "sim") {
	$command = "make simrun LISTRUN=1 LIST_APPS=$words[0] LIST_SIM_SIZES=$words[1] LIST_SIM_SCHEDULING=$sched " .
	    "LIST_SPINS=$words[3] LIST_THROUGHPUTS=$words[4]";
	printf("running $command\n");
	system($command);
    } elsif($words[2] eq "scenar") {
	$command = "( cd $words[0]; make scenarrun LISTRUN=1 LIST_APPS=$words[0] LIST_SIM_SIZES=$words[1] LIST_SCENAR_SCHEDULING=$sched " .
	    "LIST_SCENARIOS=\\\$\\\(SCENARIO$words[3]\\\) )";
	printf("running $command\n");
	system($command);
    } else {
	printf("illegal type $words[1]\n");
	die;
    }
}
