AC_INIT(README)
AC_PREREQ(2.4)
AC_CONFIG_HEADER(include/config.h)

dnl Need these for some of the things we'll do in configure, so define them now

dnl We want to put the -g -O stuff from AC_PROG_CC in CDEBUG_FLAGS, not CFLAGS, so 
dnl store the old ones...then switch back

OS=`uname -s`
AC_PROG_ANSI_CC
AC_PROG_GCC_TRADITIONAL
AC_C_CROSS
AC_C_CONST
AC_C_INLINE

echo

AC_PROG_RANLIB
AC_PROG_INSTALL
AC_CHECK_PROG(MAKED,    makedepend, makedepend, true)
AC_CHECK_PROG(AUTOCONF, autoconf,   autoconf,   true)
AC_CHECK_PROG(AUTOHDR,  autoheader, autoheader, true)

echo 

AC_CHECK_HEADERS(fcntl.h memory.h unistd.h string.h strings.h bstring.h varargs.h stdarg.h stdlib.h time.h syslog.h ifaddrs.h crypt.h)
AC_CHECK_HEADERS(sys/stat.h sys/param.h sys/signal.h sys/resource.h sys/socket.h sys/uio.h sys/time.h sys/wait.h)
AC_CHECK_HEADERS(sys/ttychars.h sys/ipc.h sys/bitypes.h sys/mbuf.h sys/sem.h sys/select.h)
AC_CHECK_HEADERS(sys/file.h sys/ioctl.h sys/fcntl.h sys/filio.h sys/sockio.h)
AC_CHECK_HEADERS(sys/termios.h sys/termio.h termios.h termio.h sys/un.h)
AC_CHECK_HEADERS(netinet/in_systm.h net/route.h machine/endian.h paths.h nl_types.h)
AC_HEADER_TIME
AC_HEADER_STDC
AC_HEADER_DIRENT


echo

if test "$OS" != "IRIX"; then
AC_CHECK_LIB(socket,  main)
dnl usual lib for dlopen
AC_CHECK_LIB(dl,      main)
fi
dnl AC_CHECK_LIB(sun,     main)
dnl lib for HP's shl_load
AC_CHECK_LIB(dld,     main)
dnl AIX lib for dlopen
AC_CHECK_LIB(svld,     main)
AC_CHECK_LIB(termcap, setupterm, [TELNET_LIBS="$TELNET_LIBS -ltermcap"; TELNET_DEFS="$TELNET_DEFS -DHAVE_SETUPTERM"], 
 AC_CHECK_LIB(curses, setupterm, [TELNET_LIBS="$TELNET_LIBS -lcurses"; TELNET_DEFS="$TELNET_DEFS -DHAVE_SETUPTERM"], 
  AC_CHECK_LIB(ncurses, setupterm, [TELNET_LIBS="$TELNET_LIBS -lncurses"; TELNET_DEFS="$TELNET_DEFS -DHAVE_SETUPTERM"],
   AC_CHECK_LIB(termcap, tgetent, [TELNET_LIBS="$TELNET_LIBS -ltermcap"],
    AC_CHECK_LIB(curses, tgetent, [TELNET_LIBS="$TELNET_LIBS -lcurses"], 
     AC_CHECK_LIB(ncurses, tgetent, [TELNET_LIBS="$TELNET_LIBS -lncurses"])
)))))
AC_CHECK_LIB(c,  gethostbyname, [STATIC_LIBS=""], AC_CHECK_LIB(resolv,  gethostbyname, [STATIC_LIBS="-lresolv"]))

AC_MSG_CHECKING(if we can use -lnsl)
AC_CACHE_VAL(r_cv_use_libnsl, [AC_TRY_RUN(main() { if (gethostbyname("localhost")) exit(0); exit(-1); }, r_cv_use_libnsl=no, r_cv_use_libnsl=yes, r_cv_use_libnsl=yes)])
AC_MSG_RESULT($r_cv_use_libnsl)
if test "$r_cv_use_libnsl" = "yes"; then LIBS="$LIBS -lnsl"; fi

AC_CHECK_SHLIBS(libc libnsl libsocket libresolv libdgc)
dnl just a trick to get these into include/config.h
if test "yes" = "no"; then
AC_DEFINE_UNQUOTED(LIBC_NAME)
AC_DEFINE_UNQUOTED(LIBNSL_NAME)
AC_DEFINE_UNQUOTED(LIBRESOLV_NAME)
AC_DEFINE_UNQUOTED(LIBSOCKET_NAME)
AC_DEFINE_UNQUOTED(LIBDGC_NAME)
fi

echo

AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SIGNAL
AC_SOCKADDR_SUN_LEN

AC_MSG_CHECKING(for sig_atomic_t)
AC_CACHE_VAL(r_cv_type_sig_atomic_t,
[AC_EGREP_CPP(sig_atomic_t, [
#include <sys/types.h>
#include <signal.h>
#if HAVE_SYS_SIGNAL_H
#include <sys/signal.h>
#endif], r_cv_type_sig_atomic_t=yes, r_cv_type_sig_atomic_t=no)])dnl
AC_MSG_RESULT($r_cv_type_sig_atomic_t)
if test "$r_cv_type_sig_atomic_t" = "no"; then
AC_DEFINE(sig_atomic_t, int)
fi

AC_MSG_CHECKING(for cc_t)
AC_CACHE_VAL(r_cv_have_cct, AC_TRY_LINK([cc_t],[#include <termio.h>],[cc_t foo;],r_cv_have_cct=yes,r_cv_have_cct=no))
if test $r_cv_have_cct = no; then AC_DEFINE(NO_CC_T) fi
AC_MSG_RESULT($r_cv_have_cct)

AC_MSG_CHECKING(for sig_t)
AC_CACHE_VAL(r_cv_have_sigt, AC_TRY_LINK([#include <signal.h>], [sig_t i = (sig_t)0;], r_cv_have_sigt=yes, r_cv_have_sigt=no))
if test "$r_cv_have_sigt" = "no"; then [FTPDEFS="$FTPDEFS -DCOMPAT"]; fi
AC_MSG_RESULT($r_cv_have_sigt)

echo

AC_MSG_CHECKING(for sys_errlist)
AC_CACHE_VAL(r_cv_have_sysel, AC_TRY_LINK(,[extern char **sys_errlist;char **foo = sys_errlist;], r_cv_have_sysel=yes, r_cv_have_sysel=no))
if test "$r_cv_have_sysel" = "yes"; then AC_DEFINE(HAVE_SYSERRLIST) fi
AC_MSG_RESULT($r_cv_have_sysel)

AC_CHECK_FUNCS(dlopen shl_load) 

saved_libs="$LIBS"
LIBS="$LIBS $TELNET_LIBS"
AC_CHECK_FUNCS(setupterm)
AC_CHECK_FUNCS(tgetent)
LIBS="$saved_libs"

AC_CHECK_FUNCS(gethostbyname_r gethostbyaddr_r getpwuid_r getservbyname_r)
AC_CHECK_FUNCS(memset memcmp memmove strchr strrchr strdup strerror)
AC_CHECK_FUNCS(bcopy bcmp bzero index rindex)
AC_CHECK_FUNCS(setenv putenv unsetenv getenv)
AC_CHECK_FUNCS(getdomainname rresvport setsid setpgid getifaddrs)
AC_CHECK_FUNCS(waitpid flock dgettext sigaction sigprocmask)
AC_CHECK_FUNCS(re_comp strspn getcwd vfork herror genget getopt)
AC_VPRINTF
AC_KERBEROS5
AC_IDENT
AC_SKEY

echo

dnl If your path doesn't have these, some of the respective clients won't work.
AC_PATH_PROG(ORIG_FINGER, finger)
AC_PATH_PROG(TROUTEPROG,  traceroute)
if test "$TROUTEPROG" != ""; then 
AC_DEFINE_UNQUOTED(TROUTEPROG,  "$TROUTEPROG") 
fi
AC_PATH_PROG(PINGPROG, ping)
if test "$PINGPROG" != "";  then 
AC_DEFINE_UNQUOTED(PINGPROG, "$PINGPROG") 
fi

echo

SRVPID_FILE="/tmp/socks5.pid"
SRVCONF_FILE="/etc/socks5.conf"
SRVIDT_FILE="/tmp/socks5.ident"
LIBCONF_FILE="/etc/libsocks5.conf"
PINCONF_FILE="/etc/socks5.plugins"

AC_ARG_WITH(debug,           --with-debug		Turn on debugging and compile time warnings,	 [if test "$GCC" = "yes"; then CDEBUG_FLAGS="-g -Wall -Wno-comment"; else CDEBUG_FLAGS="-g"; fi])
AC_ARG_WITH(internal,        --with-internal		Turn on internal NEC development stuff,          [AC_DEFINE(INTERNAL) AC_DEFINE(NEC)])
AC_ARG_WITH(static,          --with-static		Turn on static compiling,			 [if test "$withval" != "yes"; then CC="$CC $withval" ; else CC="$CC -static" ;  fi])
AC_ARG_WITH(threads,         --with-threads		Turn on threading in the server if possible, [threads="$withval"], [threads="no"])
if test "$threads" != "no"; then
        CFLAGS="$CFLAGS -D_THREAD_SAFE"
	if test "$threads" != "yes"; then
                CFLAGS="$CFLAGS -D_THREAD_SAFE -I$threads/include"
                LDFLAGS="$LDFLAGS -L$threads/lib"
	fi;
	AC_DEFINE(USE_THREADS)
	AC_CHECK_HEADERS(pthread.h)
saved_libs="$LIBS"
        AC_CHECK_LIB(pthreads, main, [LIBS="-lpthreads $LIBS";SERVER_LIBS="-lpthreads"])
        AC_CHECK_LIB(pthread, main, [LIBS="-lpthread $LIBS";SERVER_LIBS="-lpthread"])
        if test "$GCC" = "yes"; then
            if test "$OS" = "AIX"; then
                    SERVER_LIBS="$SERVER_LIBS -lc_r"
                    EXTRA_LDFLAGS="-nostartfiles"
                    EXTRA_OBJS="/lib/crt0_r.o"
            fi;
            if test "$OS" = "OSF1"; then
                    SERVER_LIBS="$SERVER_LIBS -lmach -lc_r"
            fi;
            if test "$OS" = "HP-UX"; then
                case "$OSVER" in
                *10.*)
                    SERVER_LIBS="$SERVER_LIBS -lcma -lc_r"
                esac
            fi;
        fi;
        AC_CHECK_FUNCS(pthread_create pthread_self pthread_exit)
        AC_CHECK_FUNCS(pthread_attr_init pthread_attr_setscope)
        AC_CHECK_FUNCS(pthread_attr_setdetachstate)
        AC_CHECK_FUNCS(pthread_detach)
        AC_CHECK_FUNCS(pthread_sigmask pthread_kill)
LIBS="$saved_libs"
fi
AC_ARG_WITH(libconffile,     --with-libconffile=val	Set the location of the library config file,	 [LIBCONF_FILE="$withval"]; AC_DEFINE_UNQUOTED(LIBCONF_FILE, "$withval"))
AC_ARG_WITH(srvconffile,     --with-srvconffile=val	Set the location of the server config file, 	 [SRVCONF_FILE="$withval"]; AC_DEFINE_UNQUOTED(SRVCONF_FILE, "$withval"))
AC_ARG_WITH(srvpidfile,      --with-srvpidfile=val	Set the location of the server pid file,         [SRVPID_FILE="$withval" ]; AC_DEFINE_UNQUOTED(SRVPID_FILE,  "$withval"))
AC_ARG_WITH(srvidtfile,      --with-srvidtfile=val	Set the location of the server ident file, 	 [SRVIDT_FILE="$withval" ]; AC_DEFINE_UNQUOTED(SRVIDT_FILE,  "$withval"))
AC_ARG_WITH(srvpwdfile,      --with-srvpwdfile=val	Set the location of the server password file, 	 [SRVPWD_FILE="$withval" ]; AC_DEFINE_UNQUOTED(SRVPWD_FILE,  "$withval"))
AC_ARG_WITH(default-port,    --with-default-port=val 	Set the default socks port,			 [SOCKS_DEFAULT_PORT="$withval"]; AC_DEFINE_UNQUOTED(SOCKS_DEFAULT_PORT, $withval))
AC_ARG_WITH(default-server,  --with-default-server=val 	Set the default socks server,			 AC_DEFINE_UNQUOTED(SOCKS_DEFAULT_SERVER, "$withval"))
AC_ARG_WITH(default-version, --with-default-version=val	Set the default socks version,			 AC_DEFINE_UNQUOTED(SOCKS_DEFAULT_VERSION, $withval))
AC_ARG_WITH(syslog-facility, --with-syslog-facility=val Set the syslog facility for socks5,		 syslog_fac="$withval")
if test "$syslog_fac" != "no"; then
	if test "$syslog_fac" = "yes" -o "$syslog_fac" = ""; then
		syslog_fac=LOG_DAEMON
	fi
	AC_DEFINE_UNQUOTED(SYSLOG_FAC, $syslog_fac)
fi
AC_ARG_WITH(monitor,         --with-monitor=val         Turn on monitoring,
	[MON_DIR="$withval"; MON_LIBS="-L\$(MON_DIR)/lib -lmon";
	 AC_CHECK_LIB(ipc, main)
	 AC_CHECK_LIB(compat, main)
	 MON_CFLAGS="-I\$(MON_DIR)/include"];
	AC_DEFINE(MONITOR))
AC_ARG_WITH(passwd,          --with-passwd=val          Turn on use of passwd file for passwords,        AC_DEFINE(USE_PASSWD))
AC_ARG_WITH(shadow-passwd,          --with-shadow-passwd=val          Turn on use of shadow passwd file for passwords,        AC_DEFINE(USE_SHADOW_PASSWD))
AC_ARG_WITH(plugins,         --with-plugins             Turn on NEC internal plug-in support,           AC_DEFINE(USE_PLUGINS))
AC_ARG_WITH(pinconffile,     --with-pinconffile=val   Set the location of the plug-in config file,      [PINCONFF_ILE="$withval" ]; AC_DEFINE_UNQUOTED(PINCONF_FILE, "$withval"))

AC_MAKE_SHAREDLIB
AC_CC_EXPORT_SYMBOL
AC_SUBST(SHLIB_LFLAGS)
AC_SUBST(SHLIB_LIBS)

BINRM="/bin/rm -f"
LIBS="$LIBS $STATIC_LIBS"

AC_SUBST(MON_DIR)
AC_SUBST(MON_LIBS)
AC_SUBST(MON_CFLAGS)

AC_SUBST(SOCKS_DEFAULT_PORT)
AC_SUBST(SRVPID_FILE)
AC_SUBST(SRVCONF_FILE)
AC_SUBST(LIBCONF_FILE)

AC_SUBST(CDEBUG_FLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(CFLAGS)
AC_SUBST(BINRM)
AC_SUBST(LIBS)
AC_SUBST(DEFS)

AC_SUBST(SERVER_DEFS)
AC_SUBST(SERVER_LIBS)
AC_SUBST(EXTRA_OBJS)
AC_SUBST(EXTRA_LDFLAGS)
AC_SUBST(EXPORT_LDFLAGS)
AC_SUBST(TELNET_DEFS)
AC_SUBST(TELNET_LIBS)
AC_SUBST(FTPDEFS)

AC_OUTPUT(Makefile
	lib/Makefile
	test/Makefile
	shlib/Makefile
	shlib/runsocks
	server/Makefile
	server/stopsocks
	include/Makefile
	clients/Makefile
	clients/pt/Makefile
	clients/ftp/Makefile 
	clients/finger/Makefile
	clients/archie/Makefile 
	clients/telnet/Makefile
	clients/telnet/libtelnet/Makefile
	stamp-h)

