dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/mbot.cpp)
AM_CONFIG_HEADER(inc/config.h)

AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED(HOST, "$host")

VERSION=`cat VERSION`
VERSION_STRING="mirage irc bot $VERSION"
AC_MSG_RESULT([configuring for $VERSION_STRING])

AM_INIT_AUTOMAKE(mbot, $VERSION, non_empty)

AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_DEFINE_UNQUOTED(VERSION_STRING, "$VERSION_STRING")

dnl
dnl Checks for programs.
dnl

AC_PROG_CC([-Wall -O2])
AC_PROG_CPP
AC_PROG_CXX([-Wall])
AC_PROG_CXXCPP
AC_PROG_MAKE_SET

AC_AIX
AC_MINIX

AC_PATH_PROG(LD_PATH, ld)

AC_PATH_PROG(PING_PATH, ping)
if test "$PING_PATH" != ""; then
  AC_DEFINE_UNQUOTED(PING_PATH, "$PING_PATH")
else
  AC_MSG_WARN([ping usage in module 'extra' is disabled])
fi

AC_PATH_PROG(SENDMAIL_PATH, sendmail, "", $PATH:/usr/sbin)
if test "$SENDMAIL_PATH" != ""; then
  AC_DEFINE_UNQUOTED(SENDMAIL_PATH, "$SENDMAIL_PATH")
fi
AC_PATH_PROG(ECHO_PATH, echo)
if test "$ECHO_PATH" != ""; then
  AC_DEFINE_UNQUOTED(ECHO_PATH, "$ECHO_PATH")
fi
AC_PATH_PROG(CAT_PATH, cat)
if test "$CAT_PATH" != ""; then
  AC_DEFINE_UNQUOTED(CAT_PATH, "$CAT_PATH")
fi
AC_PATH_PROG(WHOIS_PATH, whois)
if test "$WHOIS_PATH" != ""; then
  AC_DEFINE_UNQUOTED(WHOIS_PATH, "$WHOIS_PATH")
else
  AC_MSG_WARN([whois command in module 'whois' is disabled])
fi

if test "$SENDMAIL_PATH" = "" \
        || test "$ECHO_PATH" = "" \
        || test "$CAT_PATH" = ""; then
  AC_MSG_WARN([module 'log' will not send mails])
fi

dnl
dnl Checks for libraries.
dnl

AC_CHECK_LIB(socket, setsockopt)
AC_CHECK_LIB(nsl, gethostbyname)

dnl check this only because tcl sometimes requires it
AC_CHECK_LIB(m, cos)

AC_CHECK_LIB(dl, dlopen)
if test $ac_cv_lib_dl_dlopen = "no"; then
  AC_CHECK_FUNCS(dlopen)
  if test $ac_cv_func_dlopen = "no"; then
    AC_MSG_ERROR([dynamic libraries not supported])
  fi
fi

AC_CHECK_LIB(crypt, crypt)
if test $ac_cv_lib_crypt_crypt = "no"; then
  AC_CHECK_FUNCS(crypt)
fi
if test $ac_cv_lib_crypt_crypt = "yes" || test $ac_cv_func_crypt = "yes"; then
  AC_DEFINE(MBOT_CRYPT)
else
  AC_MSG_WARN([!pass command in module 'main_user' won't work])
fi

AC_CHECK_LIB(tcl, Tcl_Init)
if test $ac_cv_lib_tcl_Tcl_Init = "no"; then
  AC_CHECK_LIB(tcl8.0, Tcl_Init)
fi

dnl
dnl Checks for header files.
dnl

AC_HEADER_TIME
AC_CHECK_HEADERS(fcntl.h \
                 sys/time.h \
                 unistd.h \
                 netinet/in_systm.h \
                 netinet/tcp.h \
                 string.h \
                 strings.h \
                 crypt.h \
                 tcl.h)

if (test $ac_cv_lib_tcl_Tcl_Init = "no" && test $ac_cv_lib_tcl8_0_Tcl_Init = "no") || test $ac_cv_header_tcl_h = "no"; then
  AC_MSG_WARN(tcl module won't be built!)
  TCL=""
else
  TCL="tcl.so"
fi
AC_SUBST(TCL)

dnl
dnl Checks for typedefs, structures, and compiler characteristics.
dnl

AC_C_CONST
AC_TYPE_SIGNAL
AC_CHECK_TYPE(size_t, unsigned)
AC_CHECK_TYPE(u_long, unsigned long)
AC_CHECK_TYPE(u_int, unsigned int)
AC_CHECK_TYPE(u_short, unsigned short)
AC_CHECK_TYPE(u_char, unsigned char)

AC_MSG_CHECKING([for socklen_t in sys/socket.h])
AC_EGREP_HEADER(socklen_t, sys/socket.h,
has_socklen_t=yes, has_socklen_t=no)
AC_MSG_RESULT($has_socklen_t)
if test "$has_socklen_t" = "no"; then
  AC_DEFINE(socklen_t, int)
fi

AC_MSG_CHECKING([for struct ip in netinet/ip.h])
AC_EGREP_CPP(struct ip, [#include <netinet/ip.h>],
has_struct_ip=yes, has_struct_ip=no)
AC_MSG_RESULT($has_struct_ip)
if test "$has_struct_ip" = "yes"; then
  AC_DEFINE(HAVE_STRUCT_IP)
else
  AC_MSG_WARN([!dccchat command in module 'extra' is disabled])
fi

dnl
dnl Checks for library functions.
dnl

AC_CHECK_FUNCS(snprintf \
               memcpy)

dnl
dnl libtool's job :>
dnl

SRC_CXXFLAGS="$CXXFLAGS -Wall"

if test "$host_vendor" = "dec"; then
  MOD_CXXFLAGS="$CXXFLAGS -Wall"
else
  MOD_CXXFLAGS="$CXXFLAGS -Wall -fPIC"
fi

if test "$host_os" = "cygwin32" || test "$host_vendor" = "sun"; then
  SRC_LDFLAGS="-Wrdynamic"
elif test "$host_vendor" = "dec"; then
  SRC_LDFLAGS="-call_shared"
else
  SRC_LDFLAGS="-rdynamic"
fi

if test "$host_vendor" = "sun"; then
  MOD_LDFLAGS="-G"
elif test "$host_vendor" = "dec"; then
  MOD_LDFLAGS="-shared -expect_unresolved \*"
else
  # -Bstatic forces linking with .a libraries, openbsd apparently needs this
  MOD_LDFLAGS="-shared -Bstatic"
fi

AC_SUBST(SRC_CXXFLAGS)
AC_SUBST(MOD_CXXFLAGS)
AC_SUBST(SRC_LDFLAGS)
AC_SUBST(MOD_LDFLAGS)

if test "$LD_PATH" != ""; then
  MOD_MAKE="\$(CXX) \$(CXXFLAGS) -I\$(INCLUDE) \$< -c -o \$<.o && \$(LD) \$(LDFLAGS) $<.o -o \$@ && rm -f $<.o"
else
  MOD_MAKE="\$(CXX) \$(CXXFLAGS) -I\$(INCLUDE) \$< $(LIBS) \$(LDFLAGS) -o \$@"
fi
AC_SUBST(MOD_MAKE)

dnl The end

AC_OUTPUT(src/Makefile mod/Makefile Makefile)

