dnl Process this file with autoconf to produce a configure script.
AC_INIT(eggdrop.conf.dist)
AC_CONFIG_HEADER(config.h)

echo ""
echo "This is eggdrop's GNU configure script."
echo "It's going to run a bunch of strange tests to hopefully make"
echo "your compile work without much twiddling."
echo ""

AC_PROG_CC

dnl crazy machines
AC_AIX
AC_ISC_POSIX
AC_MINIX
dnl no cross-compiling!

if test ! x${cross_compiling} = xno; then
  echo ""
  echo "This system does not appear to have a working C compiler."
  echo "A working C compiler is required to compile eggdrop."
  echo ""
  exit
fi

dnl Checks for programs.
AC_PROG_MAKE_SET
AC_CHECK_PROG(STRIP,strip,strip)
AC_CHECK_PROG(UNAME,uname,uname)

dnl 'nawk' is NOT compatable! dammit!
dnl no longer use awk for anything
dnl AC_CHECK_PROGS(AWK,awk gawk mawk)

AC_PROG_RANLIB

dnl stop putting -g in there!  @#$!#$%!
dnl also fix strength-reduce problem in gcc
CFLAGS="-O"
if test "x$CC" = "xgcc"; then
  CFLAGS="-O2 -fno-strength-reduce"
fi

dnl test the os and set the module linking settings
if test "x$STRIP" = "x"; then
  STRIP="touch"
fi
IRIX=no
NT=no
EGGEXEC=eggdrop
LINUX=no
DEFAULT_MAKE=eggdrop
BEL_MOD_CC="${CC}"
BEL_MOD_LD="${CC}"
BEL_MOD_STRIP="${STRIP}"
SHLIB_CC="${CC}"
SHLIB_LD="${CC}"
SHLIB_STRIP="${STRIP}"
AC_MSG_CHECKING(your OS)
system=`$UNAME`
DLOPEN_1=1
case $system in
  Linux)
    CFLAGS="${CFLAGS} -Wall"
    AC_MSG_RESULT(Linux! It's what all good llama's use)
    SHLIB_LD="${CC} -shared -nostartfiles"
    BEL_MOD_LD="${CC}"
    AC_DEFINE(MODULES_OK)
    LINUX=yes
    DLOPEN_1=
    ;;
  BSD/OS)
    if test x`uname -r | cut -d . -f 1` = "x3"; then
      AC_MSG_RESULT(BSD/OS 3! ok I spose)
      BEL_MOD_CC="shlicc"
      BEL_MOD_LD="shlicc"
      BEL_MOD_STRIP="${STRIP} -d"
      SHLIB_LD="shlicc -r"
      SHLIB_STRIP="touch"
      AC_DEFINE(MODULES_OK)
    else
      AC_MSG_RESULT(BSD/OS 2! statically linked modules are the only choice)
      DEFAULT_MAKE=static
    fi
    AC_CHECK_PROG(GMAKE,gmake,gmake)
    if test "x$GMAKE" = "x"; then
      echo ""
      echo "Yoicks! you have BSD/OS & you dont have 'gmake', you may"
      echo "not be able to compile the bot successfully :("
      echo ""
    fi
    ;;
  *BSD)
    AC_MSG_RESULT(FreeBSD/NetBSD - choose your poison)
    SHLIB_CC="$CC -fpic"
    SHLIB_LD="ld -Bshareable -x"
    AC_DEFINE(MODULES_OK)
    DLOPEN_1=
    ;;
  SunOS)
    if test x`uname -r | cut -d . -f 1` = "x5"; then
      AC_MSG_RESULT(Solaris!)
      SHLIB_LD="/usr/ccs/bin/ld -G -z text"
    else
      AC_MSG_RESULT(SunOS 4 :/)
      SHLIB_LD="ld"
      SHLIB_STRIP="touch"
    fi
    AC_DEFINE(MODULES_OK)
    SHLIB_CC="${CC} -fPIC"
    BEL_MOD_CC="${CC} -fPIC"
    ;;
  OSF1)
    AC_MSG_RESULT(OSF...)
    case `uname -r | cut -d . -f 1` in
      V*)
        AC_MSG_RESULT(Digital OSF)
	SHLIB_LD="ld -shared -expect_unresolved '*'"
	SHLIB_STRIP="touch"
        AC_DEFINE(MODULES_OK)
        ;;
      1.0|1.1|1.2)
        AC_MSG_RESULT(pre 1.3)
	SHLIB_LD='ld -R -export $@:'
	AC_DEFINE(OSF1_HACKS)
        AC_DEFINE(MODULES_OK)
        ;;
      1.*)
        AC_MSG_RESULT(1.3+)
        SHLIB_CFL="$CC -fpic"
	SHLIB_LD='ld -shared'
	AC_DEFINE(OSF1_HACKS)
        AC_DEFINE(MODULES_OK)
        ;;
      *)
        AC_MSG_RESULT(Some other weird OSF! No modules for you..)
	DEFAULT_MAKE=static
	;;
    esac
    AC_DEFINE(STOP_UAC)
    ;;
  HP-UX)
    AC_MSG_RESULT(HP-UX, just shoot yourself now)
    AC_CHECK_LIB(dld,shl_load)
    AC_DEFINE(MODULES_OK)
    AC_DEFINE(HPUX_HACKS)
    ;;
  IRIX)
    AC_MSG_RESULT(you are cursed with IRIX)
    IRIX=yes
    STRIP=
    DEFAULT_MAKE=static
    ;;
  *)
    if test -r /mach; then
      AC_MSG_RESULT(NeXT...We are borg, you will be assimilated)
      AC_MSG_RESULT(break out the static modules, it's all you'll ever get :P)
      AC_MSG_RESULT(Hiya DK :P)
      AC_DEFINE(BORGCUBES)
    else
      AC_MSG_RESULT(Something unknown!!)
      AC_MSG_RESULT(If you get dynamic modules to work, be sure to let the devel team know HOW :)
    fi
    DEFAULT_MAKE=static
    ;;
esac

dnl Check for Sun libraries.
if test ${IRIX} = yes; then
  echo Skipping library tests because they CONFUSE Irix.
else
  AC_CHECK_LIB(socket,socket)
  AC_CHECK_LIB(nsl,connect)
  AC_CHECK_LIB(dns,gethostbyname)
  if test x`${UNAME}` = "xSunOS"; then
    AC_MSG_CHECKING(for SunOS 4)
    if test x`uname -r | cut -d . -f 1` = "x5"; then
      AC_MSG_RESULT(Solaris -- "*bollocks*" zarni )
    else
      AC_MSG_RESULT(SunOS -- sigh)
      dnl for suns without yp or something like that:
      AC_CHECK_LIB(dl,main)
    fi
  fi
fi

AC_C_BIGENDIAN
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(int)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(sys/time.h sys/select.h sys/rusage.h unistd.h dlfcn.h)

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

dnl Checks for library functions.
AC_CHECK_FUNCS(rename getrusage getdtablesize srandom random sigaction)
AC_CHECK_FUNCS(sigemptyset vsprintf strcasecmp setpgid clock dlopen)
AC_CHECK_FUNCS(dprintf bzero uname vsnprintf)

if test "${ac_cv_func_vsprintf}" = "no"; then
  echo ""
  echo "Your system does not have the sprintf/vsprintf libraries."
  echo "These are required to compile almost anything.  Sorry."
  echo ""
  exit
fi

if test "${ac_cv_header_stdc}" = "no"; then
  echo ""
  echo "Your system must support ANSI C Header files."
  echo "These are required for the language support. Sorry."
  exit
fi


dnl check how much space is left in filedb (informational purposes)

AC_MSG_CHECKING(space left in file database struct)
cat >abacab.c <<'EOF'
#include <stdio.h>
#include <sys/time.h>
#include "src/mod/filesys.mod/files.h"
main() {
  fprintf(stderr,"%d/%d",512-sizeof(struct filler),sizeof(filedb));
}
EOF
${CC} -o abacab abacab.c
FILEDB_SPACE=`./abacab`
AC_MSG_RESULT($FILEDB_SPACE bytes)
rm -f abacab abacab.o abacab.c
echo "   (standard is currently 48/512 bytes)"

dnl where is tcl?  is it here?

# ---------- begin robey's tcl thingies

libnames="tcl.so tcl.so.1 tcl76.so.1.0 tcl75.so.1.0 tcl80.so.1.0\
         tcl76.so.1 tcl75.so.1 tcl74.so.1 tcl80.so.1\
         tcl7.6.so tcl7.5.so tcl7.4.so tcl 8.0.so\
         tcl7.6.so.1 tcl7.5.so.1 tcl7.4.so.1 tcl8.0.so.1\
         tcl.a tcl76.a tcl75.a tcl74.a tcl80.a\
         tcl7.6.a tcl7.5.a tcl7.4.a tcl8.0.a"

# look for libtcl.a: if TCLLIB is set, check there first
if test "$TCLLIB" != ""; then
  TCLLIBFN=notfound
  for checkfn in $libnames;
  do
    if test -r $TCLLIB/lib$checkfn; then
      if test $TCLLIBFN = notfound; then
        TCLLIBFN=$checkfn
      fi
    fi
  done
  if test $TCLLIBFN = notfound; then
    echo "Environment variable TCLLIB is set, but incorrect."
    unset TCLLIB
  fi
fi

# check in common places now
AC_MSG_CHECKING(for libtcl)
if test "$TCLLIB" = ""; then
  for checkfn in $libnames;
  do
    if test "$TCLLIB" = ""; then
      for check in /usr/local/lib /usr/local/lib/tcl /usr/local/pkgs/tcl/lib /usr/lib /usr/i486-linuxaout/lib $HOME $HOME/tcl $HOME/tcl/lib;
      do
        if test -r $check/lib$checkfn; then
          if test "$TCLLIB" = ""; then
            AC_MSG_RESULT(found in $check/lib$checkfn)
            TCLLIB=$check
            TCLLIBFN=$checkfn
          fi
        fi
      done
    fi
  done
else
  AC_MSG_RESULT(found in $TCLLIB/lib$TCLLIBFN)
fi

# give up if they don't even have that
if test "$TCLLIB" = ""; then
  AC_MSG_RESULT(not found)
else
# now look for pesky tcl.h file
  if test "$TCLINC" != ""; then
    if test ! -r $TCLINC/tcl.h; then
      echo "Environment variable TCLINC is set, but incorrect."
      unset TCLINC
    fi
  fi

  # check in common places now
  AC_MSG_CHECKING(for tcl.h)
  if test "$TCLINC" = ""; then
    for check in /usr/local/include /usr/local/include/tcl /usr/local/pkgs/tcl/include /usr/include /usr/include/tcl $HOME $HOME/tcl $HOME/tcl/include;
    do
      if test -r $check/tcl.h; then
        if test "$TCLINC" != ""; then
          break
        fi
        AC_MSG_RESULT(found in $check/tcl.h)
        TCLINC=$check
        break
      fi
    done
  else
    AC_MSG_RESULT(found in $TCLINC/tcl.h)
  fi
 
  # had TCLLIB but not TCLINC?  so close... :(
  if test "$TCLINC" = ""; then
    AC_MSG_RESULT(not found)
    unset TCLLIB
    unset TCLLIBFN
  fi
fi

# done looking, now either TCLLIB & TCLINC are both set, or neither is set

if test "$TCLLIB" = ""; then
  echo ""
  echo "I can't find Tcl on this system."
  echo "*** Note: tcl8.0 thru b2 is --NOT-- useable...  Eggdrop WILL crash."
  echo ""
  echo "Eggdrop now requires Tcl to compile.  If you already have Tcl"
  echo "installed on this system, and I just wasn't looking in the right"
  echo "place for it, set the environment variables TCLLIB and TCLINC so"
  echo "I will know where to find 'libtcl.a' (or 'libtcl.so') and 'tcl.h'"
  echo "(respectively).  Then run 'configure' again."
  echo ""
  echo "Read the README file if you don't know what Tcl is or how to get"
  echo "it and install it."
  echo ""
  exit 1
else
  echo Checking $TCLINC directory for tcl version
  AC_MSG_CHECKING(for Tcl version)
  tclver=`grep TCL_VERSION $TCLINC/tcl.h | grep "7.4" | wc -l`
  if test $tclver = 0; then
    tclver=`grep TCL_VERSION $TCLINC/tcl.h | grep "7.5" | wc -l`
    if test $tclver = 0; then
      tclver=`grep TCL_VERSION $TCLINC/tcl.h | grep "7.6" | wc -l`
      if test $tclver = 0; then
        tclver=`grep TCL_VERSION $TCLINC/tcl.h | grep "8.0" | wc -l`
        if test $tclver = 1; then
	  AC_MSG_RESULT(8.0 found -- USE AT YOUR OWN RISK ********)
          need_dl=1
        else
          AC_MSG_RESULT(hmm... unknown version)
        fi
      else
        AC_MSG_RESULT(7.6 found!)
        need_dl=1
      fi
    else
      AC_MSG_RESULT(7.5 found!)
      need_dl=1
    fi
  else      
    AC_MSG_RESULT(7.4 found)
    echo "   7.4 is kind of old; you may want to upgrade soon."
    need_dl=0
  fi

  if test $need_dl = 1 -a "${ac_cv_func_dlopen}" = "no"; then
    got_dl=0
    dnl some weirdweird OS's put it here :/
    if test -r /usr/bin/libdl.so.1; then
      CFLAGS="$CFLAGS -L/usr/bin"
    fi
    AC_CHECK_LIB(dl,dlopen,got_dl=1)
    if test $got_dl = 0; then
      if test "$LINUX" = "yes"; then
        echo "Since you are on a Linux system, this has a know problem,"
        if test -r /lib/libdl.so.1; then
           echo "and you seem to have it, I know a kludge for it,"
           echo "so we'll do that..."
           echo ""
           AC_DEFINE(HAVE_DLOPEN)
           LIBS="/lib/libdl.so.1 $LIBS"
	   got_dl=1
        else 
           echo "which you DONT seem to have... doh!"
           echo "perhaps you may still have the stuff lying around somewhere"
           echo "if you work out where it is, add it to your XLIBS= lines"
           echo "and #define HAVE_DLOPEN in config.h"
           echo ""
           echo "we'll proceed on anyways, but you probably won't be able"
           echo "to 'make eggdrop' but you might be able to make the"
           echo "static bot (I'll default your make to this version)."
	   DEFAULT_MAKE=static
        fi
      else 
        echo "You don't seem to have libdl anywhere I can find it, this will"
	echo "prevent you from doing dynamic modules, I'll set your default"
	echo "make to static linking."
	DEFAULT_MAKE=static
      fi
    else
      AC_DEFINE(HAVE_DLOPEN)
      LIBS="-ldl $LIBS"
      got_dl=1
    fi 
  fi

  TCLLIBFNS=`basename $TCLLIBFN .a`
  TCLLIBFNS=`basename $TCLLIBFNS .so.1.0`
  TCLLIBFNS=`basename $TCLLIBFNS .so.1`
  TCLLIBFNS=`basename $TCLLIBFNS .so`
  if test $tclver = 1; then
    TCL_REQS="$TCLLIB/lib$TCLLIBFN"
    TCL_LIBS="-L$TCLLIB -l$TCLLIBFNS -lm"
  else
    echo ""
    echo "You're tcl version is way to old - Version 7.3 or earlier"
    echo "I suggest you download and complie a more recent version"
    echo "The most reliable current version is 7.6"
    echo ""
    exit 1
  fi
fi

AC_SUBST(TCLINC)
AC_SUBST(TCLLIB)
AC_SUBST(TCLLIBFN)
AC_SUBST(TCL_REQS)
AC_SUBST(TCL_LIBS)

# ---------- end of robey's tcl thingies

if test ! "x$GMAKE" = "x"; then
  SET_MAKE="MAKE=gmake"
fi

if test -z "$DEST"; then DEST=\${prefix}; fi

AC_SUBST(DEST)
AC_SUBST(CP1)
AC_SUBST(CP2)
AC_SUBST(CP3)
AC_SUBST(EGGEXEC)
AC_SUBST(CC)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_CC)
AC_SUBST(SHLIB_STRIP)
AC_SUBST(BEL_MOD_LD)
AC_SUBST(BEL_MOD_CC)
AC_SUBST(BEL_MOD_STRIP)
AC_SUBST(DLFLAGS)
AC_SUBST(DEFAULT_MAKE)

AC_OUTPUT(Makefile lush.h)

# if you configure again, you must re-make everything.  tough shit.
make clean

echo ""
echo "Configure is done."
echo "Type 'make' to create the bot"
echo ""
