import distutils.sysconfig
import numpy
import os
import sys
import pdb
#get all the build variables we need
Import('env','mymode','build_prefix','external_include','limix_include','build_options','limix_LIBS','limix_LIBS_str'),
localenv = env.Clone()

print "Python site-packages directory: %s" % distutils.sysconfig.get_python_lib()

#detect python and numpy include directories
python_lib_path = distutils.sysconfig.get_python_lib()
python_inc_path = distutils.sysconfig.get_python_inc()
numpy_inc_path = numpy.get_include()

#figure out target install path
abs_build_prefix = os.path.join('#',build_prefix)


#add python path and numpy path
localenv.Append(CPPPATH = [python_inc_path,numpy_inc_path])
localenv.Append(SWIGFLAGS=['-python','-c++'])
localenv.Append(SWIGPATH =[limix_include,external_include,'#/src/interfaces/python'])
localenv.Append(LIBS=['limix','nlopt'])
LIBPATH = [python_lib_path,os.path.join(abs_build_prefix,'limix'),os.path.join(abs_build_prefix,'nlopt')]
#add python lib path, windows needs this..
LIBPATH.append(os.path.join(sys.prefix,'libs'))
localenv.Append(LIBPATH=LIBPATH)

#dynamic lookups, needed with MAC at least

if(localenv['PLATFORM']=='win32'):
   #rename modules
   module_in = "_core.dll"
   module_out = "_core.pyd"
   #TODO: no idea how to change runpath settings in windows land 
elif(localenv['PLATFORM']=='darwin'):
   module_in = "_core"
   module_out = "_core.so"  
   #runpath in mac osx is *very* different than linux
   localenv.Append(LINKFLAGS = ['-undefined', 'dynamic_lookup','-Wl,-rpath,@loader_path/.'])
else:
   #LINUX
   module_in = "_core.so"
   module_out = "_core.so"
   localenv.Append(LINKFLAGS = ['-Wl,-rpath=\'$$ORIGIN\'','--enable-new-dtags'])


#run SWIG?
if build_options['reswig']:
   #build from .i file
   srclst = ['limix.i']
else:
   #use precompiled python wrapper
   srclst = ['swigged/limix_wrap.cc']
#add dependencies
localenv.Depends(srclst, limix_LIBS.values())

#build:
limixlib_swig=localenv.LoadableModule('_core',srclst)

#path of wrapper for installer
wrapper_in         = os.path.join(build_prefix,"interfaces","python","core.py")
wrapper_cc_in      = os.path.join(build_prefix,"interfaces","python","limix_wrap.cc")
#path of precompiled wrapper
wrapper_in_swigged = os.path.join('src','interfaces','python','swigged',"core.py")
#limix module in
limix_in    = os.path.join('src','interfaces','python','limix')
#limix script in
limix_bin_in    = os.path.join('src','interfaces','python','bin')

#copy files over for limix modules

#note: we can also use "Move" instead of copy but this results in permanent rebuilds of the limix
#library as the output files will be "missing"
copy_builds = []

#limix module + so
copy_commands = [Delete("$TARGET"), 
   Copy("$TARGET",limix_in),
   Copy("$TARGET",wrapper_in_swigged),
   #Copy("$TARGET",libnlopt[0]),
   Copy("$TARGET",limix_LIBS['nlopt'][0]),
   Copy(os.path.join("$TARGET",module_out),limixlib_swig[0])
   ]
if build_options['reswig']:
   copy_commands.extend([Copy("$TARGET",wrapper_in), 
      Copy(os.path.join('src','interfaces','python','swigged'),wrapper_in),
      Copy(os.path.join('src','interfaces','python','swigged'),wrapper_cc_in)])

copy_lib=Command(target="limix",source=limixlib_swig,action = copy_commands)

#limix scripts
copy_bin=Command(target="bin",source=limixlib_swig,action = [Delete("$TARGET"),Copy("$TARGET",limix_bin_in)])


AlwaysBuild([copy_lib,copy_bin])
Return('limixlib_swig')
