import os
import glob
from SCons.Errors import BuildError

try:
    from subprocess import check_output
    
except:
    raise BuildError(errstr = "import check_output failed: are you running python 2.7")
    

def get_root_flags():
    root_incs = check_output("root-config --cflags", shell=True)
    root_libs = check_output("root-config --libs", shell=True)
    return root_incs, root_libs


def get_arma_flags():
    try:
        armadillo_lib     = os.environ["ARMA_LIB"]
        armadillo_include = os.environ["ARMA_HEADER"]
        return armadillo_include, armadillo_lib

    except:
       raise BuildError(errstr = "Can't find armadillo, is it installed? - if non-standard install location please update pointer in env.sh")


def get_hdf_flags():
    try:
        hdf_lib     = os.environ["HDF_LIB"]
        hdf_include = os.environ["HDF_HEADER"]
        return hdf_include, hdf_lib

    except:
       raise BuildError(errstr = "Can't find hdf - is it installed? if non-standard install location please update pointer in env.sh")


def get_gsl_flags():
    try:
        gsl_lib     = os.environ["GSL_LIB"]
        gsl_include = os.environ["GSL_HEADER"]
        return gsl_include, gsl_lib

    except:
        raise BuildError("Can't find armadillo - if non-standard install location please update pointer in env.sh")
        

def write_compile_script(env, dir):
    includes  = " ".join(["-I" + os.path.abspath(x) for x in env["CPPPATH"]])
    lib_paths = " ".join(["-L" + os.path.abspath(x) for x in env["LIBPATH"]])
    libs      = " ".join(["-l" + x for x in env["LIBS"]])

    with open(os.path.join(dir, "compile.sh"), "w") as f:
        f.write(
            ''' # This file is auto generated by scons
g++ $1 -o $(basename "$1" .cpp) -O2 {0} {1} {2}
'''.format(includes, lib_paths, libs)
)
