dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during Netrek installation
dnl	to configure the system for the local environment.
AC_PREREQ([2.71])
AC_INIT([netrek-client-cow],[3.3.2],[quozl@netrek.org])
AC_CONFIG_SRCDIR([socket.c])
AC_CANONICAL_TARGET
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_RANLIB

AC_USE_SYSTEM_EXTENSIONS

AC_C_INLINE

#--------------------------------------------------------------------
#	Include sys/select.h if it exists and if it supplies things
#	that appear to be useful.  This appears to be true only on
#	the RS/6000 under AIX.  Some systems like OSF/1 have a
#	sys/select.h that's of no use, and other systems like SCO
#	UNIX have a sys/select.h that's pernicious.  If there isn't
#	a sys/select.h, then make sure that "fd_set" is defined in
#	sys/types.h.
#--------------------------------------------------------------------

AC_MSG_CHECKING(if fd_set requires sys/select.h)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>]], [[fd_set readMask, writeMask;]])],[AC_MSG_RESULT(no) ],[AC_EGREP_HEADER(fd_set, sys/select.h,
      AC_DEFINE(NEED_SYS_SELECT_H) AC_MSG_RESULT(yes),
      AC_DEFINE(NO_FD_SET) AC_MSG_RESULT(fd_set missing))])

#--------------------------------------------------------------------
#	Check for various typedefs and provide substitutes if
#	they don't exist.
#--------------------------------------------------------------------

AC_CHECK_HEADERS(unistd.h memory.h math.h stdlib.h)
AC_CHECK_HEADERS(sys/timeb.h sys/ptyio.h sys/fcntl.h fcntl.h)
AC_CHECK_HEADERS(ctype.h machine/endian.h sys/resource.h)
AC_CHECK_HEADERS(sys/wait.h netinet/in.h netinet/tcp.h sys/filio.h)

AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_SIZE_T
AC_FUNC_FORK
AC_STRUCT_TM

AC_MSG_CHECKING(for itimer in time.h)
AC_EGREP_HEADER(itimerval, time.h, AC_MSG_RESULT(yes) , AC_DEFINE(NEED_SYS_TIME_H) AC_MSG_RESULT(no))

AC_CHECK_SIZEOF(long)

AC_MSG_CHECKING(for u_int in sys/types.h)
AC_EGREP_HEADER(u_int, sys/types.h, AC_MSG_RESULT(yes) , AC_DEFINE(NO_U_INT) AC_MSG_RESULT(no))

AC_MSG_CHECKING(for PATH_MAX in limits.h)
AC_EGREP_CPP(PATH_MAX, [
#include <limits.h>
PATH_MAX
], AC_DEFINE(NO_PATH_MAX) AC_MSG_RESULT(no), AC_MSG_RESULT(yes))

AC_CHECK_FUNCS(strcmpi strncmpi)

#--------------------------------------------------------------------
#	Locate the X11 header files and the X11 library archive.
#--------------------------------------------------------------------

AC_PATH_X

AC_DEFINE(HAVE_X11)
AC_DEFINE(HAVE_X11_XPM_H)
NOWIN32="#"

if test -z "$NOX11" ; then

if test -z "$x_libraries" ; then
echo checking for X11 header files
XINCLUDES=""
AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <X11/Intrinsic.h>]])],[],[XINCLUDES="nope"])
if test "$XINCLUDES" = nope; then
    dirs="/usr/unsupported/include /usr/local/include /usr/X386/include \
      /usr/include/X11R4 /usr/X11R5/include /usr/include/X11R5 \
      /usr/openwin/include /usr/X11/include /pub/X11R5/include \
      /usr/local/X11R5/include /usr/X11R6/include /usr/include/X11R6 \
      /pub/X11R6/include /usr/local/X11R6/include"

    for i in $dirs ; do
	if test -r $i/X11/Intrinsic.h; then
	    XINCLUDES=" -I$i"
	fi
    done
fi
if test "$XINCLUDES" = nope; then
  echo "Warning:  couldn't find any X11 include files."
  XINCLUDES=""
fi

echo "checking for X11 library archive"
AC_CHECK_LIB(X11, main, XLIBSWLIB="-lX11", XLIBSWLIB=nope)
if test "$XLIBSWLIB" = nope; then
    dirs="/usr/unsupported/lib /usr/local/lib /usr/X386/lib \
      /usr/lib/X11R4 /usr/X11R5/lib /usr/lib/X11R5 /usr/openwin/lib \
      /usr/X11/lib /pub/X11R5/lib /usr/local/X11R5/lib \
      /usr/X11R6/lib /usr/lib/X11R6 /pub/X11R6/lib /usr/local/X11R6/lib"

    for i in $dirs ; do
	if test -r $i/libX11.a; then
	    XLIBSWDIR="-L$i"
	    XLIBSWLIB="-lX11"
	fi
    done
fi
if test "$XLIBSWLIB" = nope ; then
    AC_CHECK_LIB(Xwindow, main, XLIBSWLIB=-lXwindow)
fi
if test "$XLIBSWLIB" = nope ; then
    echo "Warning:  couldn't find the X11 library archive.  Using -lX11."
    XLIBSWLIB=-lX11
fi
else
XINCLUDES="-I$x_includes"
XLIBSWDIR="-L$x_libraries"
XLIBSWLIB="-lX11"
fi

fi

AC_SUBST(NOX11)
AC_SUBST(NOWIN32)

SAVELIBS="$LIBS"
LIBS="$LIBS $XLIBSWDIR $XLIBSWLIB"
SAVEINCS="$INCS"
INCS="$INCS $XINCLUDES"
AC_CHECK_LIB(Xpm, main, [AC_DEFINE(HAVE_XPM) XPMLIBS="-lXpm"])
AC_CHECK_HEADERS(X11/xpm.h)
LIBS="$SAVELIBS"
INCS="$SAVEINCS"

# Fix link order for Xpm
XLIBSW="$XLIBSWDIR $XPMLIBS $XLIBSWLIB"

INCS="$INCS $XINCLUDES"
AC_SUBST(INCS)
LIBS="$LIBS $XLIBSW"

#--------------------------------------------------------------------
#	Check for the existence of various libraries.  The order here
#	is important, so that then end up in the right order in the
#	command line generated by Make.
#--------------------------------------------------------------------

AC_CHECK_LIB(Xbsd, main, [LIBS="$LIBS -lXbsd"])
AC_CHECK_LIB(socket, main, [LIBS="$LIBS -lsocket"])
AC_CHECK_LIB(inet, main, [LIBS="$LIBS -linet"])
AC_CHECK_LIB(seq, main, [LIBS="$LIBS -lseq"])
AC_CHECK_LIB(sun, main, [LIBS="$LIBS -lsun"])
AC_CHECK_FUNCS(usleep random setstate strftime ftime)
AC_CHECK_LIB(m, main, [LIBS="$LIBS -lm"])
AC_CHECK_FUNCS(nint)
AC_EGREP_HEADER(rint, math.h, , AC_DEFINE(NEED_RINT_DEC))
AC_REPLACE_FUNCS(usleep setstate strdup rint)

#--------------------------------------------------------------------
#       Check for system dependent programs
#--------------------------------------------------------------------

#--------------------------------------------------------------------
#       Check for Imlib
#--------------------------------------------------------------------
AC_SEARCH_LIBS([imlib_load_image], [Imlib2], [],
               [AC_MSG_ERROR([Could not find Imlib2])])

#--------------------------------------------------------------------
#       Check for SDL
#--------------------------------------------------------------------

AM_PATH_SDL(1.2.4, AC_DEFINE(HAVE_SDL),[])
AM_CONDITIONAL(HAVE_SDL, [test x"$no_sdl" != x"yes"])

if test x$no_sdl != xyes; then
  have_SDLmixer=no
  AC_CHECK_LIB(SDL_mixer, Mix_OpenAudio, [have_SDLmixer=yes SDL_MIXER_LIBS="-lSDL_mixer"])
  if test x$have_SDLmixer != xyes; then
    AC_MSG_ERROR([*** Can't find the SDL_mixer library Try: http://www.libsdl.org/projects/SDL_mixer/])
    AC_SUBST(SDL_MIXER_LIBS)
  fi
fi

#--------------------------------------------------------------------
#       Emit output
#--------------------------------------------------------------------
AC_CONFIG_FILES([system.mk])
AC_OUTPUT
