#!/usr/bin/env python
import sys
import os
import itertools
import platform
import subprocess

infiles=("infile.dna","infile.smm","infile.iam","infile.hap")
#infiles=("infile.dna","infile.hap")
datatype=("s","b","a", "h")
#,"b","a","h")
parmfiles=("parmfile.ml","parmfile.bayes")

def main():
    #read options
    compileflag=True
    flags = []
    myhelp = set(["--help", "--h", "-h", "-help"])
    alloptions = set(["-single", "-thread", "-parallel", "--compile", "--nocompile"]) # except cores
    arguments = set(sys.argv[1:])
    argc = len(sys.argv[1:])
    # help section
    needhelp = len(arguments.intersection(myhelp)) > 0
    if needhelp:
        print "Syntax:"
        print "   python rt <-single | -thread | -parallel> <--compile | --nocompile> <--cores=#num>"
        print " the options options can be combined"
        sys.exit(0)
    # default is core=1
    coreset=arguments.difference(alloptions)
    if len(coreset)>0:
        (dada,corestr) = list(coreset)[0].split("=")
        cores = int(corestr)
    else:
        cores=1
    # default is to compile
    nocompile = set(["--nocompile"])
    neednocompile = len(arguments.intersection(nocompile)) > 0
    # default is all
    runset = set(["-single", "-thread", "-parallel"])
    needrun = len(arguments.intersection(runset)) > 0
    if not needrun:
        needrunset = runset
    else:
        needrunset = arguments.intersection(runset)

    for f in needrunset:
        run(f,neednocompile,cores)

def run(f, neednocompile, cores):
    print "in run "+f
    program = "../migrate-n"
    if f=="-single":
        if not neednocompile:
            compile()
        runsim(program, 1, infiles,datatype,f)
    if f=="-thread":
        if not neednocompile:
            compile("thread")
        runsim(program, 1, infiles,datatype,f)
    if f=="-parallel":
        if not neednocompile:
            compile("mpis")
        program = "../migrate-n-mpi"
        if cores == 1:
            myos = find_os()
            cores = int(findcores(myos))+1
        runsim(program, cores, infiles,datatype,f)
        
def compile(tag=""):
        os.system("cd ..; make clean;configure;make "+tag)

def runsim(program, cores, infiles, datatype, tag):
    print "in runsim"
    for i,d in itertools.izip(infiles,datatype):
        for p in parmfiles:
            p1 = "t_"+tag+"_"+p+"_"+i+"_"+d
            print p, " ", i, " ", d
            if d=="s" or d=="h":
                tp = "0.0 0.1 0.01"
                mp = "0.0 10000.0 1000.0"
            else:
                tp = "0.0 100.0 10.0"
                mp = "0.0 100.0 10.0"
                
            os.system("cp "+p+" "+p1)
            os.system("perl -p -i -e 's/INFILE/"+i+"/g;s/MYTAG/"+tag+"/g;s/DATATYPE/"+d+"/g;s/SIMTHETAPRIOR/"+tp+"/g;s/SIMMIGRPRIOR/"+mp+"/g;' "+p1)
            os.system("submit "+program+" "+p1+" log"+p1+" "+str(cores)+" \"4:00:00\"")

def find_os():
    return platform.system()

def findcores(thisos):
    if thisos == "Linux":
        arg = ["grep proc /proc/cpuinfo | wc -l"]
    if thisos == "Darwin":
        arg = ["/usr/sbin/sysctl hw.logicalcpu | awk '{ print $2}'"]    
    myprocess = subprocess.Popen(arg,
                                 shell=True,
                                 stdout=subprocess.PIPE)
    a = myprocess.communicate()
    return a[0].rstrip()
                                                        

if __name__ == "__main__":
        main()
        
