dnl *********************************
dnl * autoconf configuration script *
dnl *********************************

AC_INIT(SETVARS)
AC_CONFIG_HEADER(EnginePl/config.h)



dnl ***************************
dnl * initial variable values *
dnl ***************************

PROLOG_NAME='GNU Prolog'
PROLOG_VERSION='1.1.2'
PROLOG_DATE='Nov 29 1999'
PROLOG_COPYRIGHT='Copyright (C) 1999 Daniel Diaz'

TOP_LEVEL=gprolog
GPLC=gplc
HEXGPLC=hexgplc
ENV_VARIABLE=PL_PATH

USE_REGS=yes
USE_LINEDIT=yes
USE_SOCKETS=yes
USE_FD_SOLVER=yes

LIB_LINEDIT=liblinedit.a
LIB_ENGINE_PL=libengine_pl.a
LIB_BIPS_PL=libbips_pl.a
LIB_ENGINE_FD=libengine_fd.a
LIB_BIPS_FD=libbips_fd.a



dnl ******************
dnl * variable names *
dnl ******************

AC_SUBST(PROLOG_NAME)
AC_SUBST(PROLOG_VERSION)
AC_SUBST(PROLOG_DATE)
AC_SUBST(PROLOG_COPYRIGHT)

AC_SUBST(TOP_LEVEL)
AC_SUBST(GPLC)
AC_SUBST(HEXGPLC)
AC_SUBST(ENV_VARIABLE)

AC_SUBST(CFLAGS_PREFIX_REG)
AC_SUBST(CFLAGS_MACHINE)
AC_SUBST(CFLAGS_UNSIGNED_CHAR)
AC_SUBST(LDLIBS)

AC_SUBST(USE_REGS)
AC_SUBST(USE_LINEDIT)
AC_SUBST(USE_SOCKETS)
AC_SUBST(USE_FD_SOLVER)

AC_SUBST(ROOT_DIR)
AC_SUBST(INSTALL_DIRNAME)
AC_SUBST(INSTALL_DIR)
AC_SUBST(LINKS_DIR)
AC_SUBST(MAKE_LE_DIRS)
AC_SUBST(MAKE_FD_DIRS)
AC_SUBST(MAKE_LE_INTERF_OBJS)
AC_SUBST(MAKE_SOCKETS_OBJS)

AC_SUBST(MAKE_INSTALL_LINKS)
AC_SUBST(MAKE_INSTALL_LE)
AC_SUBST(MAKE_INSTALL_FD)
AC_SUBST(MAKE_INSTALL_DOC)
AC_SUBST(MAKE_INSTALL_EXAMPLES)

AC_SUBST(MAKE_UNINSTALL_PKG)

AC_SUBST(LIB_LINEDIT)
AC_SUBST(LIB_ENGINE_PL)
AC_SUBST(LIB_BIPS_PL)
AC_SUBST(LIB_ENGINE_FD)
AC_SUBST(LIB_BIPS_FD)



dnl ***********************
dnl * checks for programs *
dnl ***********************

AC_PROG_CC

if test "$GCC" != yes
then
	AC_MSG_ERROR(gcc is required)
fi

dnl for old versions of autoconf
dnl EXEEXT=''
dnl AC_SUBST(EXEEXT)

AC_EXEEXT
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_PROG_INSTALL
AC_PROG_LN_S




dnl *******************
dnl * package options *
dnl *******************

AC_ARG_WITH(c-flags,[  --with-c-flags[=FLAGS] specify C flags],
		[WITH_C_FLAGS="$withval"],
		[WITH_C_FLAGS=yes])

AC_ARG_WITH(links,[  --with-links[=PATH]    set PATH for link to executables],
		[WITH_LINKS="$withval"],
		[WITH_LINKS=yes])

AC_ARG_ENABLE(regs,[  --disable-regs         do not use machine registers],
              [case "$enableval" in
		   no) AC_DEFINE(NO_USE_REGS);;
		   *)  ;;
	       esac])

AC_ARG_ENABLE(linedit,[  --disable-linedit      do not include line editor facility],
              [case "$enableval" in
		   no) AC_DEFINE(NO_USE_LINEDIT) USE_LINEDIT=no;;
		   *)  USE_LINEDIT=yes;;
	       esac])

AC_ARG_ENABLE(sockets,[  --disable-sockets      do not include sockets facility],
              [case "$enableval" in
		   no) AC_DEFINE(NO_USE_SOCKETS)  USE_SOCKETS=no;;
		   *)  USE_SOCKETS=yes;;
	       esac])

AC_ARG_ENABLE(fd-solver,[  --disable-fd-solver    do not include the FD constraint solver],
              [case "$enableval" in
		   no) AC_DEFINE(NO_USE_FD_SOLVER) USE_FD_SOLVER=no;;
		   *)  USE_FD_SOLVER=yes;;
	       esac])



dnl *******************************
dnl * host detection and features *
dnl *******************************

AC_CANONICAL_HOST

case "$host_cpu" in
	sparc)          AC_DEFINE(M_sparc);;
	i*86)           AC_DEFINE(M_ix86);;
        powerpc|ppc)    AC_DEFINE(M_powerpc);;
esac

case "$host_os" in
	sunos*)         AC_DEFINE(M_sunos);;
	solaris*)       AC_DEFINE(M_solaris);;
	linux*)         AC_DEFINE(M_linux);;
	cygwin*)        AC_DEFINE(M_cygwin);;
esac

case "$host" in
	sparc*sunos*)   AC_DEFINE(M_sparc_sunos);;
	sparc*solaris*) AC_DEFINE(M_sparc_solaris);;
	*86*linux*)     AC_DEFINE(M_ix86_linux);;
        powerpc*linux*) AC_DEFINE(M_powerpc_linux);;
	*86*cygwin*)	AC_DEFINE(M_ix86_cygwin);;
	*)              AC_MSG_ERROR(unsupported architecture)
esac


AC_CHECK_SIZEOF(long)




dnl ********************
dnl * C compiler flags *
dnl ********************

AC_C_INLINE

case "$WITH_C_FLAGS" in
	debug) CFLAGS='-g -Wall';;
	no)    CFLAGS='';;
	yes)   CFLAGS='-O3 -finline-functions -fomit-frame-pointer';;
	*)     CFLAGS=$withval;;

esac

CFLAGS_PREFIX_REG='-ffixed-%s'
CFLAGS_UNSIGNED_CHAR='-funsigned-char'

case "$host" in
	*sparc*sunos4.1.3) CFLAGS_MACHINE='-msupersparc';;
	*sparc*sunos*)     CFLAGS_MACHINE=;;
	*sparc*solaris)    CFLAGS_MACHINE='-msupersparc';;
        powerpc*linux*)    CFLAGS_MACHINE=;;
	*86*linux*)        CFLAGS_MACHINE='-m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2';;
esac



dnl ********************
dnl * libraries to add *
dnl ********************

LDLIBS='-lm'

if test "$USE_SOCKETS" = yes 
then
	AC_CHECK_FUNC(socket,[],
                      AC_CHECK_LIB(socket,socket,LDLIBS="$LDLIBS -lsocket",
                                   AC_MSG_ERROR(cannot find socket library)))

	AC_CHECK_FUNC(gethostbyname,[],
                      AC_CHECK_LIB(nsl,gethostbyname,LDLIBS="$LDLIBS -lnsl",
                                   AC_MSG_ERROR(cannot find socket library)))
fi



dnl ********************
dnl * config.h defines *
dnl ********************

AC_DEFINE_UNQUOTED(PROLOG_NAME,      "$PROLOG_NAME")
AC_DEFINE_UNQUOTED(PROLOG_VERSION,   "$PROLOG_VERSION")
AC_DEFINE_UNQUOTED(PROLOG_DATE,      "$PROLOG_DATE")
AC_DEFINE_UNQUOTED(PROLOG_COPYRIGHT, "$PROLOG_COPYRIGHT")

AC_DEFINE_UNQUOTED(TOP_LEVEL,        "$TOP_LEVEL")
AC_DEFINE_UNQUOTED(GPLC,             "$GPLC")
AC_DEFINE_UNQUOTED(HEXGPLC,          "$HEXGPLC")
AC_DEFINE_UNQUOTED(ENV_VARIABLE,     "$ENV_VARIABLE")

AC_DEFINE_UNQUOTED(M_CPU,            "$host_cpu")
AC_DEFINE_UNQUOTED(M_VENDOR,         "$host_vendor")
AC_DEFINE_UNQUOTED(M_OS,             "$host_os")

AC_DEFINE_UNQUOTED(CC,               "$CC")
AC_DEFINE_UNQUOTED(CFLAGS_PREFIX_REG,"$CFLAGS_PREFIX_REG")
AC_DEFINE_UNQUOTED(CFLAGS,           "$CFLAGS")  
AC_DEFINE_UNQUOTED(CFLAGS_MACHINE,   "$CFLAGS_MACHINE")
AC_DEFINE_UNQUOTED(LDFLAGS,          "$LDFLAGS")
AC_DEFINE_UNQUOTED(LDLIBS,           "$LDLIBS")

AC_DEFINE_UNQUOTED(EXE_SUFFIX,       "$EXEEXT")

AC_DEFINE_UNQUOTED(LIB_LINEDIT,      "$LIB_LINEDIT")
AC_DEFINE_UNQUOTED(LIB_ENGINE_PL,    "$LIB_ENGINE_PL")
AC_DEFINE_UNQUOTED(LIB_BIPS_PL,      "$LIB_BIPS_PL")
AC_DEFINE_UNQUOTED(LIB_ENGINE_FD,    "$LIB_ENGINE_FD")
AC_DEFINE_UNQUOTED(LIB_BIPS_FD,      "$LIB_BIPS_FD")



dnl ***************
dnl * directories *
dnl ***************

cd ..
ROOT_DIR=`pwd`
cd src

INSTALL_DIRNAME=$TOP_LEVEL-$PROLOG_VERSION

case "$prefix" in
        in-place)
	   INSTALL_DIR=$ROOT_DIR
           MAKE_INSTALL_DOC=
           MAKE_INSTALL_EXAMPLES=
           MAKE_UNINSTALL_PKG='$(UNINSTALL_IN_PLACE)'
           if test "$WITH_LINKS" = yes
	   then
		WITH_LINKS=no
	   fi;;

	none|NONE)
	   PREFIX=/usr/local
           INSTALL_DIR=$PREFIX/$INSTALL_DIRNAME
           MAKE_INSTALL_DOC='$(INSTALL_DOC)'
           MAKE_INSTALL_EXAMPLES='$(INSTALL_EXAMPLES)'
           MAKE_UNINSTALL_PKG='$(UNINSTALL_ALL)';;

	*) PREFIX=$prefix
           INSTALL_DIR=$PREFIX/$INSTALL_DIRNAME
           MAKE_INSTALL_DOC='$(INSTALL_DOC)'
           MAKE_INSTALL_EXAMPLES='$(INSTALL_EXAMPLES)'
           MAKE_UNINSTALL_PKG='$(UNINSTALL_ALL)';;
esac

echo "--> directory for the installation: $INSTALL_DIR"

case "$WITH_LINKS" in
	''|no)
	   LINKS_DIR=
           MAKE_INSTALL_LINKS=;;

	yes)
	   case "$exec_prefix" in
		none|NONE) LINKS_DIR=$PREFIX/bin;;
		*) LINKS_DIR=$exec_prefix/bin;;
	   esac
           MAKE_INSTALL_LINKS='$(INSTALL_LINKS)';;

	*)
	   LINKS_DIR=$WITH_LINKS
           MAKE_INSTALL_LINKS='$(INSTALL_LINKS)';;
esac

if test "$LINKS_DIR" = ""
then
           echo "--> directory for link to binaries: none (no links)"
else
           echo "--> directory for link to binaries: $LINKS_DIR"
fi

if test "$USE_LINEDIT" = yes
then
	MAKE_LE_DIRS='$(LE_DIRS)'
	MAKE_INSTALL_LE='$(INSTALL_LE)'
	MAKE_LE_INTERF_OBJS='$(LE_INTERF_OBJS)'
else
	MAKE_LE_DIRS=
	MAKE_INSTALL_LE=
	MAKE_LE_INTERF_OBJS='$(NO_LE_INTERF_OBJS)'
fi

if test "$USE_SOCKETS" = yes
then
	MAKE_SOCKETS_OBJS='$(SOCKETS_OBJS)'
else
	MAKE_SOCKETS_OBJS='$(NO_SOCKETS_OBJS)'
fi

if test "$USE_FD_SOLVER" = yes
then
	MAKE_FD_DIRS='$(FD_DIRS)'
	MAKE_INSTALL_FD='$(INSTALL_FD)'
else
	MAKE_FD_DIRS=
	MAKE_INSTALL_FD=
fi

echo "$INSTALL_DIRNAME" >VERSION
echo "$INSTALL_DIRNAME" >../VERSION
echo "$PROLOG_VERSION" >version_no.tex
if diff version_no.tex ../doc/version_no.tex >/dev/null 2>&1
then
	rm version_no.tex
else
	mv version_no.tex ../doc/version_no.tex

fi
echo "$PROLOG_COPYRIGHT" >../doc/copyright.tex


if test -f TestsPl/Makefile.in
then
	OTHER_MAKEFILES='TestsPl/Makefile DevUtils/Makefile'
else
	OTHER_MAKEFILES=
fi

AC_OUTPUT(Makefile
          TopComp/Makefile
          Pl2Wam/Makefile
          Wam2Ma/Makefile
          Ma2Asm/Makefile
          Fd2C/Makefile
          Linedit/Makefile
          EnginePl/Makefile
          BipsPl/Makefile
          EngineFD/Makefile
          BipsFD/Makefile
          $OTHER_MAKEFILES)
