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

AC_INIT(Pce.in)

AC_CONFIG_HEADER(config.h)
AC_SUBST(YACC)
AC_SUBST(NETLIBS)
AC_SUBST(PL)
AC_SUBST(PLLIBS)
AC_SUBST(PLBASE)
AC_SUBST(PLARCH)
AC_SUBST(BOOTPL)
AC_SUBST(PLINCL)
AC_SUBST(XLIB)
AC_SUBST(XINCLUDES)
AC_SUBST(XLIBS)
AC_SUBST(CXXLIBHACK)
AC_SUBST(PCEHOME)
AC_SUBST(CIFLAGS)
AC_SUBST(COFLAGS)
AC_SUBST(CWFLAGS)
AC_SUBST(CMFLAGS)
AC_SUBST(ETAGS)
AC_SUBST(ARCH)
AC_SUBST(OS)
AC_SUBST(TAR_CP_FLAGS)
AC_SUBST(ALLTARGET)
AC_SUBST(RTSUFFIX)
AC_SUBST(LD)
AC_SUBST(SO)
AC_SUBST(LDSOFLAGS)
AC_SUBST(SONAMEFLAG)
AC_SUBST(SOWHOLEARCHIVE)
AC_SUBST(CXXTARGETS)
AC_SUBST(GCCLIB)
AC_SUBST(SOEXTRAOBJ)
AC_SUBST(PLTARGET)
AC_SUBST(builddir)
AC_SUBST(VMAJOR)
AC_SUBST(VMINOR)
AC_SUBST(VPATCH)
AC_SUBST(LDCONFIG)
AC_SUBST(AR)
AC_SUBST(MAKESHELL)
AC_SUBST(MAKE)
AC_SUBST(RSYNC)
AC_SUBST(QPENTRIES)
AC_SUBST(QPBIN)
AC_SUBST(QPPATH)
AC_SUBST(SP_PATH)

RTSUFFIX=""
QPENTRIES=""
QPBIN="sun4-5"
QPPATH="/usr/quintus"
CXXLIBHACK=""

LD=ld
AC_PROG_CC
AC_PROG_RANLIB
AC_CHECK_PROGS(AR, ar /usr/ccs/bin/ar, ar)
AC_CHECK_PROGS(LDCONFIG, ldconfig, "true")
AC_CANONICAL_HOST
changequote(,)dnl
ARCH=`echo $host | sed 's/\([^-]*\)-[^-]*-[^-]*/\1/'`
OS=`echo $host | sed 's/[^-]*-[^-]*-\([^-]*\)/\1/'`
VMAJOR=`sed 's/\([0-9]*\).*/\1/' ../VERSION`
VMINOR=`sed 's/[0-9]*\.\([0-9]*\).*/\1/' ../VERSION`
VPATCH=`sed 's/[0-9]*\.[0-9]*\.\([0-9]*\).*/\1/' ../VERSION`
changequote([,])dnl

swipl=maybe
runtime=no

AC_ARG_ENABLE(syslibh, [  --enable-syslibh	  Prototype file to enable gcc -Wall],
	      [case "$enableval" in
	          yes)	AC_DEFINE(SYSLIB_H, <h/syslib.h>)
			syslibh=yes ;;
		  no)   ;;
		  *)	AC_DEFINE(SYSLIB_H, "$enableval")
			syslibh=yes ;;
	      esac])
AC_ARG_WITH(pl, [  --without-pl		  Donot include SWI-Prolog],
	    [case "$withval" in
		yes)	swipl=yes ;;
		no)     swipl=no ;;
	     esac])
AC_ARG_ENABLE(shared, [  --enable-shared	  Prepare for shared lib],
	      [case "$enableval" in
		   yes) MAKE_SHARED_OBJECT=yes ;;
		   no)  MAKE_SHARED_OBJECT=no ;;
	       esac])
AC_ARG_ENABLE(runtime, [  --enable-runtime	  Runtime system only],
	      [case "$enableval" in
		   yes) AC_DEFINE(O_RUNTIME)
		        runtime=yes
			RTSUFFIX=rt ;;
		   no)  runtime=no ;;
	       esac])
AC_ARG_ENABLE(passwd, [  --enable-passwd	  Password protected version],
	      [case "$enableval" in
		   yes) AC_DEFINE(O_LICENCE)
		        licence=yes ;;
		   no)  licence=no ;;
	       esac])
AC_ARG_ENABLE(mt, [  --enable-mt		  Enable Multi-threading],
	      [case "$enableval" in
		   yes) AC_DEFINE(_REENTRANT)
			;;
		   *)  ;;
	       esac])


if test ! -z "$CIFLAGS"; then CFLAGS="$CIFLAGS $CFLAGS"; fi

# Sometimes this is wrong !
PWD=`pwd`

AC_MSG_CHECKING("Path to installation")
PCEHOME=`dirname $PWD`
AC_MSG_RESULT("$PCEHOME")

AC_MSG_CHECKING("Path relative to $PCEHOME")
builddir=`basename $PWD`
AC_MSG_RESULT("$builddir")

if test "$swipl" != "no"; then
  if test "$runtime" = "yes"; then
    if test -z "$PL"; then
      if test "$exec_prefix" = "NONE"; then
	if test "$prefix" = "NONE"; then
	  plprefix=/usr/local
	else
	  plprefix="$prefix"
	fi
      else
	plprefix="$exec_prefix"
      fi
      if test -r $plprefix/lib/rt-pl-*/bin/$ARCH-$OS/pl; then
        PL=$plprefix/lib/rt-pl-*/bin/$ARCH-$OS/pl
	AC_MSG_RESULT("PL=$PL")
      else
        AC_MSG_RESULT("No $plprefix/lib/rt-pl-*/bin/$ARCH-$OS/pl");
      fi
    fi
    plcandidates="plrt"
  else
    plcandidates="pl swipl swi-prolog"
  fi
  AC_CHECK_PROGS(PL, $plcandidates, "none")
  if test $PL = "none"; then
     if test "$swipl" = yes; then
        AC_ERROR("Cannot find SWI-Prolog. SWI-Prolog must be installed first")
     else
        swipl=no
     fi
  else
     swipl=yes
     if test -z "$PLINCL"; then
         AC_CHECKING("Running $PL -dump-runtime-variables")
         eval `$PL -dump-runtime-variables`
     fi
  fi
  if test "$swipl" = yes; then
    if test -z "$MAKE_SHARED_OBJECT" && test "$PLSHARED" = "yes"; then
       MAKE_SHARED_OBJECT=yes
    fi
    AC_MSG_RESULT("		PLBASE=$PLBASE")
    AC_MSG_RESULT("		PLARCH=$PLARCH")
    AC_MSG_RESULT("		PLLIBS=$PLLIBS")
    AC_MSG_RESULT("		PLLDFLAGS=$PLLDFLAGS")
    AC_MSG_RESULT("		MAKE_SHARED_OBJECT=$MAKE_SHARED_OBJECT")
    
    case "$PLARCH" in
	*)			LDFLAGS="$PLLDFLAGS" ;;
    esac
    ALLTARGET=xpce-pl
    PLTARGET=nosoall
  else
    ALLTARGET=xpce
  fi
else
  ALLTARGET=xpce
fi

dnl Frank C: find the pl development include dir, should be same
dnl version. Is up to the user?

AC_ARG_WITH(pl-include,[  --with-pl-include=DIR   find the pl include files in DIR],
	[ case $withval in
		no|yes) echo 'Specify dir for pl-include please (ignored)';;
		*)	PLINCL="$withval";;
	  esac
	],
	[
		if test -z "$PLINCL"; then
	 	  PLINCL="$PLBASE/include"
		fi
	])

dnl The following part is to decide for making a shared library or
dnl other dynamic linkable object.  This isn't easy and and is mostly
dnl done on the basis what I know to work for specific operating systems.
dnl How to make this any better?
dnl
dnl Only the sunos, solaris and linux version are supposed to really work.
dnl Rest needs to be validated.

if test -z "$MAKE_SHARED_OBJECT"; then
  case "$OS" in
    *solaris* | *sunos* | *linux* | *irix* | *osf* | *aix*)
      MAKE_SHARED_OBJECT=yes;;
  esac
fi

if test "$MAKE_SHARED_OBJECT" = "yes"; then
  PLTARGET=soall			 # Link XPCE dynamically to prolog
  BOOTPL=pl
  
  AC_MSG_CHECKING("Extension for shared objects")
  SO=so
  case "$OS" in
    *aix*)	SO="o"
    	        ;;
    *hpux*)	SO="sl"
		;;
  esac
  AC_MSG_RESULT("$SO")

  if test ! -z "$GCC"; then
    AC_MSG_CHECKING("Looking for -lgcc")
    if GCCLIB=`${CC-cc} -print-libgcc-file-name`; then
      AC_MSG_RESULT("$GCCLIB")
    else
cat > conftest.c <<EOF
main(){}
EOF
      ${CC-cc} -c conftest.c
      ${CC-cc} -v -o conftest conftest.o 2> conftest.out 1>/dev/null
      changequote(,)dnl
      GCCLIBDIR=`grep lgcc conftest.out | sed 's/.*-L\([^ ]*gcc[^ ]*\).*/\1/'`
      changequote([,])dnl
      if test -r "$GCCLIBDIR/libgcc.a"; then
	GCCLIB="$GCCLIBDIR/libgcc.a"
	AC_MSG_RESULT("$GCCLIB")
      else
	AC_MSG_RESULT("no")
      fi
    fi
    rm -f conftest*
    CMFLAGS="$CMFLAGS -fPIC"

dnl See whether gcc supports building shared objects.  Some older versions
dnl recognise the flags, but don't behave appropirately.

    AC_MSG_CHECKING("whether ${CC-cc} -shared works")
    cat > conftest.c <<EOF
int
dummy()
{ return 1+1;
}
EOF
    ${CC-cc} -o conftest.$SO -fpic -shared conftest.c >/dev/null 2>&1
    if test -r conftest.$SO; then
      AC_MSG_RESULT("yes")
      LD=${CC-cc}
      LDSOFLAGS="-shared"
      GCCLD=true
    else
      AC_MSG_RESULT("no")
    fi
    rm -f conftest*

dnl GCC can be configured to use GNU-ld or native ld.  Check whether the
dnl relevant GNU-ld options are supported.    

    AC_MSG_CHECKING([whether ${CC-cc} -Wl,-soname works])
    cat > conftest.c <<EOF
int
dummy()
{ return 1+1;
}
EOF
    ${CC-cc} -o conftest.o -fpic -c conftest.c >/dev/null 2>&1
    ${CC-cc} -o conftest.$SO -shared -Wl,-soname,libconftest.$SO.1 conftest.o >/dev/null 2>&1
    if test -r conftest.$SO; then
      AC_MSG_RESULT("yes")
      SONAMEFLAG='-Wl,-soname,$(LIBXPCESOM)'
    else
      AC_MSG_RESULT("no")
    fi
    rm -f conftest*

    AC_MSG_CHECKING([whether ${CC-cc} -Wl,-whole-archive works])
    cat > conftest1.c <<EOF
int
dummy1()
{ return 1+1;
}
EOF
    cat > conftest2.c <<EOF
int
dummy2()
{ return 1+1;
}
EOF
    ${CC-cc} -o conftest1.o -fpic -c conftest1.c >/dev/null 2>&1
    ${CC-cc} -o conftest2.o -fpic -c conftest2.c >/dev/null 2>&1 
    ${AR} ru conftest.a conftest1.o conftest2.o
    ${RANLIB} conftest.a
    ${CC-cc} -o conftest.$SO -shared -Wl,-whole-archive conftest.a >/dev/null 2>&1
    if test -r conftest.$SO; then
      AC_MSG_RESULT("yes")
      SOWHOLEARCHIVE="-Wl,-whole-archive"
    else
      AC_MSG_RESULT("no")
      SOWHOLEARCHIVE="-u pceSend -u pceOpen"
    fi
    rm -f conftest*
  fi

  case "$OS" in
    *solaris* | *sunos5*)
	if test -z "$GCCLD"; then
	    LD=/usr/ccs/bin/ld
	    LDSOFLAGS="-G"
	    SONAMEFLAG="-h $(LIBXPCESOM)"
	    SOWHOLEARCHIVE="-u pceSend -u pceOpen"
	    if "$GXX" != "NONE"; then
	      LDSOFLAGS="$LDSOFLAGS -u __builtin_new"
	    fi
	fi
	if test -z "$GCC"; then
	    CMFLAGS="$CMFLAGS -K"
	fi
	;;
    *sunos*)
	if test -z "$GCCLD"; then
	    LD=ld
	    LDSOFLAGS="-assert nodefinitions"
	    SOWHOLEARCHIVE="-u _pceSend -u _pceOpen"
	    SONAMEFLAG=""
	    SOEXTRAOBJ=Initialize.o
	fi
	if test -z "$GCC"; then
	    CMFLAGS="$CMFLAGS -K"
	fi
	;;
    *linux*)
	CXXLIBHACK="-lc -Wl,-Bstatic"
	;;
    *irix*)
	;;
    *osf*)
	if test -z "$GCCLD"; then
	    LD=ld
	    LDSOFLAGS="-shared -expect_unresolved \*"
	fi
	if test -z "$GCC"; then
	    CMFLAGS="$CMFLAGS -std"
	fi
	AC_DEFINE(VARIABLE_POINTER_OFFSET)
	;;
    *aix*)
        LDSOFLAGS="-bI:$PLBASE/include/SWI-Exports"
	AC_DEFINE(__unix__)
	QPENTRIES=entries.c
	;;
    *hpux*)
    	SO="sl"
	LDSOFLAGS="-b"
	;;
    *)
	;;
  esac
else
  BOOTPL=xpce
fi

dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dnl % Begin Quintus specific configuration
dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if test -d ../quintus/src; then

AC_CHECK_PROGS(QPproduct, qpc qld quintus prolog, ":")
if test $QPproduct != ":"; then
    AC_CHECK_PROGS(QGETPATH, qgetpath, ":")
    if test $QGETPATH != ":"; then
	AC_PATH_PROG(QPPRODUCT, $QPproduct)
	AC_MSG_CHECKING("Quintus directory")
	QPPATH=`$QGETPATH -q $QPPRODUCT`
	AC_MSG_RESULT($QPPATH)
	AC_MSG_CHECKING("Quintus host type identifier")
	QPBIN=`$QGETPATH -h $QPPRODUCT`
	QPBIN=`basename $QPBIN`
	AC_MSG_RESULT($QPBIN)
    fi
fi


fi

dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dnl % End Quintus specific configuration
dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dnl % Begin SICStus specific configuration
dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if test -d ../sicstus/src; then
AC_CHECK_PROGS(SICSTUS, sicstus, ":")
if test $SICSTUS != ":"; then
cat > conftest.sh << _EOS_
$SICSTUS << _EOF_
absolute_file_name('\\\$SP_PATH', X),
write(X), nl, halt(0).
halt(1).
_EOF_
_EOS_
SP_PATH=`sh conftest.sh 2>/dev/null`
rm -f conftest.sh

if test ! -d "$SP_PATH"; then SP_PATH=""; fi
fi
fi

dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dnl % End SICStus specific configuration
dnl %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if test "$runtime" = yes; then
  PLTARGET=rt-$PLTARGET
fi

AC_CHECK_PROGS(MAKESHELL, bash sh, "/bin/sh")
AC_CHECK_PROGS(MAKE, gmake make, "make")
AC_CHECK_PROGS(ETAGS, etags ctags, ":")
AC_CHECK_PROGS(RSYNC, rsync ./rsync.sh)
AC_CHECK_PROGS(GXX, g++, "NONE")
if test "$GXX" != "NONE"; then
  AC_DEFINE(O_CPLUSPLUS)
  CXXTARGETS=cpp.o
  CXX="$GXX"
else
  CXXTARGETS=""
fi
AC_PROG_LN_S
AC_PROG_CPP
AC_PROG_CXX
AC_ISC_POSIX
AC_HEADER_STDC
if test ! -z "$GCC"; then
    COFLAGS=-O2
    if test "$ac_cv_header_stdc" = "yes" || test "$syslibh" = "yes"; then
        CWFLAGS=-Wall
    else
	CWFLAGS=
    fi
    CMFLAGS="$CMFLAGS -funsigned-char"
else
    COFLAGS=-O
    CWFLAGS=
fi
CFLAGS="$CMFLAGS"
AC_C_INLINE
AC_FUNC_ALLOCA
AC_C_BIGENDIAN

AC_CHECK_PROGS(YACC, bison yacc, yacc)
if test $YACC = "bison"; then YACC="bison -y"; fi

AC_CHECK_LIB(socket, socket,
	     [NETLIBS="$NETLIBS -lsocket"; LIBS="$LIBS -lsocket"]
	     AC_DEFINE(HAVE_LIBSOCKET))
AC_CHECK_LIB(nsl, t_bind,
	     [NETLIBS="$NETLIBS -lnsl"]
	     AC_DEFINE(HAVE_LIBNSL))

AC_CHECK_HEADERS(unistd.h string.h memory.h sys/time.h sys/file.h pwd.h)
AC_CHECK_HEADERS(sys/select.h sys/param.h malloc.h sys/resource.h stropts.h)
AC_CHECK_HEADERS(frame.h sys/timeb.h sys/times.h siginfo.h bstring.h)
AC_CHECK_HEADERS(sys/socketvar.h conio.h sys/access.h)
AC_CHECK_FUNCS(vsscanf on_exit memmove select popen strerror timelocal)
AC_CHECK_FUNCS(getdtablesize socket fork ftime getpid select getlogin)
AC_CHECK_FUNCS(getcwd setsid grantpt gethostname fstat gettimeofday)
AC_CHECK_FUNCS(rename atexit on_exit tmpnam sysinfo sigaction getpwnam)
AC_CHECK_FUNCS(shutdown)
AC_HEADER_TIME
AC_HEADER_DIRENT
AC_C_CHAR_UNSIGNED

if test "$ac_cv_func_vsscanf" = "yes"; then
AC_MSG_CHECKING("cast to va_list")
AC_TRY_RUN(
[
#include <stdarg.h>
main()
{ long v[2];
  void *ptr[2];

  ptr[0] = &v[0];
  ptr[1] = &v[1];
  vsscanf("34 45", "%d%d", (va_list)ptr);
  if ( v[0] == 34 && v[1] == 45 )
    exit(0);

  exit(1);
}], AC_DEFINE(HAVE_CAST_VA_LIST)
    AC_MSG_RESULT("yes"),
    AC_MSG_RESULT("no"),
    AC_MSG_RESULT("assuming no"))
fi

if test "$ac_cv_func_ftime" != "yes"; then
AC_MSG_CHECKING("tm_gmtoff")
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <time.h>
],
[ struct tm *tm;
  int i;
  
  i = tm->tm_gmtoff;
], AC_DEFINE(HAVE_TM_GMTOFF)
   AC_MSG_RESULT("yes"),
   AC_MSG_RESULT("no"))
fi

AC_TYPE_SIGNAL
AC_MSG_CHECKING("for BSD signal handlers")
AC_TRY_RUN(
[
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static int signalled;
static RETSIGTYPE catch (s) int s; { signalled = 1; }
main()
{ signal(SIGINT, catch);
  kill(getpid(), SIGINT);
  while(!signalled)
    ;
  if ( signal(SIGINT, catch) == catch )
    exit(0);
  exit(1);
}], AC_DEFINE(BSD_SIGNALS)
    AC_MSG_RESULT("BSD signals"),
    AC_MSG_RESULT("Posix signals"),
    AC_MSG_RESULT("assuming Posix signals"))

dnl The code below checks whether we can use siginfo () to set a signal
dnl handler that is passed the child's pid.  This code works on Solaris,
dnl but on irix, it compiles, but the handler is not passed the proper
dnl information.  Therefore we need to run the program.  If this fails,
dnl the system will fall back to use signal() and waitpid(), but this
dnl is more clumsy, and fails to work properly on Solaris.

AC_MSG_CHECKING("for usable siginfo")
AC_TRY_RUN(
[
#include <stdio.h>
#include <signal.h>
#include <siginfo.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef SA_NOMASK
#define SA_NOMASK 0
#endif

int child;

static void
child_changed(int sig, siginfo_t *info, void *uctx)
{ fprintf(stderr, "child_changed() called, child = %d, info = %p\n",
	  child, info);
  if ( info && info->si_pid == child )
    exit(0);

  exit(1);
}

int
main()
{ struct sigaction action, oaction;

  memset((char *) &action, 0, sizeof(action));
  action.sa_sigaction = child_changed;
  action.sa_flags     = SA_SIGINFO|SA_NOMASK|SA_RESTART;
  sigaction(SIGCHLD, &action, &oaction);

  if ( (child = fork()) == 0 )
  { sleep(1);
    exit(0);
  }

  sleep(8);
  return 1;
}
], AC_DEFINE(USE_SIGINFO)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no),
   AC_MSG_RESULT(assuming no))


AC_MSG_CHECKING("for union wait")
if test "$GCC" = yes; then
    ac_oldcflags="$CFLAGS"
    CFLAGS="$CFLAGS -Werror"
fi
AC_TRY_COMPILE(
[
#include <sys/wait.h>
],
[ union wait status;
  waitpid(1, &status, WNOHANG);
], AC_DEFINE(UNION_WAIT)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))
if test "$GCC" = "yes"; then CFLAGS="$ac_oldcflags"; fi

AC_MSG_CHECKING("for tagged lvalues")
AC_TRY_COMPILE(
[
],
[ int x;
  *(char *)&x = 'a';
  exit(0);
], AC_DEFINE(TAGGED_LVALUE)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))

AC_MSG_CHECKING("Checking struct termios.c_line")
AC_TRY_COMPILE(
[
#include <termios.h>
],
[ struct termios buf;
  buf.c_line = 0;
  exit(0);
], AC_DEFINE(TERMIOS_HAS_C_LINE)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no))


AC_MSG_CHECKING('ioctl(line, I_PUSH, "ttcompat")')
AC_TRY_RUN(
[
#include <stdio.h>
#include <fcntl.h>
#include <stropts.h>
main()
{ int line;

  if ( (line = open("/dev/tty", O_RDWR)) >= 0 &&
       ioctl(line, I_PUSH, "ttcompat") == 0 )
    exit(0);
  exit(1);
}
], AC_DEFINE(HAVE_TTCOMPAT)
   AC_MSG_RESULT(yes),
   AC_MSG_RESULT(no),
   AC_MSG_RESULT(assuming no))

BEFOREXLIBS="$LIBS"

AC_PATH_X

if test "$static_x_libraries" = "yes"; then
    XLIBS="$XLIB/libXt.a $XLIB/libX11.a"
else
    XLIBS="-lXt -lX11"
fi

if test "$x_libraries" = ""; then
    XLIB=/usr/lib
else
    XLIB="$x_libraries"
fi
if test "$x_includes" = ""; then
XINCLUDES=/usr/include
else
XINCLUDES="$x_includes"
fi

ac_oldlibs="$LIBS"
if test ! -z "$x_libraries"; then
  LIBS="-L$x_libraries $LIBS"
fi
XSMLIBS=""
AC_CHECK_LIB(SM, main,
	     XSMLIBS="-lSM -lICE",
	     ,
	     -lX11 -lXt -lICE)
XLIBS="$XLIBS $XSMLIBS"
echo "	XLIBS=$XLIBS"
if test ! -z "$x_libraries"; then
  LIBS="-L$x_libraries $XLIBS $ac_oldlibs"
else
  LIBS="$XLIBS $ac_oldlibs"
fi

dnl The SunOS 4.1.x linker is broken.  This hacks around this problem
dnl the real linking hacks around it in yet another way ...

case "$OS" in
  *sunos*) LIBS="$BEFOREXLIBS -lX11 $XSMLIBS";;
esac

dnl Check the critical but not very standard libXpm and libjpeg libraries
dnl and make sure they required headers can be found.

AC_ARG_WITH(xpm,[  --with-xpm=<dir>	  Location of Xpm include/lib],
	[case $withval in 
		no|yes)	echo 'Specify dir for XPM please';
			exit 1 ;;
		*)	LIBS="-L${withval}/lib ${LIBS}"
		        XLIBS="-L${withval}/lib ${XLIBS}"
			CFLAGS="-I${withval}/include ${CFLAGS}"
			CPPFLAGS="-I${withval}/include ${CPPFLAGS}"
			CIFLAGS="-I${withval}/include ${CIFLAGS}"
			echo "LIBS=${LIBS}"
	esac])
AC_ARG_WITH(jpeg,[  --with-jpeg=<dir>	  Location of JPEG include/lib],
	[case $withval in 
		no|yes)	echo 'Specify dir for JPEG please';
			exit 1 ;;
		*)	LIBS="-L${withval}/lib ${LIBS}"
		        XLIBS="-L${withval}/lib ${XLIBS}"
			CPPFLAGS="-I${withval}/include ${CPPFLAGS}"
			CIFLAGS="-I${withval}/include ${CIFLAGS}"
			echo "LIBS=${LIBS}"
	esac])

dnl Clear the cache if the answer is no so re-running configure
dnl after fixing the problem works fine without removing the cache file

if test "$ac_cv_lib_Xpm_main" != yes; then unset ac_cv_lib_Xpm_main; fi
if test "$ac_cv_lib_jpeg_main" != yes; then unset ac_cv_lib_jpeg_main; fi
if test "$ac_cv_header_X11_xpm_h" != yes; then unset ac_cv_header_X11_xpm_h; fi
if test "$ac_cv_header_jpeglib_h" != yes; then unset ac_cv_header_jpeglib_h; fi
if test "$ac_cv_header_jerror_h" != yes; then unset ac_cv_header_jerror_h; fi
	
AC_CHECK_LIB(jpeg, main,
	     [XLIBS="$XLIBS -ljpeg"]
	     AC_DEFINE(HAVE_LIBJPEG))

AC_CHECK_LIB(Xpm, main,
	     [XLIBS="$XLIBS -lXpm"]
	     AC_DEFINE(HAVE_LIBXPM))

if test "$ac_cv_lib_Xpm_main" != "yes"; then
cat << _EOM_
*****************************************************************
* ERROR: Could not find library -lXpm.				*
* Please install the XPM library and rerun configure.		*
* The primary source for libXpm is at:				*
*								*
*     http://www-sop.inria.fr/koala/lehors/xpm.html		*
*****************************************************************
_EOM_
exit 1
fi

AC_CHECK_HEADERS(X11/xpm.h)
if test "$ac_cv_header_X11_xpm_h" != yes; then
cat << _EOM_
*****************************************************************
* ERROR: Could not find <X11/xpm.h>				*
* Please install the XPM development files and rerun configure.	*
* The primary source for libXpm is at:				*
*								*
*     http://www-sop.inria.fr/koala/lehors/xpm.html		*
*****************************************************************
_EOM_
exit 1
fi

if test "$ac_cv_lib_jpeg_main" != "yes"; then
cat << _EOM_
*****************************************************************
* ERROR: Could not find library -ljpeg.				*
* Please install the JPEG library and rerun configure.		*
* The primary source for libjpeg is at:				*
*								*
*     http://www.ijg.org/					*
*****************************************************************
_EOM_
exit 1
fi

AC_CHECK_HEADERS(jpeglib.h jerror.h)
if test "$ac_cv_header_jpeglib_h" != yes -o \
        "$ac_cv_header_jerror_h" != yes; then
cat << _EOM_
*****************************************************************
* ERROR: Could not find <jpeglib.h> or <jerror.h>		*
* Please install the JPEG development files and rerun configure.*
* The primary source for libjpeg is at:				*
*								*
*     http://www.ijg.org/					*
*****************************************************************
_EOM_
exit 1
fi


AC_CHECK_FUNCS(XtPopupSpringLoaded XmIsMotifWMRunning XInitThreads)
LIBS="$ac_oldlibs"

AC_MSG_CHECKING("memory model")
echo '#include "confdefs.h"' > conftest.c
cat $srcdir/test/m-model.c >> conftest.c
eval $ac_link
if test -s conftest && eval `./conftest`; then
    msg=
    if test ! -z "$POINTER_OFFSET"; then
	AC_DEFINE_UNQUOTED(POINTER_OFFSET, $POINTER_OFFSET)
	msg="$msg POINTER_OFFSET=$POINTER_OFFSET"
    fi
    if test -z "$msg"; then msg="yes"; fi
    AC_MSG_RESULT("$msg")
else
    AC_MSG_RESULT("Could not compile m-model.c")
fi
rm -rf conftest*

dnl See which parts of the sources are there, and check the Makefiles we
dnl need to update.  You cannot call AC_OUTPUT more then once!

Makefiles="Makefile ../xpce.spec"
Makedirs="../CXX/demo ../quintus/src ../prolog/pwboot ../sicstus/src"
OptionalMakefiles="../pl/src/Makefile.bin"
for d in $Makedirs; do
    if test -r $d/Makefile.in; then
	Makefiles="$Makefiles $d/Makefile"
    fi
done
for f in $OptionalMakefiles; do
    if test -r $f.in; then
	Makefiles="$Makefiles $f"
    fi
done

AC_OUTPUT($Makefiles)
