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

AC_PREREQ(2.13)
AC_INIT(INSTALL)
AM_INIT_AUTOMAKE(gale,0.99egg)
AM_CONFIG_HEADER(include/gale/config.h:config.h.in)
AC_CANONICAL_HOST
AM_PROG_LIBTOOL
AM_MAINTAINER_MODE

AC_ARG_ENABLE(adns,
[  --enable-adns           use asynchronous DNS resolver],
[case "${enableval}" in
  yes) use_adns=true ;;
  no)  use_adns=false ;;
  *) AC_MSG_ERROR(bad value ${enableval} for --enable-adns) ;;
esac],[use_adns=true])
if $use_adns ; then
  AC_DEFINE(HAVE_ADNS)
fi

AC_ARG_WITH(socks,
[  --with-socks            use SOCKS firewall proxy (requires libsocks)],
[case "${withval}" in
  yes) use_socks=true ;;
  no)  use_socks=false ;;
  *) AC_MSG_ERROR(bad value ${withval} for --with-socks) ;;
esac],[use_socks=false])

dnl Configure subpackages
AC_CONFIG_SUBDIRS(liboop)

no_wacky_libs=""
GALE_LIBS="\${top_builddir}/libgale/libgale.la"

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_YACC
PROG_LDCONFIG=:

dnl Add prefix to LDFLAGS and INCLUDES to help find previously installed foo.
test NONE != "$exec_prefix" && LDFLAGS="$LDFLAGS -L${exec_prefix}/lib"
if test NONE != "$prefix" ; then
  INCLUDES="$INCLUDES -I${prefix}/include"
  test NONE = "$exec_prefix" && LDFLAGS="$LDFLAGS -L${prefix}/lib"
fi

dnl Add special GCC warnings, but don't enable them yet.
warning_flags="$CFLAGS"
error_flags="$CFLAGS"
if test yes = "$GCC" ; then
  warning_flags="-Wall -Wno-comment $CFLAGS"
  error_flags="-Werror $warning_flags"
fi

dnl System type checks.
case "$host" in
  *-linux-*)
    AC_PATH_PROG(PROG_LDCONFIG, ldconfig, :, $PATH:/usr/sbin:/sbin)
    ;;
  *-sgi-irix6*)
    if test -n "$LPATH" ; then
      LDFLAGS="-Wl,-rpath,$LPATH $LDFLAGS"
    fi
    no_wacky_libs=yes
    AC_DEFINE(OS_IRIX)
    ;;
  *-hp-hpux*)
    AC_DEFINE(OS_HPUX)
    ;;
  *-*-solaris2*)
    AC_DEFINE(OS_SOLARIS)
    ;;
  *-*-*bsd*)
    AC_DEFINE(OS_BSD)
    ;;
esac

dnl Checks for libraries.
if test -z "$no_wacky_libs" ; then
  AC_CHECK_LIB(nsl,main)
  AC_CHECK_LIB(socket,socket)
fi

termcap_lib=""
AC_CHECK_LIB(termcap,tgetent,[termcap_lib="-ltermcap"],[
  AC_CHECK_LIB(ncurses,tgetent)])
AC_CHECK_LIB(resolv,res_query)

AC_CHECK_LIB(crypto,EVP_OpenInit,[GALE_LIBS="$GALE_LIBS -lcrypto"],[
  AC_MSG_ERROR(cannot find OpenSSL crypto library)
])

dnl Make sure 'iconv' supports multibyte UTF-8 conversion.
libs_without_iconv="$LIBS"
AC_CHECK_LIB(iconv,iconv)
AC_MSG_CHECKING([for working iconv])
save_flags="$CFLAGS"
CFLAGS="$error_flags $save_flags -DICONV_CONST="
AC_TRY_RUN([#include "${srcdir}/iconvtest.c"],[
  AC_MSG_RESULT([yes, no const])
  AC_DEFINE(HAVE_ICONV)
  AC_DEFINE(ICONV_CONST,[])
],[
  CFLAGS="$warning_flags $save_flags -DICONV_CONST=const"
  AC_TRY_RUN([#include "${srcdir}/iconvtest.c"],[
    AC_MSG_RESULT([yes, const])
    AC_DEFINE(HAVE_ICONV)
    AC_DEFINE(ICONV_CONST,const)
  ],[
    AC_MSG_RESULT(no)
    LIBS="$libs_without_iconv"
  ],
  [AC_MSG_RESULT([unknown, assuming no])])
],[AC_MSG_RESULT([unknown, assuming no])])
CFLAGS="$save_flags"

AC_CHECK_LIB(dl,dlopen,[
  DL_LIBS="-ldl"
  AC_DEFINE(HAVE_LIBDL)])
AC_CHECK_LIB(readline,readline,[
  GALE_LIBS="$GALE_LIBS -lreadline"
  AC_DEFINE(HAVE_LIBREADLINE)],[],$termcap_lib)

if test x$use_socks = xtrue ; then
AC_CHECK_LIB(socks,SOCKSinit,[
  GALE_LIBS="$GALE_LIBS -lsocks"
  AC_DEFINE(HAVE_SOCKS) 
],[
  AC_MSG_ERROR(cannot find SOCKS library)
])
fi

AC_CHECK_LIB(gc,GC_malloc,[GALE_LIBS="$GALE_LIBS -lgc"],[
  AC_MSG_ERROR(cannot find Boehm GC library, see INSTALL directions)
])

AC_MSG_CHECKING([for local copy of liboop])
if test -f "${srcdir}/liboop/oop.h"; then
  AC_MSG_RESULT(yes)
  INCLUDES="-I\${top_srcdir}/liboop $INCLUDES"
  build_liboop=true
  if $use_adns; then
    AC_CHECK_LIB(adns,adns_init,[:],[
      AC_MSG_ERROR(cannot find adns library, try configure --without-adns)
    ])
    GALE_LIBS="$GALE_LIBS \${top_builddir}/liboop/liboop-adns.la"
  fi
  GALE_LIBS="$GALE_LIBS \${top_builddir}/liboop/liboop.la"
else
  AC_MSG_RESULT(no)
  AC_CHECK_LIB(oop,oop_sys_new,[:],[
    AC_MSG_ERROR(cannot find liboop library, see INSTALL directions)
  ])
  if $use_adns; then
    AC_CHECK_LIB(oop-adns,oop_adns_new,[:],[
      AC_MSG_ERROR(cannot find liboop-adns library, was liboop built for adns?)
    ],-loop)
    AC_CHECK_LIB(adns,adns_init,[:],[
      AC_MSG_ERROR(cannot find adns library, try configure --without-adns)
    ])
    GALE_LIBS="$GALE_LIBS -loop-adns -loop -ladns"
  else
    GALE_LIBS="$GALE_LIBS -loop"
  fi
fi

GALE_LIBS="$GALE_LIBS $termcap_lib"

dnl Checks for header files.
AC_CHECK_HEADERS(sys/bitypes.h sys/select.h curses.h term.h dlfcn.h readline/readline.h getopt.h)
AC_CHECK_HEADERS(openssl/evp.h,,[AC_MSG_ERROR(cannot find OpenSSL headers)])

test -d /usr/include/gc && CPPFLAGS="$CPPFLAGS -I/usr/include/gc"
AC_CHECK_HEADERS(gc_backptr.h)
AC_CHECK_HEADERS(gc.h,,[
  AC_MSG_ERROR(cannot find Boehm GC headers, see INSTALL directions)
])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(long)

dnl Checks for library functions.

dnl Output.

CFLAGS_NOWARN="$CFLAGS"
CFLAGS="$warning_flags"
INCLUDES="-I\${top_srcdir}/include -I\${top_builddir}/include $INCLUDES"
INCLUDES="-DGALE_SYS_DIR=\\\"${sysconfdir}/gale\\\" $INCLUDES"

AC_SUBST(CFLAGS_NOWARN)
AC_SUBST(INCLUDES)
AC_SUBST(ZEPHYR_LIBS)
AC_SUBST(DL_LIBS)
AC_SUBST(GALE_LIBS)
AC_SUBST(PROG_LDCONFIG)

AM_CONDITIONAL(BUILD_LIBOOP,test x$build_liboop = xtrue)

AC_OUTPUT(
	gale-config
	gale-install
	Makefile 
	libgale/Makefile 
	gsub/Makefile
	gsend/Makefile
	kutils/Makefile
	server/Makefile
	gdomain/Makefile
	include/Makefile 
	include/gale/Makefile
)
