#!/bin/sh

prefix=/usr/local
bindir=$prefix/bin
bindir_spec=no
libdir=$prefix/lib/ocaml/camlp4
#libdir=`camlp4 -where`
libdir_spec=no
ocamlsrc_spec=false
ocamlsrc=OCAMLSRC
ocamlc_spec=false
ocamlc=ocamlc


usage (){
    echo "Usage:"
    echo "./configure [OPTION]..."
    echo
    echo "Recognized options are:"
    echo "  --prefix <path>	installation prefix [$prefix]"
    echo "  --bindir <path>	user executables [$bindir]"
    echo "  --libdir <path>	camlp4 libraries [$libdir]"
    echo "  --ocamlsrc <path>	ocaml source directory [no default]"
    echo \
     "  --ocamlc <path>	ocaml compiler to be used [ocamlc.opt or ocamlc]"

}

while : ; do
  case "$1" in
    "") break;;
    -help|--help) usage; exit 2;;
    -prefix|--prefix) bindir_spec=yes
                      bindir=$2/bin
                      libdir_spec=yes
                      libdir=$2/lib/ocaml
		      shift;;
    -bindir|--bindir) bindir_spec=yes
                      bindir=$2
		      shift;;
    -libdir|--libdir) libdir_spec=yes
                      libdir=$2
		      shift;;
    -ocamlsrc|--ocamlsrc)  ocamlsrc_spec=true
	                   ocamlsrc=$2
			   shift;;
    -ocamlc|--ocamlc)	ocamlc_spec=true
	  		ocamlc=$2
			shift;;
     *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done



ocv=$($ocamlc -version)
if [ $? -ne 0 ] ; then
    echo compiler $ocamlc not found. Please adjust \$PATH.
    exit 1
fi

if [ $ocv \< "3.09" -o $ocv \> "3.09.99" ] ; then
    echo ocaml version $ocv found. Need 3.09.x.
    exit 1
fi

if [ $ocamlc_spec = false ] ; then
    which ocamlc.opt > /dev/null
    if [ $? ] ; then
	echo ocamlc.opt found.

	ocvo=$(ocamlc.opt -version)

	if [ $ocv != $ocvo ] ; then
	    echo ocamlc and ocamlc.opt have different versions! Please clean up!
	    exit 1
	fi
	ocamlc=ocamlc.opt
    else
	echo ocamlc.opt not found.
    fi
fi


if [ $libdir_spec = false ]; then
    camlp4 >/dev/null 2>&1
    if [ $? -eq 0 ] ; then
	libdir=`camlp4 -where`
    fi
fi


CHECK_SRC_DEPS="\
	/camlp4/camlp4/reloc.cmo \
	/camlp4/lib/token.cmo \
	/camlp4/lib/extfun.cmo \
	/camlp4/lib/stdpp.cmo \
	/camlp4/lib/gramext.cmo \
	/camlp4/lib/grammar.cmo \
	/camlp4/lib/plexer.cmo \
	/camlp4/camlp4/spretty.cmo \
	/camlp4/camlp4/quotation.cmo \
	/camlp4/camlp4/pcaml.cmo \
        "

ocamlsrc_complete=true

if [ $ocamlsrc_spec = "true" ] ; then
    echo Checking ocaml source directory $ocamlsrc ...
    if [ -d $ocamlsrc ] ; then
	for f in $CHECK_SRC_DEPS ; do
	    if [ -f $ocamlsrc/$f ] ; then
		true;
	    else
		echo $ocamlsrc/$f not found!
		ocamlsrc_complete=false
	    fi
	done
	
    else
	echo $ocamlsrc does not exist!
	ocamlsrc_complete=false
    fi
else
    ocamlsrc_complete=false
fi

if [ $ocamlsrc_complete = true ] ; then
    echo ... all necessary files found!
else
    echo "Ocaml source directory not specified (consider --ocamlsrc)"
    echo "or not all necessary files found (cd <ocamlsrc>; ./configure; make world)"
    echo Test frame \"qocheck\" will not compile.
fi

# Summary of the configuration
echo
echo SUMMARY:
echo "  compile with $ocamlc"
echo "  Paths for installation:"
echo "    binaries   will be copied to $bindir"
echo "    libraries  will be copied to $libdir"
if [ $ocamlsrc_complete = "true" ] ; then
    echo "  Testframe \"qocheck\" enabled"
    checkgoal=qocheck
else
    echo "  Testframe \"qocheck\" disabled"
    checkgoal=
fi

sed -e "s|@@@CAMLP4LIBDIR@@@|$libdir|" \
    < check_config.ml.in > check_config.ml


sed -e "s|@@@BINDIR@@@|$bindir|" \
    -e "s|@@@CAMLP4LIBDIR@@@|$libdir|" \
    -e "s|@@@OCAMLSRCDIR@@@|$ocamlsrc|" \
    -e "s|@@@CHECKGOAL@@@|$checkgoal|" \
    -e "s|@@@OCAMLC@@@|$ocamlc|" \
    < Makefile.in > Makefile