dnl This file is in the public domain.

dnl Process this file with Autoconf to produce a configure script.

dnl Remove the datarootdir/docdir tests below if this is increased:
AC_PREREQ([2.59])
AC_INIT([darcs], [2.1.1rc1], [bugs@darcs.net])
AC_CONFIG_SRCDIR([src/darcs.lhs])

dnl Write our invocation date/time and flags into a file so that
dnl Darcs knows how it was built.

ac_cmdline_args="$*"
AC_CONFIG_COMMANDS([config.command],
                   [ echo > config.command "# configured $date"
                     echo >>config.command "./configure $args"
		     chmod +x config.command
                   ],
                   [date="`date`"; args="$ac_cmdline_args"])

dnl The following is now the authoritative location of the version
dnl number. All references to the darcs version number should
dnl originate here. From Haskell this is easily done by importing
dnl the module "Autoconf ( darcs_version )".
dnl
dnl The version string must not contain any spaces. The version
dnl state will automatically be set to any of:
dnl
dnl    "prerelease #n"
dnl    "release candidate #n"
dnl    "release"
dnl    "unstable branch"
dnl    "stable branch"
dnl    "unknown branch"
dnl
dnl The new darcs custom is to increase the version number
dnl directly after a release, since the checking for a tag
dnl indicates whether the number is really correct.

DARCS_VERSION=$PACKAGE_VERSION
AC_SUBST(DARCS_VERSION)

dnl See if we are in a darcs repo and also have darcs available,
dnl and whether we're in a repository...

AC_CHECK_PROG(DARCS, darcs, darcs)
AC_MSG_CHECKING([if we're in a darcs repository])
if test ! -z "$DARCS" && test -e "_darcs/hashed_inventory"; then
  I_AM_IN_REPO=yes
else
  I_AM_IN_REPO=no
fi
AC_MSG_RESULT($I_AM_IN_REPO)
AC_SUBST(I_AM_IN_REPO)

dnl Get rid of the lousy '-g -O2' defaults

CFLAGS=$CFLAGS

dnl Compile with multi thread support. Required on Solaris at least.

CPPFLAGS="$CPPFLAGS -D_REENTRANT"

dnl Find gcc

dnl AC_PROG_CC
dnl AC_PROG_CPP

dnl Make sure a decent "install" is installed.

AC_PROG_INSTALL

dnl Check to see if we are compiling under Windows

AC_CHECK_HEADER(windows.h, [SYS=windows], [SYS=other])
AC_SUBST(SYS)

dnl Find ghc

AC_CHECK_PROG(GHC, ghc, ghc)
if test -z "$GHC"; then
  AC_MSG_ERROR([Cannot find ghc in your path; please set the GHC environment variable!!])
fi
AC_SUBST(GHCFLAGS)
echo found `$GHC --version`

AC_CHECK_PROG(GHC_PKG, ghc-pkg, ghc-pkg)
if test -z "$GHC_PKG"; then
  AC_MSG_ERROR([Cannot find ghc-pkg in your path; please set the GHC_PKG environment variable!!])
fi

dnl Use -hide-all-packages by default

GHCFLAGS="$GHCFLAGS -hide-all-packages -package haskell98"

dnl Check for the base package first
AC_MSG_CHECKING([base package])
ghc_base_pkg=`$GHC_PKG list base --simple-output | sed -e 's/.*\(base-3\.[[0-9.]]*\).*/\1/'`
if test -z "$ghc_base_pkg"; then
  AC_MSG_RESULT(unknown)
  GHCFLAGS="$GHCFLAGS -package base"
else
  AC_MSG_RESULT($ghc_base_pkg)
  GHCFLAGS="$GHCFLAGS -package $ghc_base_pkg"
fi

dnl Make sure compiler actually works:

TRY_COMPILE_GHC([main = putStr "Hello world!\n"],,
                AC_MSG_ERROR(Compile failed.))

dnl Before anything else, we need to decide what core compiler features to
dnl use, becaues this affects all the tests below (e.g. some packages may
dnl not be available with profiling)

dnl Make default to not profile...

AC_MSG_CHECKING([whether to profile])
AC_ARG_ENABLE(profile,
              AS_HELP_STRING([--enable-profile],[enable profiling]),
              profile=$enableval,profile=no)
AC_MSG_RESULT($profile)

if test "$profile" = yes; then
  GHCFLAGS="$GHCFLAGS -prof -auto-all"
fi

dnl See if we want to use -threaded to get more responsive progress reporting.
AC_MSG_CHECKING([whether to use the threaded runtime])
AC_ARG_ENABLE(threaded,AS_HELP_STRING([--disable-threaded],
              [don't compile with threaded runtime]),[AC_MSG_RESULT([no])],
              [MYOLDGHCFLAGS=$GHCFLAGS
               GHCFLAGS="$GHCFLAGS -threaded"
               TRY_COMPILE_GHC([main = putStr "Hello world!\n"],
                [AC_MSG_RESULT([yes])],
                [AC_MSG_RESULT([failed, threaded runtime disabled])
                 AC_MSG_WARN([Unable to compile with threaded runtime!])
                 GHCFLAGS=$MYOLDGHCFLAGS])])

INIT_WORKAROUND

dnl Figure out how to convert a file descriptor to a Handle

WORKAROUND_openFd

dnl Handle posix signals nicely:

WORKAROUND_POSIXSIGNALS([installHandler, raiseSignal, Handler(..), Signal,
                         sigINT, sigHUP, sigABRT, sigALRM, sigTERM, sigPIPE,])

AC_CHECK_HEADERS([siginfo.h])

dnl Look for Text.Regex

GHC_CHECK_MODULE(Text.Regex( mkRegex, matchRegex, Regex ), text, mkRegex undefined,,
  GHC_CHECK_MODULE(Text.Regex( mkRegex, matchRegex, Regex ), regex-compat, mkRegex undefined,,
    AC_MSG_ERROR(Cannot find Text.Regex; try installing the Haskell packages regex-compat or text?)
  )
)

dnl Look for Network.URI

GHC_CHECK_MODULE(Network.URI, network, undefined,,
                 AC_MSG_ERROR(Cannot find Network.URI; try installing the package network?))

dnl Look for native http library

GHC_CHECK_MODULE(Network.HTTP( urlEncode ), HTTP, urlEncode undefined,
                 [HAVE_HTTP=True
                  AC_DEFINE([HAVE_HTTP])],[HAVE_HTTP=False])

AC_SUBST(HAVE_HTTP)

dnl Look for System.FilePath

GHC_CHECK_MODULE(System.FilePath, filepath, isValid,,
    GHC_CHECK_MODULE(System.FilePath, FilePath, isValid,,
                 AC_MSG_ERROR(Cannot find System.FilePath; try installing the Haskell package filepath?)))

dnl See if we need a package for QuickCheck

GHC_CHECK_MODULE(Test.QuickCheck.Test ( quickCheck ), QuickCheck, quickCheck True,,
                 AC_MSG_ERROR(Cannot find Test.QuickCheck.Test; try installing version 2.1 of the Haskell package quickcheck?))

dnl See if we have an adequate version of bytestring
GHC_CHECK_MODULE(Data.ByteString.Internal( fromForeignPtr ),
                 bytestring,
                 fromForeignPtr,
                 [bytestringfound=yes],
                 [bytestringfound=no])
if test $bytestringfound = "yes"; then
  AC_MSG_CHECKING([whether to use ByteString])
  AC_ARG_ENABLE(bytestring,
                AS_HELP_STRING([--disable-bytestring], [do not use bytestring, even if it is present]),
                bytestring=$enableval,
                bytestring=yes)
  AC_MSG_RESULT($bytestring)
  # If we aren't instructed to avoid bytestring, and we did successfully find bytestring...
  if test $bytestring = "yes"; then
    CPPFLAGS="$CPPFLAGS -DHAVE_BYTESTRING"
  fi
fi

dnl See if we have haskeline >= 0.3.1
GHC_CHECK_MODULE(System.Console.Haskeline( runInputT, defaultSettings, getInputLine, handleDyn),
                 haskeline,
                 runInputT defaultSettings (getInputLine "prompt: ") :: IO (Maybe String),
                 [haskelinefound=yes],
                 [haskelinefound=no])
if test $haskelinefound = "yes"; then
  AC_MSG_CHECKING([whether to use haskeline])
  AC_ARG_ENABLE(haskeline,
                AS_HELP_STRING([--disable-haskeline], [do not use haskeline, even if it is present]),
                haskeline=$enableval,
                haskeline=yes)
  AC_MSG_RESULT($haskeline)
  if test $haskeline = "yes"; then
    GHCFLAGS="$GHCFLAGS -DHAVE_HASKELINE"
  fi
fi

dnl See if we need any packages from after the split base

GHC_CHECK_MODULE(System.Directory, directory, doesFileExist "foo" :: IO Bool,,
                 AC_MSG_ERROR(Cannot find System.Directory; try installing the Haskell package directory?))
GHC_CHECK_MODULE(System.Time, old-time, getClockTime :: IO ClockTime,,
                 AC_MSG_ERROR(Cannot find System.Time; try installing the Haskell package old-time?))
GHC_CHECK_MODULE(System.Process, process, runCommand "foo" :: IO ProcessHandle,,
                 AC_MSG_ERROR(Cannot find System.Process; try installing the Haskell package process?))
dnl note that [[Int]] is for [Int] in Haskell
GHC_CHECK_MODULE(Data.Array.Unboxed, array, listArray (0,0) :: [[Int]] -> UArray Int Int,,
                 AC_MSG_ERROR(Cannot find Data.Array.Unboxed; try installing the Haskell package array?))
dnl for make_changelog only
GHC_CHECK_MODULE(System.Locale, old-locale, rfc822DateFormat :: String,,
                 AC_MSG_ERROR(Cannot find System.Locale; try installing the Haskell package old-locale?))

dnl See if we need the util or mtl packages for Control.Monad

GHC_CHECK_MODULE(Control.Monad.Error, mtl, strMsg "foo" :: String,,
                 AC_MSG_ERROR(Cannot find Control.Monad.Error; try installing the Haskell package mtl?))

dnl See if we need a package for parsec...

GHC_CHECK_MODULE(Text.ParserCombinators.Parsec, parsec, errorPos undefined,,
                 AC_MSG_ERROR(Cannot find Text.ParserCombinators.Parsec; try installing the Haskell package parsec?))

dnl Check if we need package html

GHC_CHECK_MODULE(Text.Html, html, text "foo",,
                 AC_MSG_ERROR(Cannot find Text.Html; try installing the Haskell package html?))

dnl Check if we need package containers

GHC_CHECK_MODULE(Data.Map, containers, empty,,
                 AC_MSG_ERROR(Cannot find Data.Map; try installing the Haskell package containers?))

dnl On windows see if we need a package for random

if test "$SYS" = "windows"; then
  GHC_CHECK_MODULE(System.Random( randomIO ), random, randomIO :: IO Int,,
                   AC_MSG_ERROR(Cannot find System.Random; try installing the Haskell package random?))
fi

dnl Deal with systems on which getCurrentDirectory uses '\\' rather than '/':

WORKAROUND_getCurrentDirectory
AC_SUBST(GHC_SEPARATOR)

dnl Deal with systems without createLink

WORKAROUND_createLink

dnl Deal with GHCs without createDirectoryIfMissing

WORKAROUND_createDirectoryIfMissing

dnl Handle systems with a buggy renameFile

WORKAROUND_renameFile

dnl Deal with systems without bracketWithError

WORKAROUND_bracketOnError

dnl Handle systems that don't do POSIX file modes

WORKAROUND_fileModes

WORKAROUND_executable

dnl Make default to have optimizations on...

AC_MSG_CHECKING([whether to optimize])
AC_ARG_ENABLE(optimize,
              AS_HELP_STRING([--disable-optimize],[disable optimization]),
              optimize=$enableval, optimize=yes)
AC_MSG_RESULT($optimize)

if test "$optimize" = yes; then
  GHCFLAGS="$GHCFLAGS -O2 -funbox-strict-fields"
  if test "$CFLAGS" = ""; then
    CFLAGS="$CFLAGS -O2"
  fi
fi

dnl Make default to not use hpc

AC_MSG_CHECKING([whether to use Haskell Program Coverage])
AC_ARG_ENABLE(hpc,
              AS_HELP_STRING([--enable-hpc],[enable Haskell Program Coverage]),
              hpc=$enableval,hpc=no)
AC_MSG_RESULT($hpc)

if test "$hpc" = yes; then
  GHCFLAGS="$GHCFLAGS -fhpc"
fi

dnl See if we want to use mmap... defaults to yes, except under
dnl Windows, where this is always disabled.

AC_MSG_CHECKING([whether to use mmap])
AC_ARG_ENABLE(mmap, AS_HELP_STRING([--disable-mmap],
              [do not use mmap (use this flag if you get bus errors)]),
              enable_mmap=$enableval,enable_mmap=yes)
if test "$SYS" = "windows"; then
  AC_MSG_RESULT(no (Windows))
else
  AC_MSG_RESULT($enable_mmap)
fi
USE_MMAP="True"
if test "$enable_mmap" = "no" || test "$SYS" = "windows" ; then
  USE_MMAP="False"
fi
AC_SUBST(USE_MMAP)

TARGETS="darcs doc/darcs.1"
INSTALLWHAT="installbin"
BUILDDOC="no"

dnl Check whether to generate the AUTHORS file...
if test -d "_darcs"; then
  TARGETS="$TARGETS AUTHORS"
fi

dnl Check whether to use crazy PackedString debugging...

AC_MSG_CHECKING([whether to do PackedString debugging])
AC_ARG_ENABLE(debug-ps,
              [  --enable-debug-ps       enable PackedString debug mode],
               debugps=$enableval, debugps=no)
AC_MSG_RESULT($debugps)


if test "$debugps" = yes; then
  GHCFLAGS="$GHCFLAGS -DDEBUG_PS"
fi

dnl Find latex and friends

AC_MSG_CHECKING([whether to build manual])
AC_ARG_WITH(manual,
	AS_HELP_STRING([--without-manual],
                           [do not try to build manual]),
           ,with_manual=yes)
AC_MSG_RESULT($with_manual)

if test "$with_manual" = "yes"; then
  AC_CHECK_PROG(LATEX, latex, latex)
  if test -z "$LATEX"; then
    AC_MSG_WARN([Cannot find latex in your path!])
  fi
  AC_CHECK_PROG(PDFLATEX, pdflatex, pdflatex)
  if test -z "$PDFLATEX"; then
    AC_MSG_WARN([Cannot find pdflatex in your path!])
  fi
  AC_CHECK_PROG(DVIPS, dvips, dvips)
  if test -z "$DVIPS"; then
  AC_MSG_WARN([Cannot find dvips in your path!])
  fi
  if test ! '(' '(' -z "$LATEX" ')' -o '(' -z "$DVIPS" ')' ')'; then
    TARGETS="$TARGETS doc/manual/darcs.ps"
    INSTALLWHAT="$INSTALLWHAT installdocs"
    BUILDDOC="yes"
  fi
  if test ! '(' -z "$PDFLATEX" ')'; then
    TARGETS="$TARGETS doc/manual/darcs.pdf doc/manual/patch-theory.pdf"
  fi
  AC_CHECK_PROG(LATEX2HTML, latex2html, latex2html)
  if test -z "$LATEX2HTML"; then
    AC_MSG_WARN([Cannot find latex2html in your path!])
    PREPROCHTML=""
    AC_CHECK_PROG(HTLATEX, htlatex, htlatex)
    if test -z "$HTLATEX"; then
      AC_MSG_WARN([Cannot find htlatex in your path either!])
      AC_CHECK_PROG(HEVEA, hevea, hevea)
      if test -z "$HEVEA"; then
        AC_MSG_WARN([Cannot find hevea in your path either!])
        MAKEMANUAL="touch doc/manual/index.html; echo Cannot make manual!"
      else
        TARGETS="$TARGETS doc/manual/index.html"
        MAKEMANUAL="$HEVEA -o doc/manual/index.html src/darcs.tex"
      fi
    else
      TARGETS="$TARGETS doc/manual/index.html"
      MAKEMANUAL="cd doc/manual && $HTLATEX ../../src/darcs.tex && ln -sf darcs.html index.html"
    fi
  else
    TARGETS="$TARGETS doc/manual/index.html"
    MAKEMANUAL="$LATEX2HTML -split +1 -dir doc/manual src/darcs.tex"
    dnl the following tells "preproc" to generate "rawhtml" sections for latex2html.
    PREPROCHTML="--html"
  fi
else
  BUILDDOC="no"
fi

AC_SUBST(TARGETS)
AC_SUBST(MAKEMANUAL)
AC_SUBST(INSTALLWHAT)
AC_SUBST(PREPROCHTML)

dnl Look for sendmail...

AC_ARG_WITH(sendmail, [  --with-sendmail=PATHTOSENDMAIL],
            [ SENDMAIL=$with_sendmail
              HAVE_SENDMAIL="True" ],
            [ STUPID_PATH=/usr/sbin:/usr/lib
              AC_PATH_PROG(SENDMAIL, sendmail, "", $PATH:$STUPID_PATH)
              if test -z "$SENDMAIL"; then
                SENDMAIL=""
                HAVE_SENDMAIL="False"
              else
                HAVE_SENDMAIL="True"
              fi ])
AC_SUBST(SENDMAIL)
AC_SUBST(HAVE_SENDMAIL)

dnl Look for MAPISendMail

AC_CHECK_LIB(mapi32, MAPISendMail, HAVE_MAPI="True", HAVE_MAPI="False")
AC_SUBST(HAVE_MAPI)

if test "$HAVE_SENDMAIL" = "False" -a "$HAVE_MAPI" = "False"; then
  AC_MSG_WARN([Can't find sendmail or mapi so you won't be able to push by email!])
fi

dnl Allow use of color in output to be disabled

AC_ARG_ENABLE(color, AS_HELP_STRING([--disable-color],[do not use ansi color escapes]))

dnl export color flag
USE_COLOR="True"
if test "$enable_color" = "no"; then
  USE_COLOR="False"
fi
AC_SUBST(USE_COLOR)

dnl Try to link with static versions of libraries.

AC_ARG_WITH(static-libs, [  --with-static-libs      link with static versions of libraries])
if test "x$with_static_libs" = "xyes"; then
  LDFLAGS="-static $LDFLAGS"
fi

dnl Check whether to use existential type witnesses

AC_MSG_CHECKING([for gadt type witnesses])
AC_ARG_WITH(type-witnesses,
            AS_HELP_STRING([--with-type-witnesses],
                           [use GADT type witnesses (EXPERIMENTAL!)])
            ,with_typew=$withval
            ,with_typew=no)

if test "$with_typew" = "yes"; then
  GHCFLAGS="$GHCFLAGS -fglasgow-exts"
  AC_DEFINE([GADT_WITNESSES])
  AC_MSG_RESULT([yes])
else
  AC_MSG_RESULT([no])
fi

dnl look for zlib library and header.
dnl Use GHC_CHECK_LIBRARY and not AC_CHECK_HEADER / AC_CHECK_LIB because GHC may
dnl use a different library and header than the system GCC (on Windows, issue 813)

GHC_CHECK_LIBRARY([z],[zlib.h deflate],[Int -> Int -> Int],
                  [czlibfound=yes], [czlibfound=no])

if test $czlibfound = "yes"; then
  AC_MSG_CHECKING([whether to use the C zlib])
  AC_ARG_ENABLE(c-zlib,
                AS_HELP_STRING([--disable-c-zlib], [do not use the C zlib, even if it is present]),
                czlib=$enableval,
                czlib=yes)
  AC_MSG_RESULT($czlib)
fi

if test $czlib != "yes"; then
  dnl See if we have an adequate version of the haskell zlib
  GHC_CHECK_MODULE(Codec.Compression.GZip( decompress ),
                   zlib,
                   decompress undefined,
                   [haskellzlibfound=yes],
                   [haskellzlibfound=no])

  if test $bytestring = "yes" -a $haskellzlibfound = "yes"; then
    AC_MSG_CHECKING([whether to use the Haskell zlib])
    AC_ARG_ENABLE(haskell-zlib,
                  AS_HELP_STRING([--disable-haskell-zlib], [do not use the Haskell zlib, even if it is present (note that currently the interface is somewhat inefficient at reading large files, so you should be cautious of using the Haskell zlib)]),
                  haskellzlib=$enableval,
                  haskellzlib=yes)
    AC_MSG_RESULT($haskellzlib)
  fi

  if test "$haskellzlib" = "yes"; then
    CPPFLAGS="$CPPFLAGS -DHAVE_HASKELL_ZLIB"
  else
    AC_MSG_ERROR([Cannot find a suitable zlib library; for the C zlib please set the LDFLAGS environment variable, or for the Haskell zlib install at least version 0.4.0.4, and don't disable bytestring!)])
  fi
fi

dnl Find libwww and other libraries needed by hswww.c

AC_ARG_WITH(libwww,
            AS_HELP_STRING([--with-libwww],[use libwww]))

if test "$with_libwww" == "yes"; then
  AC_MSG_CHECKING([for libwww])
  if eval libwww-config --version 2>/dev/null >/dev/null; then
    CPPFLAGS="$CPPFLAGS -DHAVE_LIBWWW"
    WWWVERSION=`libwww-config --version`
    LIBS="$LIBS `libwww-config --libs`"
    CFLAGS="$CFLAGS `libwww-config --cflags`"
    have_libwww=yes
    AC_MSG_RESULT($WWWVERSION)
  else
    AC_MSG_RESULT(FAILED)
    AC_MSG_WARN([libwww-config was not found])
  fi
fi

dnl Find libcurl and other libraries needed by hscurl.c

AC_ARG_WITH(libcurl,
            AS_HELP_STRING([--without-libcurl],[do not use libcurl]))

AC_ARG_WITH(curl, AS_HELP_STRING([--with-curl=<DIR>],[use curl installed in directory <DIR>]))

CURLLIBS=
CURL_CONFIG="curl-config"

if test "$with_libcurl" != "no" -a "$have_libwww" != "yes"; then
  if test "$with_curl" != ""; then
    CURL_CONFIG="$with_curl/bin/curl-config"
  fi

  AC_MSG_CHECKING([for libcurl])
  if eval $CURL_CONFIG --version 2>/dev/null >/dev/null; then
    ver=`$CURL_CONFIG --version | sed -e "s/libcurl //g"`
    opt="--libs"
    CURLCFLAGS=`$CURL_CONFIG --cflags`
    if test "x$with_static_libs" = "xyes"; then
      CURLCFLAGS="$CURLFLAGS -DCURL_STATICLIB"
      opt="--static-libs"
    fi
    CURLLIBS=`$CURL_CONFIG $opt | sed "s_-arch @<:@^ @:>@@<:@^ @:>@*__"`
    AC_MSG_RESULT($ver)
  else
    AC_MSG_RESULT(FAILED)
    AC_MSG_WARN([curl-config was not found])
  fi
fi

if test "$CURLLIBS" != ""; then
  # we enable curl
  CPPFLAGS="$CPPFLAGS -DHAVE_CURL"

  CFLAGS="$CFLAGS $CURLCFLAGS"
  LIBS="$LIBS $CURLLIBS"
  old_LIBS="$LIBS"
  AC_CHECK_LIB(curl, curl_global_init,
               [],
               AC_MSG_ERROR(Cannot actually use libcurl; try specifying --without-libcurl.))
  AC_CHECK_LIB(curl, curl_multi_timeout,
               CPPFLAGS="$CPPFLAGS -DCURL_MULTI_TIMEOUT")
  LIBS="$old_LIBS"

  # enable pipelining for curl >= 7.18.0
  AC_MSG_CHECKING([for libcurl pipelining])
  let vernum=0x0`$CURL_CONFIG --vernum`
  let minver=0x071200 # 7.18.0
  if test $vernum -ge $minver; then
    CPPFLAGS="$CPPFLAGS -DCURL_PIPELINING"
    AC_MSG_RESULT([yes])
    # enable pipelining by default for curl >= 7.19.1
    let minver=0x071301 # 7.19.1
    if test $vernum -ge $minver; then
      CPPFLAGS="$CPPFLAGS -DCURL_PIPELINING_DEFAULT"
    fi
  else
    AC_MSG_RESULT([no])
    AC_MSG_WARN([Requires libcurl version >= 7.18.0])
  fi
fi

dnl Check for libcurses/terminfo...

AC_ARG_ENABLE(libcurses, [  --without-curses        do not use libcurses])
AC_ARG_ENABLE(terminfo, [  --without-terminfo        do not use Haskell terminfo])

HAVE_CURSES=False
HAVE_TERMINFO=False
if test "$with_libcurses" != "no"; then
  # term.h is documented to require curses.h and actually needs it on
  # Solaris, for instance, though not on GNU/Linux.
  AC_CHECK_HEADER(term.h,
      [AC_SEARCH_LIBS(tgetent, [curses ncurses], [HAVE_CURSES=True])], ,
      [#include <curses.h>
      ])
fi

if test "$HAVE_CURSES" != "True"; then
  if test "$with_terminfo" != "no"; then
    GHC_CHECK_MODULE(System.Console.Terminfo ( setupTermFromEnv ),
                     terminfo,
                     setupTermFromEnv,
                     [HAVE_TERMINFO=True],)
  fi

  if test "$HAVE_TERMINFO" != "True"; then
    AC_MSG_WARN([Cannot find libcurses or Haskell terminfo, disabling color.])
  else
    CPPFLAGS="$CPPFLAGS -DHAVE_TERMINFO"
  fi
else
  # The precondition for this is: we both have curses available and not disabled
  CPPFLAGS="$CPPFLAGS -DHAVE_CURSES"
fi
AC_SUBST(HAVE_CURSES)

dnl Look for a suitable diff command

AC_CHECK_PROGS(DIFF, [gdiff gnudiff diff])

dnl Look for NullSoft Installer System

AC_PATH_PROGS(MAKENSIS, [makensis.exe])

dnl Check endianness

AC_C_BIGENDIAN([BIGENDIAN="True"],[BIGENDIAN="False"])
AC_SUBST(BIGENDIAN)

dnl Check for a haddock 2.0.0 or more recent

AC_CHECK_PROG(HADDOCK, haddock, haddock)
AC_MSG_CHECKING([haddock version])
if test "$HADDOCK" = ""; then
  HADDOCK_VERSION=0
else
  HADDOCK_VERSION=`haddock --version | head -n 1 | sed 's/^.* \(@<:@0-9@:>@@<:@0-9@:>@*\)\..*/\1/g'`
fi
AC_SUBST(HADDOCK_VERSION)
AC_MSG_RESULT(version $HADDOCK_VERSION)
if test "$HADDOCK_VERSION" -lt 2; then
  AC_MSG_WARN([You need haddock 2.0.0 or later to build API docs])
fi

dnl Write results

dnl Remove these two tests when autoconf >2.59 can be required.
if test -z "${datarootdir}"; then
  datarootdir=${datarootdir:-'${prefix}/share'}
  AC_SUBST(datarootdir)
fi
if test -z "${docdir}"; then
  docdir=${docdir:-'${datarootdir}/doc/${PACKAGE_TARNAME}'}
  AC_SUBST(docdir)
fi

AC_DEFUN([AC_DATAROOTDIR_CHECKED])
OUTPUT_WORKAROUND
AC_CONFIG_FILES([autoconf.mk src/Autoconf.lhs
           tools/cgi/darcs.cgi tools/cgi/README tools/cgi/cgi.conf
	   release/darcs.spec])
AC_OUTPUT

cat <<[EOF]

The build is configured as follows:

    bindir          = $bindir
    sbindir         = $sbindir
    mandir          = $mandir
    datadir         = $datadir
    sysconfdir      = $sysconfdir
    libexecdir      = $libexecdir

    Build Manual    = $BUILDDOC

If you want to adjust any of these values, edit autoconf.mk and
src/Autoconf.lhs -- or run configure with appropriate settings.

[EOF]

dnl Responsibility shifts to the user after this point.
