#$Id$
import os
import re
try:
	import SCons.Tool.applelink as applelink
except:
	pass
#include <Python.h>
#include <boost/python.hpp>
PYTHONPREFIX="/usr"
FINKPREFIX=None
env=Environment()
platform=env['PLATFORM']

opts = Options('custom.py')
if platform=="darwin":
    opts.Add('CXX', 'The C++ compiler.',"g++-3.3")
    opts.Add('FINK_PREFIX', 'Prefix for fink on Mac OS X, usually /sw','/sw')
else:
    opts.Add('CXX', 'The C++ compiler.')




class PythonConfig(object):
    def __init__(self, version="2.3", prefix="/usr", libdir=None, incdir=None, libname=None):
        self.version=version
        if libdir:
            self.libdir=libdir
        else:
            self.libdir=prefix+"/lib"
        self.prefix=prefix
        if libname:
            self.libname=libname
        else:
            self.libname="python"+self.version
        if incdir:
            self.incdir=incdir
        else:
            self.incdir=self.prefix+"/include/python"+self.version
PYTHONSEARCH=[\
    PythonConfig(version="2.7"),\
    PythonConfig(version="2.6"),\
    PythonConfig(version="2.5"),\
    PythonConfig(version="2.4"),\
    PythonConfig(version="2.3"),\
    PythonConfig(version="2.4",\
                    libdir="/Library/Frameworks/Python.framework/Versions/2.4/lib",\
                    libname="python",\
                    incdir="/Library/Frameworks/Python.framework/Versions/2.4/include/python2.4"),\
                    PythonConfig(version="2.3"),\
                    PythonConfig(version="2.4")]


env = Environment(options=opts)

try:
  PYTHONSEARCH.insert(0,PythonConfig(version="2.4", prefix=env['FINK_PREFIX'],\
        libdir=env['FINK_PREFIX']+"/lib/python2.4/config/"))
except:
    pass

print env['PLATFORM']
conf = Configure(env)
PYTHON_CONFIG=None
for c in PYTHONSEARCH:
    if conf.CheckCHeader(c.incdir+"/Python.h"):
        PYTHON_CONFIG=c
        break
if not PYTHON_CONFIG:
    print 'Python header not found'
    Exit(1)
#if not conf.CheckCHeader('/usr/include/python2.3/Python.h'):
#    print 'Python 2.3 must be installed'
#    Exit(1)
BOOST_PREFIX="/sw"
env.Append(CPPPATH=[PYTHON_CONFIG.incdir])
if platform =="cygwin":
  env.Append(CPPPATH="/usr/local/include/boost-1_32")
else:
  env.Append(CPPPATH=BOOST_PREFIX+"/include")
print PYTHON_CONFIG.incdir
print env['CPPPATH']
if not conf.CheckCXXHeader('boost/python.hpp'):
    print 'Boost/python must be installed'
    Exit(1)

if not conf.CheckCXXHeader('boost/shared_ptr.hpp'):
    print 'Boost must be installed'
    Exit(1)
    
conf.env.Append(CPPPATH=["../../kernel"])
if not conf.CheckCHeader('mod2.h'):
    print 'must have a Singular build directory'
    Exit(1)
env = conf.Finish()

env = Environment(options=opts)
if (env['PLATFORM']=="darwin"):
    applelink.generate(env)
    
SING_ROOT="../.."
SING_ARCH=re.sub("\r|\n","",os.popen("../../singuname.sh").read())
#PYTHON_VERSION="2.3"
sources=["python.cc", "poly_wrap.cc", "vector_wrap.cc", "CF_wrap.cc",\
             "number_wrap.cc", "playground.cc", "ideal_wrap.cc", "interpreter_support.cc",\
             "ring_wrap.cc", "intvec_wrap.cc"]
#Program("boosttest", 
#	          "boost.cpp",\
#	          CXX="g++-3.3",\
#              CPPPATH=[".", SING_ROOT+"/kernel",\
#                       SING_ROOT+"/Singular", SING_ROOT+"/"+SING_ARCH +"/include",\
#                       PYTHON_CONFIG.incdir, BOOST_PREFIX+"/include", "/sw/include"],
#              LIBS=["boost_python",PYTHON_CONFIG.libname], CPPDEFINES=["NDEBUG","BUILD_MODULE"], SHLIBPREFIX="")
env['LDMODULESUFFIX'] = '.so'
if platform=="cygwin":
  sources.append("mywrapper.cc")
  sources.remove("python.cc")
def module_target(*args,**keywords):
    if env['PLATFORM']=="darwin":
        print "Hello this is a Mac"
        keywords['LINKFLAGS']="-bundle_loader ../../Singular/Singular"
        try:
            keywords['LIBPATH'].append(env['FINK_PREFIX']+"/lib")
            keywords['CPPPATH'].append(env['FINK_PREFIX']+"/include")
        except:
            pass
        return env.LoadableModule(*args,**keywords)
    else:
        if platform=="cygwin":
          keywords['CPPPATH'].append("/usr/local/include/boost-1_32")
          keywords['LIBPATH'].append("/usr/local/lib")
          keywords['LIBPATH'].append("/bin")
          return env.StaticLibrary(*args,**keywords)
        else:
          return env.SharedLibrary(*args,**keywords)
module_target("python_module", 
              sources,\
              CPPPATH=[SING_ROOT+"/kernel",\
                       SING_ROOT+"/Singular", SING_ROOT+"/"+SING_ARCH +"/include",\
                       PYTHON_CONFIG.incdir],\
              LIBPATH=[PYTHON_CONFIG.libdir],\
              CXXFLAGS="-O2",\
              LIBS=["boost_python",PYTHON_CONFIG.libname], CPPDEFINES=["NDEBUG","BUILD_MODULE"], SHLIBPREFIX="")


