dnl Process this file with autoconf to produce a configure script.
dnl
AC_INIT(FTP.c)

AC_CONFIG_HEADER(Config.h)

nc_cv_ncurses=yes
nc_cv_curses=yes
nc_cv_readline=yes
nc_cv_termnet=yes
nc_cv_socks=yes
nc_cv_shared_libc=yes
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_termnet=$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 | 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.  The alternative)
		AC_WARN(ncurses library will link at least, but version 1.8.6 of)
		AC_WARN(ncurses doesn't work very well with FreeBSD and ncftp.)
		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

AC_PROG_CC

if test "$os" = hp-ux ; then
	if test "$ac_cv_prog_gcc" = yes ; then
		if test "$CFLAGS" != "" ; then
			# Shouldn't be in there.
			CFLAGS=`echo "$CFLAGS" | sed 's/-Aa//g'`
		fi
	else
		# If you're not using gcc, then you better have a cc/c89
		# that is usable.  If you have the barebones compiler, it
		# won't work.  The good compiler uses -Aa for the ANSI
		# compatible stuff.
		x=`echo $CFLAGS | grep 'Aa' 2>/dev/null`
		if test "$x" = "" ; then
			CFLAGS="$CFLAGS -Aa"
		fi
	fi
	# Also add _HPUX_SOURCE to get the extended namespace.
	x=`echo $CFLAGS | grep '_HPUX_SOURCE' 2>/dev/null`
	if test "$x" = "" ; then
		CFLAGS="$CFLAGS -D_HPUX_SOURCE"
	fi
fi


AC_TRY_COMPILE(,[extern void exit(int status);],protos=y,protos=n)
if test "$protos" = n ; then
		AC_WARN([Your compiler is probably too old/crippled to be of any use.])
		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(GZCAT,gzcat)
AC_PATH_PROG(ZCAT,zcat)
AC_PATH_PROG(LS,ls,/bin/ls)

## Here's a big mess so I can get my own copies of some libraries used.

if test "$nc_cv_extra_dirs" = yes ; then
	b1=`cd .. ; pwd`
	b2=`cd ../.. ; pwd`
	j=''
	if test "$prefix" != /usr/local && test "$prefix" != NONE ; then
		j=$prefix
	fi

	for i in $HOME $j $b1 $b2 ; do
		if test "$i" != "/" ; then
			AC_MSG_CHECKING(for $i/include directory)
			if test -r "$i/include" ; then
				k=yes
				case "$CPPFLAGS" in
					*${i}/include*)
						k='had it already'
						;;
					*)
						CPPFLAGS="$CPPFLAGS -I$i/include"
						;;
				esac
				AC_MSG_RESULT($k)
			else
				AC_MSG_RESULT(no)
			fi

			AC_MSG_CHECKING(for $i/lib directory)
			if test -r "$i/lib" ; then
				k=yes
				case "$LDFLAGS" in
					*${i}/lib*)
						k='had it already'
						;;
					*)
						LDFLAGS="$LDFLAGS -L$i/lib"
						;;
				esac
				AC_MSG_RESULT($k)
			else
				AC_MSG_RESULT(no)
			fi
		fi

		# Common on Solaris.
		if test -r /usr/ccs/lib ; then
			LDFLAGS="$LDFLAGS -L/usr/ccs/lib"
		fi

	done

fi


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

unam=`uname -a 2>/dev/null`
if test "$unam" != "" ; then
	AC_DEFINE_UNQUOTED(UNAME, "$unam")
fi

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

if test "x$GZCAT" = x ; then
	if test "x$ZCAT" != x ; then
		# See if zcat is really gzcat.  gzcat has a --version option, regular
		# zcat does not.
		AC_MSG_CHECKING(if zcat is really gzcat in disguise)
		if $ZCAT --version 2> /dev/null ; then
			AC_DEFINE_UNQUOTED(GZCAT, "$ZCAT")
			AC_MSG_RESULT(yes)
		else
			AC_MSG_RESULT(no)
		fi
	fi
else
	AC_DEFINE_UNQUOTED(GZCAT, "$GZCAT")
fi

if test "x$ZCAT" != x ; then
	AC_DEFINE_UNQUOTED(ZCAT, "$ZCAT")
fi

cchost=`uname -n 2>/dev/null`
host_os=`uname -s 2>/dev/null`

gccdbflags="-g -Wall -Wshadow -Wconversion -Wstrict-prototypes -Wmissing-prototypes"

if test "x$CFLAGS" = x ; then CFLAGS="-g" ; fi

AC_C_CONST

if test "$host_os" != IRIX ; then
AC_CHECK_LIB(socket,socket)
fi		# end if not IRIX

AC_CHECK_LIB(inet,main)

if test "$host_os" != IRIX ; then
AC_CHECK_LIB(nsl,main)
fi		# end if not IRIX

AC_CHECK_LIB(resolv,main)


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" = yes ; then
AC_CHECK_LIB(readline,add_history)
else
ac_cv_lib_readline=no
fi

if test "$ac_cv_lib_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=''

if test $nc_cv_curses = yes ; then
	AC_MSG_CHECKING(for an alternative System V curses library)
	# Look for a more System V compliant library first.
	if test -f /usr/5include/curses.h ; then
		CPPFLAGS="$CPPFLAGS -I/usr/5include"
		LDFLAGS="$LDFLAGS -L/usr/5lib"
		AC_MSG_RESULT(yes)
	else
		AC_MSG_RESULT(no)
	fi

	# 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" = yes ; 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.])
				else
					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
		b1=`cd .. ; pwd`
		b2=`cd ../.. ; pwd`
		j=''
		if test "$prefix" != /usr/local && test "$prefix" != NONE ; then
			j=$prefix
		fi

		for i in /usr $HOME $j $b1 $b2 ; do
			AC_MSG_CHECKING(for $i/include/ncurses directory)
			if test -r "$i/include/ncurses" ; then
				k=yes
				case "$CPPFLAGS" in
					*${i}/include/ncurses*)
						k='had it already'
						;;
					*)
						CPPFLAGS="$CPPFLAGS -I$i/include/ncurses"
						;;
				esac
				AC_MSG_RESULT($k)
			else
				AC_MSG_RESULT(no)
			fi
		done
	fi
fi

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

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

elif test "x$CURSESLIB" = xncurses ; then

	LIBS="$LIBS -lncurses"

	if test -d /usr/include/ncurses ; then
		CPPFLAGS="$CPPFLAGS -I/usr/include/ncurses"
	elif test -d /usr/local/ncurses ; then
		CPPFLAGS="$CPPFLAGS -I/usr/local/ncurses"
	elif test -d /usr/local/include/ncurses ; then
		CPPFLAGS="$CPPFLAGS -I/usr/local/include/ncurses"
	fi

	AC_DEFINE(HAVE_LIBNCURSES)

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
fi


# Readline and Ncurses could both define "backspace".
# Warn about this if we have both things in our definitions list.

if test "$ac_cv_lib_readline" = yes && test "$ac_cv_lib_ncurses" = yes ; then

AC_MSG_CHECKING(if readline and ncurses will link together)
j="$LIBS"
LIBS="-lreadline -lncurses"
AC_TRY_LINK(,[
readline("prompt");
endwin();
],k=yes,k=no)
if test "$k" = no ; then
	AC_MSG_RESULT(no)
	# Remove '-lreadline' from LIBS.
	LIBS=`echo $j | sed s/-lreadline//g`
	ac_cv_lib_readline=no
	AC_WARN([The versions of GNU readline and ncurses you have installed on this system
can't be used together, because they use the same symbol, backspace. If
possible, recompile one of the libraries with -Dbackspace=back_space, then
re-run configure.])

else
	AC_MSG_RESULT(yes)
	LIBS="$j"
fi

fi

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 select poll getcwd getwd memmove mktime symlink)
AC_CHECK_FUNCS(strerror strcasecmp remove unlink tcgetpgrp fcntl flock)

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/select.h sys/time.h readline/history.h cursesX.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)
if test $nc_cv_ncurses = yes ; then
AC_CHECK_HEADERS(ncurses.h)
fi
AC_HEADER_TIME

#-----------------------------------------------------------------------------
# See if <sys/select.h> is includable after <sys/time.h>
if test "$ac_cv_header_sys_select_h" = yes ; then
	AC_MSG_CHECKING([if <sys/select.h> is compatible with <sys/time.h>])
	AC_TRY_COMPILE([#include <sys/time.h>
	#include <sys/select.h>],[
	fd_set a;
	struct timeval tmval;

	FD_ZERO(&a);
	tmval.tv_sec = 0;],selecth=yes,selecth=no)

	if test "$selecth" = yes ; then
		AC_DEFINE(CAN_USE_SYS_SELECT_H)
	fi
	AC_MSG_RESULT($selecth)
fi
#-----------------------------------------------------------------------------

AC_FUNC_SETVBUF_REVERSED
AC_PROG_GCC_TRADITIONAL

AC_TYPE_SIZE_T


dnl AC_FUNC_VPRINTF

AC_OUTPUT(Makefile)
