#!/bin/sh

##############################################################################
#
# Configuration script for prooftree
# 
# Hendrik Tews Copyright (C) 2011 - 2013
# 
# This file is part of "prooftree".
# 
# "prooftree" 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.
# 
# "prooftree" 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 "prooftree". If not, see <http://www.gnu.org/licenses/>.
# 
# $Id: configure,v 1.6 2013/03/28 08:02:00 tews Exp $
# 
##############################################################################

root=/usr/local
bindir=$root/bin
mandir=$root/share/man
native=
ocamldep=
ocamlc=
prooftree=
ocamldoc=
lablgtkdir=
force_byte="false"

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]"
}

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;;
    -test-byte)       force_byte="true";;
     *) echo "Unknown option \"$1\"." 1>&2; usage; exit 2;;
  esac
  shift
done


# check for ocamlc
ocbv=$(ocamlc -version)
if [ $? -ne 0 ] ; then
    echo compiler ocamlc not found. 
    echo Please install ocaml and/or adjust \$PATH
    exit 1
else
    echo ocamlc version $ocbv found.
fi

# check for ocamlopt.opt
ocvo=$(ocamlopt.opt -version)
if [ $? -eq 0 ] ; then
    echo ocamlopt.opt version $ocvo found. Native compilation enabled.
    native=true
else
    echo ocamlopt.opt not found. Native compilation disabled.
    native=false
fi

if [ $force_byte = "true" ] ; then
    native=false
fi

if [ $native = "true" ] ; then
    ocamldep=ocamldep.opt
    ocamlc=ocamlopt.opt
    prooftree=prooftree.opt
    ocamldoc=ocamldoc.opt
else
    ocamldep=ocamldep
    ocamlc=ocamlc
    prooftree=prooftree.byte
    ocamldoc=ocamldoc
fi

# check ocamldep
ocdepv=$($ocamldep -version)
if [ $? -ne 0 ] ; then
    echo $ocamlc exists but $ocamldep not. 
    echo Please check your ocaml installation!
    exit 1
fi

# check ocamldoc
ocdocv=$($ocamldoc -version)
if [ $? -ne 0 ] ; then
    echo $ocamlc exists but $ocamldoc not. 
    echo Please check your ocaml installation!
    exit 1
fi

# check for lablgtk
check_lablgtk () {
    echo test $ocamlc -I $1
    if [ $native = "true" ] ; then
	$ocamlc -o /dev/null -I $1 lablgtk.cmxa gtkInit.cmx
    else
	$ocamlc -o /dev/null -I $1 lablgtk.cma gtkInit.cmo
    fi
}

if check_lablgtk "+lablgtk2" ; then
    lablgtkdir="+lablgtk2"
elif ocamlfind query lablgtk2 > /dev/null ; then
    lablgtkdir=$(ocamlfind query lablgtk2)
    if check_lablgtk $lablgtkdir ; then
	true
    else
	lablgtkdir=""
    fi
fi

if [ $lablgtkdir = "" ] ; then
    echo library LablGtk not found. Please install package liblablgtk2-ocaml-dev.
    exit 1
fi


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


# Make the Makefile
sed -e "s|@BINDIR@|$bindir|" \
    -e "s|@MANDIR@|$mandir|" \
    -e "s|@PROOFTREE@|$prooftree|" \
    -e "s|@OCAMLC@|$ocamlc|" \
    -e "s|@OCAMLDEP@|$ocamldep|" \
    -e "s|@OCAMLDOC@|$ocamldoc|" \
    -e "s|@LABLGTKDIR@|$lablgtkdir|" \
    Makefile.in > Makefile

