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

AC_INIT(install-sh)
AC_CONFIG_HEADER(config.h)

AC_ARG_WITH(prolog,
   [ --with-prolog=PLBINARY   use given SWI Prolog binary])

AC_SUBST(PL)
AC_SUBST(PLLIBS)
AC_SUBST(PLBASE)
AC_SUBST(PLARCH)
AC_SUBST(PLINCL)
AC_SUBST(COFLAGS)
AC_SUBST(CWFLAGS)
AC_SUBST(CMFLAGS)
AC_SUBST(ETAGS)
AC_SUBST(LD)
AC_SUBST(SO)
AC_SUBST(LDSOFLAGS)
AC_SUBST(RUNTEX)

LD=ld
AC_PROG_CC

# Do not cache this, it changes too often in many configurations
unset ac_cv_prog_PL

if test -z "$PLINCL"; then
plcandidates="$with_prolog pl swipl swi-prolog"
AC_CHECK_PROGS(PL, $plcandidates, "none")
if test $PL = "none"; then
   AC_ERROR("Cannot find SWI-Prolog. SWI-Prolog must be installed first")
else
   AC_CHECKING("Running $PL -dump-runtime-variables")
   eval `$PL -dump-runtime-variables`
fi
PLINCL="$PLBASE/include"
AC_MSG_RESULT("		PLBASE=$PLBASE")
AC_MSG_RESULT("		PLARCH=$PLARCH")
AC_MSG_RESULT("		PLLIBS=$PLLIBS")
AC_MSG_RESULT("		PLLDFLAGS=$PLLDFLAGS")
AC_MSG_RESULT("		PLSHARED=$PLSHARED")
fi

case "$PLARCH" in
    *)			LDFLAGS="$PLLDFLAGS" ;;
esac

if test "$PLSHARED" = "yes"; then
  if test ! -z "$GCC"; then
    CMFLAGS="$CMFLAGS -fpic"
  fi

  SO="so"				 # Default shared object extension
  LD="$CC"				 # Default linking through gcc
  LDSOFLAGS="-shared"			 # Create a shared object

  case "$PLARCH" in
    *solaris* | *sunos5*)	LD=/usr/ccs/bin/ld
				LDSOFLAGS="-G"
    				;;
    *sunos*)			LD=ld
				LDSOFLAGS="-assert nodefinitions"
				;;
    *linux*)                    ;;
    *irix*)			if test -z "$GCC"; then
				  CWFLAGS="-woff 1164"
				fi
				;;
    *osf*)			if test -z "$GCC"; then
				  LD=ld
				  LDSOFLAGS="-shared -expect_unresolved \*"
				  CMFLAGS="$CMFLAGS -std"
				fi
				;;
    *aix*)			SO="o"
				LDSOFLAGS="-bI:$PLBASE/include/SWI-Exports"
				AC_DEFINE(__unix__)
				;;
    *hpux*)			SO="sl"
				LDSOFLAGS="-b"
				;;
    *)				;;
  esac
else
  SHLIB=""
fi

AC_CHECK_PROGS(MAKE, gmake make, "make")
AC_MSG_CHECKING("whether make is GNU-make")
if ($MAKE -v 2>&1) | grep GNU > /dev/null; then
    AC_MSG_RESULT(yes)
    gmake=yes
else
    VPATH="VPATH = "
    gmake=no
fi
AC_CHECK_PROGS(ETAGS, etags ctags, ":")
AC_CHECK_PROGS(RUNTEX, runtex, ":")
AC_PROG_INSTALL
AC_PROG_CPP
AC_ISC_POSIX
AC_HEADER_STDC
if test ! -z "$GCC"; then
    if test $gmake = yes; then
        COFLAGS='$(shell if test -r .coflags; then cat .coflags; else echo "-O2"; fi)'
    else
	COFLAGS=-O2
    fi
    CWFLAGS="$CWFLAGS -Wall"
else
    COFLAGS=-O
fi
CFLAGS="$CMFLAGS"
AC_C_BIGENDIAN

AC_CHECK_HEADERS(malloc.h unistd.h sys/time.h fcntl.h floatingpoint.h)
AC_CHECK_FUNCS(mmap strerror)

AC_OUTPUT(Makefile)
