#!/usr/bin/env python

"""
$Id: install-plucker,v 1.30 2000/10/31 18:46:22 nordstrom Exp $

Install Plucker on a Unix/Linux system

Copyright 2000 Michael Nordstrm <micke@sslug.dk>

Distributable under the GNU General Public License Version 2 or newer.
"""

import string, sys, os, shutil

VERSION="1.0"
PYVERSION="1.0"

# are we using the binary package or the source package?
BINPKG='no'

# default is to install everything
TARGET="install"

# set up the Python environment
PYTHON_LIBVER = string.split(sys.version)[0]
while '.' in PYTHON_LIBVER:
        trim = PYTHON_LIBVER[:string.rfind(PYTHON_LIBVER, '.')]
        if '.' not in trim:
                break
        PYTHON_LIBVER = trim
PYTHON_LIBDIR=os.path.join(os.path.join(sys.prefix, 'lib'),'python%s' % PYTHON_LIBVER)
MODULE_DIR=os.path.join(PYTHON_LIBDIR, "site-packages")

def get_answer(question, default = 'n'):
	while 1:
		answer = raw_input (question + "? <y/n> [" + default + "] ")
		if answer:
			if answer == 'y' or answer == 'Y':
				ret = 1
				break
			elif answer == 'n' or answer == 'N':
				ret = 0
				break
			else:
				print "Please answer y or n"
		else:
			if default == 'y' or default == 'Y':
				ret = 1
			else:
				ret = 0
			break
	return ret

#
# Main
#

print "\nThis script will guide you through the installation of Plucker %s" % VERSION
print "If you run this script as root the default values should be OK, if you"
print "want to install Plucker as a normal user you have to change the paths."

CONFIGOPT=""
PREFIX="/usr/local"
answer = raw_input("\nInstallation path prefix [" + PREFIX + "]: ")
if answer:
	PREFIX=answer
	CONFIGOPT = CONFIGOPT + " --prefix=%s" % PREFIX

BIN_DIR=os.path.join(PREFIX, "bin")
answer = raw_input("In which directory do you want to install the binary files [" + BIN_DIR + "]: ")
if answer:
	BIN_DIR=answer
	CONFIGOPT = CONFIGOPT + " --bindir=%s" % BIN_DIR

answer = raw_input("In which directory do you want to install the documentation [" + os.path.join(PREFIX, "doc", "plucker") + "]: ")
if answer:
	CONFIGOPT = CONFIGOPT + " --with-docdir=%s" % answer

answer = raw_input("In which directory do you want to install the Plucker data [" + os.path.join(PREFIX, "share", "plucker") + "]: ")
if answer:
	CONFIGOPT = CONFIGOPT + " --with-pluckerdir=%s" % answer

MOD_DIR=""
init_py = os.path.join(MODULE_DIR, "PyPlucker", "__init__.py")
if os.path.exists(init_py) and get_answer("Use existing Python parser in %s" % MODULE_DIR, 'y'):
		TARGET="install-config install-manual"
else:
	answer = raw_input("In which directory do you want to install the Python parser [" + MODULE_DIR + "]: ")
	if answer:
		MOD_DIR=answer
		CONFIGOPT = CONFIGOPT + " --with-pypluckerdir=%s" % os.path.join(MOD_DIR, "PyPlucker")

if BINPKG != 'yes' and get_answer("Will you be using prc-tools-2.0 to compile Plucker"):
	CONFIGOPT = CONFIGOPT + " --enable-palmos"

if BINPKG != 'yes' and get_answer("Do you want to build the color version"):
	CONFIGOPT = CONFIGOPT + " --enable-color"

print "\nRunning ./configure%s\n" % CONFIGOPT
os.system("cd .. && ./configure%s" % CONFIGOPT)

if not get_answer("Continue", 'y'):
	sys.exit(1)

print "\nRunning make...\n"
os.system("cd .. && make && make %s" % TARGET)

if BIN_DIR:
	if not MOD_DIR:
		MOD_DIR=MODULE_DIR

	if os.path.exists(os.path.join(BIN_DIR, "plucker-build")):
		os.remove(os.path.join(BIN_DIR, "plucker-build"))
	os.symlink(os.path.join(MOD_DIR, "PyPlucker", "Spider.py"), os.path.join(BIN_DIR, "plucker-build"))

	if os.path.exists(os.path.join(BIN_DIR, "plucker-decode")):
		os.remove(os.path.join(BIN_DIR, "plucker-decode"))
	os.symlink(os.path.join(MOD_DIR, "PyPlucker", "PluckerDocs.py"), os.path.join(BIN_DIR, "plucker-decode"))

	if os.path.exists(os.path.join(BIN_DIR, "plucker-dump")):
		os.remove(os.path.join(BIN_DIR, "plucker-dump"))
	os.symlink(os.path.join(MOD_DIR, "PyPlucker", "Decode.py"), os.path.join(BIN_DIR, "plucker-dump"))

if os.getuid() != 0 and os.path.exists(os.path.join(BIN_DIR, "plucker-setup")) and get_answer("\nDo you want to run plucker-setup now", 'y'): 
	os.system(os.path.join(BIN_DIR, "plucker-setup"))
