#! /bin/sh
# $Id: configure,v 1.2 2002/02/28 11:27:14 mitya Exp $

# Parse command-line arguments

bindir=
libdir=
p4lib=

OC=ocamlc
PP4=camlp4
CC=cc
AWK=awk
MAKE=make


usage()
{
    echo "Dynamic Caml version 0.1 configure script"
    echo "Parameters:"
    echo "  -libdir <dir>     where to install library"
    echo "  -p4lib <dir>      where to install syntax extensions"
    echo "  -oc <oc-name>     Objective Caml compiler (default:\`ocamlc')"
    echo "  -pp4 <oc-name>    Caml P4  (default: \`camlp4')"
    echo "  -cc <cc-name>     C compiler name (default: \`cc')"
    echo "  -awk <awk-name>   AWK interpreter (default: \`awk')"
    echo "  -make <make-name> GNU make (default: \`make')"
}

while : ; do
  case "$1" in
    "") break;;
    -libdir|--libdir)
        libdir=$2; shift;;
    -p4lib)
	p4lib=$2; shift;;
    -oc)
	OC=$2; shift;;
    -pp4)
        PP4=$2; shift;;
    -awk)
        AWK=$2; shift;;
    -make)
	MAKE=$2; shift;;
    -cc)
	CC=$2; shift;;
    -help)
        usage; exit 4;;
    *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done

# Check Ocaml

OLIBDIR=`$OC -v | sed -n -e 's/Standard library directory: //p'`
OLIBDIR=`echo $OLIBDIR | sed -e s=\\\\\\\\=/=g`
echo OCaml library found in $OLIBDIR

LIBDIR=${libdir:-$OLIBDIR}

echo "Library will be installed in $LIBDIR"

# Check camlP4
P4DIR=`$PP4 -where`
P4LIB=${p4lib:-$P4DIR}
echo "Syntax extensions will be installed in $P4LIB"

# chech for flags
if [ $CC="gcc" ]; then CFLAGS='-O3'; fi

# determine ocaml version
OCAMLVER=$(echo `ocamlc -v | sed -n 1s/\[A-Z,a-z\]\*//gp`)
OCAML_MAJOR=$(echo $OCAMLVER | sed -n s/\\.[0-9]\*//gp)
OCAML_MINOR=$(echo $OCAMLVER | sed -n s/[0-9]\*\\.//gp)

if test  $OCAML_MAJOR -lt 3 -o $OCAML_MINOR -lt 4 ; then
    echo "ERROR!"
    echo "ocaml version 3.04 or greater is required."
    exit 2
fi

if [ -e bytecode/$OCAMLVER.instruct.h ] ; then
    cp bytecode/$OCAMLVER.instruct.h bytecode/instruct.h
else
    echo "Warning: OCaml version $OCAMLVER found."
    echo "It seems so that I do not have instruct.h for this version of OCaml"
    echo "I will try to use the file from version 3.00"
    echo "If tests fail, please copy file instruct.h from byterun subdir"
    echo "of your ocaml distribution source tree into bytecode/ subdir"
    echo "of my distribution and do make."
    echo "If tests still fail, contact my author."
    cp bytecode/3.04.instruct.h bytecode/instruct.h
fi


# Where to install
echo "LIBDIR=$LIBDIR" > Makefile.in
echo "P4LIB=$P4LIB" >> Makefile.in
echo "OC=$OC" >> Makefile.in
echo "PP4=$PP4" >> Makefile.in
echo "CC=$CC" >> Makefile.in
echo "AWK=$AWK" >> Makefile.in
echo "MAKE=$MAKE" >> Makefile.in
echo "P4DIR=$P4DIR" >> Makefile.in
echo "OLIBDIR=$OLIBDIR" >> Makefile.in
echo "CFLAGS=-DOCAML_MINOR=$OCAML_MINOR -DOCAML_MAJOR=$OCAML_MAJOR $CFLAGS" >> Makefile.in

#
# $Log: configure,v $
# Revision 1.2  2002/02/28 11:27:14  mitya
# Required Ocaml version is now 3.04.
# 3.00 is no longer supported
#
#




