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_INIT(ntserv/daemonII.c)
AC_CONFIG_HEADER(include/config.h)
AC_PREFIX_DEFAULT("/usr/local/games/netrek-server-vanilla")

#----------------------------------------------------
#	For which code are we checking? (server, cow)
#----------------------------------------------------

AC_MSG_CHECKING(for used sources)
if test -f "$srcdir/playerlist.c"; then
  code=cow
  AC_MSG_RESULT(COW)
  AC_DEFINE(COW)
elif test -f "$srcdir/ntserv/daemonII.c"; then
  code=server
  AC_MSG_RESULT(Vanilla SERVER)
  AC_DEFINE(SERVER)
else
  echo "	Unknown Sources"
  exit 1
fi
 
if test "$code" = server; then
  # default the prefix
  if test "x$prefix" = "xNONE"; then
    prefix=${ac_default_prefix}
  fi
  # expand the other directories
  if test "x${exec_prefix}" = 'xNONE'; then
   exec_prefix=${prefix}
  fi
  if test "x${bindir}" = 'x${exec_prefix}/bin'; then
    bindir=${exec_prefix}/bin
  fi
  if test "x${libdir}" = 'x${exec_prefix}/lib'; then
    libdir=${exec_prefix}/lib
  fi
  if test "x${sysconfdir}" = 'x${prefix}/etc'; then
    sysconfdir=${prefix}/etc
  fi
  if test "x${localstatedir}" = 'x${prefix}/var'; then
    localstatedir=${prefix}/var
  fi
  # place the important ones for use by the C code
  AC_DEFINE_UNQUOTED(BINDIR,"${bindir}")
  AC_DEFINE_UNQUOTED(LIBDIR,"${libdir}")
  AC_DEFINE_UNQUOTED(SYSCONFDIR,"${sysconfdir}")
  AC_DEFINE_UNQUOTED(LOCALSTATEDIR,"${localstatedir}")
  AC_SUBST(LOGNAME)

  AC_ARG_WITH(inl, [  --with-inl              INL Mode ], INL=1, INL=0)
  if test "x$INL" = x1 ; then
	AC_DEFINE(PKEY,129)
	AC_DEFINE(LTD_STATS)
	AC_DEFINE(LTD_PER_RACE)
	AC_SUBST(PKEY)
	AC_SUBST(INL)
  fi

  AC_ARG_WITH(gum, [  --with-gum              Compile and install gum ],
	GUM=do_gum)
  AC_SUBST(GUM)
fi

if test "$code" = cow; then
echo "Produce a stable release (y/n)?"
read stable

AC_SUBST(stable)
 if test "$stable" = "y"; then
  echo "Producing stable release"
  AC_DEFINE(STABLE)
 else
  stable=nope
 fi
fi

AC_PROG_INSTALL
AC_PROG_CC
AC_PROG_CPP
AC_PROG_LN_S
AC_PROG_MAKE_SET

AC_AIX

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_TRY_COMPILE( [#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_HEADER_STDC
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 sys/filio.h gdbm.h)
AC_CHECK_HEADERS(ncurses.h)

AC_CHECK_LIB(gdbm, gdbm_open, [LIBS="$LIBS -lgdbm"])
#AM_PATH_GLIB(1.2.10, [LIBS="$LIBS $GLIB_LIBS" CFLAGS="$CFLAGS $GLIB_CFLAGS"], [AC_MSG_ERROR(GLIB not in path)])

AC_FUNC_WAIT3
AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_SIZE_T
AC_FUNC_VFORK
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))



#--------------------------------------------------------------------
#	Check for gmp and mp
#--------------------------------------------------------------------

AC_ARG_WITH(gmp,[  --with-gmp              GMP Dir], MPINC="-I$withval/include"  MPLIB="-L$withval/lib -lgmp" search=false, MPINC=nope MPLIB=nope)
if test "$MPINC" = nope; then
    AC_MSG_CHECKING(for mp.h)
    AC_EGREP_CPP(GNU_MP,[ 
#include <mp.h> 
__GNU_MP__
],
        [gnu_mp_ok=yes],
        [gnu_mp_ok=no])
    if test "x$gnu_mp_ok" = xyes; then
        MPINC=nope
	search=true
	AC_MSG_RESULT(yes)
    else
        MPINC=""
	search=false
	AC_MSG_RESULT(no)
    fi
fi

AC_MSG_CHECKING(for gmp.h)
#   if test "$MPINC" = nope; then
#      AC_TRY_CPP([#include <gmp.h>], [MPINC="-I/usr/include" search=false], MPINC=nope)
#   fi

if test "$MPINC" = nope; then
   dirs="$srcdir/gmp-2.0.2 /usr/unsupported/include /usr/local/include \
      /usr/gnu/include /usr/gnu/include/mp /usr/local/gnu/include \
      /usr/local/gnu/include/mp /usr/include/gnu /usr/local/lib/gmp-2.0.2"
   for i in $dirs; do
      if test -r $i/gmp.h; then
	MPINC="-I$i"
      fi
   done
fi

if test "$MPINC" = nope; then

  if test "$code" = cow; then
# 	mp not supported with new mkkey.
   AC_TRY_CPP([#include <mp.h>], MPINC=nope , MPINC=nope)
  else
   AC_TRY_CPP([#include <mp.h>], MPINC="" , MPINC=nope)
  fi

   if test "$MPINC" = nope; then
      MPINC="# -I."
      NORSA="#"
      AC_MSG_RESULT(no)
   else
     AC_MSG_RESULT(yes)
   fi
else
   AC_MSG_RESULT(yes)
fi

AC_SUBST(MPINC)

# Checking for GMP 2
SAVEINCS="$INCS"
INCS="$INCS $XINCLUDES"
AC_EGREP_CPP("gmp=.*2", [
  #include <gmp.h> 
  gmp=__GNU_MP__
  ],AC_DEFINE(HAVE_GMP2_H))
INCS="$SAVEINCS"

if test "$MPLIB" = nope; then
  if test "$search" = true; then
    MPLIB=nope
  else
    AC_CHECK_LIB(mp, main, MPLIB="-lmp", MPLIB=" ")
    AC_CHECK_LIB(gmp, main, MPLIB="$MPLIB -lgmp", MPLIB=nope)
  fi
  if test "$MPLIB" = nope; then
    dirs="$srcdir/gmp-2.0.2 /usr/unsupported/lib /usr/local/lib /usr/gnu/lib \
      /usr/local/gnu/lib /usr/lib/gnu /usr/local/lib/gmp-2.0.2" 
    for i in $dirs; do
      if test -r $i/libmp.a; then
    	MPLIB="-L$i -lmp"
      fi
      if test -r $i/libgmp.a; then
	MPLIB="-L$i -lgmp"
	AC_MSG_RESULT(yes)
      fi
    done
  fi
fi
if test "$MPLIB" = nope; then
  if test "$code" = cow; then
   AC_CHECK_LIB(mp, main, MPLIB=bsdmp, MPLIB=nope)
  else
   AC_CHECK_LIB(mp, main, MPLIB="-lmp", MPLIB=nope)
  fi
   if test "$MPLIB" = bsdmp; then
      echo "Sorry COW requires gmp not mp for RSA."
      MPLIB="# -L. -lmp -lgmp"
      NORSA="#"
   fi
   if test "$MPLIB" = nope; then
      echo Warning mp or gmp library not found, turning RSA off.
      MPLIB="# -L. -lmp -lgmp"
      NORSA="#"
   fi
fi
AC_SUBST(MPLIB)

if test "$code" = cow; then
 if test -r "$srcdir/mkkey.c"; then
    echo "	RSA utilities for cow build found."
 else
    echo
    echo Warning RSA utilities for cow build not found, turning RSA off.
    NORSA="#"
    echo Check your favorite ftp site for a blessed client.
    echo In case you can not find one contact brmbugs.
    echo
 fi
fi
if test "$code" = server; then
 AC_MSG_CHECKING(res-rsa/configure)
 if test -r "$srcdir/res-rsa/configure"; then
    AC_CONFIG_SUBDIRS(res-rsa)
    AC_MSG_RESULT(RSA utilities found)
    RSAINC="-I${srcdir}/res-rsa"
    RSALIB="-L`pwd`/res-rsa -lrsa"
    NORSA=""
 else
    echo no
    AC_MSG_RESULT(Warning: RSA utilities for server build not found, turning RSA off)
    NORSA="#"
 fi
fi

AC_SUBST(RSAINC)
AC_SUBST(RSALIB)
AC_SUBST(NORSA)

#--------------------------------------------------------------------
#	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(V3, main, [LIBS="$LIBS -lV3"])
# AC _CHECK_LIB(PW, main, [LIBS="$LIBS -lPW"])
AC_CHECK_LIB(socket, main, [LIBS="$LIBS -lsocket"])
AC_CHECK_LIB(resolv, main, [LIBS="$LIBS -lresolv"])
AC_CHECK_LIB(inet, main, [LIBS="$LIBS -linet"])
AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"])
AC_CHECK_LIB(seq, main, [LIBS="$LIBS -lseq"])
AC_CHECK_LIB(sun, main, [LIBS="$LIBS -lsun"])

if test "$code" = server; then
AC_CHECK_LIB(ipc, main, [LIBS="$LIBS -lipc"])
AC_CHECK_LIB(shm, main, [LIBS="$LIBS -lshm"])
AC_CHECK_LIB(stuff, main, [LIBS="$LIBS -lstuff"])

AC_CHECK_LIB(crypt, crypt, [LIBCRYPT="-lcrypt"]) 
AC_SUBST(LIBCRYPT)

AC_CHECK_LIB(termcap, main, [LIBTERMCAP="-ltermcap"]) 
AC_SUBST(LIBTERMCAP)

AC_CHECK_LIB(ncurses, newwin, [LIBCURSES="-lncurses"], 
     AC_CHECK_LIB(curses, newwin, [LIBCURSES="-lcurses"])
) 
AC_SUBST(LIBCURSES)

fi

#--------------------------------------------------------------------
#    	Check for type of signals
#--------------------------------------------------------------------

AC_TYPE_SIGNAL
AC_SYS_RESTARTABLE_SYSCALLS
AC_CACHE_CHECK(for signals style,vanilla_cv_signals_type,
AC_TRY_RUN([
#include <signal.h>
#include <sys/time.h>
test(int x){ }
int main(){
int c;
static struct itimerval udt;
signal(SIGALRM,test);
udt.it_interval.tv_sec = 1;
udt.it_interval.tv_usec = 10;
udt.it_value.tv_sec = 1;
udt.it_value.tv_usec = 10;
(void) setitimer(ITIMER_REAL, &udt, (struct itimerval *) 0);
for (c=0; c<5; c++)
pause();
exit(0);}
], vanilla_cv_signals_type=BSD,
   vanilla_cv_signals_type=SYSV_or_POSIX,
   vanilla_cv_signals_type=POSIX
))

if test "x$vanilla_cv_signals_type" = xBSD; then
	AC_DEFINE(BSD_SIGNALS)
else
	if test "x$vanilla_cv_signals_type" = xSYSV_or_POSIX; then
		AC_CHECK_FUNC(sigset, AC_DEFINE(SYSV_SIGNALS),
				      AC_DEFINE(POSIX_SIGNALS))
	else
		AC_DEFINE(POSIX_SIGNALS)
	fi
fi


# AC_CHECK_LIB(BSD, main, [LIBS="$LIBS -lBSD"])
# AC_CHECK_LIB(bsd, main, [LIBS="$LIBS -lbsd"])

AC_CHECK_FUNCS(usleep random setstate strftime ftime strcmpi strncmpi)

AC_CHECK_LIB(m, main, [LIBS="$LIBS -lm"])
AC_CHECK_FUNCS(nint,HAVE_NINT="yes",
		AC_CHECK_LIB(sunmath,nint,HAVE_NINT="-lsunmath",
		HAVE_NINT="no")
	      )

NINT_LIBS="";
if test "x$HAVE_NINT" = "xno"; then
	HAVE_NINT="0"
	AC_SUBST(HAVE_NINT)
else
	if test "x$HAVE_NINT" = "xyes"; then
		NINT_LIBS=${HAVE_NINT}
	fi
	HAVE_NINT="1"
	AC_SUBST(HAVE_NINT)
fi

AC_SUBST(NINT_LIBS)

AC_EGREP_HEADER(rint, math.h, , AC_DEFINE(NEED_RINT_DEC))

if test "$code" = cow; then
	AC_REPLACE_FUNCS(setstate strdup rint)
fi
if test "$code" = server; then
	AC_REPLACE_FUNCS(random strdup rint)
fi

#--------------------------------------------------------------------
#       Check for system dependend programs
#--------------------------------------------------------------------

AC_PROG_LIBTOOL

AC_MSG_CHECKING(for Mac OS X/Darwin ranlib)
if test "$OSTYPE" != "darwin"; then
    RANLIB_FLAGS=
    AC_MSG_RESULT(no)
else
    RANLIB_FLAGS='-c'
    AC_MSG_RESULT(yes)
fi

AC_SUBST(RANLIB_FLAGS)

if test "$code" = server; then

AC_MSG_CHECKING(for netstat)
dirs="/bsd /usr/bsd /etc /bin /usr/bin /usr/etc"
NETSTAT="nice -20 /usr/ucb/netstat -f inet"
for i in $dirs; do
  if test -x $i/netstat; then
     NETSTAT="nice -20 ${i}/netstat -a"
     AC_DEFINE_UNQUOTED(NETSTAT,"$NETSTAT")
     AC_MSG_RESULT(yes)
     break
  fi
done

AC_MSG_CHECKING(for uptime)
dirs="/bsd /usr/bsd /etc /bin /usr/bin /usr/etc"
for i in $dirs; do
  if test -x $i/uptime; then
     AC_DEFINE_UNQUOTED(UPTIME,"${i}/uptime")
     AC_MSG_RESULT(yes)
     break
  fi
done

fi

AC_OUTPUT(system.mk Makefile ntserv/Makefile tools/Makefile
	sequencer/Makefile newstartd/Makefile robots/Makefile
	keycomp/Makefile robotd/Makefile
	docs/Makefile tools/no_geno_timer tools/geno_timer
	docs/sample_geno_timer_crontab docs/sample_sysdef
	tools/setpath tools/setpath.csh tools/admin/Makefile)

if test "x$GUM" = xdo_gum; then
ifdef([AC_OUTPUT_SUBDIRS],[], [AC_DEFUN([AC_OUTPUT_SUBDIRS],[subdirs=$1; _AC_OUTPUT_SUBDIRS])])
	AC_OUTPUT_SUBDIRS(gum)
fi

echo ""
echo "Directories"
echo "    Binaries (external use) : $bindir"
echo "    Binaries (internal use) : $libdir"
echo "    Configuration files     : $sysconfdir"
echo "    Local state files       : $localstatedir"
echo ""