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='')

    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')

    py_dest = env['DESTDIR'] + os.path.join(sys.prefix, "lib", "python" + py_ver, "site-packages")

    install = env.Install(py_dest, source = [ mucipherc, 'mucipherc.py',  'mucipher.py'])

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