dnl Process this file with autoconf to produce a configure script.
dnl
AC_INIT(FTP.c)
AC_PREREQ(2.4)
AC_CONFIG_HEADER(Config.h)

nc_cv_ncurses=yes
nc_cv_curses=yes
nc_cv_readline=yes
nc_cv_termnet=no
nc_cv_socks=no
nc_cv_socks5=no
nc_cv_shared_libc=no
nc_cv_extra_dirs=yes

AC_ARG_ENABLE(ncurses,[  --disable-ncurses       do not try to find and use the ncurses library],nc_cv_ncurses=$enableval)
AC_ARG_ENABLE(curses,[  --disable-curses        do not try to find and use the curses library],nc_cv_curses=$enableval)
AC_ARG_ENABLE(readline,[  --disable-readline      do not try to find and use the readline library],nc_cv_readline=$enableval)
AC_ARG_ENABLE(termnet,[  --disable-termnet       do not try to find and use the termnet library],nc_cv_termnet=$enableval)
AC_ARG_ENABLE(socks,[  --disable-socks         do not try to find and use the socks library],nc_cv_socks=$enableval)
AC_ARG_ENABLE(socks5,[  --disable-socks5         do not try to find and use the socks5 library],nc_cv_socks5=$enableval)
AC_ARG_ENABLE(shared-libc,[  --disable-shared-libc   do not try to find and use the shared std C lib],nc_cv_shared_libc=$enableval)
AC_ARG_ENABLE(extra-dirs,[  --disable-extra-dirs    do not look for additional -L and -I dirs],nc_cv_extra_dirs=$enableval)

dnl Look for programs in the prefix directory too.
PATH="${prefix}/bin:${PATH}"

os=`uname -s 2>/dev/null | tr '[A-Z]' '[a-z]'`

if test "$os" = freebsd ; then
	rev=`uname -r | cut -c1`
	if test "$rev" = 1 ; then
		AC_WARN(Won't use the curses library on FreeBSD 1.x, because it)
		AC_WARN(is missing too much of it's functionality.  Try ncurses.)
		nc_cv_curses=no
	fi
fi

if test "$os" = ultrix ; then
	AC_WARN(Won't use curses or cursesX on ULTRIX.)
	AC_WARN(They don't work to my satisfaction. Try ncurses instead.)
	nc_cv_curses=no
fi

host=`uname -n 2>/dev/null | tr '[A-Z]' '[a-z]'`
Z10=''
Z11=''
Z20='#'
Z21='#'
if test "$host" = redwing ; then
		Z20=''
		Z21=''
		Z10='#'
		Z11='#'
fi
AC_SUBST(Z10)
AC_SUBST(Z11)
AC_SUBST(Z20)
AC_SUBST(Z21)

AC_PROG_CC
wi_CFLAGS
wi_PROTOTYPES

if test "$wi_cv_prototypes" = no ; then
	AC_WARN([Your compiler is probably won't be able to compile the package.])
	AC_WARN([Get gcc, or any compiler that supports function prototypes.])
fi

AC_PROG_MAKE_SET
AC_PATH_PROG(MORE,more,more)
AC_PATH_PROG(LS,ls,/bin/ls)
nc_PATH_PROG_ZCAT

# See if we should add -I/usr/local/include -L/usr/local/lib, etc.
wi_EXTRA_DIRS(yes, /usr/local /usr/ccs, -)

# Look for SunOS' /usr/5lib and 5include directories, because
# we want to use those for the curses stuff.  Cheat a little and
# don't do this if it looks like we are going to use ncurses.
if test -f /usr/local/include/ncurses/unctrl.h && test "$nc_cv_ncurses" = yes ; then
	echo >/dev/null		# no op
else
wi_SYSV_EXTRA_DIRS
fi

AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBS)

wi_DEFINE_UNAME
AC_DEFINE_UNQUOTED(MORE, "$MORE")
AC_DEFINE_UNQUOTED(LS, "$LS")

AC_C_CONST

AC_CHECK_LIB(socket,socket)
AC_CHECK_LIB(inet,main)

wi_LIB_NSL
wi_LIB_RESOLV

if test "$nc_cv_socks5" = yes ; then
# Look for the "SOCKS" library for use with Firewalls/Gateways.
savelibs="$LIBS"
AC_CHECK_LIB(db,          main)
AC_CHECK_LIB(isode,       main)
AC_CHECK_LIB(com_err,     main)
AC_CHECK_LIB(crypto,      main)
AC_CHECK_LIB(krb5,        main)
AC_CHECK_LIB(gssapi_krb5, main)
AC_CHECK_LIB(socks5,SOCKSinit)
      if test "$ac_cv_lib_socks5" = yes ; then
              nc_cv_socks=no
      else
              LIBS="$savelibs"
      fi
else
ac_cv_lib_socks5=no
fi

if test "$nc_cv_socks" = yes ; then
# Look for the "SOCKS" library for use with Firewalls/Gateways.
AC_CHECK_LIB(socks,SOCKSinit)
	if test "$ac_cv_lib_socks" = yes ; then
		# Socks requires PASV.
		# Use FTP_DATA_PORT_MODE == 1 (kPassiveMode)
		AC_DEFINE(FTP_DATA_PORT_MODE,1)
	fi
else
ac_cv_lib_socks=no
fi


if test "$nc_cv_termnet" = yes ; then
# Look for the "TERM" library for use with Linux, etc.
AC_CHECK_LIB(termnet,main)
else
ac_cv_lib_termnet=no
fi

if test "$nc_cv_readline" = no; then
AC_CHECK_LIB(getline,gl_histadd)
fi

# We don't need termcap directly, but on some systems you need to link
# with -lcurses -ltermcap.

AC_CHECK_LIB(termcap,tgetstr,nc_cv_lib_termcap=yes,nc_cv_lib_termcap=no)

CURSESLIB=''
TERMCAPLIB=''
obselete_lib=no

if test $nc_cv_curses = yes ; then
	# cursesX is an Ultrix oddity
	AC_CHECK_LIB(cursesX,endwin,CURSESLIB=cursesX)

	if test "$CURSESLIB" = "" ; then
		AC_CHECK_LIB(curses,wnoutrefresh,CURSESLIB=curses)
	fi

	# Okay, maybe that didn't work, but maybe you needed to
	# use -lcurses -ltermcap.
	if test "$CURSESLIB" = "" ; then
		if test "$nc_cv_lib_termcap" = yes ; then
			unset ac_cv_lib_curses
			AC_MSG_WARN([Did not detect curses, but trying again with termcap:])
			AC_CHECK_LIB(curses,wnoutrefresh,CURSESLIB=curses,CURSESLIB='',-ltermcap)
			if test "$CURSESLIB" = "curses" ; then
				TERMCAPLIB=-ltermcap
			else
				# Okay, you don't have one ncftp can use, but maybe
				# we should tell the user if they have a shitty curses
				# library.

				unset ac_cv_lib_curses
				if test "$nc_cv_lib_termcap" = yes ; then
					AC_CHECK_LIB(curses,endwin,obselete_lib=yes,obselete_lib=no,-ltermcap)
				else
					AC_CHECK_LIB(curses,endwin,obselete_lib=yes,obselete_lib=no)
				fi

				if test "$obselete_lib" = no ; then	
					AC_MSG_WARN([Did not detect a useable curses library.])
				fi
			fi
		fi
	fi

	if test "$CURSESLIB" = "" ; then
		# Try termlib with Solaris 2.3
		AC_CHECK_LIB(termlib,endwin,CURSESLIB=termlib)
	fi
fi

if test $nc_cv_ncurses = yes ; then
	AC_CHECK_LIB(ncurses,endwin,CURSESLIB=ncurses)
	if test "$CURSESLIB" = ncurses && test "$nc_cv_extra_dirs" = yes ; then
		# See if we should add -I/usr/local/include/ncurses, etc.
		wi_EXTRA_DIRS(yes, /usr/local /usr /usr/ccs, ncurses)

		# See if we should add -I/usr/local/ncurses
		wi_EXTRA_IDIR(/usr/local/ncurses)

		# Some people (me) have two versions of ncurses on
		# the system.  Use the one in /usr/local first.
		case "$CPPFLAGS" in
			*/usr/local/include/ncurses*/usr/include/ncurses*)
				CPPFLAGS=`echo "$CPPFLAGS" | sed 's@-I/usr/include/ncurses@@g'`
		esac
	fi
fi

if test "x$CURSESLIB" = xcurses -o "x$CURSESLIB" = xcursesX -o "x$CURSESLIB" = "xtermlib" ; then

	LIBS="$LIBS -l$CURSESLIB"
#	if test "x$CURSESLIB" = xcursesX ; then
#		LIBS="$LIBS -lcurses"
#	fi
	AC_DEFINE(HAVE_LIBCURSES)

elif test "x$CURSESLIB" = xncurses ; then
	LIBS="$LIBS -lncurses"
	AC_DEFINE(HAVE_LIBNCURSES)
fi

if test "$TERMCAPLIB" != "" ; then
	LIBS="$LIBS -l$TERMCAPLIB"
	AC_DEFINE(HAVE_LIBTERMCAP)
fi

if test "$obselete_lib" = yes ; then	
	if test "x$CURSESLIB" != xncurses ; then
			AC_MSG_WARN([You have an older curses library, but it])
			AC_MSG_WARN([can't be used because it does not have the])
			AC_MSG_WARN([functions wnoutrefresh and doupdate, which])
			AC_MSG_WARN([NcFTP needs.  Consider installing the ncurses])
			AC_MSG_WARN([library, from prep.ai.mit.edu, in /pub/gnu.])
	fi
fi

# Readline must be listed before -lcurses and/or -ltermcap.
if test "$nc_cv_readline" = yes ; then
	READLINELIB=''
	AC_CHECK_LIB(readline,readline,READLINELIB=readline)
	if test "$READLINELIB" != "" ; then
		if test "$CURSESLIB" != "" ; then
			LIBS=`echo "$LIBS" | sed "s@$CURSESLIB@$READLINELIB -l$CURSESLIB@"`
		elif test "$TERMCAPLIB" != "" ; then
			LIBS=`echo "$LIBS" | sed "s@$TERMCAPLIB@$READLINELIB -l$TERMCAPLIB@"`
		fi
		AC_DEFINE(HAVE_LIBREADLINE)
	fi
else
	ac_cv_lib_readline=no
fi


if test "$ac_cv_lib_readline" = yes ; then
	# Make sure we link termcap with readline if we aren't already
	# linking curses or termcap.
	case "$LIBS" in
		*termcap*|*curses*)
			;;
		*)
			LIBS="$LIBS -ltermcap"
			;;
	esac
	AC_CHECK_FUNCS(filename_completion_function)
fi


wi_READLINE_WITH_NCURSES
AC_CHECK_LIB(posix,tcgetpgrp)

if test "$nc_cv_shared_libc" = yes ; then
# This should be the last library to look for.
AC_CHECK_LIB(c_s,main)
else
ac_cv_lib_c_s=no
fi

AC_FUNC_STRFTIME

AC_CHECK_FUNCS(getdomainname getcwd getwd memmove mktime symlink setlinebuf)
AC_CHECK_FUNCS(strerror strcasecmp remove unlink tcgetpgrp fcntl flock)

AC_MSG_CHECKING([for domain name in /etc/resolv.conf])
RESOLV_CONF_DOMAIN=""
if test -f /etc/resolv.conf ; then
	changequote(, )dnl
	RESOLV_CONF_DOMAIN=`sed 's/[#;].*//' /etc/resolv.conf | sed -n 's/domain[ 	]//p' | sed -n 1,1p | tr -d ' 	'`
	changequote([, ])dnl
	if test "$RESOLV_CONF_DOMAIN" = "" ; then
		AC_MSG_RESULT([no])
	else
		AC_MSG_RESULT($RESOLV_CONF_DOMAIN)
		RESOLV_CONF_DOMAIN=" # -DDOMAINNAME=\\\"$RESOLV_CONF_DOMAIN\\\""
	fi
else
	AC_MSG_RESULT([no])
fi
AC_SUBST(RESOLV_CONF_DOMAIN)

if test "x$CURSESLIB" = x ; then
# Don't check these unless we have a curses library.
	ac_cv_func_curs_set=no
	ac_cv_func_getmaxyx=no
	ac_cv_func_beep=no
	ac_cv_func_notimeout=no
else
	AC_CHECK_FUNCS(curs_set getmaxyx beep notimeout)
	if test $ac_cv_func_getmaxyx = no ; then
		AC_MSG_CHECKING(for maxx or _maxx in WINDOW structure)
		AC_TRY_COMPILE([#include <curses.h>],[
		WINDOW *w;
	
		w = newwin(10, 10, 1, 1);
		w->maxx = 0;
		delwin(w);],maxx=yes,_maxx=yes)
		if test "$maxx" = yes ; then
			AC_DEFINE(HAVE_MAXX)
			AC_MSG_RESULT(maxx)
		else
			# Most systems use _maxx as their internal field.
			AC_DEFINE(HAVE__MAXX)
			AC_MSG_RESULT([assuming _maxx])
		fi
	fi
fi

if test $ac_cv_func_getcwd = no && test $ac_cv_func_getwd = no ; then
AC_WARN(This system does not have either getwd or getcwd?)
AC_WARN(I find that a little hard to believe.)
AC_WARN(You may want to try -DHAVE_GETWD anyway.)
AC_WARN([
This could also mean that your compiler isn't working])
AC_WARN(with this configure script.  Check the ./config.log)
AC_WARN(and look for anomalies.)
fi

if test $ac_cv_func_remove = no && test $ac_cv_func_unlink = no ; then
AC_WARN(This system does not have either remove or unlink?)
AC_WARN(I find that a little hard to believe.)
AC_WARN(You may want to try -DHAVE_UNLINK anyway.)
fi

AC_HEADER_STDC
AC_CHECK_HEADERS(string.h memory.h unistd.h stdarg.h termios.h termio.h sgtty.h)
AC_CHECK_HEADERS(utime.h sys/time.h readline/history.h cursesX.h ncurses.h)
AC_CHECK_HEADERS(limits.h termnet.h term/termnet.h fcntl.h sys/ioctl.h curses.h)
AC_CHECK_HEADERS(net/errno.h sys/file.h sys/bsdtypes.h)
AC_HEADER_TIME

AC_FUNC_SETVBUF_REVERSED
AC_FUNC_GETPGRP
AC_PROG_GCC_TRADITIONAL

AC_TYPE_SIZE_T

AC_OUTPUT(Makefile)
