#! /bin/sh

# $Id$

# defaults:
with_lex=1
with_wlex=1
with_wlex_compat=1
with_ulex=1
with_pp=1
lexlist="utf8,iso88591,iso88592,iso88593,iso88594,iso88595,iso88596,iso88597,iso88598,iso88599,iso885910,iso885913,iso885914,iso885915,iso885916"
version="1.2.9"
exec_suffix=""

help_lex="Enable/disable ocamllex-based lexical analyzer for the -lexlist encodings"
help_wlex="Enable/disable wlex-based lexical analyzer for UTF-8"
help_wlex_compat="Enable/disable wlex-style compatibility package for UTF-8 and ISO-8859-1"
help_ulex="Enable/disable ulex-based lexical analyzer for UTF-8"
help_pp="Enable/disable the build of the preprocessor (pxp-pp)"

options="lex wlex wlex_compat ulex pp"
lexlist_options="utf8 usascii iso88591 iso88592 iso88593 iso88594 iso88595 iso88596 iso88597 iso88598 iso88599 iso885910 iso885913 iso885914 iso885915 iso885916 koi8r windows1250 windows1251 windows1252 windows1253 windows1254 windows1255 windows1256 windows1257 windows1258 cp437 cp737 cp775 cp850 cp852 cp855 cp856 cp857 cp860 cp861 cp862 cp863 cp864 cp865 cp866 cp869 cp874 cp1006 macroman"

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
	printf '    -lexlist %s\n' "$lexlist"
}


usage () {
    formatted_lexlist=""
    n=0
    for lexname in $lexlist_options; do
	formatted_lexlist=`printf '%s%-12s' "$formatted_lexlist" $lexname`
	n=`expr $n + 1`
	if [ $n -eq 5 ]; then
	    n=0
	    formatted_lexlist=`printf '%s\n    ' "$formatted_lexlist"`
	fi
    done

	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
-lexlist <comma-separated-list-of-encodings>:
  Supported encodings are:
    $formatted_lexlist
    all: selects all of them

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
			;;
		-enable-*)
			opt=`echo "$1" | sed -e 's/-enable-//' -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
			;;
		-disable-*)
			opt=`echo "$1" | sed -e 's/-disable-//' -e 's/-/_/g'`
			check_opt "$opt"
			eval "with_$opt=0"
			shift
			;;
	        -lexlist)
		        if [ "$2" = "all" ]; then
			    lexlist="$lexlist_options"
			else
			    lexlist="$2"
			fi
			shift
			shift
			;;
                -prefix|--prefix)
                        echo "[prefix ignored]"; shift 2 ;;
                --prefix=)
                        echo "[prefix ignored]"; shift ;;
                -destdir|--destdir)
                        echo "[destdir ignored]"; shift 2 ;;
                --destdir=)
                        echo "[destdir ignored]"; 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 camlp4 version


printf "%s" "Checking for camlp4... "
if camlp4; then
    if camlp4 -loaded-modules >/dev/null 2>/dev/null; then
        echo "3.10 style"
        camlp4_style="310"
        camlp4_opts="-package camlp4 -syntax camlp4o -ppopt pa_extend.cmo -ppopt q_MLast.cmo"
    else
        echo "3.09 style"
        camlp4_style="309"
        camlp4_opts="-package camlp4 -syntax camlp4o -ppopt pa_extend.cmo -ppopt q_MLast.cmo -ppopt -loc -ppopt loc"
    fi
else
    echo "not found"
    echo "Make sure the camlp4 command is in your PATH"
    exit 1
fi


######################################################################
# Check ocamllex.opt

printf "%s" "Checking for ocamllex.opt..."
r=`ocamllex.opt -help 2>&1`
lex_opt=""
case "$r" in
    *usage*)
	echo "found"
	lex_opt=".opt" ;;
    *)
	echo "not found" ;;
esac

######################################################################
# 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

printf "%s" "Checking for netunidata... "
if ocamlfind query netunidata >/dev/null 2>/dev/null; then
        echo "found"
        netunidata="netunidata"
else
        echo "not found"
        netunidata=""
fi

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

if [ $with_wlex_compat -gt 0 ]; then
    if [ $with_wlex -eq 0 ]; then
	echo "WARNING! -with-wlex-compat implies -with-wlex"
	with_wlex=1
    fi

    if [ $with_lex -eq 0 ]; then
	echo "WARNING! -with-wlex-compat implies -with-lex for ISO-8859-1"
	with_lex=1
	lexlist="iso88591"
    fi

    if grep iso88591 >/dev/null 2>/dev/null <<EOF
$lexlist
EOF
    then :
    else
	echo "WARNING! -with-wlex-compat implies -lexlist iso88591"
	lexlist="$lexlist,iso88591"
    fi
fi

if [ $with_wlex -gt 0 ]; then
	printf "%s" "Checking for wlexing... "
	if ocamlfind query wlexing >/dev/null 2>/dev/null; then
		printf "%s" "library found, "
		
		out=`wlex -help 2>&1`
		case "$out" in
		    usage*) echo "generator found" ;;
		    *)
			echo "generator not found"
			echo "*** It is now required that the full 'wlex' tool is installed, sorry."
			echo "wlex support is disabled"
			with_wlex=0
			with_wlex_compat=0 ;;
		esac
	else
		echo "not found"
		echo "wlex support is disabled"
		with_wlex=0
		with_wlex_compat=0
	fi
fi

######################################################################
# ulex

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

# If ulex not found/disabled, also disable pxp-pp:

if [ $with_ulex -eq 0 ]; then
    with_pp=0
fi


######################################################################
# Check Lexing.lexbuf type

printf "%s" "Checking Lexing.lexbuf type... "
cat <<EOF >tmp.ml
open Lexing
let lb = from_string "";;
let _ = lb.lex_mem;;
let _ = lb.lex_start_p;;
let _ = lb.lex_curr_p;;
EOF

lexbuf_307=""
if ocamlc -c tmp.ml >/dev/null 2>/dev/null; then
    echo "new style"
    lexbuf_307="-D LEXBUF_307"
else
    echo "old style"
fi

rm -f tmp.*

######################################################################
# Check type of camlp4 locations

printf "%s" "Checking type of camlp4 location... "
cat <<EOF >tmp.ml
open Stdpp;;
raise_with_loc (0,0) Not_found;;
EOF

if ocamlc -c -I +camlp4 tmp.ml >/dev/null 2>/dev/null; then
    echo "old style"
    camlp4_loc=""
else
    echo "new style"
    camlp4_loc="-ppopt -DOCAML_NEW_LOC"
fi

rm -f tmp.*


######################################################################
# immutable strings

printf "%s" "Checking for -safe-string... "

string_opts=""
if ocamlc -safe-string; then
    echo "yes"
    string_opts="-safe-string"
else
    echo "no"
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"

if [ $with_pp -gt 0 ]; then
    pkglist="$pkglist pxp-pp"
fi

genpkglist=""
# Generated packages

allgenpkglist=""
# Package names that can possibly be generated

lexlist=`echo "$lexlist" | sed -e 's/,/ /g'`

# reqall: the predecessor list for package "pxp":
reqall="pxp-engine"
for lexname in $lexlist; do
    include=1
    if [ "$lexname" = "utf8" ]; then
	if [ $with_wlex -gt 0 -o $with_ulex -gt 0 ]; then
	    # Leave UTF-8 out
	    include=0
	fi
    fi
    if [ $include -gt 0 ]; then
	reqall="$reqall pxp-lex-$lexname"
    fi
    genpkglist="$genpkglist pxp-lex-$lexname"
done

if [ $with_wlex -gt 0 -a $with_ulex -eq 0 ]; then
    reqall="$reqall pxp-wlex-utf8"
fi
if [ $with_ulex -gt 0 ]; then
    reqall="$reqall pxp-ulex-utf8"
fi

if [ $with_wlex -gt 0 ]; then
    genpkglist="$genpkglist pxp-wlex-utf8"
fi

if [ $with_wlex_compat -gt 0 ]; then
    genpkglist="$genpkglist pxp-wlex"
fi

if [ $with_ulex -gt 0 ]; then
    genpkglist="$genpkglist pxp-ulex-utf8"
fi

for lexname in $lexlist_options; do
    allgenpkglist="$allgenpkglist pxp-lex-$lexname"
done

allgenpkglist="$allgenpkglist pxp-wlex pxp-wlex-utf8"

######################################################################
# Write META for pkglist

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 META and Makefile for lexlist

for enc in $lexlist; do
        pkg="pxp-lex-$enc"
	echo "Writing gensrc/$pkg/META and Makefile"
	mkdir -p "gensrc/$pkg"
	touch "gensrc/$pkg/gen_dir"
	sed -e "s/@VERSION@/$version/g" \
	    -e "s/@ENCNAME@/$enc/g" \
	    gensrc/pxp-lex-pattern/META.in >gensrc/$pkg/META
	echo "# THIS IS A GENERATED FILE - DO NOT EDIT MANUALLY!" >gensrc/$pkg/Makefile
	sed -e "s/@ENCNAME@/$enc/g" \
	    gensrc/pxp-lex-pattern/Makefile.in >>gensrc/$pkg/Makefile
done

######################################################################
# Write META for wlex

if [ $with_wlex -gt 0 ]; then
    pkg="pxp-wlex-utf8"
    echo "Writing gensrc/$pkg/META"
    sed -e "s/@VERSION@/$version/g" \
	-e "s/@ENCNAME@/$enc/g" \
	gensrc/$pkg/META.in >gensrc/$pkg/META
fi

if [ $with_wlex_compat -gt 0 ]; then
    pkg="pxp-wlex"
    echo "Writing gensrc/$pkg/META"
    sed -e "s/@VERSION@/$version/g" \
	-e "s/@ENCNAME@/$enc/g" \
	gensrc/$pkg/META.in >gensrc/$pkg/META
fi

######################################################################
# Write META for ulex

if [ $with_ulex -gt 0 ]; then
    pkg="pxp-ulex-utf8"
    echo "Writing gensrc/$pkg/META"
    sed -e "s/@VERSION@/$version/g" \
	-e "s/@ENCNAME@/$enc/g" \
	gensrc/$pkg/META.in >gensrc/$pkg/META
fi

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

echo "Writing Makefile.conf"
cat <<_EOF_ >Makefile.conf
VERSION = $version
PKGLIST = $pkglist
GENPKGLIST = $genpkglist
ALLGENPKGLIST = $allgenpkglist
EXEC_SUFFIX = $exec_suffix
LEXBUF_307 = $lexbuf_307
LEX_OPT = $lex_opt
CAMLP4_LOC = $camlp4_loc
CAMLP4_STYLE = $camlp4_style
CAMLP4_OPTS = $camlp4_opts
NETUNIDATA = $netunidata
STRING_OPTS = $string_opts
_EOF_

######################################################################
# make oasis happy: setup.save will be picked up by "make postconf"
# and will be appended to setup.data. That way the config update
# will reach oasis.

rm -f setup.save
echo "pkg_version=\"$version\"" >>setup.save
echo "prefix=\"<not interpreted>\"" >>setup.save
echo "destdir=\"\"" >>setup.save

for opt in $options; do
    e="o=\$with_$opt"
    eval "$e"
    if [ $o -gt 0 ]; then
        echo "$opt=\"true\"" >>setup.save
    else
        echo "$opt=\"false\"" >>setup.save
    fi
done

######################################################################
# 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."
