Import('env')

import sys
import os

sources = Split("""
  mucipher.i
  wraphelp.c
""")

def file_copy(target, source, env):
    open(str(target[0]), "w").write(open(str(source[0])).read())

py_ver = str(sys.version_info[0]) + "." + str(sys.version_info[1])
py_inc = os.path.join(sys.prefix, "include", "python" + py_ver)

if os.path.exists(os.path.join(py_inc, "Python.h")):
    env_swigpy = Environment(tools = ['default', 'swig'])
    env_swigpy.SConsignFile()
    
    env_swigpy.Append(CPPPATH = env['CPPPATH'] + [py_inc])
    env_swigpy.Append(LIBPATH = env['LIBPATH_MUCIPHER'])
    env_swigpy.Append(LIBS = ['ucipher'])
    env_swigpy.Replace(SHLIBPREFIX='')

    if not os.path.exists("mucipher.py"):
        open(str("mucipher.py"), "w").write( open(os.path.abspath("../../../Mucipher/python/mucipher.py")).read())
    if not os.path.exists("mucipher.i"):
        open(str("mucipher.i"), "w").write(open(os.path.abspath("../mucipher.i")).read())
    if not os.path.exists("wraphelp.c"):
        open(str("wraphelp.c"), "w").write(open(os.path.abspath("../wraphelp.c")).read())
    env_swigpy.Command("mucipher.i", "../mucipher.i", file_copy)
    env_swigpy.Command("wraphelp.c", "../wraphelp.c", file_copy)

    mucipherc = env_swigpy.SharedLibrary('_mucipherc', sources, SWIGFLAGS='-python')
    if type(mucipherc) == type([]):
        mucipherc = mucipherc[0]
    py_dest = env['DESTDIR'] + os.path.join(sys.prefix, "lib", "python" + py_ver, "site-packages")

    dp1 = env_swigpy.Install(py_dest, 'mucipherc.py')
    dp2 = env_swigpy.Install(py_dest, mucipherc)
    dp3 = env_swigpy.Install(py_dest, "mucipher.py")
    Depends(dp1, dp2)

    env.Alias('install_mucipher',  [dp1, dp2, dp3])
    env.Alias('install', 'install_mucipher')
else:
	print "WARNING: Python.h include not found, please install Python's development packages"
