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

nc_cv_socks5=no
no_signals=yes

AC_ARG_ENABLE(socks5,[  --enable-socks5        try to find and use the socks5 library],nc_cv_socks5=yes,nc_cv_socks5=no)
AC_ARG_ENABLE(signals,[  --enable-signals       allow use of signal handling],[no_signals=no])


wi_OS_VAR

changequote(<<, >>)dnl
MAINDIR=`pwd`
if test -f "$MAINDIR/dos2unix.sh" ; then
	chmod u+rwx $MAINDIR/dos2unix.sh
	for dir in . ../sio ../Strn ; do
		if [ -d "$dir" ] ; then
			dir=`cd "$dir" ; pwd`
			find "$dir" -name '*.in' -exec "$MAINDIR/dos2unix.sh" {} \;
			find "$dir" -name '*.h' -exec "$MAINDIR/dos2unix.sh" {} \;
			find "$dir" -name '*.c' -exec "$MAINDIR/dos2unix.sh" {} \;
		fi
	done
fi
MAKE=make
STATIC=""
BDYNAMIC="# -Wl,-Bdynamic"
BSTATIC="# -Wl,-Bstatic"
LIBSET='$(LIB)'
Z31=''
Z32=''
Z33=''
Z34=''
REDWING='no'
SFLAG='-s'

case "$host" in
	redwing*)	CC=cc
			LDFLAGS=''
			STATIC=''
			Z31='	-@mv $(STGZFILE) newbin/'
			Z32='	-@mv $(DTGZFILE) newbin/'
			Z33='	-@mv $(MTGZFILE) newbin/'
			Z34='	-@mv $(PTGZFILE) newbin/'
			REDWING='yes'
			;;
esac

case "$os" in
	linux)
		BDYNAMIC="-Wl,-Bdynamic"
		BSTATIC="-Wl,-Bstatic"
		if [ "$MAKEWORLD_SH" = "" ] ; then
			LIBSET='$(LIBSO) $(LIB)'
		fi
		;;
	freebsd)
		BDYNAMIC="-Wl,-Bdynamic"
		BSTATIC="-Wl,-Bstatic"
		;;
	macosx*|rhapsody)
		SFLAG='-Wl,-x'
		;;
esac

changequote([, ])dnl

AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(STATIC)
AC_SUBST(SFLAG)
AC_SUBST(LIBS)
AC_SUBST(DEFS)
AC_SUBST(MAKE)
AC_SUBST(Z31)
AC_SUBST(Z32)
AC_SUBST(Z33)
AC_SUBST(Z34)
AC_SUBST(LIBSET)
AC_SUBST(BDYNAMIC)
AC_SUBST(BSTATIC)
AC_SUBST(MAINDIR)

if test -f ./samples/ncftpput2probe.c ; then
	PROBE="ncftpget2probe ncftpput2probe"
else
	PROBE=""
fi
AC_SUBST(PROBE)

AC_PROG_CC
wi_REQUEST_NO_Y2K_WARNINGS
wi_CFLAGS_LFS64
wi_DEFINE_UNAME

if test -f /usr/src/include/eos.h ; then
	if test "$REDWING" = no ; then
		prefix=/usr/src
	fi
fi

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

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

dnl For the sample programs.
wi_NET_LIBS

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


dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h unistd.h utime.h nserve.h resolv.h arpa/nameser.h)
AC_TIME_WITH_SYS_TIME

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_TYPE_OFF_T
AC_TYPE_MODE_T
AC_TYPE_PID_T
AC_TYPE_UID_T
wi__RES_DEFDNAME
wi_USE_LONG_LONG

dnl Checks for library functions.
if test "$SYS" != sunos ; then
	# Use getwd on SunOS -- getcwd does a "popen("/bin/pwd")" -- aaaccck.
	#
	AC_CHECK_FUNCS(getcwd)
fi
AC_CHECK_FUNCS(getwd gethostname socket strerror strstr)
AC_CHECK_FUNCS(getpass strcasecmp getdomainname mktime symlink inet_ntop)
AC_CHECK_FUNCS(res_init snprintf vsnprintf strtoq sigaction waitpid)
AC_CHECK_FUNCS(open64 stat64 fstat64 lstat64 lseek64 llseek)
wi_FUNC_SIGSETJMP
AC_FUNC_SETVBUF_REVERSED

wi_HEADER_SYS_SELECT_H
AC_FUNC_SELECT_ARGTYPES

if test -f ../sio/sio.h ; then
	ldir=`cd ../sio ; pwd`
	LDFLAGS="$LDFLAGS -L${ldir}"
	CPPFLAGS="$CPPFLAGS -I${ldir}"
	LIBS="$LIBS -lsio"
fi

if test -f ../Strn/Strn.h ; then
	ldir=`cd ../Strn ; pwd`
	LDFLAGS="$LDFLAGS -L${ldir}"
	CPPFLAGS="$CPPFLAGS -I${ldir}"
	LIBS="$LIBS -lStrn"
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

wi_PROG_TAR
AC_PROG_RANLIB

if test "$LONGEST_INT" = "long long" ; then
	if sed 's/^#define longest_int.*/#define longest_int long long/;
s/^#define longest_uint.*/#define longest_uint unsigned long long/' ncftp.h > temp.h ; then
		mv temp.h ncftp.h
		chmod a+r ncftp.h
	fi
else
	if sed 's/^#define longest_int.*/#define longest_int long/;
s/^#define longest_uint.*/#define longest_uint unsigned long/' ncftp.h > temp.h ; then
		mv temp.h ncftp.h
		chmod a+r ncftp.h
	fi
fi

CPPFLAGS="-I$MAINDIR $CPPFLAGS"

if test "$no_signals" = no ; then
	dv1='#	if 0'
	dv2='#if 0'
else
	dv1='#	if 1'
	dv2='#if 1'
fi

if sed "s!^.*/. %config1!${dv1} /* %config1!;s!^.*/. %config2!${dv2} /* %config2!" ncftp.h > temp.h ; then
	mv temp.h ncftp.h
	chmod a+r ncftp.h
fi

if sed "s!^.*/. %config1!${dv1} /* %config1!;s!^.*/. %config2!${dv2} /* %config2!" syshdrs.h > temp.h ; then
	mv temp.h syshdrs.h
	chmod a+r syshdrs.h
fi

changequote(<<, >>)dnl
#
# Configure sio specially, like it would do.
#
if [ -d ../sio ] ; then
	if sed "s!^.*/. %config1!${dv1} /* %config1!;s!^.*/. %config2!${dv2} /* %config2!" ../sio/sio.h > temp.h ; then
		mv temp.h ../sio/sio.h
		chmod a+r ../sio/sio.h
	fi
	if sed "s!^.*/. %config1!${dv1} /* %config1!;s!^.*/. %config2!${dv2} /* %config2!" ../sio/usio.h > temp.h ; then
		mv temp.h ../sio/usio.h
		chmod a+r ../sio/usio.h
	fi

	patterns1=""
	patterns2=""
	if [ "$SYS" = solaris ] ; then
		patterns1='s!/. %configure%.*!#define SAccept SAcceptS!'
		patterns2='s!/. %configure%.*!#define UAccept UAcceptS!'
	fi

	if [ "$patterns1" != "" ] ; then
		sed "$patterns1" < ../sio/sio.h > tmpfile
		if [ $? -eq 0 ] ; then
			mv tmpfile ../sio/sio.h
			chmod 644 ../sio/sio.h
		else
			/bin/rm tmpfile
		fi
	fi

	if [ "$patterns2" != "" ] ; then
		sed "$patterns2" < ../sio/usio.h > tmpfile
		if [ $? -eq 0 ] ; then
			mv tmpfile ../sio/usio.h
			chmod 644 ../sio/usio.h
		else
			/bin/rm tmpfile
		fi
	fi
fi
changequote([, ])dnl

if test -d bin.only ; then
	binonly="bin.only/Makefile bin.only/samples/Makefile"
else
	binonly=""
fi

LIBS=`echo "$LIBS" | sed 's/^ *//;s/ *$//;s/  */ /g'`
LDFLAGS=`echo "$LDFLAGS" | sed 's/^ *//;s/ *$//;s/  */ /g'`
CPPFLAGS=`echo "$CPPFLAGS" | sed 's/^ *//;s/ *$//;s/  */ /g'`
CFLAGS=`echo "$CFLAGS" | sed 's/^ *//;s/ *$//;s/  */ /g'`
DEFS=`echo "$DEFS" | sed 's/^ *//;s/ *$//;s/  */ /g'`

samples=''
for sample in minincftp monkey ncftpget ncftpput ncftpls simpleget ncftpsyncput misc ; do
	if test -f samples/$sample/Makefile.in ; then
		samples="$samples samples/$sample/Makefile"
	fi
done

AC_OUTPUT([Makefile ../Strn/Makefile ../sio/Makefile $samples $binonly])
