#!/bin/sh

##################################
#
#  Configuration script for otags
# 
##################################

cmdline="$0 $*"

root=/usr/local
bindir=$root/bin
bindir_spec=no
libdir=$root/lib/ocaml/camlp4
#libdir=`camlp4 -where`
libdir_spec=no
#splaydir=$root/lib/ocaml
#splaydir_spec=no
ocamlc=
ocamlopt=
native=
byterequest=0
versioncheck=1
ocamlbindir=

usage (){
    echo "Usage:"
    echo "./configure [OPTION]..."
    echo
    echo "Recognized options are:"
    echo "  --prefix <path>	installation prefix [/usr/local]"
    echo "  --bindir <path>	user executables [PREFIX/bin]"
    echo "  --libdir <path>	camlp4 libraries [PREFIX/lib/ocaml]"
    echo "  --bytecode 		don't use native compiler (for testing only)"
    echo "  --no-version-check 	don't check for correct ocaml version (not recommended)"
    echo "  --ocaml <path>	location of ocaml binaries [searched in PATH]"
}

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
#                      splaydir_spec=yes
#                      splaydir=$2/lib/ocaml
		      shift;;
    -bindir|--bindir) bindir_spec=yes
                      bindir=$2
		      shift;;
    -libdir|--libdir) libdir_spec=yes
                      libdir=$2
		      shift;;
    -bytecode|--bytecode)  byterequest=1;;
    -no-version-check|--no-version-check)
	  versioncheck=0;;
    -ocaml|--ocaml)   ocamlbindir="$2/";
	              shift;;
     *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done


# check for ocamlc
ocv=$(${ocamlbindir}ocamlc -version)
if [ $? -ne 0 ] ; then
    echo compiler ${ocamlbindir}ocamlc not found. 
    echo Please adjust \$PATH or use --ocaml
    exit 1
else
    echo ${ocamlbindir}ocaml version $ocv found.
    ocamlc=${ocamlbindir}ocamlc
fi

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


# check for ocamlopt.opt
ocvo=$(${ocamlbindir}ocamlopt.opt -version)
if [ $? -eq 0 -a $byterequest = 0 ] ; then
    echo ${ocamlbindir}ocamlopt.opt version $ocvo found. Native compilation enabled.

    if [ "$ocv" != "$ocvo" ] ; then
	echo ocamlc and ocamlopt.opt have different versions! Please clean up!
	exit 1
    fi
    ocamlopt=${ocamlbindir}ocamlopt.opt
    native=true
else

    # check for ocamlopt
    ocvo=$(${ocamlbindir}ocamlopt -version)
    if [ $? -eq 0 -a $byterequest = 0 ] ; then
	echo ${ocamlbindir}ocamlopt version $ocvo found. Nativs compilation enabled.

	if [ "$ocv" != "$ocvo" ] ; then
	    echo ocamlc and ocamlopt have different versions! Please clean up!
	    exit 1
	fi
	ocamlopt=${ocamlbindir}ocamlopt
	native=true
    else
	echo Neither ocamlopt nor ocamlopt.opt found. Native compilation disabled.
	native=false
    fi
fi

if [ $native = "true" ] ; then
    ncamlgoals="camlp4o_pr_emacs camlp4o_pr_vi"
    otags=otags.opt
else
    ncamlgoals=
    otags=otags.byte
fi

# check for ocamlc.opt
ocvo=$(${ocamlbindir}ocamlc.opt -version)
if [ $? -eq 0 ] ; then
    echo ${ocamlbindir}ocamlc.opt version $ocvo found.

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

# check for camlp4o
ocvo=$(${ocamlbindir}camlp4o -version)
if [ $? -eq 0 ] ; then
    echo ${ocamlbindir}camlp4o version $ocvo found.

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


# check for camlp4
ocvo=$(${ocamlbindir}camlp4 -version)
if [ $? -eq 0 ] ; then
    echo ${ocamlbindir}camlp4 version $ocvo found.

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



./mk_conf "$cmdline" $libdir $bindir $native > conf.ml

# Summary of the configuration
echo
echo "  Configuration summary:"
echo "    binaries   will be copied to $bindir"
echo "    libraries  will be copied to $libdir"
if [ $native = "true" ] ; then
    echo "    native-code compilation enabled"
else
    echo "    native-code compilation disabled"
fi



sed -e "s|tplbin|$bindir|" \
    -e "s|tpllib|$libdir|" \
    -e "s|tplnative|$native|" \
    -e "s|tplocamlc|$ocamlc|" \
    -e "s|tplocamlopt|$ocamlopt|" \
    -e "s|tplcamlp4o|$camlp4o|" \
    -e "s|tplncamlgoals|$ncamlgoals|" \
    -e "s|tplotags|$otags|" \
    Makefile.here.tpl > Makefile.here
#    -e "s|tplsplay|$splaydir|" \

