#!/bin/sh

# Generic Python application configure script for Unix

PACKAGE_NAME='Zope'
PACKAGE_LABEL="$PACKAGE_NAME-3.3.1"

DEFAULT_PREFIX="/usr/local/$PACKAGE_LABEL"
prefix="$DEFAULT_PREFIX"


#########################################################################
# XXX The code that searches for an "acceptable" Python is really evil  #
# and doesn't make much sense for a general packaging tool.  Wee        #
# really need to find a better way to deal with this; it may be that    #
# we should simply look for several executable names, and use the       #
# first one we find.  If the installer wants something else, they can   #
# use --with-python.                                                    #
#########################################################################


# Place the optimal target version number (as returned by sys.version)
# below
TARGET="2.4.3"

# Order a list of "acceptable" python version numbers (as returned by
# sys.version) below in "best" to "worst" order, not including the
# target version.  Up to six acceptable python versions are allowed.
# Do not include the target version number in this list!

# We need to list future Python versions here as well, otherwise they
# wouldn't be acceptable :(
ACCEPTABLE="2.4.7 2.4.6 2.4.5 2.4.4 2.4.2 2.4.1"

# provide the executable names for all the acceptable versions
# (and the target version) below
EXENAMES="python python2 python2.4"


# where are we?
HERE="`dirname $0`"

# should we be quiet?
QUIET=""

usage()
{
    echo
    echo "configure [--help] [--quiet] [--with-python=path] [--prefix=path] "
    #echo "          [--ignore-largefile] [--ignore-zlib]"
    echo
    echo "Create a Makefile suitable for building Zope"
    echo
    echo "Options: "
    echo " --help              shows usage and quits"
    echo " --quiet             suppress nonessential output"
    echo " --with-python       specify a path to a Python interpreter to use"
    echo " --prefix            specify an installation path for binary data"
    #echo " --ignore-largefile  ignore large file support warnings"
    #echo " --ignore-zlib       ignore warnings about zlib"
    echo
    echo "Given no options, configure will search your PATH for a suitable"
    echo "Python interpreter and use '$DEFAULT_PREFIX' as a prefix."
    echo
}

# bootstrap ourselves by finding a Python interpreter if necessary
get_python() {
    OLDIFS="$IFS"
    # Why are we playing with the IFS like this???
    IFS=":"
    FOUND=""
    VERSION=""
    FOUNDLIST=""
    out "Testing for an acceptable Python interpreter..."
    out ""
    for DIR in $PATH; do
        IFS="$OLDIFS"
        for EXECUTABLE in $EXENAMES; do
            FULL="$DIR/$EXECUTABLE"
            if [ -x "$FULL" -a -f "$FULL" ]; then
                CMD="import string,sys;a=string.split(sys.version)[0]"
                # Strip trailing + from version number
                CMD="$CMD;a=(a[-1]=='+')and(a[:-1])or(a);print a"
                VERSION=`"$FULL" -c "$CMD"`
                out "  Python version $VERSION found at $FULL"
                if [ "$VERSION" = "$TARGET" ]; then
                    FOUND="$FULL"
                    FOUNDVERSION=$VERSION
                    break 2
                else
                    i=1;
                    for ACC in $ACCEPTABLE; do
                        i=`expr $i + 1`
                        for SLOT in $FOUNDLIST; do
                            if [ $SLOT -eq $i ]; then
                                # slot "i" already populated.  This means we've
                                # already found this particular version of
                                # python.  Continue the for ACC in 
                                # $ACCEPTABLE loop and don't overwrite the
                                # one we already found (interpreters first
                                # on the path win).
                                continue 2
                            fi
                        done
                        if [ "$VERSION" = "$ACC" ]; then
                            FOUNDLIST="$FOUNDLIST $i"
                            eval "FOUND$i=$FULL"
                            eval "FOUNDVERSION$i=$VERSION"
                        fi
                    done
                fi
            fi
        done
    done
    if [ "$VERSION" = "$TARGET" ]; then
        out
        out "  The optimum Python version ($TARGET) was found at $FOUND."
    elif [ -z "$FOUND1" ] && [ -z "$FOUND2" ] && [ -z "$FOUND3" ] &&
         [ -z "$FOUND4" ] && [ -z "$FOUND5" ] && [ -z "$FOUND6" ] ; then
        out
        out "  No suitable Python version found.  You should install"
        out "  Python version $TARGET before continuing.  Versions"
        out "  $ACCEPTABLE also work, but not as optimally."
        exit 1
    else
        if   [ -n "$FOUND1" ]; then
            FOUND=$FOUND1
            FOUNDVERSION=$FOUNDVERSION1
        elif [ -n "$FOUND2" ]; then
            FOUND=$FOUND2
            FOUNDVERSION=$FOUNDVERSION2
        elif [ -n "$FOUND3" ]; then
            FOUND=$FOUND3
            FOUNDVERSION=$FOUNDVERSION3
        elif [ -n "$FOUND4" ]; then
            FOUND=$FOUND4
            FOUNDVERSION=$FOUNDVERSION4
        elif [ -n "$FOUND5" ]; then
            FOUND=$FOUND5
            FOUNDVERSION=$FOUNDVERSION5
        elif [ -n "$FOUND6" ]; then
            FOUND=$FOUND6
            FOUNDVERSION=$FOUNDVERSION6
        fi
        out
        out "  An acceptable, but non-optimal Python version ($FOUNDVERSION)"
        out "  was found at '$FOUND'.  This may be because you have an older "
        out "  or a newer Python version than the optimal version, $TARGET."
        out "  If it's an older version, consider installing $TARGET and run"
        out "  'configure' again."
        out
        out "  If you're not happy with the choice 'configure' made, you can"
        out "  always manually specify a Python interpreter by rerunning"
        out "  the 'configure' script with the '--with--python' option."
    fi
    out ""
}

out() {
    if [ -z "$QUIET" ]; then
        echo $1
    fi
}


while [ "$1" ] ; do
    OPT="$1"
    shift 1
    case "$OPT" in
    --help | --hel | --he | --h | -h)
        usage
        exit 0
        ;;
    --with-python=* | --with-pytho=* | --with-pyth=* | --with-pyt=* )
        FOUND=`echo $OPT | sed -e 's/--[^=][^=]*=//'`
        # use eval to do tilde expansion
        eval "FOUND='$FOUND'"
        ;;
    --with-python | --with-pytho | --with-pyth | --with-pyt )
        FOUND="$1"
        shift 1
        ;;
    --quiet | --quie | --qui | --qu | --q | -q)
        QUIET="true"
        ;;
    --force | --forc | --for | --fo | --f | -f)
        FORCE_APP_HOME="true"
        ;;
    --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=* )
        prefix=`echo $OPT | sed -e 's/--[^=][^=]*=//'`
        # use eval to do tilde expansion
        eval "prefix='$prefix'"
        ;;
    --prefix | --prefi | --pref | --pre | --pr | --p )
        prefix="$1"
        shift 1
        ;;
    *)
        echo "argument not understood: $OPT" >&2
        usage >&2
        exit 2
        ;;
    esac
done

out ""
out "Configuring Zope installation"
out ""

if [ -z "$FOUND" ]; then
    get_python
else
    out "Using Python interpreter at $FOUND"
    out ""
fi

if [ -d "$prefix" -a ! "$FORCE_APP_HOME" ] ; then
    echo 2>&1 "Installation directory $prefix already exists."
    echo 2>&1 "Specify a directory that isn't being used, or"
    echo 2>&1 "use --force to use it anyway."
    out ""
    exit 2
fi

# iff $prefix starts with '/', $FIRSTPART will be ''
FIRSTPART="`echo $prefix | cut -f1 -d/`"
if [ ! "" = "" ] ; then
    prefix="`pwd`/$prefix"
fi

sed -e "s|@prefix@|$prefix|g" \
    -e "s|@PYTHON@|$FOUND|g" \
    "$HERE/Makefile.in" > "$HERE/Makefile"

exit $?
