#!/bin/sh

makeconfigureneeded=
TARGET=
PREFIX=
DESTDIR=
SMLCM=
MLLEX=
MLYACC=
HEAP_SUFFIX=
DEBUGLIB=librml_g.a
PROFLIB=librml_p.a
TRACLIB=librml_t.a

if [ -r ./config.cache ]; then
  . ./config.cache
fi

while [ "$#" != 0 ]; do
    arg=$1
    shift
    case "$arg" in
    --build=*)
	TARGET=`echo $arg | sed 's/--build=//'`
	if [ ! \( -d "config/$TARGET" -a -r "config/$TARGET/config.h" \) ]; then
	  echo ./configure: You have indicated target "'$TARGET'".
	  echo This software does not directly support that configuration.
	  echo For a list of supported configurations, see the directory config/.
	  exit 1
	fi
	makeconfigureneeded=1
	;;
    --prefix=*)
	PREFIX=`echo $arg | sed 's/--prefix=//'`
	;;
    --destdir=*)
	DESTDIR=`echo $arg | sed 's/--destdir=//'`
	;;
    --nosmlnj)
	SMLCM=nosmlnj
	MLLEX=mllex
	MLYACC=mlyacc
	HEAP_SUFFIX=nosmlnj
	;;
    --disable-debug)
	DEBUGLIB=
	;;
    --enable-debug)
	DEBUGLIB=librml_g.a
	;;
    --disable-profile)
	PROFLIB=
	;;
    --enable-profile)
	PROFLIB=librml_p.a
	;;
    --disable-tracing)
	TRACLIB=
	;;
    --enable-tracing)
	TRACLIB=librml_t.a
	;;
    *)
	echo ./configure: bad option "'$arg'"
	echo usage: ./configure [options]
	echo valid options are:
	echo '--build=<cpu-os-compiler>'
	echo '--prefix=<path>'
	echo '--destdir=<path>'
	echo '--nosmlnj'
	echo '--{disable,enable}-{debug,profile}'
	exit 1
    esac
done

if [ -z "$TARGET" ]; then
  TARGET=`etc/config.guess`
  if [ "$?" != 0 ]; then
    echo ./configure: etc/config.guess failed to classify current host
    exit 1
  fi
  eval $TARGET
  echo ./configure: TARGET is $TARGET
  makeconfigureneeded=1
fi

# SML/NJ configuration
# Note: --nosmlnj MUST be given if SML/NJ isn't available

if [ -z "$SMLCM" ]; then
  makeconfigureneeded=1
  PATH=/usr/lib/smlnj/bin:$PATH
  MLLEX=`etc/which ml-lex` || MLLEX=`etc/which mllex` || MLLEX=`etc/which ml-lex.bat`
  if [ "$?" != 0 ]; then
    echo ./configure: unable to locate ml-lex or mllex in '$PATH'.
    exit 1
  fi
  SMLCM=`echo $MLLEX | sed 's,ml-lex,sml-cm,'`
  if [ ! -x $SMLCM ]; then
    SMLCM=`echo $MLLEX | sed 's,ml-lex,sml,'`
    if [ ! -x $SMLCM ]; then
      SMLCM=`echo $MLLEX | sed 's,mllex,sml,'`
      if [ ! -x $SMLCM ]; then
        echo ./configure: unable to locate sml-cm or sml.
        exit 1
      fi
    fi
  fi
  MLYACC=`echo $MLLEX | sed 's,ml-lex,ml-yacc,'`
  if [ ! -x $MLYACC ]; then
    MLYACC=`echo $MLLEX | sed 's,mllex,ml-yacc,'`
    if [ ! -x $MLYACC ]; then
      MLYACC=`echo $MLLEX | sed 's,mllex,mlyacc,'`
      if [ ! -x $MLYACC ]; then
        echo ./configure: unable to locate ml-yacc or mlyacc.
        exit 1
      fi
    fi
  fi
  echo ./configure: SMLCM is $SMLCM
  case "$TARGET" in
  x86-cygwin-gcc|x86-mingw32-gcc|x86_64-mingw32-gcc)
	HEAP_SUFFIX=x86-win32
	;;
  *)
	if [ -z "$SMLNJ_HOME" ]; then
	  echo ./configure: unable to determine SML/NJ installation directory
          echo Please set the SMLNJ_HOME environment variable as it is different on all platforms
	  exit 1
	fi
	BIN_DIR=$SMLNJ_HOME/bin/
	if [ "$?" != 0 -o -z "$BIN_DIR" ]; then
	  echo ./configure: unable to determine SML/NJ installation directory
          echo $SMLNJ_HOME/bin/ does not exist
	  exit 1
	fi
	ARCH_OPSYS_HEAPSUFFIX=`$BIN_DIR/.arch-n-opsys`
	if [ "$?" != 0 ]; then	    
	  echo ./configure: unable to determine SML/NJ heap suffix
	  exit 1
	fi
	echo $ARCH_OPSYS_HEAPSUFFIX
	eval $ARCH_OPSYS_HEAPSUFFIX
  esac
  echo ./configure: SML/NJ heap suffix is $HEAP_SUFFIX
fi

if [ -z "$PREFIX" ]; then
  PREFIX=`pwd`/$TARGET
  echo ./configure: PREFIX is $PREFIX
fi

echo TARGET=$TARGET > ./config.cache
echo PREFIX=$PREFIX >> ./config.cache
echo DESTDIR=$DESTDIR >> ./config.cache
case "$TARGET" in
  x86-mingw32-gcc|x86_64-mingw32-gcc)
	#SMLDIR=`etc/which_dir sml.bat`
	SMLDIR=$SMLNJ_HOME
	# transform c:\a\b -> '/c/a/b'
	SMLDIR=`echo $SMLDIR | sed 's/://g'`
	SMLDIR=`echo $SMLDIR | sed 's|\\\|/|g'`
	HEAPDIR=$SMLNJ_HOME
	# HEAPDIR=`echo $HEAPDIR | sed 's|\\\|/|g'`
	SMLDIR=/$SMLDIR/bin
	SMLRUN=$SMLDIR/.run/run.x86-win32.exe 
	echo SMLCM=\`echo \"$SMLRUN @SMLload=$HEAPDIR/bin/.heap/sml\"\` >> ./config.cache
	echo MLLEX=\`echo \"$SMLRUN @SMLload=$HEAPDIR/bin/.heap/ml-lex\"\` >> ./config.cache
	echo MLYACC=\`echo \"$SMLRUN @SMLload=$HEAPDIR/bin/.heap/ml-yacc\"\` >> ./config.cache
	;;
  *)
	echo SMLCM=$SMLCM   >> ./config.cache
	echo MLLEX=$MLLEX   >> ./config.cache
	echo MLYACC=$MLYACC >> ./config.cache

esac
echo HEAP_SUFFIX=$HEAP_SUFFIX >> ./config.cache
echo DEBUGLIB=$DEBUGLIB >> ./config.cache
echo PROFLIB=$PROFLIB >> ./config.cache
echo TRACLIB=$TRACLIB >> ./config.cache

echo ./configure: updated config.cache
if [ ! -z "$makeconfigureneeded" ]; then
  echo ./configure: make configure
  make configure
fi
echo ./configure: done
exit 0
