#!/bin/sh
# This is the configuration script for Rubber.
# As a part of Rubber, it is covered by the GPL (see COPYING for details).
# (c) Emmanuel Beffara, 2002--2006

# this is the only place where there is version number:
version=1.1
release=1

python=""
prefix=/usr/local
bindir='${prefix}/bin'
datadir='${prefix}/share'
moddir='${datadir}/rubber'
mandir='${prefix}/man'
infodir='${prefix}/info'

###  First, parse the command line.

while test -n "$1"; do
	case $1 in
		--help)
			cat <<EOF
Usage: $0 [options]
Options: [defaults in brackets after descriptions]
  --help             print this message
  --python=PYTHON    use the interpreter named PYTHON
  --prefix=PREFIX    install files in PREFIX [$prefix]
  --bindir=DIR       user executables in DIR [PREFIX/bin]
  --datadir=DATADIR  read-only data in DATADIR [PREFIX/share]
  --moddir=DIR       python modules in DIR [DATADIR/rubber]
  --mandir=DIR       man documentation in DIR [PREFIX/man]
  --infodir=DIR      texinfo documentation in DIR [PREFIX/info]
EOF
			exit
			;;

		--python=*)
			python=${1#*=}
			;;
		--prefix=*)
			prefix=${1#*=}
			;;
		--bindir=*)
			bindir=${1#*=}
			;;
		--datadir=*)
			datadir=${1#*=}
			;;
		--moddir=*)
			moddir=${1#*=}
			;;
		--mandir=*)
			mandir=${1#*=}
			;;
		--infodir=*)
			infodir=${1#*=}
			;;
		*)
			echo "$0: unrecognized option $1" 1>&2
	esac
	shift
done


###  Check that we can install the program.

if test -z "$python"; then
	for cmd in python2.2 python2 python; do
		if which $cmd > /dev/null 2>&1; then
			python=$cmd
			break
		fi
	done

	if test -z "$python"; then
		cat <<EOF
I could not run the Python interpreter. Since Rubber is written in Python, you
need a working Pyhton installation. Please make it so...
EOF
		exit 1
	fi

	if test $python = python; then
		echo "I found Python, this is a good start."
	else
		echo "I found Python 2 (as $python), good."
	fi


elif which $python > /dev/null 2>&1; then
	echo "I will use $python as the interpreter."

else
	cat <<EOF
You told me to use $python as the interpreter but I cannot run it.
No offense, but you're not helping, here.
EOF
	exit 1
fi

python=`which $python`

$python setup.py check || exit 1

echo "The system seems all right, so let's configure Rubber."


###  Configure the package

cat > settings.py <<EOF
sub = {
  "python": "$python",
  "version": "$version",
  "release": "$release",
  "prefix": "$prefix",
  "bindir": "$bindir",
  "datadir": "$datadir",
  "moddir": "$moddir",
  "mandir": "$mandir",
  "infodir": "$infodir"
}
EOF

echo "settings.py generated"

env PYTHONPATH=.:$PYTHONPATH $python setup.py config
