#!/bin/sh

##############################################################################
#
# Configuration script for otags III
# 
# Hendrik Tews Copyright (C) 2010 - 2017
# 
# This file is part of "Otags III".
# 
# "Otags III" is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# "Otags III" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License in file COPYING in this or one of the parent
# directories for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "Otags III". If not, see
# <http://www.gnu.org/licenses/>.
# 
##############################################################################

set -e

REQUIRED_OCAML_VERSION=4.05
OTAGS_VERSION=1

root=/usr/local
bindir=$root/bin
mandir=$root/share/man
native=
native_goals=
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 "  --mandir <path>	man pages [PREFIX/share/man]"
    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=$2/bin
                      mandir=$2/share/man
		      shift;;
    -bindir|--bindir) bindir=$2
		      shift;;
    -mandir|--mandir) mandir=$2
		      shift;;
    -bytecode|--bytecode)  byterequest=1;;
    -no-version-check|--no-version-check)
	  versioncheck=0;;
    -ocaml|--ocaml)   ocamlbindir="$2/";
    	              shift;;

    # The following option is only here for simplifying the 
    # make-distribution script.
    -abra-print-version) 
	  echo $REQUIRED_OCAML_VERSION.$OTAGS_VERSION;
	  exit 0;;
     *) echo "Unknown option \"$1\"." 1>&2; usage; exit 2;;
  esac
  shift
done

# disable error checking
set +e

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

# check ocamlc version
if [ $versioncheck = 1 ] ; then
    if [ "$ocbv" \< $REQUIRED_OCAML_VERSION ] ; then
	echo This version of otags needs at least OCaml $REQUIRED_OCAML_VERSION.x.
	echo Aborting.
	exit 1
    fi
    if [ "$ocbv" \> $REQUIRED_OCAML_VERSION".99" ] ; then
	cat - <<EOF

WARNING: You attempt to compile otags with a more recent OCaml version than
it was released for. (This version of otags was released for OCaml $REQUIRED_OCAML_VERSION.x.)

If it compiles without errors, otags should just ignore the new OCaml
features (if there are any). If you only see errors in the file
tags.ml you may get a working version by deleting the offending match
cases in tags.ml. For other errors I don't have any generic advice to
share. Good luck!

EOF
    fi
fi

# disable error checking
set +e

# check for ocamlopt.opt
ocvo=$(${ocamlbindir}ocamlopt.opt -version)
if [ $? -eq 0 ] ; then
    set -e
    if [ $ocbv != $ocvo ] ; then
	echo ${ocamlbindir}ocamlc and ${ocamlbindir}ocamlopt.opt have different versions.
	echo Please check your installation!
	echo Aborting.
	exit 1
    fi

    echo ${ocamlbindir}ocamlopt.opt version $ocvo found. 
    
    if ${ocamlbindir}ocamlopt.opt -o /dev/null \
	unix.cmxa dynlink.cmxa ;
    then
	echo Native compilation enabled.
	native=true
    else
	echo ocamlopt.opt unix.cmxa dynlink.cmxa failed.
	echo Native compilation disabled.
	native=false
    fi
else
    set -e
    echo ocamlopt.opt not found. Native compilation disabled.
    native=false
fi

if [ $byterequest = 1 ] ; then
    native=false
fi

if [ $native = "true" ] ; then
    ocamldep=${ocamlbindir}ocamldep.opt
    ocamlc=${ocamlbindir}ocamlc.opt
    ocamlopt=${ocamlbindir}ocamlopt.opt
    otags=otags.opt
else
    ocamldep=${ocamlbindir}ocamldep
    ocamlc=${ocamlbindir}ocamlc
    ocamlopt=false
    otags=otags.byte
fi

if [ $native = "true" ] ; then
    # disable error checking
    set +e

    # check ocamlc.opt if native detected
    ocvo=$(${ocamlc} -vnum)
    if [ $? -ne 0 ] ; then
	set -e
	echo $ocamlopt exists but $ocamlc not.
	echo Please check your ocaml installation!
	echo Aborting.
	exit 1
    elif [ $ocvo != $ocbv ] ; then
	set -e
	echo $ocamlc and $ocamlopt have different versions!
	echo Please check your installation!
	echo Aborting.
	exit 1
    else
	set -e
	echo ${ocamlc} version $ocvo found.
    fi
fi


# disable error checking
set +e

# check ocamldep
ocdepv=$($ocamldep -vnum)
if [ $? -ne 0 -o $ocdepv != $ocbv ] ; then
    set -e
    echo $ocamlc exists but $ocamldep not. 
    echo Please check your ocaml installation!
    echo Aborting.
    exit 1
else
    set -e
fi

if [ $ocdepv != $ocbv ] ; then
    echo $ocamlc and $ocamldep have different versions.
    echo Please check your ocaml installation!
    echo Aborting.
    exit 1
fi


# Summary of the configuration
echo
echo "  Configuration summary:"
echo "    binaries   will be installed in $bindir"
echo "    man pages  will be installed in $mandir"
if [ $native = "true" ] ; then
    echo "    native-code compilation enabled"
else
    echo "    native-code compilation disabled"
fi

# Make conf.ml
sed -e "s|@REQUIRED_OCAML_VERSION@|$REQUIRED_OCAML_VERSION|" \
    -e "s|@OTAGS_VERSION@|$OTAGS_VERSION|" \
    conf.ml.in > conf.ml


# Make the Makefile
sed -e "s|@BINDIR@|$bindir|" \
    -e "s|@MANDIR@|$mandir|" \
    -e "s|@OTAGS@|$otags|" \
    -e "s|@OCAMLC@|$ocamlc|" \
    -e "s|@OCAMLOPT@|$ocamlopt|" \
    -e "s|@OCAMLDEP@|$ocamldep|" \
    Makefile.in > Makefile
