#!/bin/sh
#
# Simple configure script for mod_auth_pubtkt
#

# Defaults
APXS=/usr/sbin/apxs
test -x $APXS || unset APXS
if [ -z $APXS ]; then
  APXS=/usr/bin/apxs
  test -x $APXS || unset APXS
fi
if [ -z $APXS ]; then
  APXS=/usr/bin/apxs2
  test -x $APXS || unset APXS
fi

ME=`basename $0`
DIR=`dirname $0`
if [ $DIR = '.' ]; then
  DIR=`pwd`
fi

usage() {
  echo "usage: $ME [--apxs=/path/to/apxs] [--apachever=<1.3|2|2.2|2.4>] [--debug]"
}
die() {
  echo $*
  exit 2
}

# Retrograde option handling to allow for primitive getopts
ac_prev=
for ac_option
do
	# If the previous option needs an argument, assign it.
	if test -n "$ac_prev"; then
		eval "$ac_prev=\$ac_option"
		ac_prev=
		continue
	fi
	ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`
	case $ac_option in
		--apxs=*)
			APXS=$ac_optarg
			;;
		--apxs)
			ac_prev=APXS
			;;
		--apachever=*)
			VERSION=$ac_optarg
			;;
		--debug)
			DEBUG="-g -Wall -ansi -Wno-implicit-function-declaration -Wno-long-long" 
			;;
		-h | --help)
			usage;
			exit 0
			;;
		*)
			usage;
			exit 1
			;;
	esac
done

# Sanity checks
test "$ac_prev" = "APXS" && die "Error: option '--apxs' requires an argument"
test -n "$APXS" || die "Error: cannot locate apxs (use --apxs=/path/to/apxs)"
test -x $APXS || die "Error: missing apxs '$APXS' (use --apxs=/path/to/apxs)"

# Get Apache version
if [ -z "$VERSION" ]; then
	HTTPD=`$APXS -q SBINDIR`/`$APXS -q TARGET`
	test -x $HTTPD || die "Error: cannot determine apache version (use --apachever=<1.3|2|2.2|2.4>)"
	VERSION=`$HTTPD -v | head -1 | sed -e 's/.*Apache\///' -e 's/^\([0-9]\.[0-9]*\).*/\1/'`
fi
# Standardise
test $VERSION = '1'   && VERSION=1.3
test $VERSION = '2.0' && VERSION=2
test $VERSION = '20'  && VERSION=2
test $VERSION = '22'  && VERSION=2.2
test $VERSION = '24'  && VERSION=2.4
if [ $VERSION != '1.3' -a $VERSION != '2' -a $VERSION != '2.2' -a $VERSION != '2.4' ]; then
  die "Error: apache version '$VERSION' not supported"
fi

# Generate Makedefs
DIV="#-------------------------------------------------------------------------"
WARNING="# Generated by $ME, do not edit!"
test -f Makedefs && rm -f Makedefs
test -f Makedefs && die "Error deleting Makedefs"

echo $DIV >> Makedefs
echo $WARNING >> Makedefs
echo >> Makedefs
echo "VERSION = $VERSION" >> Makedefs
echo "APXS = $APXS" >> Makedefs
test -n "$DEBUG" && echo "CFLAGS += $DEBUG" >> Makedefs
if [ "$VERSION" = "1.3" ]; then
  echo "CFLAGS += -DAPACHE13" >> Makedefs
  echo "TARGET = mod_auth_pubtkt.so" >> Makedefs
else
  if [ $VERSION = "2.2" ]; then
    echo "CFLAGS += -DAPACHE22" >> Makedefs
  elif [ $VERSION = "2.4" ]; then
	echo "CFLAGS += -DAPACHE24" >> Makedefs
  fi
  echo "TARGET = mod_auth_pubtkt.la" >> Makedefs
fi
echo "BASEDIR = $DIR" >> Makedefs

# proper handling of Universal Binaries under Mac OS X
HTTPD="`${APXS} -q SBINDIR`/`${APXS} -q TARGET`"
if test -x /usr/bin/lipo; then
	ARCHITECTURES=`/usr/bin/lipo -info $HTTPD | sed -e 's/.*://'`
	for ARCH in $ARCHITECTURES; do
		echo "CFLAGS += -arch ${ARCH}" >> Makedefs
		echo "LDFLAGS += -arch ${ARCH}" >> Makedefs
	done
fi

if [ -d /usr/share/man ]; then
  echo "MANPATH = /usr/share/man" >> Makedefs
else
  echo "MANPATH = /usr/man" >> Makedefs
fi

echo >> Makedefs
echo $WARNING >> Makedefs
echo $DIV >> Makedefs

# Finish with a 'make clean'
make -s clean
