dnl
dnl *** PLEASE NOTE ***
dnl *** PLEASE NOTE ***
dnl *** PLEASE NOTE ***
dnl
dnl Update $VERSION before making a new release
dnl

dnl Process this file with autoconf to produce a configure script.
dnl
AC_REVISION($Revision: 1.319 $)
AC_INIT(lib/krb/getrealm.c)
AC_CONFIG_HEADER(include/config.h)

dnl
dnl definitions
dnl

PACKAGE=krb4
AC_SUBST(PACKAGE)dnl
VERSION=0.9.8
AC_SUBST(VERSION)dnl
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE")dnl
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")dnl

# This may be overridden using --prefix=/usr to configure
AC_PREFIX_DEFAULT(/usr/athena)

AC_CANONICAL_HOST
CANONICAL_HOST=$host
AC_SUBST(CANONICAL_HOST)

AC_PROG_MAKE_SET
AC_ARG_PROGRAM

# We want these before the checks, so the checks can modify their values.
test -z "$LDFLAGS" && LDFLAGS=-g

dnl
dnl check for programs
dnl

AC_KRB_PROG_LN_S
AC_PROG_CC
AC_PROG_CPP
AC_ISC_POSIX
AC_KRB_PROG_YACC
AC_PROG_LEX
AC_PROG_RANLIB
dnl XXX ugly hack for brokeness of AIX installbsd
AC_CONFIG_AUX_DIR_DEFAULT
if test `uname` = AIX; then
	INSTALL="$ac_install_sh"
fi
AC_PROG_INSTALL
AC_PROG_AWK
AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo, :)

dnl
dnl check for build options
dnl

AC_TEST_PACKAGE(socks,socks.h,libsocks5.a,-lsocks5)
CFLAGS="$SOCKSINCLUDE $CFLAGS"
LIBS="$SOCKSLIB $LIBS"

dnl Check if we want to use shared libraries
AC_ARG_WITH(shared,
[  --with-shared           create shared libraries for Kerberos])

AC_ARG_WITH(cracklib,
[  --with-cracklib=dir     use the cracklib.a in dir],
)

AC_ARG_WITH(dictpath,
[  --with-dictpath=path    use this dictionary with cracklib]
)

(test -z "$with_cracklib" && test -n "$with_dictpath") ||
(test -n "$with_cracklib" && test -z "$with_dictpath") &&
AC_MSG_ERROR(--with-cracklib requires --with-dictpath and vice versa)
test -n "$with_cracklib" &&
CRACKLIB="-L$with_cracklib -lcrack" &&
AC_MSG_RESULT(Using cracklib in $with_cracklib)
AC_SUBST(CRACKLIB)dnl
test -n "$with_dictpath" &&
AC_MSG_RESULT(Using dictpath=$with_dictpath) &&
AC_DEFINE_UNQUOTED(DICTPATH,"$with_dictpath")

AC_ARG_WITH(mailspool,
[  --with-mailspool=dir    this is the mail spool directory]
)

test -n "$with_mailspool" &&
AC_DEFINE_UNQUOTED(KRB4_MAILDIR, "$with_mailspool")

AC_ARG_ENABLE(random-mkey,
[  --enable-random-mkey    use new code for master keys],[
if test "$enableval" = "yes"; then
	AC_DEFINE(RANDOM_MKEY,1)
fi
])

AC_ARG_WITH(mkey,
[  --with-mkey=file        where to put the master key],[
if test -n "$withval"; then
	AC_DEFINE_UNQUOTED(MKEYFILE,"$withval")
fi
])

otp=yes
AC_ARG_ENABLE(otp,
[  --disable-otp           if you don't want OTP support],
[
if test "$enableval" = "no"; then
	otp=no
fi
])

if test "$otp" = "yes"; then
	AC_DEFINE(OTP)
	LIB_otp='-L$(top_builddir)/lib/otp -lotp'
	OTP_dir=otp
	LIB_SUBDIRS="$LIB_SUBDIRS otp"
fi
AC_SUBST(LIB_otp)
AC_SUBST(OTP_dir)

mmap=yes
AC_ARG_ENABLE(mmap,
[  --disable-mmap          disable use of mmap],
[
if test "$enableval" = "no"; then
	mmap=no
fi
])
if test "$mmap" = "no"; then
	AC_DEFINE(NO_MMAP)
fi

berkeley_db=db
AC_ARG_WITH(berkeley-db,
[  --without-berkeley-db   if you don't want berkeley db],[
if test "$withval" = no; then
	berkeley_db=""
fi
])

afs_support=yes
AC_ARG_WITH(afs-support,
[  --without-afs-support   if you don't want support for afs],[
if test "$withval" = no; then
	AC_DEFINE(NO_AFS)
	afs_support=no
fi
])

des_quad=guess
AC_ARG_WITH(des-quad-checksum,
[  --with-des-quad-checksum=kind
                           default checksum to use (new, old, or guess)],[
des_quad="$withval"
])
if test "$des_quad" = "new"; then
	AC_DEFINE(DES_QUAD_DEFAULT,DES_QUAD_NEW)
elif test "$des_quad" = "old"; then
	AC_DEFINE(DES_QUAD_DEFAULT,DES_QUAD_OLD)
else
	AC_DEFINE(DES_QUAD_DEFAULT,DES_QUAD_GUESS)
fi	

AC_ARG_WITH(afsws,
[  --with-afsws=dir     use AFS includes and libraries from dir=/usr/afsws],
AFSWS=$withval,
AFSWS=/usr/afsws
)
test "$AFSWS" = "yes" && AFSWS=/usr/afsws
AC_SUBST(AFSWS)

# If we have AFS include file rx/rx.h build Rx security module rxkad
test -f $AFSWS/include/rx/rx.h && LIB_SUBDIRS="$LIB_SUBDIRS rxkad"
AC_SUBST(LIB_SUBDIRS)

AC_TEST_PACKAGE(readline,readline.h,libreadline.a,-lreadline)

dnl
dnl Shared library stuff has to be different everywhere
dnl

AC_SUBST(CFLAGS)dnl
AC_SUBST(LDFLAGS)dnl

case ${with_shared} in
  yes ) with_shared=yes;;
  no  ) with_shared=no;;
  *   ) with_shared=no;;
esac

# NOTE: Building shared libraries may not work if you do not use gcc!
#
# OS		$SHLIBEXT
# HP-UX		sl
# Linux		so
# NetBSD	so
# FreeBSD	so
# OSF		so
# SunOS5	so
# SunOS4	so.0.5
# Irix		so
#
# LIBEXT is the extension we should build (.a or $SHLIBEXT)
REAL_PICFLAGS="-fpic"
LDSHARED='$(CC) $(PICFLAGS) -shared'
LIBPREFIX=lib
REAL_SHLIBEXT=so
changequote({,})dnl
SHLIB_VERSION=`echo $VERSION | sed 's/\([0-9.]*\).*/\1/'`
changequote([,])dnl
case "${host}" in
*-*-hpux*)
	REAL_SHLIBEXT=sl
	REAL_LD_FLAGS='-Wl,+b$(libdir)'
	if test -z "$GCC"; then
		LDSHARED="ld -b"
		REAL_PICFLAGS="+z"
	fi
	;;
*-*-linux*)
	REAL_LD_FLAGS='-Wl,-rpath,$(libdir)'
	;;
*-*-*bsd*)
	REAL_SHLIBEXT=so.$SHLIB_VERSION
	LDSHARED='ld -Bshareable'
	REAL_LD_FLAGS='-Wl,-R$(libdir)'
	;;
*-*-osf*)
	REAL_LD_FLAGS='-Wl,-rpath,$(libdir)'
	REAL_PICFLAGS=
	LDSHARED='ld -shared -expect_unresolved \*'
	;;
*-*-solaris2*)
	REAL_LD_FLAGS='-Wl,-R$(libdir)'
	if test -z "$GCC"; then
		LDSHARED='$(CC) -G'
		REAL_PICFLAGS="-Kpic"
	fi
	;;
*-*-sunos*)
	REAL_SHLIBEXT=so.$SHLIB_VERSION
	REAL_LD_FLAGS='-Wl,-L$(libdir)'
	;;
*-*-irix*)
	REAL_LD_FLAGS='-Wl,-rpath,$(libdir)'
	REAL_PICFLAGS=
	;;
*-*-os2_emx*)
	LIBPREFIX=
	EXECSUFFIX='.exe'
	RANLIB=EMXOMF
	LD_FLAGS=-Zcrtdll
	REAL_SHLIBEXT=nobuild
	;;
*-*-cygwin32*)
	EXECSUFFIX='.exe'
	REAL_SHLIBEXT=nobuild
	;;
*)	REAL_SHLIBEXT=nobuild
	REAL_PICFLAGS= 
	;;
esac

if test "${with_shared}" != "yes" ; then 
 PICFLAGS=""
 SHLIBEXT="nobuild"
 LIBEXT="a"
else
 PICFLAGS="$REAL_PICFLAGS"
 SHLIBEXT="$REAL_SHLIBEXT"
 LIBEXT="$SHLIBEXT"
 LD_FLAGS="$REAL_LD_FLAGS"
fi

AC_SUBST(REAL_PICFLAGS) dnl
AC_SUBST(REAL_SHLIBEXT) dnl
AC_SUBST(REAL_LD_FLAGS) dnl

AC_SUBST(PICFLAGS) dnl
AC_SUBST(SHLIBEXT) dnl
AC_SUBST(LDSHARED) dnl
AC_SUBST(LD_FLAGS) dnl
AC_SUBST(LIBEXT) dnl
AC_SUBST(LIBPREFIX) dnl
AC_SUBST(EXECSUFFIX) dnl

dnl
dnl Check if we need to use weak-stuff
dnl

AC_HAVE_PRAGMA_WEAK

dnl
dnl Check for endian-ness, this breaks cross compilation
dnl
AC_C_BIGENDIAN

dnl
dnl Check for constness
dnl
AC_C_CONST

dnl
dnl Check for inline keyword
dnl
AC_C_INLINE

dnl
dnl Check for strange operating systems that you need to handle differently
dnl

AC_KRB_SYS_NEXTSTEP
AC_KRB_SYS_AIX

if test "$krb_cv_sys_aix" = yes ;then
	AFS_EXTRA_OBJS='$(srcdir)/afsl.exp dlfcn.o'
	AC_SUBST(AFS_EXTRA_OBJS)
	AFS_EXTRA_LIBS=afslib.so
	AC_SUBST(AFS_EXTRA_LIBS)
fi

#
# AIX needs /lib/pse.exp for getmsg, but alas that file is broken in
# AIX414
#

if test -f /lib/pse.exp ;then
	LIBS="$LIBS -Wl,-bI:/lib/pse.exp"
fi

dnl
dnl Various checks for headers and their contents
dnl

AC_HEADER_STDC

AC_CHECK_HEADERS(arpa/ftp.h arpa/inet.h arpa/nameser.h)
AC_CHECK_HEADERS(arpa/telnet.h bsd/bsd.h bsdsetjmp.h)
AC_CHECK_HEADERS(crypt.h curses.h dbm.h dirent.h err.h errno.h fcntl.h grp.h)
AC_CHECK_HEADERS(io.h lastlog.h limits.h login.h maillock.h ndbm.h net/if.h)
AC_CHECK_HEADERS(net/if_tun.h net/if_var.h netdb.h netinet/in.h)
AC_CHECK_HEADERS(netinet/in6_machtypes.h netinet/in_systm.h)
AC_CHECK_HEADERS(netinet/ip.h netinet/tcp.h paths.h pty.h pwd.h)
AC_CHECK_HEADERS(resolv.h rpcsvc/dbm.h sac.h security/pam_modules.h)
AC_CHECK_HEADERS(shadow.h siad.h signal.h stropts.h sys/bitypes.h)
AC_CHECK_HEADERS(sys/category.h sys/file.h sys/filio.h)
AC_CHECK_HEADERS(sys/ioccom.h sys/ioctl.h sys/locking.h sys/mman.h)
AC_CHECK_HEADERS(sys/param.h sys/proc.h sys/pty.h sys/ptyio.h sys/ptyvar.h)
AC_CHECK_HEADERS(sys/resource.h sys/select.h sys/socket.h)
AC_CHECK_HEADERS(sys/sockio.h sys/stat.h sys/str_tty.h sys/stream.h)
AC_CHECK_HEADERS(sys/stropts.h sys/strtty.h sys/syscall.h)
AC_CHECK_HEADERS(sys/sysctl.h sys/termio.h sys/time.h sys/timeb.h)
AC_CHECK_HEADERS(sys/times.h sys/tty.h sys/types.h sys/uio.h)
AC_CHECK_HEADERS(sys/un.h sys/utsname.h sys/wait.h syslog.h)
AC_CHECK_HEADERS(termio.h termios.h tmpdir.h ttyent.h udb.h ulimit.h)
AC_CHECK_HEADERS(unistd.h userpw.h usersec.h util.h utime.h utmp.h)
AC_CHECK_HEADERS(utmpx.h wait.h winsock.h)

AC_HEADER_TIME
AC_DECL_SYS_SIGLIST
AC_SUBST(ac_cv_header_err_h)dnl

AC_GROK_TYPES(int8_t int16_t int32_t int64_t)
AC_GROK_TYPES(u_int8_t u_int16_t u_int32_t u_int64_t)

AC_MSG_CHECKING(for strange sys/bitypes.h)
AC_CACHE_VAL(krb_cv_int8_t_ifdef, [
AC_TRY_COMPILE([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_BITYPES_H
#include <sys/bitypes.h>
#endif
#ifdef HAVE_NETINET_IN6_MACHTYPES_H
#include <netinet/in6_machtypes.h>
#endif
],
int8_t x;
,
krb_cv_int8_t_ifdef=no,
krb_cv_int8_t_ifdef=yes)])
AC_MSG_RESULT($krb_cv_int8_t_ifdef)
if test "$krb_cv_int8_t_ifdef" = "yes"; then
  AC_DEFINE(HAVE_STRANGE_INT8_T)dnl
fi

dnl
dnl Various checks for libraries and their contents
dnl

dnl
dnl System V is have misplaced the socket routines, should really be in libc
dnl

AC_FIND_FUNC(socket, socket,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif],
[0,0,0])
AC_FIND_FUNC(gethostbyname, nsl,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif],
"foo")

dnl
dnl Horror AIX needs -lodm -lcfg to link login
dnl

AC_FIND_FUNC(odm_initialize, odm)
AC_FIND_FUNC(getattr, cfg)
AC_FIND_FUNC(setpcred, s)
AC_FIND_FUNC(logwtmp, util)

AC_FIND_FUNC(logout, util)
AC_FIND_FUNC_NO_LIBS(tgetent, termcap curses)
	
dnl 
dnl See if there is any X11 present
dnl
AC_PATH_XTRA
if test "$no_x" = "yes" ; then
	MAKE_X_PROGS_BIN=""
	MAKE_X_PROGS_LIBEXEC=""
else
	MAKE_X_PROGS_BIN='$(X_PROGS_BIN)'
	MAKE_X_PROGS_LIBEXEC='$(X_PROGS_LIBEXEC)'
fi
AC_SUBST(MAKE_X_PROGS_BIN)dnl
AC_SUBST(MAKE_X_PROGS_LIBEXEC)dnl

save_CFLAGS="$CFLAGS"
CFLAGS="$X_CFLAGS $CFLAGS"
save_LIBS="$LIBS"
dnl LIBS="$X_LIBS $X_PRE_LIBS $X_EXTRA_LIBS $LIBS"
LIBS="$X_PRE_LIBS $X_EXTRA_LIBS $LIBS"
save_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS $X_LIBS"

AC_FIND_FUNC_NO_LIBS(XauReadAuth, Xau X11)
ac_xxx="$LIBS"
LIBS="$LIB_XauReadAuth $LIBS"
AC_CHECK_FUNCS(XauWriteAuth)
if test "$ac_cv_func_XauWriteAuth" != "yes"; then
  XauWriteAuth_c=writeauth.c
  XauWriteAuth_o=writeauth.o
fi
AC_SUBST(XauWriteAuth_c)dnl
AC_SUBST(XauWriteAuth_o)dnl
LIBS="$ac_xxx"

CFLAGS=$save_CFLAGS
LIBS=$save_LIBS
LDFLAGS=$save_LDFLAGS

dnl
dnl Look for berkeley db, gdbm, and ndbm in that order.
dnl

save_LIBS="$LIBS"
AC_FIND_FUNC_NO_LIBS(dbopen, $berkeley_db)
LIBS="$LIB_dbopen $LIBS"
AC_FIND_FUNC_NO_LIBS(dbm_firstkey, $berkeley_db gdbm ndbm)
if test -n "$LIB_dbopen"; then
  LIB_DBM="$LIB_dbopen"
else
  LIB_DBM="$LIB_dbm_firstkey"
fi
AC_SUBST(LIB_DBM)dnl
LIBS="$save_LIBS"

AC_FIND_FUNC(syslog, syslog)

AC_CACHE_CHECK(for working snprintf,ac_cv_func_snprintf_working,
ac_cv_func_snprintf_working=yes
AC_TRY_RUN([
#include <stdio.h>
#include <string.h>
int main()
{
changequote(`,')dnl
	char foo[3];
changequote([,])dnl
	snprintf(foo, 2, "12");
	return strcmp(foo, "1");
}],:,ac_cv_func_snprintf_working=no,:))
: << END
@@@funcs="$funcs snprintf"@@@
END
if test "$ac_cv_func_snprintf_working" = yes; then
	foo=HAVE_SNPRINTF
	AC_DEFINE_UNQUOTED($foo)
fi

AC_CHECK_FUNCS(asnprintf asprintf vasprintf vasnprintf vsnprintf)

AC_CHECK_FUNCS(_getpty _scrsize _setsid _stricmp chroot fchmod fcntl)
AC_CHECK_FUNCS(forkpty frevoke gethostname getlogin getpriority getservbyname)
AC_CHECK_FUNCS(getspnam getspuid gettimeofday getuid grantpt)
AC_CHECK_FUNCS(innetgr mktime ptsname rand random)
AC_CHECK_FUNCS(revoke setitimer setlogin setpgid setpriority)
AC_CHECK_FUNCS(setproctitle setregid setresgid setresuid setreuid setsid)
AC_CHECK_FUNCS(setutent strcasecmp strncasecmp swab ttyname ttyslot ulimit)
AC_CHECK_FUNCS(uname unlockpt vhangup yp_get_default_domain _dns_gethostbyname)
AC_FIND_FUNC_NO_LIBS(getpwnam_r,c_r)
if test "$ac_cv_func_getpwnam_r" = yes; then
	AC_CACHE_CHECK(if getpwnam_r is posix,ac_cv_func_getpwnam_r_posix,
	ac_libs="$LIBS"
	LIBS="$LIBS $LIB_getpwnam_r"
	AC_TRY_RUN([
#include <pwd.h>
int main()
{
	struct passwd pw, *pwd;
	return getpwnam_r("", &pw, NULL, 0, &pwd) < 0;
}
],ac_cv_func_getpwnam_r_posix=yes,ac_cv_func_getpwnam_r_posix=no,:)
LIBS="$ac_libs")
if test "$ac_cv_func_getpwnam_r_posix" = yes; then
	AC_DEFINE(POSIX_GETPWNAM_R)
fi
fi

AC_FIND_FUNC_NO_LIBS(getsockopt, ,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif],
[0,0,0,0,0])
AC_FIND_FUNC_NO_LIBS(setsockopt, ,
[#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif],
[0,0,0,0,0])

dnl Cray stuff
AC_CHECK_FUNCS(getudbnam setlim)

AC_FIND_FUNC(res_search, resolv,
[
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_RESOLV_H
#include <resolv.h>
#endif
],
[0,0,0,0,0])

AC_FIND_FUNC(dn_expand, resolv,
[
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_RESOLV_H
#include <resolv.h>
#endif
],
[0,0,0,0,0])


AC_FUNC_MMAP
AC_FUNC_ALLOCA

#
# Test for POSIX (broken) getlogin
#

if test "$ac_cv_func_getlogin" = yes; then
AC_CACHE_CHECK(if getlogin is posix, ac_cv_func_getlogin_posix, [
if test "$ac_cv_func_getlogin" = yes -a "$ac_cv_func_setlogin" = yes; then
	ac_cv_func_getlogin_posix=no
else
	ac_cv_func_getlogin_posix=yes
fi
])
if test "$ac_cv_func_getlogin_posix" = yes; then
	AC_DEFINE(POSIX_GETLOGIN, 1)
fi
fi

AC_FIND_IF_NOT_BROKEN(hstrerror, resolv,
[#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif],
17)

AC_BROKEN(chown daemon err errx fchown flock getcwd getdtablesize gethostname)
AC_BROKEN(getopt getusershell inet_aton initgroups iruserok lstat memmove)
AC_BROKEN(mkstemp putenv rcmd readv setegid setenv seteuid strdup)
AC_BROKEN(strerror strftime strlwr strnlen strsep strtok_r strupr unsetenv)
AC_BROKEN(verr verrx vsyslog vwarn vwarnx warn warnx writev)

AC_CACHE_CHECK(if realloc if broken, ac_cv_func_realloc_broken, [
ac_cv_func_realloc_broken=no
AC_TRY_RUN([
#include <stddef.h>
#include <stdlib.h>

int main()
{
	return realloc(NULL, 17) == NULL;
}
],:, ac_cv_func_realloc_broken=yes, :)
])
if test "$ac_cv_func_realloc_broken" = yes ; then
	AC_DEFINE(BROKEN_REALLOC)
fi

AC_KRB_FUNC_GETCWD_BROKEN

dnl
dnl Figure what authentication modules should be built
dnl

AC_MSG_CHECKING(which authentication modules should be built)

LIB_AUTH_SUBDIRS=

if test "$ac_cv_header_siad_h" = yes; then
	LIB_AUTH_SUBDIRS="$LIB_AUTH_SUBDIRS sia"
fi

if test "$ac_cv_header_security_pam_modules_h" = yes; then
	LIB_AUTH_SUBDIRS="$LIB_AUTH_SUBDIRS pam"
fi

case "${host}" in
changequote(,)dnl
*-*-irix[56]*) LIB_AUTH_SUBDIRS="$LIB_AUTH_SUBDIRS afskauthlib" ;;
changequote([,])dnl
esac

AC_MSG_RESULT($LIB_AUTH_SUBDIRS)

AC_SUBST(LIB_AUTH_SUBDIRS)dnl

dnl
dnl Figure out if we have tunnels
dnl

AC_MSG_CHECKING(for tunnel devices)

APPL_KIP_DIR=

if test "$ac_cv_header_net_if_tun_h" = "yes"; then
	APPL_KIP_DIR=kip
fi

AC_MSG_RESULT($ac_cv_header_net_if_tun_h)

AC_SUBST(APPL_KIP_DIR)dnl

dnl
dnl Checks for prototypes and declarations
dnl

AC_NEED_PROTO([
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
],
crypt)

AC_NEED_PROTO([
#include <stdio.h>
],
fclose)

AC_NEED_PROTO([
#include <string.h>
],
strtok_r)

AC_NEED_PROTO([
#include <unistd.h>
],
getusershell)

AC_CHECK_VAR([#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif],
h_errno)

AC_CHECK_VAR([#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif],
h_errlist)

AC_CHECK_VAR([#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif],
h_nerr)

AC_CHECK_VAR([#ifdef HAVE_ERR_H
#include <err.h>
#endif],[__progname])

AC_CHECK_DECLARATION([#include <stdlib.h>], optarg)
AC_CHECK_DECLARATION([#include <stdlib.h>], optind)
AC_CHECK_DECLARATION([#include <stdlib.h>], opterr)
AC_CHECK_DECLARATION([#include <stdlib.h>], optopt)

dnl
dnl According to ANSI you are explicitly allowed to cast to void,
dnl but the standard fails to say what should happen. Some compilers
dnl think this is illegal:
dnl
dnl void foo(void)
dnl {
dnl   return (void)0;
dnl }
dnl
dnl Thus explicitly test for void
dnl
AC_TYPE_SIGNAL
if test "$ac_cv_type_signal" = "void" ; then
	AC_DEFINE(VOID_RETSIGTYPE, 1)
fi

dnl
dnl Check for fields in struct utmp
dnl
AC_EGREP_HEADER(ut_user, utmp.h, AC_DEFINE(HAVE_UT_USER))
AC_EGREP_HEADER(ut_host, utmp.h, AC_DEFINE(HAVE_UT_HOST))
AC_EGREP_HEADER(ut_addr, utmp.h, AC_DEFINE(HAVE_UT_ADDR))
AC_EGREP_HEADER(ut_type, utmp.h, AC_DEFINE(HAVE_UT_TYPE))
AC_EGREP_HEADER(ut_pid, utmp.h, AC_DEFINE(HAVE_UT_PID))
AC_EGREP_HEADER(ut_id, utmp.h, AC_DEFINE(HAVE_UT_ID))
AC_EGREP_HEADER(ut_syslen, utmpx.h, AC_DEFINE(HAVE_UT_SYSLEN))
AC_EGREP_HEADER(ut_exit, utmpx.h, AC_DEFINE(HAVE_UT_EXIT))

AC_STRUCT_ST_BLKSIZE

dnl
dnl Check for struct winsize
dnl

AC_KRB_STRUCT_WINSIZE

dnl
dnl Check for some common types
dnl

AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T

AC_MSG_CHECKING(for ssize_t)
AC_CACHE_VAL(krb_cv_type_ssize_t,[
AC_TRY_COMPILE([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <stddef.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif],
[ssize_t foo = 1;],
krb_cv_type_ssize_t=yes,
krb_cv_type_ssize_t=no)])
AC_MSG_RESULT($krb_cv_type_ssize_t)
if test "$krb_cv_type_ssize_t" = no; then
	AC_DEFINE(ssize_t,int)
fi


dnl
dnl Check for broken ultrix sys/socket.h
dnl

AC_MSG_CHECKING(for broken sys/socket.h)
AC_CACHE_VAL(krb_cv_header_sys_socket_h_broken, [
AC_TRY_COMPILE(
[#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>],[],
krb_cv_header_sys_socket_h_broken=no,
krb_cv_header_sys_socket_h_broken=yes)])
AC_MSG_RESULT($krb_cv_header_sys_socket_h_broken)
AC_SUBST(krb_cv_header_sys_socket_h_broken)

dnl
dnl Check for sa_len in sys/socket.h
dnl

AC_MSG_CHECKING(for sa_len in struct sockaddr)
AC_CACHE_VAL(krb_cv_struct_sockaddr_sa_len, [
AC_TRY_COMPILE(
[#include <sys/types.h>
#include <sys/socket.h>],
[struct sockaddr sa;
int foo = sa.sa_len;],
krb_cv_struct_sockaddr_sa_len=yes,
krb_cv_struct_sockaddr_sa_len=no)
])
AC_MSG_RESULT($krb_cv_struct_sockaddr_sa_len)
if test "$krb_cv_struct_sockaddr_sa_len" = yes; then
	AC_DEFINE(SOCKADDR_HAS_SA_LEN)
fi

dnl
dnl Check for ouid in sys/siad.h
dnl

if test "$ac_cv_header_siad_h" = yes; then
AC_MSG_CHECKING(for ouid in struct siaentity)
AC_CACHE_VAL(krb_cv_struct_siaentity_ouid, [
AC_TRY_COMPILE(
[#include <siad.h>
],
[SIAENTITY e;
int foo = e.ouid;],
krb_cv_struct_siaentity_ouid=yes,
krb_cv_struct_siaentity_ouid=no)
])
AC_MSG_RESULT($krb_cv_struct_siaentity_ouid)
if test "$krb_cv_struct_siaentity_ouid" = yes; then
	AC_DEFINE(SIAENTITY_HAS_OUID)
fi
fi

dnl
dnl you can link with getmsg on AIX 3.2 but you cannot run the program
dnl

AC_CACHE_CHECK(for working getmsg, ac_cv_func_getmsg,
AC_TRY_RUN(
[
#include <stdio.h>

int main()
{
  getmsg(open("/dev/null", 0), NULL, NULL, NULL);
  return 0;
}
], ac_cv_func_getmsg=yes, ac_cv_func_getmsg=no, ac_cv_func_getmsg=no))
test "$ac_cv_func_getmsg" = "yes" &&
AC_DEFINE(HAVE_GETMSG)

dnl
dnl Test if we are using berkeley db
dnl

save_LIBS="$LIBS"
LIBS="$LIB_DBM $LIBS"
AC_CACHE_CHECK(for berkeley db, krb_cv_lib_berkeleydb,
AC_TRY_RUN(
[
#include <unistd.h>
#include <fcntl.h>
#include <ndbm.h>
int main()
{
  DBM *d;

  d = dbm_open("conftest", O_RDWR | O_CREAT, 0666);
  if(d == NULL)
    return 1;
  dbm_close(d);
  return access("conftest.db", F_OK) != 0;
}], krb_cv_lib_berkeleydb=yes, krb_cv_lib_berkeleydb=no,
krb_cv_lib_berkeleydb=no))
test "$krb_cv_lib_berkeleydb" = "yes" &&
AC_DEFINE(HAVE_NEW_DB)
LIBS="$save_LIBS"

dnl
dnl Tests for editline
dnl

AC_FIND_FUNC_NO_LIBS(el_init, edit)
AC_FIND_FUNC_NO_LIBS(readline, readline)

if test "$with_readline"; then
	AC_DEFINE(HAVE_READLINE, 1)
	editline_OBJS=
	LIB_readline="$READLINELIB "'$(LIB_tgetent)'
	INCLUDE_readline="$READLINEINCLUDE"
elif test "$ac_cv_func_el_init" = yes; then
	AC_DEFINE(HAVE_READLINE, 1)

	editline_OBJS=edit_compat.o
	LIB_readline='-L$(top_builddir)/lib/editline -leditline '"$LIB_el_init"' $(LIB_tgetent)'

	INCLUDE_readline='-I$(top_builddir)/lib/editline -I$(top_srcdir)/lib/editline'
elif test "$ac_cv_func_readline" = yes; then
	AC_DEFINE(HAVE_READLINE, 1)
	editline_OBJS=
	LIB_readline='-lreadline $(LIB_tgetent)'
	INCLUDE_readline=
else
	AC_DEFINE(HAVE_READLINE, 1)
	editline_OBJS="editline.o complete.o sysunix.o"
	LIB_readline='-L$(top_builddir)/lib/editline -leditline $(LIB_tgetent)'
	INCLUDE_readline='-I$(top_builddir)/lib/editline -I$(top_srcdir)/lib/editline'
fi
AC_SUBST(LIB_readline)
AC_SUBST(INCLUDE_readline)
AC_SUBST(editline_OBJS)

dnl telnet muck --------------------------------------------------

AC_DEFINE(AUTHENTICATION)dnl
AC_DEFINE(KRB4)dnl
AC_DEFINE(ENCRYPTION)dnl
AC_DEFINE(DES_ENCRYPTION)dnl
AC_DEFINE(DIAGNOSTICS)dnl
AC_DEFINE(OLD_ENVIRON)dnl

# Simple test for streamspty, based on the existance of getmsg(), alas
# this breaks on SunOS4 which have streams but BSD-like ptys
#
# And also something wierd has happend with dec-osf1, fallback to bsd-ptys

AC_MSG_CHECKING(for streamspty)
case "`uname -sr`" in
SunOS\ 4*|OSF1*|IRIX\ 4*|HP-UX\ ?.10.*)
	krb_cv_sys_streamspty=no
	;;
AIX*)
	os_rel=`uname -v`.`uname -r`
	if expr "$os_rel" : "3*" >/dev/null 2>&1; then
		krb_cv_sys_streamspty=no
	else
		krb_cv_sys_streamspty="$ac_cv_func_getmsg"
	fi
	;;
*)
	krb_cv_sys_streamspty="$ac_cv_func_getmsg"
	;;
esac
if test "$krb_cv_sys_streamspty" = yes; then
	AC_DEFINE(STREAMSPTY)
fi
dnl AC_SUBST(STREAMSPTY)
AC_MSG_RESULT($krb_cv_sys_streamspty)

AC_MSG_CHECKING([if /bin/ls takes -A])
if /bin/ls -A > /dev/null 2>&1 ;then
	AC_DEFINE(HAVE_LS_A)
	krb_ls_a=yes
else
	krb_ls_a=no
fi
AC_MSG_RESULT($krb_ls_a)
	
dnl ------------------------------------------------------------
AC_CACHE_CHECK(for suffix of preformatted manual pages, krb_cv_sys_cat_suffix,
if grep _version /etc/man.conf > /dev/null 2>&1; then
	krb_cv_sys_cat_suffix=0
else
	krb_cv_sys_cat_suffix=number
fi)
if test "$krb_cv_sys_cat_suffix" = number; then
		CATSUFFIX='$$s'
else
		CATSUFFIX=0
fi
AC_SUBST(CATSUFFIX)

dnl ------------------------------------------------------------

KRB_KAFS_LIB='-L$(top_builddir)/lib/kafs -lkafs'
if test "$krb_cv_sys_aix" = yes; then
	KRB_KAFS_LIB="$KRB_KAFS_LIB -lld"
fi
AC_SUBST(KRB_KAFS_LIB)dnl

dnl ------------------------------------------------------------


dnl This is done by AC_OUTPUT but we need the result here.

test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'

for i in bin lib libexec sbin; do
	i=${i}dir
	foo=`echo $i | tr 'xindiscernible' 'XINDISCERNIBLE'`
	x="\$${i}"
	eval y="$x"
	while test "x$y" != "x$x"; do
		x="$y"
		eval y="$x"
	done
	AC_DEFINE_UNQUOTED($foo,"$x")
done

dnl
dnl We are all set to emit the Makefiles and config.h
dnl
AC_OUTPUT(					\
Makefile 					\
include/Makefile				\
include/sys/Makefile				\
						\
man/Makefile					\
						\
lib/Makefile					\
lib/com_err/Makefile				\
lib/des/Makefile				\
lib/krb/Makefile				\
lib/kdb/Makefile				\
lib/kadm/Makefile				\
lib/acl/Makefile				\
lib/kafs/Makefile				\
lib/roken/Makefile				\
lib/otp/Makefile				\
lib/sl/Makefile					\
lib/editline/Makefile				\
lib/rxkad/Makefile				\
lib/auth/Makefile				\
lib/auth/pam/Makefile				\
lib/auth/sia/Makefile				\
lib/auth/afskauthlib/Makefile			\
						\
kuser/Makefile					\
server/Makefile					\
slave/Makefile					\
admin/Makefile					\
kadmin/Makefile					\
						\
appl/Makefile					\
						\
appl/afsutil/Makefile 				\
appl/ftp/Makefile				\
appl/ftp/common/Makefile			\
appl/ftp/ftp/Makefile				\
appl/ftp/ftpd/Makefile				\
appl/telnet/Makefile				\
appl/telnet/libtelnet/Makefile			\
appl/telnet/telnet/Makefile			\
appl/telnet/telnetd/Makefile			\
appl/bsd/Makefile 				\
appl/kauth/Makefile				\
appl/popper/Makefile				\
appl/movemail/Makefile				\
appl/sample/Makefile				\
appl/xnlock/Makefile				\
appl/kx/Makefile				\
appl/kip/Makefile				\
appl/otp/Makefile				\
doc/Makefile					\
) dnl end of AC_OUTPUT

dnl
dnl This is the release version name-number[beta]
dnl Update before making a new release
dnl
KRB4VERSION="$PACKAGE-$VERSION"

cat > include/newversion.h.in <<EOF
char *krb4_long_version = "@(#)\$Version: $KRB4VERSION by @USER@ on @HOST@ ($host) @DATE@ \$";
char *krb4_version = "$KRB4VERSION";
EOF

if test -f include/version.h && cmp -s include/newversion.h.in include/version.h.in; then
	echo "include/version.h is unchanged"
	rm -f include/newversion.h.in
else
 	echo "creating include/version.h"
 	User=${USER-${LOGNAME}}
 	Host=`(hostname || uname -n) 2>/dev/null | sed 1q`
 	Date=`date`
	mv -f include/newversion.h.in include/version.h.in
	sed -e "s/@USER@/$User/" -e "s/@HOST@/$Host/" -e "s/@DATE@/$Date/" include/version.h.in > include/version.h
fi
