#!/usr/bin/env python
# $Id: pythonconf.py,v 1.1 2005/10/29 18:38:42 devel Exp $
# Author: Jim E. Brooks http://www.jimbrooks.org
# Print info about Python's installation on this system.

import sys, os
from string import *

# Try to determine Python's major version numbers.
# Unfortunately, sys.version returns too much, forcing it to be parsed
# Worse, the official docs say to use sys.version[:3] which will break
# when Python 10.0 is one day released.
# The major version numbers are needed to determine paths such as:
# /usr/local/lib/python2.3/config/
pythonVersion = sys.version[:3]

# For cygwin, the cygwin version of Python must be in PATH.
# The Windows version will generate MSDOS filenames
# which g++ won't accept eg: -Lc:\python24/lib
if sys.platform.find( "win32" ) != -1 or sys.prefix[1] == ":" or sys.prefix[0] == "\\":
    sys.stderr.write( "ERROR: PATH should point to cygwin version of Python. \n" );
    sys.exit(1)

i = 0
while i < len(sys.argv):
    arg = sys.argv[i]
    if arg == "-cc-flags":
        if sys.platform.find( "darwin" ) != -1:
            print "-Wno-long-double"
    elif arg == "-ld-flags":
        # Print ld flags (linker args).
        if sys.platform.find( "darwin" ) != -1:
            # Apple Mac OS X.
            pass
        else:
            # GNU.
            # fix: "ImportError: lib-dynload/math.so: undefined symbol: PyDict_SetItemString"
            print "-export-dynamic"
    elif arg == "-inc":
        # Print args for C include files
        print "-I" + sys.prefix + "/include/python" + str(pythonVersion)
    elif arg == "-lib" or arg == "-libs":
        # Print library args.
        if sys.platform.find( "darwin" ) != -1:
            libs = "-framework python "
        else:
            libs = "-L" + sys.prefix + "/lib/python" + str(pythonVersion) + "/config" + " -lpython" + str(pythonVersion) + " "
            if find( sys.platform, "bsd" ) != -1:
                libs += "-pthread "
            else:
                libs += "-lpthread "
            if find( sys.platform, "linux" ) != -1:
                libs += "-lutil -ldl "
        print libs[:-1]   # chop final space
    elif arg == "-install-dir":
        # Print where to install Python modules.
        if os.environ["USER"] == "root":
            # root is installing.
            print sys.prefix + "/lib/python" + str(pythonVersion) + "/site-packages"
        elif os.environ["USER"] != "":
            # Normal user is installing.
            if os.environ["HOME"] != "":
                print os.environ["HOME"] + "/bin"
            else:
                sys.stderr.write( "ERROR: Your HOME env var needs to be set \n" );
                sys.exit(1)
        else:
            sys.stderr.write( "ERROR: Your USER env var needs to be set \n" );
            sys.exit(1)
    elif arg == "-min":
        # Check if Python version satisfies minium version requirements (if specified).
        minPythonVersion = sys.argv[i+1]
        if float(pythonVersion) < float(minPythonVersion):
            sys.stderr.write( "ERROR: Required minimum version of Python is " + str(minPythonVersion) + ".\n" )
            sys.stderr.write( "ERROR: This version of Python is " + str(pythonVersion) + ".\n" )
            sys.exit(1)
    i += 1
