dnl C->Haskell Binding Compiler
dnl Copyright (c) [1999..2001] Manuel M. T. Chakravarty <chak@cse.unsw.edu.au>
dnl 
dnl This library is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU Library General Public
dnl License as published by the Free Software Foundation; either
dnl version 2 of the License, or (at your option) any later version.
dnl 
dnl This library is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dnl Library General Public License for more details.
dnl 
dnl You should have received a copy of the GNU Library General Public
dnl License along with this library (COPYING.LIB); if not, write to the Free
dnl Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
dnl MA  02111-1307  USA

dnl ######################################################################
dnl Process this file with autoconf to produce a configure script.
dnl ######################################################################


dnl Initialise.
AC_INIT(toplevel/Version.hs)

dnl Select a specific Haskell compiler and/or flags.
AC_ARG_WITH(hc, 
            [  --with-hc=HC            use Haskell compiler HC],
	    [HC=$withval])
AC_ARG_WITH(hcflags, 
            [  --with-hcflags=HCFLAGS  specify flags for Haskell compiler],
	    [HCFLAGS=$withval])

dnl Select whether to use --add-package during installation.
AC_ARG_ENABLE(add_package,
	      [  --enable-add-package    register package during installation],
	      [ENABLE_ADD_PACKAGE=$enableval])

dnl Link with electric fence?
AC_ARG_WITH(efence, 
	    [  --with-efence           link with efence malloc debugger],
	    [efence=$withval])

dnl Check for OS and set dynamic library suffix and temporary directory.
DLSUFFIX=".so"
TMPDIR="/tmp"
AC_CYGWIN
if test "$CYGWIN" = "yes"; then
  DLSUFFIX=".dll"
  TMPDIR="C:\TMP"
fi

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PATH_PROG(BASENAME,basename)
AC_PATH_PROG(EXPR,expr)
AC_PATH_PROGS(GREP,egrep grep)
AC_PATH_PROG(HEAD,head)
AC_PATH_PROG(SED,sed)
AC_PATH_PROG(MKDIR,mkdir)
AC_PATH_PROG(MV,mv)

dnl This is far from elegant, but works for extracting the plain version number
C2HS_VERSION=`$GREP '^versnum' toplevel/Version.hs\
	      | $SED '-e s/versnum.* "//' '-e s/"//'`

dnl Haskell compiler; currently only GHC 4.08 upwards and nhc98 1.14 have the 
dnl required FFI and existentially quantified variables correctly working.
AC_PATH_PROGS(HC, $HC ghc nhc98)
if test -z "$HC"; then
 AC_MSG_ERROR([Could not find a Haskell compiler!
Currently supported are ghc 4.08 upwards & nhc98 1.14.
** Check \"http://haskell.org\". **])
fi
dnl The following can happen if a wrong --with-hc=HC option is given.
if test ! -x "$HC"; then
 AC_MSG_ERROR([File not found: $HC])
fi

dnl Determine basic compiler name.
case `$BASENAME $HC` in
  ghc*) hc_base=ghc;;
  nhc98*) hc_base=nhc98;;
esac

dnl Check ghc's details.
if test $hc_base = ghc; then
  dnl set GHC, as this is what the following command inspects
  GHC=$HC
  CTK_GHC_VERSION(hc_vers,hc_maj_vers,hc_min_vers,hc_pl)
  CTK_PROG_CHECK_VERSION($hc_vers, -lt, 4.08, [
    AC_MSG_ERROR([We need to have version 4.08 upwards of ghc!
** Check \"http://haskell.org\". **])])
  dnl Determine compilation system identifier.
  SYS=ghc$hc_maj_vers
  HCID="GHC $hc_vers"
  HIDIROPT=-i
  REQUIRES_HASKELL="ghc = $hc_vers"
fi

dnl Check nhc98's details.
if test $hc_base = nhc98; then
  AC_CACHE_CHECK([nhc98 version], ctk_cv_prog_nhc98_version, [
    ctk_cv_prog_nhc98_version=`$HC --version 2>&1 | $HEAD -n 1 | $SED -e 's/.*v\([[0-9]]\)\.\([[0-9]]*\).*/\1.\2/'`
  ])
  hc_maj_vers=`echo $ctk_cv_prog_nhc98_version | $SED -e 's/\([[0-9]]\)\.[[0-9]]*/\1/'`
  hc_min_vers=`echo $ctk_cv_prog_nhc98_version | $SED -e 's/[[0-9]]\.\([[0-9]]*\)/\1/'`
  if test $hc_maj_vers -lt 1 -o $hc_min_vers -lt 14; then
   AC_MSG_ERROR([You need version 1.14 upwards of nhc98!
  ** Check \"http://haskell.org/\". **])
  fi
  dnl Determine compilation system identifier.
  SYS=nhc$hc_maj_vers
  HCID="nhc98 $hc_maj_vers.$hc_min_vers"
  HIDIROPT=-I
  dnl FIXME: In the case of nhc98, do we have to require a specific version, 
  dnl        too? 
  REQUIRES_HASKELL=nhc
fi

dnl Determine whether the Haskell system supports packages.
dnl
if test $hc_base = ghc; then
  HASPKG=yes
fi
if test $hc_base = nhc1; then
  HASPKG=no
fi

dnl And nhc does it yet differently...
BEGIN_NHC="{- NHC does some things differently..."
END_NHC="-}"
BEGIN_NOT_NHC=
END_NOT_NHC=
if test $hc_base = nhc98; then
  BEGIN_NHC=
  END_NHC=
  BEGIN_NOT_NHC="{- That's the right way, but nhc doesn't unnderstand it."
  END_NOT_NHC="-}"
fi

dnl Checks for libraries.

dnl Info needed for the package configuration.
dnl
dnl The `exec_prefix=$prefix' line shouldn't be necessary, but for some reason 
dnl `exec_prefix' is otherwise defined to be `NONE' here.
test "x$prefix" = xNONE && prefix=$ac_default_prefix
# Let make expand exec_prefix.
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
LIBDIR=`eval echo \`eval echo $libdir\``

dnl Needed substitutions.
AC_SUBST(DLSUFFIX)
AC_SUBST(TMPDIR)
AC_SUBST(C2HS_VERSION)
AC_SUBST(HCFLAGS)
AC_SUBST(LIB_HCFLAGS)
AC_SUBST(SYS)
AC_SUBST(HCID)
AC_SUBST(BEGIN_NHC)
AC_SUBST(END_NHC)
AC_SUBST(BEGIN_NOT_NHC)
AC_SUBST(END_NOT_NHC)
AC_SUBST(ENABLE_ADD_PACKAGE)
AC_SUBST(LIBDIR)
AC_SUBST(HASPKG)
AC_SUBST(REQUIRES_HASKELL)

dnl Write the results...
AC_OUTPUT([
  toplevel/C2HSConfig.hs
  mk/config.mk
  c2hs-config
  c2hs.conf
  c2hs.spec
], [chmod +x c2hs-config])
