#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import fnmatch

def locate(pattern, root=os.curdir):
  ''' Locate all files matching supplied filename pattern in and below supplied root directory. '''
  for path, dirs, files in os.walk(os.path.abspath(root)):
      for filename in fnmatch.filter(files, pattern):
          yield os.path.join(path, filename)
def locate_last(pattern, root=os.curdir):
  for file in locate(pattern, root):
      ret = file
  return ret

env = Environment()

# Figure out the library and link paths for the platform
# Include directories to look for 'Python.h' in
if sys.platform[:3] != "win":
  python_inc_path = os.path.join(sys.prefix, 'include', 'python' + sys.version[:3])
  env.Append(CPPPATH=[python_inc_path])
  env.Append(LIBPATH=['/usr/local/lib/'])

  boost_path = os.environ.get('BOOST_ROOT')
  if boost_path:
    env.Append(CPPPATH=[boost_path])

  libs = ['boost_python', 'exiv2']

else:
  # windows

  boost_path = os.environ.get('BOOST_ROOT')

  if os.environ.get('WindowsSdkDir') != None:
     winsdk_path = os.environ.get('WindowsSdkDir')
  else:
     winsdk_path = os.environ.get('VCINSTALLDIR')

  python_path = os.path.split(sys.executable)[0]
  python_ver = sys.version[0]+sys.version[2]

  exiv2_path = os.path.abspath(os.getcwd() + '\\..\\..\\exiv2')
  expat_path = os.path.abspath(os.getcwd() + '\\..\\..\\expat-2.0.1')

  python_inc_path = python_path + '\\include'
  exiv2_inc_path = exiv2_path + '\\msvc\\include'
  exiv2_src_path = exiv2_path + '\\src'
  expat_inc_path = expat_path + '\\include'
  boost_inc_path = boost_path

  env.Append(CPPPATH=[python_inc_path, exiv2_inc_path, exiv2_src_path,expat_inc_path, boost_inc_path])
  env.Append(CPPFLAGS='/EHsc /D_DLL')

  # Libraries to link against for win
  boost_lib = locate_last('boost_python*mt*.lib',boost_path +
      '\\bin.v2\\libs\\python\\build')
  expat_lib = expat_path + '\\win32\\bin\\release\\libexpat.lib'
  exiv2_lib = exiv2_path + '\\msvc\\exiv2lib\\Release\\exiv2.lib'
  python_lib = python_path +'\\libs\\python'+ python_ver +'.lib'
  uuid_lib = winsdk_path + '\\lib\\uuid.lib'
  kernel_lib = winsdk_path + '\\lib\\kernel32.lib'

  libs = [python_lib, exiv2_lib, boost_lib, expat_lib, '/NODEFAULTLIB:LIBCMT.lib']
  if os.environ.get('WindowsSdkDir') != None: # MSVC Express needs extra libs
     libs = libs + [uuid_lib, kernel_lib]

if sys.platform[:6] == 'darwin':
  # MacOS X
  # link the Python framework
  env['FRAMEWORKS'] += ['Python']

# Libraries to link against
env.Append(LIBS=libs)

# Build shared library libpyexiv2
cpp_sources = ['libpyexiv2.cpp', 'libpyexiv2_wrapper.cpp']
libpyexiv2 = env.SharedLibrary('libpyexiv2', cpp_sources)

# on windows, add a post-build step to embed the manifest using mt.exe
if sys.platform[:3] == "win":
  # The number at the end of the line indicates the file type (1: EXE; 2:DLL)
  env.AddPostAction(libpyexiv2,
      'cd build && "' + winsdk_path +
      '\\bin\\mt.exe" -nologo -manifest libpyexiv2.dll.manifest '
      '-outputresource:libpyexiv2.dll;#2'
      )

# Install the shared library and the Python module, invoked using
# 'scons install'. If DESTDIR is specified on the command line when invoking
# scons, it will be prepended to each installed target file. See
# http://www.gnu.org/prep/standards/html_node/DESTDIR.html for reference.
python_lib_path = os.path.join(sys.prefix,
 'lib', 'python' + sys.version[:3], 'site-packages')
dest_dir = ARGUMENTS.get('DESTDIR')
if (dest_dir is None) or (not os.path.isabs(dest_dir)):
  install_dir = python_lib_path
else:
  install_dir = os.path.join(dest_dir, python_lib_path[1:])
env.Install(install_dir, [libpyexiv2, 'pyexiv2.py'])
env.Alias('install', install_dir)
