import os, os.path
import copy, glob, sys
import sipconfig
import PyQt4.pyqtconfig

src_path = '@CMAKE_SOURCE_DIR@'
build_path = '@CMAKE_BINARY_DIR@'
python_path = src_path + '/python'
gdal_inc_dir = '@GDAL_INCLUDE_DIR@'
geos_inc_dir = '@GEOS_INCLUDE_DIR@'
geos_library = '@GEOS_LIB_NAME@'
geos_library_path = '@GEOS_LIB_PATH@'
gdal_library = '@GDAL_LIB_NAME@'
gdal_library_path = '@GDAL_LIB_PATH@'

qt_libs = ["QtCore","QtGui","QtNetwork","QtSvg","QtXml"]
if sys.platform == 'darwin':
  qt_libs.append("QtSql")
  # possibility of universal build of bindings
  osx_archs = '@CMAKE_OSX_ARCHITECTURES@'
  if osx_archs == 'ppc;i386' or osx_archs == 'i386;ppc':
    osx_universal = '@CMAKE_OSX_SYSROOT@'
  else:
    osx_universal = ''
else:
  osx_universal = ''

if len(sys.argv)>1:
  intdir = "/" + sys.argv[1]
else:
  intdir = ""

if len(sys.argv)>2:
  export = sys.argv[2]
else:
  export = ""

# create paths for temporary files if don't exist
if not os.path.isdir("./core"):
  os.mkdir("./core")
if not os.path.isdir("./gui"):
    os.mkdir("./gui")

##########################################################################
# SIP -> *.CPP + *.H

# The name of the SIP build file generated by SIP and used by the build
# system.
build_file_core = build_path + "/python/core/core.sbf"
build_file_gui = build_path + "/python/gui/gui.sbf"

# Get the SIP configuration information.
config = PyQt4.pyqtconfig.Configuration()

# Get the extra SIP flags needed by the imported qt module.  Note that
# this normally only includes those flags (-x and -t) that relate to SIP's
# versioning system.
qt_sip_flags = config.pyqt_sip_flags

# directory where modules will be installed
mod_dir = os.path.join(config.default_mod_dir, "qgis")

# directory where sip files will be installed
sip_dir_core = os.path.join(config.default_sip_dir, "qgis/core")
sip_dir_gui = os.path.join(config.default_sip_dir, "qgis/gui")
  
# Run SIP to generate the code.
print "Parsing SIP files for 'core' library..."
cmd = " ".join([config.sip_bin, "-c", "core", "-b", build_file_core, "-I", config.pyqt_sip_dir, qt_sip_flags, python_path + "/core/core.sip"])
print cmd
os.system(cmd)

print "Parsing SIP files for 'gui' library..."
cmd = " ".join([config.sip_bin, "-c", "gui", "-b", build_file_gui, "-I", python_path, "-I", config.pyqt_sip_dir, qt_sip_flags, python_path + "/gui/gui.sip"])
print cmd
os.system(cmd)


##########################################################################
# MAKEFILES

print "Creating makefiles..."

# We are going to install the SIP specification file for this module and
# its configuration module.
installs = []

# directories relative to core (gui) directories
installs.append([[python_path + "__init__.py", python_path + "qgisconfig.py"], mod_dir])


installs_core = copy.copy(installs)
installs_gui = copy.copy(installs)

# install all sip files
sips_core = glob.glob(python_path + "/core/*.sip")
for sip in sips_core:
  installs_core.append([os.path.basename(sip), sip_dir_core])

sips_gui = glob.glob(python_path + "/gui/*.sip")
for sip in sips_gui:
  installs_gui.append([os.path.basename(sip), sip_dir_gui])


# Create the Makefile.  The QtModuleMakefile class provided by the
# pyqtconfig module takes care of all the extra preprocessor, compiler and
# linker flags needed by the Qt library.
makefile_core = sipconfig.ModuleMakefile(
        configuration=config,
        qt=qt_libs,
        build_file=build_file_core,
        installs=installs_core,
        install_dir=mod_dir,
        dir="core",
        universal=osx_universal)

makefile_gui = sipconfig.ModuleMakefile(
        configuration=config,
        qt=qt_libs,
        build_file=build_file_gui,
        installs=installs_gui,
        install_dir=mod_dir,
        dir="gui",
        universal=osx_universal)

# common settings for both core and gui libs
for mk in [ makefile_core, makefile_gui ]:
  mk.extra_libs = ["qgis_core"]
  if geos_library!="":
    mk.extra_libs.append(geos_library)
  if gdal_library!="":
    mk.extra_libs.append(gdal_library)
  mk.extra_lib_dirs = [build_path+"/src/core"+intdir]
  if geos_library_path!="":
    mk.extra_lib_dirs.append(geos_library_path)
  if gdal_library_path!="":
    mk.extra_lib_dirs.append(gdal_library_path)
  mk.extra_include_dirs = [src_path+"/src/core", src_path+"/src/core/composer", 
                           src_path+"/src/core/raster",
                           src_path+"/src/core/renderer",
                           src_path+"/src/core/spatialindex",
                           src_path+"/src/core/symbology",
                           build_path, # qgsconfig.h, qgssvnversion.h
                           gdal_inc_dir,
                           geos_inc_dir]
  mk.extra_cxxflags = ["-DCORE_EXPORT="+export]

# more settings for gui lib
makefile_gui.extra_libs.append("qgis_gui")
makefile_gui.extra_lib_dirs.append(build_path+"/src/gui"+intdir)
makefile_gui.extra_include_dirs.append(src_path+"/src/gui")
makefile_gui.extra_include_dirs.append(build_path+"/src/gui")
makefile_gui.extra_include_dirs.append(build_path+"/src/ui")
makefile_gui.extra_include_dirs.append(src_path+"/src/plugins") # because of qgisplugin.h TODO: sort out
makefile_gui.extra_cxxflags.append("-DGUI_EXPORT="+export)

# Generate the Makefile itself.
makefile_core.generate()
makefile_gui.generate()

##########################################################################
# QGIS CONFIG

print "Creating qgisconfig.py..."

# Now we create the configuration module.  This is done by merging a Python
# dictionary (whose values are normally determined dynamically) with a
# (static) template.
content = {
    # Publish where the SIP specifications for this module will be
    # installed.
    "qgis_sip_dir":    config.default_sip_dir,

    "qgis_mod_dir":    mod_dir,

    # Publish the set of SIP flags needed by this module.  As these are the
    # same flags needed by the qt module we could leave it out, but this
    # allows us to change the flags at a later date without breaking
    # scripts that import the configuration module.
    "qgis_sip_flags":  qt_sip_flags
}

# This creates the qgisconfig.py module from the qgisconfig.py.in
# template and the dictionary.
sipconfig.create_config_module(build_path+"/python/qgisconfig.py", src_path+"/python/qgisconfig.py.in", content)

print "Done"