#!/bin/bash
# $Id: configure 1611 2007-12-04 01:40:09Z deraugla $

prefix=''
bindir=''
libdir=''
mandir=''
mode=T
name=camlp5

if [ -f myconfig ]; then
  . ./myconfig
fi

# a local which command for sh
which () {
IFS=":" # set words separator in PATH to be ':' (it allows spaces in dirnames)
for i in $PATH; do
  if test -z "$i"; then $i=.; fi 
  if [ -f "$i/$1" ] ; then
	IFS=" "
        echo $i/$1
	break
  fi
done
}

# Parse command-line arguments

while : ; do
  case "$1" in
    "") break;;
    -name|--name)
        name=$2; shift;;
    -prefix|--prefix)
        prefix=$2; shift;;
    -bindir|--bindir)
        bindir=$2; shift;;
    -libdir|--libdir)
        libdir=$2; shift;;
    -mandir|--mandir)
        mandir=$2; shift;;
    -transitional|--transitional)
        mode=T;;
    -strict|--strict)
        mode=S;;
    -help|--help)
        echo "Usage: configure [options]"
        echo "Options:"
        echo "  -bindir <dir>, --bindir <dir>"
        echo "        Directory where the binaries will be installed"
        echo "        Default: same than command ocamlc"
        echo "  -libdir <dir>, --libdir <dir>"
        echo "        Directory where the camlp5 directory will be installed"
        echo "        Default: same than 'ocamlc -where'"
        echo "  -mandir <dir>, --mandir <dir>"
        echo "        Directory where the manual pages will be installed"
        echo "        Default: <bindir>/../man"
        echo "  -prefix <dir>, --prefix <dir>"
        echo "        Set bindir, libdir and mandir respectively to"
        echo "        <dir>/bin, <dir>/lib/ocaml, <dir>/man."
        echo "  -transitional, --transitional"
        echo "        compile in transitional mode"
        echo "  -strict, --strict"
        echo "        compile in strict mode" 
        echo "  -help, --help"
        echo "        this message"
       exit 0;;
    *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done

if [ "$mode" = "T" -o "$mode" = "S" ]; then
  :
else
  echo 1>&2
  echo "Launch \"configure\" with the mode you want:" 1>&2
  echo "   --transitional" 1>&2
  echo "   --strict" 1>&2
  echo 1>&2
  exit 2
fi

echo "mode=$mode" > myconfig

# Sanity checks

case "$prefix" in
  /*) ;;
  "") ;;
   *) echo "The -prefix directory must be absolute." 1>&2; exit 2;;
esac
case "$bindir" in
  /*) ;;
  "") ;;
   *) echo "The -bindir directory must be absolute." 1>&2; exit 2;;
esac
case "$libdir" in
  /*) ;;
  "") ;;
   *) echo "The -libdir directory must be absolute." 1>&2; exit 2;;
esac
case "$mandir" in
  /*) ;;
  "") ;;
   *) echo "The -mandir directory must be absolute." 1>&2; exit 2;;
esac

# Check Ocaml

if ocamlc -v >/dev/null 2>&1; then :
else
    echo "You need the command ocamlc accessible in the path!"
    echo "Configuration script failed!"
    exit 1
fi

# Check versions

oversion=$(ocamlc -v | head -1 | sed 's/^.*version //')
OVERSION=$(echo "$oversion" | sed -e 's/[ +].*$//')

if [ -d "ocaml_stuff/$OVERSION" -a -f "ocaml_src/main/ast2pt.ml_$OVERSION" ]
then :
else
  echo
  echo "Sorry: the compatibility with ocaml version \"$oversion\""
  echo "is not yet implemented. Please report."
  echo
  echo "Configuration failed."
  # to give the ability to do "make steal":
  echo "OVERSION=$OVERSION" > config/Makefile
  exit 2
fi

# Evaluate installation directories

x=$(ocamlc -where)
y=$(echo $x | sed -e 's|\\\\|/|g')
x="$(echo 'Sys.os_type;;' | ocaml | egrep 'Win32|Cygwin')"
if test "$x" = ""; then win=false; else win=true; fi
x="$(echo 'Sys.os_type;;' | ocaml | grep 'Win32')"
if test "$x" = ""; then win32=false; else win32=true; fi
OLIBDIR="$y"

if test "$win" = "true"; then
    EXE=.exe
else
    EXE=
fi

if test "$win32" = "true"; then
    EXT_OBJ=.obj
    EXT_LIB=.lib
else
    EXT_OBJ=.o
    EXT_LIB=.a
fi

if test "$bindir" != ""; then
    BINDIR=$bindir
elif test "$prefix" != ""; then
    BINDIR='$(PREFIX)/bin'
else
    BINDIR=$(dirname "$(which ocamlc)")
fi
if test "$libdir" != ""; then
    LIBDIR=$libdir
elif test "$prefix" != ""; then
    LIBDIR='$(PREFIX)/lib/ocaml'
else
    LIBDIR=$(ocamlc -where)
fi
if test "$mandir" != ""; then
    MANDIR=$mandir
elif test "$prefix" != ""; then
    MANDIR='$(PREFIX)/man'
else
    MANDIR=$(dirname "$BINDIR")/man
fi

if ocamlc.opt > /dev/null 2>&1 && ocamlopt.opt > /dev/null 2>&1; then
    OPT=.opt
else
    OPT=
fi

VERSION="$(grep "value version =" main/pcaml.ml | sed -e 's/^[^"]*"\([^"]*\).*$/\1/')"

(
echo NAME=$name
echo MODE=$mode
echo EXE=$EXE
echo OPT=$OPT
echo EXT_OBJ=$EXT_OBJ
echo EXT_LIB=$EXT_LIB
echo OVERSION=$OVERSION
echo VERSION=$VERSION
echo "OTOP=\$(TOP)/ocaml_stuff/$OVERSION"
if ocamlc -w y 2>/dev/null; then
  echo "OCAMLC_W_Y=\"-w y\""
else
  echo "OCAMLC_W_Y="
fi
echo OLIBDIR=$OLIBDIR
if test "$prefix" != ""; then
  echo PREFIX=$prefix
fi
echo BINDIR=$BINDIR
echo LIBDIR=$LIBDIR
echo MANDIR=$MANDIR
) > config/Makefile.cnf

rm -f config/Makefile
cat config/Makefile.tpl > config/Makefile
echo >> config/Makefile
cat config/Makefile.cnf >> config/Makefile

cd ocaml_src/main
cp "ast2pt.ml_$OVERSION" ast2pt.ml
cd ../..

echo
echo "Resulting configuration file (config/Makefile.cnf):"
echo
cat config/Makefile.cnf

echo
if [ "$mode" = "T" ]; then
  echo "=== transitional mode ==="
else
  echo "=== strict mode ==="
fi
echo
