#!/usr/bin/env python
# -*-Python-*-
#
#   Copyright 1998 Rob Tillotson <robt@debian.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License, version 2,
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public License
#   along with this program; if not, write the Free Software Foundation,
#   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

__version__ = '$Id: configure,v 1.7 1998/08/29 01:09:00 rob Exp $'

import sys, os, string
import getopt, re, stat

version = string.join(string.split(string.split(sys.version)[0],'.')[:2],'.')

platform_dir = 'plat-%s' % sys.platform

install_dirs = [ platform_dir, 'App', 'Conduit', 'Tk', 'util' ]

python_lib = os.path.join(os.path.join(sys.prefix, 'lib'),
			  'python%s' % version)

module_path = os.path.join(python_lib, 'site-packages/PDA/Palm')


if module_path[0] == '/': module_path = module_path[1:]

pilot_link = '/usr/local'
static_python = 0
debian = 0

## some utility functions which may be useful elsewhere

search_prefixes = ['/usr/local', '/usr', '/opt']

def locate_matching_file(fname, paths = search_prefixes):
    """Search for a matching filename (using a regular expression) in a
    list of possible paths, returning the full filename that was found."""
    for path in paths:
	try:
	    files = os.listdir(path)
	except os.error:
	    continue
	for fn in files:
	    if re.match(fname, fn):
		return os.path.join(path, fn)

def guess_prefix(fname, dir, paths = search_prefixes):
    """Look in standard Unix locations for a file, assuming that it is
    in a specified subdirectory.  For instance, if DIR is 'bin', then
    the file will be searched for in /usr/bin, /usr/local/bin, etc.
    This function is meant for determining prefixes, as used in
    program configuration.
    """
    for path in paths:
	d = os.path.join(path, dir)
	try:
	    files = os.listdir(d)
	except os.error:
	    continue
	for fn in files:
	    if re.match(fname, fn):
		return path

pilot_link = guess_prefix('libpisock', 'lib')


optlist, args = getopt.getopt(sys.argv[1:], '', [
    'pilot-link-prefix=',
    'static',
    'shared',
    'debian'
    ])

for o, v in optlist:
    if o == '--pilot-link-prefix':
	pilot_link = v
    elif o == '--static':
	static_python = 1
    elif o == '--shared':
	static_python = 0
    elif o == '--debian':
	debian = 1

appmodules = [
    '_pdapalm _pdapalm.c -I%(pilot_link)s/include -I%(pilot_link)s/include/libpisock -L%(pilot_link)s/lib -lpisock',
    '_Doc _Doc.c'
    ]

# write the setup file
f = open('Setup.in', 'w')
if not static_python:
    f.write('*shared*\n')
for x in appmodules:
    f.write(x % vars())
    f.write('\n')
f.close()


# Process makefile.

fi = open('Makefile.in', 'r')
fo = open('Makefile', 'w')
fo.write('# This file is automatically generated it.  Do not edit it.\n\n')
for l in fi.readlines():
    l = re.sub('%%platform_dir%%', platform_dir, l)
    l = re.sub('%%module_path%%', module_path, l)
    l = re.sub('%%install_dirs%%', string.join(install_dirs), l)
    l = re.sub('%%binaries%%', (static_python and ('%s/python' % platform_dir) or
				('%s/*.so' % platform_dir)),l)
    l = re.sub('%%makepython%%', (static_python and 'make python' or 'make'), l)
    fo.write(l)
fo.close()
fi.close()


oldcwd = os.getcwd()

try:
    os.mkdir(platform_dir)
except os.error:
    pass

os.chdir(platform_dir)

os.system('make -f ../Makefile.pre.in srcdir=.. VPATH=.. boot')

print "\n\nNow, type 'make'.\n\n"

