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

AC_REVISION($Id: configure.in,v 1.20 2002/02/15 00:56:27 tanner Exp $)

AC_INIT(CREDITS)

AC_PREFIX_DEFAULT(/usr/games/crossfire)


dnl we want a config.h file instead of -D options.
AC_CONFIG_HEADER(include/autoconf.h)
AC_CONFIG_AUX_DIR(utils/)

dnl Checks for programs.
dnl AC_PROG_YACC
AC_CANONICAL_SYSTEM
AC_PROG_CC

case "$target" in
  alpha-dec-osf*)
    # If we are not using gcc, we want the ansi version of cc.
    if test -z "$GCC"; then
#      CFLAGS="$CFLAGS -std1 -warnprotos" # Not yet ready for warnprotos...
      CFLAGS="$CFLAGS -std1"
    fi
    ;;
  *)
    ;;
esac


AC_PROG_CPP
AC_PROG_LEX
AC_PROG_AWK
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_ARG_ENABLE(old-layout, [ --enable-old-layout    Use the old (0.95.3 and earlier) install layout],
	old_layout=yes, old_layout=no)



dnl check for some other programs
AC_PATH_PROG(MKDIR, mkdir)
AC_PATH_PROG(TAR, tar)
AC_PATH_PROG(DEPEND, makedepend)
AC_PATH_PROG(CP, cp)
AC_PATH_PROG(BASENAME, basename)
AC_PATH_PROG(SED, sed)
AC_PATH_PROG(RM, rm)
AC_PATH_PROG(AR, ar)
AC_PATH_PROG(LATEX, latex)
AC_PATH_PROG(GZIP, gzip)
AC_PATH_PROG(GUNZIP, gunzip)
AC_PATH_PROG(PERL, perl)

if test -n "$GZIP" -a -z "$GUNZIP" ; then
    echo "Found gzip, but not gunzip - setting GUNZIP to gzip -c";
    GUNZIP="$GZIP -c"
fi;

if test -z "$COMPRESS" ; then
	AC_PATH_PROG(COMPRESS, compress)
	AC_PATH_PROG(UNCOMPRESS, uncompress)
fi

AC_PATH_PROG(BZIP, bzip2)
AC_PATH_PROG(BUNZIP, bunzip2)

if test -n "$BZIP" -a -z "$BUNZIP" ; then
    echo "Found bzip2, but not bunzip2 - setting BUNZIP to bzip2 -c";
    BUNZIP="$BZIP -c"
fi;

if test -z "$COMPRESS" -a -z "$GZIP" -a -z "$BZIP" ; then
	echo "Unable to find either compress, bzip2,  or gzip - hope you don't plan on compressing";
	echo "any files.";
fi;

AC_PATH_PROGS(BOURNE_SHELL, sh ksh bash)

dnl The follow two are really just a way to specify where the xpm/gtk information
dnl resides if not with the normal X11 stuff (ie, in /opt/X11 and not /usr
dnl /usr/X11

AC_ARG_WITH(ldflags, [  --with-ldflags=dir      provide addition linker directives to find libraries ],
		EXTRA_LIB="$withval")

AC_ARG_WITH(includes, [  --with-includes=dir     provide different compiler options to find headers with ],
		EXTRA_INC="$withval")

dnl nsl, socket may be needed for the X-windowing system, so check
dnl for them before before checking for X.

AC_CHECK_LIB(nsl, main)
AC_CHECK_LIB(socket, main)

dnl Checks for libraries.
dnl Start of X11 libraries
AC_PATH_XTRA

CPPFLAGS="$CPPFLAGS $EXTRA_INC"
LDFLAGS="$LDFLAGS $EXTRA_LIB"

dnl Some of these checks are probably excessive.  Unfortunately, on
dnl some systems, to link in Xaw, Xt you need SM, which needs ICE.
dnl the order of the checks below is important
OLD_LD_FLAGS="$LDFLAGS"
LDFLAGS="$X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS"

AC_CHECK_LIB(X11, main, AC_DEFINE(HAVE_LIBX11) X11LIBS="$X11LIBS -lX11")

AC_CHECK_LIB(ICE, main, AC_DEFINE(HAVE_LIBICE) X11LIBS="$X11LIBS -lICE")

AC_CHECK_LIB(SM, main, AC_DEFINE(HAVE_LIBSM) X11LIBS="$X11LIBS -lSM", , $X11LIBS)

AC_CHECK_LIB(Xext, main,  AC_DEFINE(HAVE_LIBXEXT) X11LIBS="$X11LIBS -lXext", , -lX11)

AC_CHECK_LIB(Xt, main,  AC_DEFINE(HAVE_LIBXT) X11LIBS="$X11LIBS -lXt", , $X11LIBS)

AC_CHECK_LIB(Xmu, main,  AC_DEFINE(HAVE_LIBXMU) X11LIBS="$X11LIBS -lXmu", ,$X11LIBS)

AC_CHECK_LIB(Xaw, main, AC_DEFINE(HAVE_LIBXAW) X11LIBS="-lXaw $X11LIBS", , $X11LIBS)

LDFLAGS="$OLD_LD_FLAGS"
X11LIBS="$X_LIBS $X11LIBS"

AC_CHECK_LIB(Xpm, main,  AC_DEFINE(HAVE_LIBXPM) X11LIBS="$X11LIBS -lXpm", , $X11LIBS )

AC_CHECK_LIB(m, main)

AC_CHECK_LIB(png, main,  AC_DEFINE(HAVE_LIBPNG) X11LIBS="$X11LIBS -lpng", , $X11LIBS )

dnl Misc libraries.
AC_CHECK_LIB(crypt, main)
AC_CHECK_LIB(des, des_crypt)

dnl Gros - Changed this to support some external programs that do not like dmalloc (like Python).
dnl If you really need to make some memory debugging, uncomment the dmalloc line and comment out the dmalloclp one.

AC_CHECK_LIB(dmalloclp, main)
dnl AC_CHECK_LIB(dmalloc, main)

dnl Gros - Support for the dynamic loader library
AC_CHECK_LIB(dl, main)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h strings.h sys/file.h sys/ioctl.h sys/time.h time.h unistd.h stddef.h stdlib.h sys/ttycom.h sys/termios.h crypt.h arpa/inet.h des.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UID_T

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_FUNC_SETPGRP
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(gettimeofday mkdir mktime rmdir select socket strcspn strerror strspn strstr strtol strcasecmp strncasecmp snprintf setsid srandom getdtablesize srand48 srand sysconf scandir)

AC_MSG_CHECKING(how many args gettimeofday uses)
AC_CACHE_VAL(ac_cv_gettimeofday_args,
 [AC_TRY_COMPILE([#include <sys/time.h>
	#include <unistd.h>],
	[struct timeval tv; struct timezone tzp;
	gettimeofday(&tv, &tzp);],
	[ac_gettimeofday_args=2],
	[AC_TRY_COMPILE([#include <sys/time.h>
		#include <unistd.h>],
		[struct timeval tv; gettimeofday(&tv);],
		[ac_gettimeofday_args=1],
		[ac_gettimeofday_args=0])])
  ac_cv_gettimeofday_args=$ac_gettimeofday_args])

ac_gettimeofday_args=$ac_cv_gettimeofday_args
if test $ac_gettimeofday_args = 1 ; then
  AC_DEFINE(HAVE_GETTIMEOFDAY)
  AC_MSG_RESULT(one argument)
elif test $ac_gettimeofday_args = 2 ; then
  AC_DEFINE(HAVE_GETTIMEOFDAY)
  AC_DEFINE(GETTIMEOFDAY_TWO_ARGS)
  AC_MSG_RESULT(two arguments)
else
  AC_MSG_RESULT(unknown)
fi


AC_SUBST(x_includes)
AC_SUBST(x_libraries)
AC_SUBST(no_x)
AC_SUBST(X11LIBS)
AC_SUBST(PERL)

dnl Do the evals because most of these are made up of sub variables and
dnl we don't want the include to have something like
dnl #define DATADIR "${prefix}/somedir"

dnl hack to set prefix - the AC_OUTPUT does this, but that is too late
dnl for our purposes here.
test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'

nprefix=`eval echo ${prefix}`

dnl I am not sure why the eval screws up the localstatdir, but see my posting
dnl on the mailing list for more info.

if eval "test x$old_layout = xyes"; then
	ndatadir=`eval echo ${datadir}`
	nlocaldir=`echo ${localstatedir}`
else
	echo ""
	echo "If you are upgrading an 0.95.3 or earlier release, you may want to use"
	echo "--enable-old-layout to use the same install directories"
	echo ""
	ndatadir=`eval echo ${datadir}`/crossfire
	nlocaldir=`eval echo ${localstatedir}`/crossfire
fi


dnl Have to do this one twice, since it uses two levels of indirection 

nlibdir=`eval echo ${libdir}`
nlibdir=`eval echo ${nlibdir}`

AC_DEFINE_UNQUOTED(LIBDIR, "${nlibdir}")
AC_DEFINE_UNQUOTED(PREFIXDIR, "${nprefix}")
AC_DEFINE_UNQUOTED(DATADIR, "${ndatadir}")
AC_DEFINE_UNQUOTED(LOCALDIR, "${nlocaldir}")
AC_DEFINE_UNQUOTED(COMPRESS,"${COMPRESS}")
AC_DEFINE_UNQUOTED(UNCOMPRESS,"${UNCOMPRESS}")
AC_DEFINE_UNQUOTED(GZIP,"${GZIP}")
AC_DEFINE_UNQUOTED(GUNZIP,"${GUNZIP}")
AC_DEFINE_UNQUOTED(BZIP,"${BZIP}")
AC_DEFINE_UNQUOTED(BUNZIP,"${BUNZIP}")

datadir=${ndatadir}
localstatedir=${nlocaldir}

AC_OUTPUT(Makefile
	crossedit/Makefile crossedit/doc/Makefile crossedit/include/Makefile
	crossedit/Cnv/Makefile crossedit/bitmaps/Makefile
	doc/Makefile doc/Developers/Makefile doc/spell-docs/Makefile
	doc/spoiler/Makefile doc/spoiler-html/Makefile
	doc/playbook/Makefile doc/playbook-html/Makefile
	lib/Makefile random_maps/Makefile socket/Makefile server/Makefile 
	include/Makefile utils/Makefile lib/checkarch.pl
	lib/collect.pl utils/add_throw.perl utils/crossloop utils/crossloop.pl
	utils/metaserver.pl 
	common/Makefile
	 )
