#! /bin/sh
# $Id: configure_batch,v 1.5.4.3 2004/07/30 14:59:15 mauny Exp $

prefix=/usr/local
bindir=''
libdir=''
mandir=''
ocaml_top=../ocaml_stuff

# Parse command-line arguments

while : ; do
  case "$1" in
    "") break;;
    -prefix|--prefix)
        prefix=$2; shift;;
    -bindir|--bindir)
        bindir=$2; shift;;
    -libdir|--libdir)
        libdir=$2; shift;;
    -mandir|--mandir)
        mandir=$2; shift;;
    -ocaml-top)
        ocaml_top=$2; shift;;
    *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done

# 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

# Generate the files

rm -f Makefile.cnf
touch Makefile.cnf

# Check Ocaml

for i in utils parsing otherlibs/dynlink; do
    if test ! -d "$ocaml_top/$i"; then
        echo "Bad value $ocaml_top for option -ocaml-top"
        echo "There is no directory $ocaml_top/$i"
        echo "Configuration script failed"
        exit 1
    fi
done

echo "EXE=$EXE" >> Makefile.cnf
echo "O=o" >> Makefile.cnf
echo "A=a" >> Makefile.cnf
echo "OPT=" >> Makefile.cnf
echo "OTOP=$ocaml_top" >> Makefile.cnf
if test -r ../../config/auto-aux/Makefile; then
    grep '^RANLIB' ../../config/auto-aux/Makefile >> Makefile.cnf
elif test -r ../../config/Makefile; then
    grep '^RANLIB' ../../config/Makefile >> Makefile.cnf
else
    echo "Could not read OCaml config Makefile"
    echo "Configuration script failed!"
    exit 1
fi

if test "$ocaml_top" = "../ocaml_stuff"; then
    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
    OLIBDIR=`ocamlc -where`
    echo "OLIBDIR=$OLIBDIR" >> Makefile.cnf
else
    echo "OLIBDIR=\$(OTOP)/boot" >> Makefile.cnf
fi

# Where to install

echo "PREFIX=$prefix" >> Makefile.cnf
case "$bindir" in
  "") echo 'BINDIR=$(PREFIX)/bin' >> Makefile.cnf
      bindir="$prefix/bin";;
   *) echo "BINDIR=$bindir" >> Makefile.cnf;;
esac
case "$libdir" in
  "") echo 'LIBDIR=$(PREFIX)/lib/camlp4' >> Makefile.cnf
      libdir="$prefix/lib/camlp4";;
   *) echo "LIBDIR=$libdir" >> Makefile.cnf;;
esac
case "$mandir" in
  "") echo 'MANDIR=$(PREFIX)/man/man1' >> Makefile.cnf
      mandir="$prefix/man/man1";;
   *) echo "MANDIR=$mandir" >> Makefile.cnf;;
esac

rm -f Makefile
cat Makefile.tpl > Makefile
cat Makefile.cnf >> Makefile

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