dnl configure.in: Input for Autoconf
dnl
dnl %%% portions-copyright-cmetz
dnl Portions of this software are Copyright 1996 by Craig Metz, All Rights
dnl Reserved. The Inner Net License Version 2 applies to these portions of
dnl the software.
dnl You should have received a copy of the license with this software. If
dnl you didn't get a copy, you may request one from <license@inner.net>.
dnl
dnl Portions of this software are Copyright 1995 by Randall Atkinson and Dan
dnl McDonald, All Rights Reserved. All Rights under this copyright are assigned
dnl to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
dnl License Agreement applies to this software.   
dnl
dnl	History:
dnl
dnl     Modified by cmetz for OPIE 2.3. Removed redundant memset/memcpy.
dnl	     Changed ls -g test around. Changed logindevperm/fbtab defines.
dnl	     Added check for /etc/environment and /etc/src.sh. Check for
dnl          /var/adm/sulog. Check for {get,put}utxline, provide libmissing
dnl          versionf of {get,put}utline. Added --enable option for anonymous
dnl          FTP. Got rid of a few unneeded checks. Check for functions only
dnl	     used by libmissing only if the replacement function that needs
dnl	     them is itself needed.
dnl	Modified by cmetz for OPIE 2.22. Check for Solaris drain bamaged ls.
dnl          Check for setlogin(). Removed duplicate checks for some funcs.
dnl     Modified by cmetz for OPIE 2.21. Filename must be in utmp[x]/wtmp[x]
dnl          defines.
dnl     Modified by cmetz for OPIE 2.2. Misc changes. Changed for libmissing
dnl          support and building its target object list. Changed to support
dnl          FUNCTION declaration et al. Added a LOT of checks and a LOT of
dnl          fixes.
dnl	Created at NRL for OPIE 2.1.

AC_INIT(README)
AC_CONFIG_HEADER(config.h)
AC_ARG_ENABLE(access-file, [  --enable-access-file=PATH  Use the OPIE access file PATH as a transition aid], AC_DEFINE_UNQUOTED(PATH_ACCESS_FILE, "$enable_access_file") echo "Using the access file in $enable_access_file -- don't say we didn't warn you!")
AC_ARG_ENABLE(anonymous-ftp, [  --enable-anonymous-ftp  Enable anonymous FTP support], AC_DEFINE(DOANONYMOUS) echo "Enabling anonymous FTP support in ftp -- don't say we didn't warn you!")

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_LN_S
AC_PROG_RANLIB
AC_PROG_YACC

AC_AIX
AC_ISC_POSIX
AC_MINIX

AC_C_CROSS

dnl We'd put PATH in these checks, but it turns out that autoconf doesn't
dnl work as documented when it comes to the colon separator...

AC_PATH_PROG(CHOWN, chown, /bin/chown, /usr/bin /bin /usr/sbin /sbin /usr/etc /etc)

AC_PATH_PROG(SU, su, /bin/su, /usr/bin /bin)
AC_PATH_PROG(ALT_SU, su,, /usr/sbin /sbin)

AC_PATH_PROG(SCHEME, scheme,, /usr/lib/iaf/scheme)
AC_PATH_PROG(LOGIN, login, /bin/login, /usr/bin /bin)
dnl AC_DEFINE_UNQUOTED(PATH_LOGIN, "$LOGIN")

if test ! -z "$SCHEME";
then
	LOGIN="$SCHEME";
fi

AC_PATH_PROG(FTPD, ftpd,, /usr/libexec /usr/etc /etc /usr/sbin /sbin /usr/lbin)
AC_PATH_PROG(INFTPD, in.ftpd,, /usr/libexec /usr/etc /etc /usr/sbin /sbin /usr/lbin)

if test -z "$FTPD"
then
	if test ! -z "$INFTPD"
	then
		FTPD="$INFTPD"
	fi
fi

AC_MSG_CHECKING(for default PATH entries)
default_path=""
save_IFS="$IFS"
IFS=" "
for i in /usr/bin /bin /usr/ucb /usr/sbin /usr/bsd /sbin /usr/bin/X11 /etc /usr/local/X11/bin /usr/X11R6/bin /your-system-is-broken
do
	IFS=":"
	for j in $PATH
	do
		if test "$i" = "$j"
		then
			if test -d "$i"
			then
				if test -z "$default_path"
				then
					default_path="$i"
				else
					default_path="$default_path:$i"
				fi
			fi
		fi
	done
	IFS=" "
done
AC_DEFINE_UNQUOTED(DEFAULT_PATH, "$default_path")
AC_MSG_RESULT($default_path)

AC_MSG_CHECKING(for test -e flag)
if sh config.testeflag
then
	result=yes
	EXISTS="-e"
else
	result=no
	EXISTS="-f"
fi
AC_SUBST(EXISTS)
AC_MSG_RESULT($result)

AC_MSG_CHECKING(for mkdir -p flag)
if test -d config.tmpdir
then
	rmdir config.tmpdir/foo/bar >/dev/null 2>/dev/null
	rmdir config.tmpdir/foo >/dev/null 2>/dev/null
	rmdir config.tmpdir >/dev/null 2>/dev/null
fi

result=no
if mkdir -p config.tmpdir/foo/bar >/dev/null 2>/dev/null
then
	if test -d config.tmpdir
	then
		if test -d config.tmpdir/foo
		then
			if test -d config.tmpdir/foo/bar
			then
				result=yes
				rmdir config.tmpdir/foo/bar >/dev/null 2>/dev/null
			fi
			rmdir config.tmpdir/foo >/dev/null 2>/dev/null
		fi
		rmdir config.tmpdir >/dev/null 2>/dev/null
	fi
fi

if test "$result" = yes
then
	MKDIR="mkdir -p"
else
	MKDIR="mkdir"
fi
AC_SUBST(MKDIR)
AC_MSG_RESULT($result)

AC_MSG_CHECKING(for ls group field)
lsg=`/bin/ls -ldg / | wc -w | awk '{print $1}'`;
ls=`/bin/ls -ld / | wc -w | awk '{print $1}'`;
result="no"
if test $ls = 9;
then
	result="yes"
else
	if test "$ls" = 8 -a "$lsg" = 9;
	then
		result="yes, with -g"
		AC_DEFINE(HAVE_LS_G_FLAG)
	fi
fi
AC_MSG_RESULT($result)

dnl Checks for various system characteristics
AC_MSG_CHECKING(for /etc/default/login)
if test $EXISTS /etc/default/login
then
	result=yes
	AC_DEFINE(HAVE_ETC_DEFAULT_LOGIN)
else
	result=no
fi
AC_MSG_RESULT($result)

AC_MSG_CHECKING(for /etc/securetty)
if test $EXISTS /etc/securetty
then
	result=yes
	AC_DEFINE(HAVE_SECURETTY)
else
	result=no
fi
AC_MSG_RESULT($result)

AC_MSG_CHECKING(for /etc/logindevperm)
if test $EXISTS /etc/logindevperm
then
	AC_MSG_RESULT(yes)
	AC_DEFINE(HAVE_LOGIN_PERMFILE, "/etc/logindevperm")
else
	AC_MSG_RESULT(no)

	AC_MSG_CHECKING(for /etc/fbtab)
	if test $EXISTS /etc/fbtab
	then
		result=yes
		AC_DEFINE(HAVE_LOGIN_PERMFILE, "/etc/fbtab")
	else
		result=no
	fi
	AC_MSG_RESULT($result)
fi

AC_MSG_CHECKING(for /etc/environment)
if test $EXISTS /etc/environment
then
	AC_MSG_RESULT(yes)
	AC_DEFINE(HAVE_LOGIN_ENVFILE, "/etc/environment")
else
	AC_MSG_RESULT(no)

	AC_MSG_CHECKING(for /etc/src.sh)
	if test $EXISTS /etc/src.sh
	then
		result=yes
		AC_DEFINE(HAVE_LOGIN_ENVFILE, "/etc/src.sh")
	else
		result=no
	fi
	AC_MSG_RESULT($result)
fi

AC_MSG_CHECKING(for /etc/shadow)
if test $EXISTS /etc/shadow
then
	result=yes
	AC_DEFINE(HAVE_ETC_SHADOW)
else
	result=no
fi
AC_MSG_RESULT($result)

AC_MSG_CHECKING(for /var/adm/sulog)
if test $EXISTS /var/adm/sulog
then
	result=yes
	AC_DEFINE(HAVE_SULOG)
else
	result=no
fi
AC_MSG_RESULT($result)

AC_MSG_CHECKING(mail spool location)
mail_spool=""
for i in /var/mail /usr/mail /var/spool/mail /usr/spool/mail
do
	if test -d $i
	then
		mail_spool="$i"
	fi
done
if test -z "$mail_spool"
then
	result="not found"
else
	result="$mail_spool"
	AC_DEFINE_UNQUOTED(PATH_MAIL, "$mail_spool")
fi
AC_MSG_RESULT($result)

dnl AC_MSG_CHECKING(where your system puts the utmp file)
dnl utmp_path=""
dnl for i in /var/run /var/adm /usr/adm /etc
dnl do
dnl	if test $EXISTS $i/utmp
dnl	then
dnl		utmp_path="$i"
dnl	fi
dnl done
dnl if test -z "$utmp_path"
dnl then
dnl	result="not found"
dnl else
dnl	result="$utmp_path"
dnl	AC_DEFINE_UNQUOTED(PATH_UTMP_AC, "$utmp_path/utmp")
dnl fi
dnl AC_MSG_RESULT($result)

dnl AC_MSG_CHECKING(where your system puts the wtmp file)
dnl wtmp_path=""
dnl for i in /var/run /var/adm /usr/adm /etc
dnl do
dnl	if test $EXISTS $i/wtmp
dnl	then
dnl		wtmp_path="$i"
dnl	fi
dnl done
dnl if test -z "$wtmp_path"
dnl then
dnl	result="not found"
dnl else
dnl	result="$wtmp_path"
dnl	AC_DEFINE_UNQUOTED(PATH_WTMP_AC, "$wtmp_path/wtmp")
dnl fi
dnl AC_MSG_RESULT($result)

AC_MSG_CHECKING(whether the system profile displays the motd)
result=no
if test $EXISTS /etc/profile
then
	if grep motd /etc/profile >/dev/null 2>/dev/null
	then
		result=yes
	fi
fi
if test "$result" = yes
then
	AC_DEFINE(HAVE_MOTD_IN_PROFILE)
fi
AC_MSG_RESULT($result)

AC_MSG_CHECKING(whether the system profile checks for mail)
result=no
if test $EXISTS /etc/profile
then
	if grep 'mail\.' /etc/profile >/dev/null 2>/dev/null
	then
		result=yes
	fi
fi
if test "$result" = yes
then
	AC_DEFINE(HAVE_MAILCHECK_IN_PROFILE)
fi
AC_MSG_RESULT($result)

dnl Random checks
AC_C_CONST

AC_MSG_CHECKING(to see if your compiler can handle void arguments)
AC_TRY_COMPILE(foo(void) { },, AC_DEFINE(HAVE_VOIDARG) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if your compiler can handle void return values)
AC_TRY_COMPILE(void foo() { },, AC_DEFINE(HAVE_VOIDRET) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if your compiler can handle void pointers)
AC_TRY_COMPILE(foo() { void *bar = (void *)0x42; bar = bar + 1; },, AC_DEFINE(HAVE_VOIDPTR) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if your compiler can handle ANSI argument lists)
AC_TRY_COMPILE(int foo(int bar, int baz) { return 0; },, AC_DEFINE(HAVE_ANSIDECL) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if your compiler can handle ANSI prototypes)
AC_TRY_COMPILE(extern int foo(int, int);,, AC_DEFINE(HAVE_ANSIPROTO) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if your compiler can handle ANSI variable arguments)
AC_TRY_COMPILE([#include <stdarg.h>
int foo(int arg, ...) {
	va_list ap;
	va_start(ap, arg);
	va_end(ap);
	return 0;
}],, AC_DEFINE(HAVE_ANSISTDARG) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(to see if you have an ANSI-style sprintf)
AC_TRY_RUN([#include <stdio.h>
int main(argc, argv)
int argc;
char *argv[];
{
  char buf[5];
  int i = 2;
  i += sprintf(buf, "1234");
  return (i == 6) ? 0 : -1;
}], AC_DEFINE(HAVE_ANSISPRINTF) AC_MSG_RESULT(yes), AC_MSG_RESULT(no), AC_MSG_RESULT(no))

dnl Checks for libraries.
AC_CHECK_LIB(crypt, crypt)
AC_CHECK_LIB(nsl, gethostname)
AC_CHECK_LIB(posix, main)
AC_CHECK_LIB(socket, socket)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(crypt.h fcntl.h limits.h termios.h sys/file.h sys/ioctl.h sys/time.h syslog.h unistd.h paths.h shadow.h signal.h sys/signal.h lastlog.h sys/utsname.h pwd.h sys/param.h string.h stdlib.h utmpx.h)

dnl Checks for typedefs, structures, and compiler characteristics.
dnl AC_TYPE_UID_T
dnl AC_TYPE_OFF_T
dnl AC_TYPE_PID_T
dnl AC_STRUCT_ST_BLKSIZE
dnl AC_STRUCT_TM

AC_MSG_CHECKING(for ut_pid in struct utmp)
AC_TRY_COMPILE([#include <sys/types.h>
#include <utmp.h>], [struct utmp foo; return (int)foo.ut_pid;], AC_DEFINE(HAVE_UT_PID) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
AC_MSG_CHECKING(for ut_type in struct utmp)
AC_TRY_COMPILE([#include <sys/types.h>
#include <utmp.h>], [struct utmp foo; return (int)foo.ut_type;], AC_DEFINE(HAVE_UT_TYPE) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
AC_MSG_CHECKING(for ut_name in struct utmp)
AC_TRY_COMPILE([#include <sys/types.h>
#include <utmp.h>], [struct utmp foo; return (int)foo.ut_name[0];], AC_DEFINE(HAVE_UT_NAME) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
dnl AC_MSG_CHECKING(for ut_user in struct utmp)
dnl AC_TRY_COMPILE([#include <sys/types.h>
dnl #include <utmp.h>], [struct utmp foo; return (int)foo.ut_user[0];], AC_DEFINE(HAVE_UT_USER) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
AC_MSG_CHECKING(for ut_time in struct utmp)
AC_TRY_COMPILE([#include <sys/types.h>
#include <utmp.h>], [struct utmp foo; return (int)foo.ut_time;], AC_DEFINE(HAVE_UT_TIME) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
AC_DEFINE(HAVE_UT_HOST)

dnl Checks for library functions.
dnl AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(gettimeofday socket strftime strstr setpriority seteuid setreuid setegid setregid setresuid setresgid getttynam sysconf setvbuf getspent endspent setgroups getgroups fpurge setlogin lstat getutxline pututxline)

dnl Libmissing...
AC_FUNC_MEMCMP
AC_FUNC_ALLOCA
AC_REPLACE_FUNCS(getusershell sigaddset sigemptyset strerror strncasecmp)
MISSING="$LIBOBJS $ALLOCA "
AC_CHECK_FUNC(atexit,, MISSING="${MISSING}atexit.o ")
AC_CHECK_FUNC(entutent,, MISSING="${MISSING}endutent.o ")
AC_CHECK_FUNC(memcpy,, MISSING="${MISSING}memcpy.o "; AC_CHECK_FUNCS(bcopy))
AC_CHECK_FUNC(memset,, MISSING="${MISSING}memset.o "; AC_CHECK_FUNCS(bzero))
AC_CHECK_FUNC(getcwd,, MISSING="${MISSING}getcwd.o "; AC_CHECK_FUNCS(getwd))
AC_CHECK_FUNC(getenv, AC_DEFINE(HAVE_GETENV), MISSING="${MISSING}env.o ")
AC_CHECK_FUNC(getutline, AC_DEFINE(HAVE_GETUTLINE), MISSING="${MISSING}getutline.o "; AC_CHECK_FUNCS(ttyslot))
AC_CHECK_FUNC(pututline,, MISSING="${MISSING}pututline.o "; AC_CHECK_FUNCS(ttyslot))
AC_CHECK_FUNC(setenv, AC_DEFINE(HAVE_SETENV), MISSING="${MISSING}env.o ")
AC_CHECK_FUNC(sigprocmask,, MISSING="${MISSING}sigprocmask.o "; AC_CHECK_FUNCS(sigblock sigsetmask))
AC_CHECK_FUNC(strchr,, MISSING="${MISSING}strchr.o "; AC_CHECK_FUNCS(index))
AC_CHECK_FUNC(strrchr,, MISSING="${MISSING}strrchr.o "; AC_CHECK_FUNCS(rindex))
AC_CHECK_FUNC(sysconf,, MISSING="${MISSING}sysconf.o "; AC_CHECK_FUNCS(getdtablesize))
AC_CHECK_FUNC(uname,, MISSING="${MISSING}uname.o "; AC_CHECK_FUNCS(gethostname))
AC_CHECK_FUNC(unsetenv, AC_DEFINE(HAVE_UNSETENV), MISSING="${MISSING}env.o ")
AC_SUBST(MISSING)

AC_MSG_CHECKING(for nonstandard gettimeofday)
AC_TRY_COMPILE([
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
],
[struct timeval tv;
gettimeofday(&tv, NULL)], AC_MSG_RESULT(no), AC_MSG_RESULT(maybe) AC_TRY_COMPILE([
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
],
[struct timeval tv;
gettimeofday(&tv)], AC_DEFINE(HAVE_ONE_ARG_GETTIMEOFDAY) AC_MSG_RESULT(yes), AC_MSG_RESULT(no)))

# Munge out LOCALBIN and LOCALMAN in canonical (no bletch) form
AC_OUTPUT(configure.munger libmissing/Makefile libopie/Makefile Makefile.munge:Makefile.in)
sh configure.munger
