#! /bin/sh
# -*- sh -*-

TEMPLATES=""

# parse any settings from command line
while [ $# -gt 0 ]
do
    case $1 in
	--perl=*)
	    perl=`echo $1 | sed 's/--perl=//'`
	    ;;
	--*)
	    cat <<EOF
Usage: $0

  --perl=PATHNAME               use PATHNAME as perl interpreter.

EOF
	    exit 1
	    ;;
	*)
	    TEMPLATES="$TEMPLATES $1"
	    ;;
    esac
    shift
done

# find perl if not set
if [ -z "$perl" ]
then
    perl="`2>&1 which perl`"

    case "$perl" in
	*/*)
	    ;;
	*)
	    echo "Couldn't find perl on your path."
	    exit 1
	    ;;
    esac
fi
echo perl is $perl

echo "Checking perl version..."
pv="`$perl -e 'print $];'`"
case "$pv" in
    5.00[456789]*)
	echo "perl version looks ok ($pv)"
	;;
    *)
	echo "I think you need a newer version (you have $pv)"
	exit 1
	;;
esac

cat > config.status <<EOF
#!/bin/sh
# config.status
# The following configuration was used
#
set -x
$0 $*
#
# You can run this file as a shell script to reconfigure WOTS.
EOF
echo "Made config.status (how configure was used)"

version=`pwd | sed -e 's,.*\-,,'`
echo "Wots version is $version"

for t in $TEMPLATES
do
    out=` echo $t | sed -e 's/.in$//' `

    sed \
        -e "s,@WHICHPERL@,$perl,g" \
        -e "s,@VERSION@,$version,g" \
        -e "s,@prefix@,$prefix,g" \
        $t > $out
    echo "Made $out from $t"
done
