#!/bin/sh

##############################################################################
#
# Configuration script for otags reloaded
# 
# Hendrik Tews Copyright (C) 2010
# 
# This file is part of "Otags reloaded".
# 
# "Otags reloaded" 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 reloaded" 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 reloaded". If not, see
# <http://www.gnu.org/licenses/>.
# 
# $Id: configure,v 1.2 2010-08-24 21:04:12 tews Exp $
# 
# main module with main function
# 
##############################################################################

REQUIRED_OCAML_VERSION=3.11
OTAGS_VERSION=1

root=/usr/local
bindir=$root/bin
mandir=$root/share/man
ocamlbuild=
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


# check for ocamlbuild
# ocamlbuild -vnum is not available in 3.11
ocbv=$(${ocamlbindir}ocamlbuild -version | sed -e 's/ocamlbuild //')
if [ $? -ne 0 ] ; then
    echo compiler ${ocamlbindir}ocamlbuild not found. 
    echo Please adjust \$PATH or use --ocaml
    exit 1
else
    echo ${ocamlbindir}ocamlbuild version $ocbv found.
    ocamlbuild=${ocamlbindir}ocamlbuild
fi

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

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

    # if [ "$ocbv" != "$ocvo" ] ; then
    # 	echo ocamlbuild and ocamlopt have different versions! Please clean up!
    # 	exit 1
    # fi
    native=true
else
    echo ocamlopt not found. Native compilation disabled.
    native=false
fi

if [ $native = "true" ] ; then
    native_goals="otags.native"
    otags=otags.native
else
    native_goals=
    otags=otags.byte
fi

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

    # if [ "$ocbv" != "$ocvo" ] ; then
    # 	echo ocamlbuild and camlp4 have different versions! Please clean up!
    # 	exit 1
    # fi
    camlp4path=$(dirname $(which ${ocamlbindir}camlp4))
else
    echo camlp4 not found.
fi


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


# Make conf.ml
sed -e "s|@CAMLP4PATH@|$camlp4path|" \
    -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|@OCAMLBUILD@|$ocamlbuild|" \
    -e "s|@NATIVE@|$native|" \
    -e "s|@NATIVE_GOALS@|$native_goals|" \
    -e "s|@OTAGS@|$otags|" \
    Makefile.in > Makefile

