#!/usr/bin/env python

# setup.py.in.distutils
#
# Copyright 2012, 2013 Brandon Invergo <brandon@invergo.net>
# Copyright 2014 Thibaut Paumard
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

HOME_LORENE="@HOME_LORENE@"

from setuptools import setup, Extension
import platform
import re

import numpy
try:
    numpy_include = numpy.get_include()
except AttributeError:
    numpy_include = numpy.get_numpy_include()


if platform.system() == 'Linux':
    doc_dir = '@prefix@/share/doc/@PACKAGE_TARNAME@'
else:
    try:
        from win32com.shell import shellcon, shell
        homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
        appdir = '@PACKAGE_TARNAME@'
        doc_dir = os.path.join(homedir, appdir)
    except:
        pass

gyoto_lib_dirs=['../lib/.libs']
gyoto_libs=['gyoto@FEATURES@']
for libspec in ('@CFITSIO_LIBS@', '@XERCES_LIBS@', '@UDUNITS_LIBS@'):
    libspec=re.split('[ \t]', libspec)
    for comp in libspec:
        if re.match('-L', comp):
            gyoto_lib_dirs.append(re.sub('^-L', '', comp))
        elif re.match('-l', comp):
            gyoto_libs.append(re.sub('^-l', '', comp))

gyoto_module = Extension('gyoto/_core',
                         sources=['core_wrap.cxx'],
                         include_dirs=[numpy_include, '@abs_srcdir@'],
                         library_dirs=gyoto_lib_dirs,
                         libraries=gyoto_libs
                         )

gyoto_std_module = Extension('gyoto/_std',
                         sources=['std_wrap.cxx'],
                         include_dirs=[numpy_include, '@abs_srcdir@'],
                         library_dirs=gyoto_lib_dirs,
                         libraries=gyoto_libs
                         )

gyoto_lorene_module = Extension('gyoto/_lorene',
                         sources=['lorene_wrap.cxx'],
                         include_dirs=[numpy_include, '@abs_srcdir@'],
                         library_dirs=gyoto_lib_dirs,
                         libraries=gyoto_libs
                         )

pymodules = ["gyoto.__init__", "gyoto.core", "gyoto.std", "gyoto_std",
             "gyoto._namespaces", "gyoto.astrobj", "gyoto.metric",
             "gyoto.spectrum", "gyoto.spectrometer"]
extmodules= [gyoto_module, gyoto_std_module]

if HOME_LORENE != "":
    pymodules.append("gyoto.lorene")
    pymodules.append("gyoto_lorene")
    extmodules.append(gyoto_lorene_module)

setup(name='@PACKAGE_NAME@',
      version='@PACKAGE_VERSION@',
      author_email='@PACKAGE_BUGREPORT@',
      py_modules=pymodules,
      ext_modules=extmodules,
      test_suite='tests'
     )
