#!/usr/bin/env python

import shutil, os, argparse, sys, stat
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__)), "pyUtils"))
from utils import Utils

class genHelper:
    @staticmethod
    def generateCompfileFull(outFileName, externalDirLoc, cc, cxx, outName, installDirName, installDirLoc, neededLibs,ldFlags = ""):
        availableLibs = ["CPPITERTOOLS","CPPPROGUTILS","ZI_LIB","BOOST","R","BAMTOOLS","CPPCMS","MATHGL","ARMADILLO",
                         "MLPACK","LIBLINEAR","CURL","GTKMM", "BIBSEQ", "BIBCPP", "SEEKDEEP", 
                         "BIBSEQDEV", "SEEKDEEPDEV", "CATCH", "JSONCPP",
                          "TWOBIT", "SEQSERVER","NJHRINSIDE", "PSTREAMS", "MONGOC", "MONGOCXX", "SHAREDMUTEX",
                           "MAGIC", "HTS", "RESTBED"]
        neededLibraries = {}
        for lib in neededLibs:
            if ":" in lib:
                libSplit = lib.split(":")
                neededLibraries[libSplit[0].upper()] = libSplit[1]
            else:
                neededLibraries[lib.upper()] = ""
        """
            @todo: Make some of these default to an envirnment CC and CXX and maybe even CXXFLAGS as well 
            @todo: Make availableLibs a more universal constant
        """
        with open(outFileName, "w") as f:
            f.write("CC = {CC}\n".format(CC = cc))
            f.write("CXX = {CXX}\n".format(CXX = cxx))
            f.write("CXXOUTNAME = {NAME_OF_PROGRAM}\n".format(NAME_OF_PROGRAM = outName))
            f.write("CXXFLAGS = -std=c++14\n")
            f.write("CXXFLAGS += -Wall -ftemplate-depth=1024 -Werror=uninitialized -Werror=return-type -Wno-missing-braces\n")
            if "" != ldFlags:
                f.write("LD_FLAGS = ")
                if not ldFlags.startswith("-"):
                    f.write("-")
                f.write("{ld_flags}\n".format(ld_flags = " ".join(ldFlags.split(","))))
            f.write("CXXOPT += -O2 -funroll-loops -DNDEBUG  \n")
            f.write("ifneq ($(shell uname -s),Darwin)\n")
            f.write("\tCXXOPT += -march=native -mtune=native\n" )
            f.write("endif\n")
            f.write("\n")
            f.write("#debug\n")
            f.write("CXXDEBUG = -g -gstabs+ \n")
            f.write("INSTALL_DIR={INSTALL_LOCATION}\n".format(INSTALL_LOCATION = os.path.join(installDirLoc,installDirName)))
            f.write("EXT_PATH=$(realpath {EXTERNAL})\n".format(EXTERNAL = externalDirLoc))
            f.write("\n")
            for lib in availableLibs:
                if lib in neededLibraries:
                    if neededLibraries[lib] == "":
                        f.write("USE_{LIB} = 1\n".format(LIB = lib))
                    else:
                        f.write("USE_{LIB} = 1#{BRANCH}\n".format(LIB = lib, BRANCH = neededLibraries[lib]))
                else:
                    f.write("USE_{LIB} = 0\n".format(LIB = lib))
                    


    @staticmethod            
    def determineCC(args):
        defaultCC = "clang-3.8"
        if Utils.isMac():
            defaultCC = "clang"
        if not args.CC:
            eCC = os.getenv("CC")
            if(eCC):
                defaultCC =  eCC
        else:
            defaultCC = args.CC[0]
        return defaultCC
    @staticmethod
    def determineCXX(args):
        defaultCXX = "clang++-3.8"
        if Utils.isMac():
            defaultCXX = "clang++"
        if not args.CXX:
            eCXX = os.getenv("CXX")
            if  eCXX:
                defaultCXX = eCXX
        else:
            defaultCXX = args.CXX[0]
        return defaultCXX
    
    @staticmethod
    def parseNjhConfigureArgs():
        parser = argparse.ArgumentParser()
        parser.add_argument('-prefix', type=str, nargs=1)
        parser.add_argument('-externalLibDir', type=str, nargs=1)
        parser.add_argument('-CC', type=str, nargs=1)
        parser.add_argument('-CXX', type=str, nargs=1)
        return parser.parse_args()
    
    @staticmethod
    def mkConfigCmd(name,libs, argv, ldflags=""):
        if libs == "":
            cmd = "./scripts/setUpScripts/njhConfigure.py -name {name} ".format(name=name)
        else:
            cmd = "./scripts/setUpScripts/njhConfigure.py -name {name} -libs {libs}".format(name=name, libs=libs)
        if "" != ldflags:
            if ldflags.startswith("-"):
                ldflags = ldflags[1:]
            cmd += " -ldFlags " + ldflags
        if len(sys.argv) > 1:
            cmd += " " + " ".join(sys.argv[1:])
        return cmd


    @staticmethod
    def mkConfigFileStr(name, libs):
        ret = """#!/usr/bin/env python
import shutil, os, argparse, sys, stat
sys.path.append("scripts/pyUtils")
sys.path.append("scripts/setUpScripts")
from utils import Utils
from genFuncs import genHelper

def main():
    name = "{name}"
    libs = "{libs}"
    args = genHelper.parseNjhConfigureArgs()
    cmd = genHelper.mkConfigCmd(name, libs, sys.argv)
    Utils.run(cmd)
    
main()
""".format(name = name, libs = libs)
        return ret
    
    
@@ -1962,163 +1962,8 @@
 n%22)%0A
-            f.write(%22ifneq ($(shell uname -s),Darwin)%5Cn%22)%0A            f.write(%22%5CtCXXOPT += -march=native -mtune=native%5Cn%22 )%0A            f.write(%22endif%5Cn%22)%0A
     

Update to setup