#!/usr/bin/env python
#
# Setup script for Berkeley DB XML package
# $Id: setup.py.template,v 1.7 2005/10/26 12:25:59 gmf Exp $

import os, sys
from distutils.core import setup, Extension

#
# Posix:
#
# db_home, xquery_home, pathan_home, and xerces_home are
# set based on the arguments  passed to the configure script.
# This can be overridden with the --with-berkeleydb, --with-xquery, 
# --with-xerces, and --with-pathan arguments.
#
# Windows: may require further editing to reflect site specifics.
#
if os.name == "posix":
  db_home = "@DB_DIR@"
  xerces_home = "@XERCES_DIR@"
  pathan_home = "@PATHAN_DIR@"
  xquery_home = "@XQUERY_DIR@"
else:
  xerces_home = "../@XERCES_WINHOME@"
  pathan_home = "../@PATHAN_WINHOME@"
  xquery_home = "../@XQUERY_HOME@"
  db_home = "../@DB_HOME@"

for arg in sys.argv:
  if arg.startswith('--with-berkeleydb='):
    db_home = arg.split('=')[1]
  elif arg.startswith('--with-xerces='):
    xerces_home = arg.split('=')[1]
  elif arg.startswith('--with-pathan='):
    pathan_home = arg.split('=')[1]
  elif arg.startswith('--with-xquery='):
    xquery_home = arg.split('=')[1]

sys.argv = filter(lambda x: not x.startswith("--with-"), sys.argv)

debug = "--debug" in sys.argv or "-g" in sys.argv

# setup complains when passed debug flags for install
if "install" in sys.argv and not "build" in sys.argv:
  sys.argv = filter(lambda x: (x != "--debug") and (x != "-g"), sys.argv)

if os.name == "posix":
  INCLUDES =    ["../../include",
                 os.path.join(db_home, "include")]

  LIBDIRS =     ["../../build_unix/.libs",
                 os.path.join(db_home, "lib"),
                 os.path.join(pathan_home, "lib"),
                 os.path.join(xquery_home, "lib"),
                 os.path.join(xerces_home, "lib")]

  LIBS =        ["dbxml-@DBXML_VERSION_MAJOR@",
                 "db_cxx-@DB_VERSION_MAJOR@",
                 "xquery", "pathan", "xerces-c"]

  DATAFILES =   []

elif os.name == "nt":
  INCLUDES =    ["../../include",
                 os.path.join(db_home, "build_win32"),
                 os.path.join(db_home, "dbinc")]

  if debug:
    LIBDIRS =   ["../../../lib",
                 "../../build_win32/Debug",
                 os.path.join(db_home, "build_win32/Debug"),
                 os.path.join(pathan_home, "lib"),
                 os.path.join(xquery_home, "lib"),
                 os.path.join(xerces_home, "Build/Win32/VC7")]
  
    LIBS =      ["libdbxml@DBXML_VERSION_MAJOR@@DBXML_VERSION_MINOR@D",
                 "@DB_LIB@D", "@XQUERY_LIB@d",
                 "@PATHAN_LIBD7@", "@XERCES_LIB@D"]

    DATAFILES = [("", ["../../../bin/debug/libdbxml@DBXML_VERSION_MAJOR@@DBXML_VERSION_MINOR@D.dll",
                       "../../../bin/debug/libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@D.dll",
                       "../../../bin/debug/@XQUERY_LIB@d.dll",
                       "../../../bin/debug/@PATHAN_LIBD7@.dll",
                       "../../../bin/debug/@XERCES_DLL@D.dll"])]
  else:
    LIBDIRS =   ["../../../lib",
                 "../../build_win32/Release",
                 os.path.join(db_home, "build_win32/Release"),
                 os.path.join(xquery_home, "lib"),
                 os.path.join(pathan_home, "lib"),
                 os.path.join(xerces_home, "Build/Win32/VC7")]
  
    LIBS =      ["libdbxml@DBXML_VERSION_MAJOR@@DBXML_VERSION_MINOR@",
                 "@DB_LIB@", "@XQUERY_LIB@",
                 "@PATHAN_LIB7@", "@XERCES_LIB@"]

    DATAFILES = [("", ["../../../bin/libdbxml@DBXML_VERSION_MAJOR@@DBXML_VERSION_MINOR@.dll",
                       "../../../bin/libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@.dll",
                       "../../../bin/@XQUERY_LIB@.dll",
                       "../../../bin/@PATHAN_LIB7@.dll",
                       "../../../bin/@XERCES_DLL@.dll"])]

else:
  print "I don't know anything about your platform '%s'." % os.name
  print "Please check the build instructions for more information"
  sys.exit(1)

# Now run with whatever settings we've got
setup(name = "dbxml",
      version = "@DBXML_VERSION_MAJOR@.@DBXML_VERSION_MINOR@.@DBXML_VERSION_PATCH@",
      description = "Berkeley DB XML Python API",
      long_description = """\
      This module provides a complete wrapping of the C++ API
      to the Berkeley DB XML (BDB XML) native XML database.  BDB
      XML can be used to store, retrieve, manage, and query (using
      XQuery) XML documents.""",
      author = "Sleepycat Software",
      author_email = "support@sleepycat.com",
      url = "http://www.sleepycat.com/products/bdbxml.html",
      py_modules = ["dbxml"],
      ext_modules = [Extension("_dbxml", ["dbxml_python_wrap.cpp"],
                               include_dirs = INCLUDES,
                               library_dirs = LIBDIRS,
                               libraries = LIBS)],
      data_files = DATAFILES)
