#! /bin/sh
# $Id: configure_batch,v 2.1 1999/05/12 06:11:18 ddr Exp $

prefix=/usr/local
bindir=''
libdir=''
mandir=''

# 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;;
    *) 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

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 -v | sed -n -e 's/Standard library directory: //p'`
OLIBDIR=`echo $OLIBDIR | sed -e s=\\\\\\\\=/=g`
echo "OLIBDIR=$OLIBDIR" >> Makefile.cnf

# 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
echo >> Makefile
cat Makefile.cnf >> Makefile

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