dnl Process this file with autoconf to produce a configure script.
AC_INIT(super.h)

AC_CONFIG_HEADER(config.h)
 
AC_ARG_ENABLE(pam,     [  --disable-pam           don't include PAM support even if compile host has PAM])
AC_PROG_CC

Uname=unknown
Major=0
Minor=0
for d in /bin /usr/bin /sbin /usr/sbin ; do
    if test -f $d/uname ; then
	Uname=`$d/uname`
	rev=`$d/uname -r`
	break
    fi
done

if test "$Uname" = HP-UX ; then
    AC_DEFINE(_HPUX_SOURCE)
    case "$CC" in
    gcc )
	CPP="${CC} -E -w"
	;;
    * )
	CPP="${CC} -E -Ae -w"
	CFLAGS="${CFLAGS} -Ae"
	;;
    esac
    changequote(<<<,>>>)dnl
    Major=`expr "$rev" : '.*\.\([0-9][0-9]*\)\..*'`
    Minor=`expr "$rev" : '.*\.[0-9][0-9]*\.\([0-9][0-9]*\).*'`
    changequote([,])dnl
    AC_DEFINE_UNQUOTED(HPUX_MAJOR,$Major)
    AC_DEFINE_UNQUOTED(HPUX_MINOR,$Minor)

elif test "$Uname" = Linux ; then
    AC_DEFINE(_BSD_SOURCE)

elif test "$Uname" = OSF1 ; then
    AC_DEFINE(_OSF_SOURCE)
    AC_DEFINE(Digital_UNIX)

elif test "$Uname" = SunOS ; then
    changequote(<<<,>>>)dnl
    Major=`/bin/expr "$rev" : '^\([0-9][0-9]*\)\.'`
    Minor=`/bin/expr "$rev" : '^[0-9][0-9]*\.\([0-9][0-9]*\)'`
    changequote([,])dnl
    test "$Major" = 5 && AC_DEFINE(SUNOS5)
    AC_DEFINE_UNQUOTED(SUNOS_MAJOR,$Major)
    AC_DEFINE_UNQUOTED(SUNOS_MINOR,$Minor)

elif test "$Uname" = Darwin ; then
    changequote(<<<,>>>)dnl
    Major=`/bin/expr "$rev" : '^\([0-9][0-9]*\)\.'`
    Minor=`/bin/expr "$rev" : '^[0-9][0-9]*\.\([0-9][0-9]*\)'`
    changequote([,])dnl

elif test "$Uname" = OpenBSD ; then
    changequote(<<<,>>>)dnl
    Major=`/bin/expr "$rev" : '^\([0-9][0-9]*\)\.'`
    Minor=`/bin/expr "$rev" : '^[0-9][0-9]*\.\([0-9][0-9]*\)'`
    changequote([,])dnl

elif test "$Uname" = AIX ; then
    Major=`/bin/uname -v`
    Minor=`/bin/uname -r`
    # Ensure we are looking at just a numeric value, just in case
    # IBM decides to add some letters or dots or underscores...
    changequote(<<<,>>>)dnl
    Major=`/bin/expr "$Major" : '^[^0-9]*\([0-9][0-9]*\)'`
    Minor=`/bin/expr "$Minor" : '\([0-9][0-9]*\)[^0-9]*$'`
    changequote([,])dnl
    AC_DEFINE_UNQUOTED(AIX_MAJOR,$Major)
    AC_DEFINE_UNQUOTED(AIX_MINOR,$Minor)
fi

AC_PROG_INSTALL

AC_HAVE_HEADERS(errno.h stdlib.h limits.h string.h standards.h \
	netdb.h locale.h auth.h hpsecurity.h prot.h shadow.h \
	fcntl.h regex.h sgtty.h unistd.h \
	memory.h malloc.h pwdadj.h sgtty.h sys/time.h stdarg.h syslog.h \
	sys/ioctl.h termio.h termios.h \
	arpa/inet.h net/route.h net/if.h netinet/in.h \
	sys/types.h sys/bsdtypes.h sys/label.h sys/audit.h \
	sys/filio.h sys/wait.h sys/param.h sys/security.h \
	sys/socket.h sys/sysinfo.h sys/systeminfo.h sys/utsname.h )

# In case the user said --without-pam instead of --disable-pam:
if test "$with_pam" = "no" ; then
    enable_pam=no
fi
if test "$enable_pam" != "no" ; then
    AC_HAVE_HEADERS(security/pam_appl.h security/pam_misc.h)
fi

AC_AIX
AC_MINIX


AC_CHECKING(for directories to include in the safe path)
SafePath=/bin:/usr/bin
test -d /usr/ucb && SafePath=${SafePath}:/usr/ucb
test -d /usr/bsd && SafePath=${SafePath}:/usr/bsd
AC_DEFINE_UNQUOTED(SAFE_PATH, "${SafePath}")

# syslog may be in one of the following.  They may also be needed for
# other reasons, so we just blindly check for them all.
AC_CHECK_LIB(bsd, main)
AC_CHECK_LIB(socket, main)
AC_CHECK_LIB(inet, main)

badredhatpam=0
if test "$enable_pam" != "no" ; then
    # RH 7.2 has messed up libpam's installation: it supplies with
    # libpam.so.0, but not libpam.so.  Ditto libpam_misc.
    test -f /etc/redhat-release -a -f /lib/libpam.so.0 \
    	-a ! -f /lib/libpam.so && badredhatpam=1

    # -ldl may be needed by -lpam.
    AC_CHECK_LIB(dl, main)
    AC_CHECK_LIB(pam, main)
    AC_CHECK_LIB(pam_misc, main)
fi

# Pick up libcrypt if it exists and this OS needs it.
# (Note that on some OS's, libcrypt does not contain the
# correct crypt(); instead, libc contains the desired crypt.)
if test "$Uname" = SunOS -a $Major = 5 -a $Minor -ge 9 ; then
    : # SunOS >= 5.9: don't link with -lcrypt
elif test "$Uname" = OpenBSD ; then
    : # don't link with -lcrypt
elif test "$Uname" = Darwin ; then
    : # don't link with -lcrypt
else
    AC_CHECK_LIB(crypt, main)
fi

if test "$enable_pam" != "no" ; then
    AC_CHECK_FUNCS(pam_start)
    test "$ac_cv_func_pam_start" = yes && AC_REPLACE_FUNCS(misc_conv)
fi

AC_CHECK_FUNCS(fileno)
AC_CHECK_FUNCS(gtty)
AC_CHECK_FUNCS(syslog)
AC_CHECK_FUNCS(strdup)

AC_CHECK_FUNCS(initgroups)
for lib in os ; do
    test "$ac_cv_func_initgroups" = yes && break
    AC_CHECK_LIB($lib, initgroups)
done

# On IRIX, -lmalloc avoids a problem with default malloc
# that causes a core dump.
AC_CHECK_LIB(malloc, main)

# May be needed on IRIX
AC_CHECK_LIB(sun, getpwnam)

# Needed on SysV for shadow passwords.
AC_CHECK_LIB(sec, main)
 
# Needed on Digital Unix for shadow passwords.
AC_CHECK_LIB(security, main)
 
# Needed on Linux for shadow passwords.
AC_CHECK_LIB(shadow, main)
 
# Needed on Solaris
AC_CHECK_LIB(nsl, main)
 
# Needed on SCO-ODT-3.0 for crypt.  
AC_CHECK_LIB(ufc, main)
 
# We want these before the checks, so the checks can modify their values.
test -z "$CFLAGS" && CFLAGS=-g auto_cflags=1

# If the user hasn't specified CFLAGS, and we're using gcc, use -O.
test -n "$auto_cflags" && test -n "$GCC" && CFLAGS="$CFLAGS -O"
# If we're on AIX, not using gcc, add the ansi spec.
test "$Uname" = AIX && test -z "$GCC" && CFLAGS="$CFLAGS -qlanglvl=ansi"

# If system supports "const", define ConstDef to be "", but if
# the system doesn't support "const", define ConstDef to be "-Dconst=".
# That makes it suitable for use in the CFLAGS.
AC_C_CONST
ConstDef=		
test "$ac_cv_c_const" != yes && ConstDef="-Dconst="

AC_SYS_LONG_FILE_NAMES
AC_SYS_INTERPRETER
test "$ac_cv_sys_interpreter" != yes && AC_DEFINE(INTERPRETER_HACK)
AC_STDC_HEADERS

AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_GETGROUPS
AC_TYPE_SIZE_T

AC_STRUCT_TM
AC_TIME_WITH_SYS_TIME

void_ok=no
AC_MSG_CHECKING(if func signal is compatible with declaring type void)
AC_TRY_COMPILE([
#include <stdio.h>
#include <signal.h>
],
changequote(<<,>>)dnl
<<void (*signal())();
signal(1, 0);>>,
changequote([,])dnl
	AC_DEFINE(HAVE_VOID_SIGNAL) void_ok=yes ; AC_MSG_RESULT(yes))
if test $void_ok = no ; then
AC_TRY_COMPILE([
#include <stdio.h>
#include <sys/signal.h>
],
changequote(<<,>>)dnl
<<void (*signal())();
signal(1, 0);>>,
changequote([,])dnl
	AC_DEFINE(HAVE_VOID_SIGNAL) void_ok=yes ; AC_MSG_RESULT(yes))
fi

AC_MSG_CHECKING(if sys_errlist is defined)
AC_TRY_COMPILE([
#include <errno.h>
#include <stdio.h>
],
changequote(<<,>>)dnl
<<extern char *sys_errlist[];
fputs(sys_errlist[0], stdout);>>,
changequote([,])dnl
	AC_DEFINE(HAVE_SYS_ERRLIST) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(if errno must be explicitly declared extern)
AC_TRY_COMPILE([
#include <errno.h>
#include <stdio.h>
],
changequote(<<,>>)dnl
<<printf("%d", errno);>>,
changequote([,])dnl
	AC_DEFINE(DONT_DECL_ERRNO) AC_MSG_RESULT(no), AC_MSG_RESULT(yes))

AC_MSG_CHECKING(if regex library is POSIX)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <regex.h>
],
changequote(<<,>>)dnl
<<
regex_t preg;
char buf[100];
regmatch_t pmatch[1];
regcomp(&preg, "a pattern", REG_EXTENDED|REG_ICASE|REG_NOSUB|REG_NEWLINE);
regexec(&preg, "a string", 1, pmatch, REG_NOTBOL|REG_NOTEOL);
regerror(1, &preg, buf, sizeof(buf));
regfree(&preg);
>>,
changequote([,])dnl
	AC_DEFINE(HAVE_POSIX_REGEX) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(if compiler supports enum)
AC_TRY_COMPILE(, [enum { red, green, blue } color;],
		AC_DEFINE(HAVE_ENUM) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_MSG_CHECKING(if close-on-exec can be done via ioctl())
AC_TRY_COMPILE([
#include <sgtty.h>
#include <stdio.h>
],
changequote(<<,>>)dnl
<<ioctl(0, FIOCLEX, NULL);>>,
changequote([,])dnl
	AC_DEFINE(HAVE_IOCTL_FIOCLEX) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_FUNC_VPRINTF

if test "$enable_pam" != "no" ; then
    AC_CHECK_FUNCS(pam_strerror)
fi

AC_CHECK_FUNCS(strerror strtol memcpy memset strerror setreuid setregid \
	getgroups innetgr iscomsec localtime sysconf sysinfo getdtablesize \
	getdomainname sysinfo tcgetattr tcsetattr uname)

# If we're enabling pam support (if PAM available), include pam.o in AC_LIBOBJ.
# Note that if we don't have pam_start() on this system, pam.o will still
# build correctly for a PAM-less system.  In other words, include pam.o
# unless the user included the configure option "--disable-pam".
if test "$enable_pam" != "no" ; then
    AC_LIBOBJ([pam])
    with_pam=1
else
    with_pam=0
fi

AC_SUBST(libdir)dnl
AC_SUBST(bindir)dnl
AC_SUBST(SafePath)dnl
AC_SUBST(ConstDef)dnl
AC_SUBST(with_pam)dnl

AC_OUTPUT(Makefile)

test "$badredhatpam" = 1 && {
    echo "#"
    echo "# WARNING."
    echo "#"
    echo "# You have /lib/libpam.so.0 but not libpam.so."
    echo "# In Redhat 7.{1,2}, this is bugid 55651, and is fixed in rpm"
    echo "# pam-0.75-18.7.  You should get that rpm to fix several pam bugs,"
    echo "# but for now you should become root, and make these links by hand:"
    echo "# ln -s /lib/libpam.so.0 /lib/libpam.so"
    echo "# ln -s /lib/libpam_misc.so.0 /lib/libpam_misc.so"
    echo "#"
    echo "# Then, rm config.cache and rerun configure."
    echo "#"
    echo "#"
}

# The test command will give a non-zero exit code if this isn't
# a bad-redhat-pam system.  So use 'exit 0' to make everyone happy.
exit 0
