#! /bin/sh

# $Id: configure,v 1.9 2002/03/15 21:50:56 gerd Exp $

# defaults:
with_lex_iso88591=1
with_lex_utf8=1
with_wlex=1
version="1.1.4"
exec_suffix=""

help_lex_iso88591="Enable/disable ocamllex-based lexical analyzer for ISO-8859-1"
help_lex_utf8="Enable/disable ocamllex-based lexical analyzer for UTF-8"
help_wlex="Enable/disable wlex-based lexical analyzer for ISO-8859-1 and UTF-8"

options="lex_iso88591 lex_utf8 wlex"

print_options () {
	for opt in $options; do
		e="o=\$with_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		if [ $o -gt 0 ]; then
			echo "    -with-$uopt"
		else
			echo "    -without-$uopt"
		fi
	done
}


usage () {
	cat <<_EOF_ >&2
usage: ./configure [ options ]

_EOF_
	for opt in $options; do
		e="help=\$help_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		echo "-with-$uopt:" >&2
		echo "-without-$uopt:" >&2
		echo "        $help" >&2
	done
	cat <<_EOF_ >&2

Defaults are:

_EOF_
	print_options >&2
	exit 1
}


check_opt () {
	for x in $options; do
		if [ "$x" = "$1" ]; then
			return 0
		fi
	done
	echo "Unknown option: $1" >&2
	exit 1
}


while [ "$#" -gt 0 ]; do
	case "$1" in
		-with-*)
			opt=`echo "$1" | sed -e 's/-with-//' -e 's/-/_/g'`
			check_opt "$opt"
			eval "with_$opt=1"
			shift
			;;
		-without-*)
			opt=`echo "$1" | sed -e 's/-without-//' -e 's/-/_/g'`
			check_opt "$opt"
			eval "with_$opt=0"
			shift
			;;
		-version*)
			echo "$version"
			exit 0
			;;
		*)
			usage
	esac
done

######################################################################
# Check ocamlfind

printf "%s" "Checking for ocamlfind... "
if ocamlfind query stdlib >/dev/null 2>/dev/null; then
	echo "found"
else
	echo "not found"
	echo "Sorry, installation is not possible without ocamlfind (findlib)!"
	echo "Make sure that ocamlfind is in your PATH, or download findlib"
	echo "from www.ocaml-programming.de"
	exit 1
fi

######################################################################
# Check netstring

printf "%s" "Checking for netstring... "
if ocamlfind query netstring >/dev/null 2>/dev/null; then
	echo "found"
else
	echo "not found"
	echo "Sorry, installation is not possible without netstring!"
	echo "Please download netstring from www.ocaml-programming.de"
	exit 1
fi

######################################################################
# Check wlex

if [ $with_wlex -gt 0 ]; then
	printf "%s" "Checking for wlexing... "
	if ocamlfind query wlexing >/dev/null 2>/dev/null; then
		echo "found"
	else
		echo "not found"
		echo "wlex support is disabled"
		with_wlex=0
	fi
fi

######################################################################
# Check Lexing module

printf "%s" "Checking Lexing.lexbuf type... "

cat <<EOF >.testscript
open Lexing;;
let f b = b.lex_buffer_len;;
EOF

ocaml .testscript >.testout 2>&1

if [ -s .testout ]; then
    echo "new style"
    lex_buffer_len=lex_buffer_end
else
    echo "old style (up to O'Caml 3.04)"
    lex_buffer_len=lex_buffer_len
fi

######################################################################
# Check cygwin

printf "%s" "Checking for cygwin... "
u=`uname`
case "$u" in
	CYGWIN*)
		echo "found"
		exec_suffix=".exe"
		;;
	*)
		echo "not found"
		;;
esac

######################################################################
# Summary

echo
echo "Effective options:"
print_options
echo

pkglist="pxp pxp-engine"
reqall="pxp-engine"
for opt in $options; do
	e="o=\$with_$opt"
	eval "$e"
	if [ $o -gt 0 ]; then
		uopt=`echo "$opt" | sed -e 's/_/-/g'`
		pkglist="$pkglist pxp-$uopt"
		# Don't include pxp-lex* into reqall if pxp-wlex is available
		if [ "$with_wlex" -gt 0 ]; then
			case "$opt" in
				lex_*)
					true ;;
				*)
					reqall="$reqall pxp-$uopt" ;;
			esac
		else
			reqall="$reqall pxp-$uopt"
		fi
	fi
done

######################################################################
# Write META.in

for pkg in $pkglist; do
	echo "Writing src/$pkg/META"
	sed -e "s/@VERSION@/$version/g" \
	    -e "s/@REQALL@/$reqall/g" \
	    src/$pkg/META.in >src/$pkg/META
done

######################################################################
# Write Makefile.conf

echo "Writing Makefile.conf"
cat <<_EOF_ >Makefile.conf
VERSION = $version
PKGLIST = $pkglist
EXEC_SUFFIX = $exec_suffix
LEX_BUFFER_LEN = $lex_buffer_len
_EOF_

######################################################################
# Finish

echo
echo "You can now compile PXP by invoking"
echo "   make all"
echo "for the bytecode compiler, and optionally by invoking"
echo "   make opt"
echo "for the native-code compiler (if supported on your architecture)."
echo "Finally, a"
echo "   make install"
echo "will install the package."
