dnl
dnl $Id: configure.in,v 1.2 2001/05/15 18:44:54 zacheiss Exp $
dnl

AC_INIT(include/mcsysinfo.h)

AC_MSG_CHECKING(OS name)
osname=`$srcdir/build/buildinfo -osname`
osnameReal=$osname
case "${osname}" in
	irix*) 
		# Change irix64 to irix
		osname="irix";
		;;
esac

dnl
dnl OS Info for compile definetions
dnl This allows us to do "-Dirix -Dirix64"
dnl
if test "$osname" = "$osnameReal"; then
	osinfodef="-D${osname}"
else
	osinfodef="-D${osname} -D${osnameReal}"
fi
AC_SUBST(osinfodef)

AC_SUBST(osname)
AC_DEFINE_UNQUOTED(${osname})
AC_MSG_RESULT($osname)

AC_MSG_CHECKING(OS version)
osver=`$srcdir/build/buildinfo -osvernodot`
AC_SUBST(osver)
AC_DEFINE_UNQUOTED(OSVER, $osver)
AC_MSG_RESULT($osver)

AC_MSG_CHECKING(OS name + major version)
ostype=`$srcdir/build/buildinfo -osnamemver`
case "${ostype}" in
	irix64*) 
		# Change irix64 to irix
		ostype=`echo $ostype | sed -e 's;irix64;irix;'`
		;;
esac
AC_SUBST(ostype)
AC_DEFINE_UNQUOTED(OSTYPE, "$ostype")
AC_MSG_RESULT($ostype)

AC_MSG_CHECKING(OS major version)
osmver=`$srcdir/build/buildinfo -osmver`
AC_SUBST(osmver)
AC_DEFINE_UNQUOTED(OSMVER, $osmver)
AC_MSG_RESULT($osmver)

AC_MSG_CHECKING(kernel instruction set arch)
if test ! -z "$KISA"; then
	kisa=$KISA
else
	kisa=`$srcdir/build/buildinfo -kisa`
fi
AC_SUBST(kisa)
AC_DEFINE_UNQUOTED(KISA, "$kisa")
AC_MSG_RESULT($kisa)

AC_SUBST_FILE(OSNAMEmf)
OSNAMEmf=$srcdir/lib/os/$osname/$osname.mf
AC_SUBST_FILE(OSNAMEMVERmf)
OSNAMEMVERmf=$srcdir/lib/os/$osname/${osname}${osmver}.mf
AC_SUBST_FILE(OSNAMEVERmf)
OSNAMEVERmf=$srcdir/lib/os/$osname/${osname}${osver}.mf

dnl
dnl Target/platform checks
dnl
AC_MSG_CHECKING(prefix)
defaultprefix=/usr/local/sysinfo
case "${kisa}-${osname}-${osver}" in
	*-aix-*) 
		defaultprefix="/opt/sysinfo";
		;;
	*-hpux-*) 
		defaultprefix="/opt/sysinfo";
		;;
	*-irix-*) 
		defaultprefix="/opt/sysinfo";
		if test "$CC" = "cc"; then
			CFLAGS="$CFLAGS -woff 1174,1552"
		fi
		;;
	*-sunos-*)
		if test $osmver -ge 5 ; then
			defaultprefix="/opt/sysinfo";
		fi
		;;
esac
if test -z "$prefix" -o "$prefix" = "NONE"; then
	prefix=$defaultprefix
fi
AC_PREFIX_DEFAULT($prefix)
AC_MSG_RESULT($prefix)

dnl
dnl configure runtime options
dnl
AC_MSG_CHECKING(cfdir)
AC_ARG_WITH(cfdir, 
[  --with-cfdir=DIR 	  config files in DIR [PREFIX/config]],
cfdir=$withval, cfdir=$prefix/config)
AC_SUBST(cfdir)
AC_DEFINE_UNQUOTED(CFDIR, "$cfdir")
AC_MSG_RESULT($cfdir)

dnl Checks for programs.
AC_PROG_CC
dnl
dnl Check for -xarch=v9 (Sun cc) or -m64 (gcc) on SunOS 5.7 and later
dnl
if test "$osname" = "sunos" && test $osver -ge 57 && \
	test "$kisa" = "sparcv9"; then
	case "$CC" in
		*gcc*)
			ORIG_CFLAGS=$CFLAGS
			xarch="-m64"
			ans="no"
			CFLAGS="$CFLAGS $xarch"
			AC_MSG_CHECKING(whether $CC supports $xarch)
			AC_TRY_COMPILE(,, 
				ans="yes", 
				CFLAGS="$ORIG_CFLAGS")
			AC_MSG_RESULT($ans)
			;;
		cc|*/cc)
			ORIG_CFLAGS=$CFLAGS
			xarch="-xarch=v9"
			ans="no"
			CFLAGS="$CFLAGS $xarch"
			AC_MSG_CHECKING(whether $CC supports $xarch)
			AC_TRY_COMPILE(,, 
				ans="yes", 
				CFLAGS="$ORIG_CFLAGS")
			AC_MSG_RESULT($ans)
			;;
	esac
	if test "$ans" = "no"; then
		cat <<__MSG__

WARNING: You must use Sun WorkShop (Forte) C Compiler 5.0 or later or
Gnu GCC 2.95.2 or later that supports 64-bit compiles if you wish to
compile SysInfo on a 64-bit Sun system.

To compile using Sun WorkShop C Compiler, set the environment variable \$CC
to the Sun WorkShop C Compiler program, then rerun \`configure'.

__MSG__
	fi
fi

AC_PROG_AWK
AC_PROG_LN_S
AC_CONFIG_AUX_DIR(build)
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PATH_PROG(ar, ar, no, $PATH:/usr/ccs/bin:/bin:/usr/bin:/usr/local/bin)
if test -z "$ar" -o "$ar" = "no"; then
	cat <<__ERRMSG__
Cannot find the "ar" program.  Adjust your \$PATH to include
it's location and rerun $0
__ERRMSG__
	exit 1
fi

dnl Checks for libraries.
AC_CHECK_LIB(volmgt, volmgt_acquire)
AC_CHECK_LIB(socket, socket,,, -lnsl)
AC_CHECK_LIB(elf, elf_version)
dnl This needs to be after -lelf check as -lelf is needed sometimes
AC_CHECK_LIB(kvm, kvm_open,,$LIBS)
AC_CHECK_LIB(resolv, res_init)
AC_CHECK_LIB(m, exp)
AC_CHECK_LIB(kstat, kstat_open)
AC_CHECK_LIB(cam, cam_open_device)
AC_CHECK_LIB(adm, main)
AC_CHECK_LIB(nsl, main)
AC_CHECK_LIB(gen, main)
dnl IRIX 32-bit systems
AC_CHECK_LIB(mld, main)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(\
arpa/inet.h \
fcntl.h limits.h \
sys/file.h sys/ioctl.h sys/time.h sys/sockio.h sys/systeminfo.h \
sys/uio.h \
net/if.h net/if_types.h net/if_var.h \
stdarg.h stdlib.h string.h strings.h \
netinet/in.h \
unistd.h \
)
dnl
dnl HPUX headers
dnl
AC_CHECK_HEADERS(\
sys/diskio.h sys/hilioctl.h sys/audio.h sys/framebuf.h \
sys/scsi.h \
)

dnl
dnl Linux headers
dnl
AC_CHECK_HEADERS(\
scsi/scsi.h scsi/sg.h \
/usr/src/linux/include/scsi/scsi.h /usr/src/linux/include/scsi/sg.h \
)

dnl
dnl IRIX headers
dnl
AC_CHECK_HEADERS(\
sys/gfx.h sys/ng1.h sys/gr1new.h sys/gr2.h sys/lg1hw.h sys/lg1.h \
sys/venice.h \
)

AC_CHECK_HEADER(sys/dlpi.h, AC_DEFINE(HAVE_DLPI))
AC_CHECK_HEADER(varargs.h, AC_DEFINE(HAVE_VARARGS))
AC_CHECK_HEADER(kvm.h, AC_DEFINE(HAVE_KVM_H))
dnl
dnl SunOS specifics
dnl
AC_CHECK_HEADER(sys/ddipropdefs.h, AC_DEFINE(HAVE_DDI_PROP))
AC_CHECK_HEADER(sys/dktp/dadkio.h, AC_DEFINE(HAVE_DADKIO))

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_BIGENDIAN
AC_TYPE_UID_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_STRUCT_ST_BLKSIZE
AC_STRUCT_ST_BLOCKS
AC_STRUCT_ST_RDEV
AC_HEADER_TIME
AC_STRUCT_TM
AC_STRUCT_TIMEZONE
dnl
dnl Searching for best value for Large_t
dnl
AC_CHECK_SIZEOF(unsigned long long)
AC_CHECK_SIZEOF(long long)
AC_CHECK_SIZEOF(longlong)
AC_CHECK_SIZEOF(long int)
AC_CHECK_SIZEOF(uint64_t)
dnl
dnl Other type sizes
dnl
AC_CHECK_SIZEOF(int64_t)


dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_FUNC_GETPGRP
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_FUNC_MMAP
AC_FUNC_SETPGRP
AC_FUNC_SETVBUF_REVERSED
AC_TYPE_SIGNAL
AC_FUNC_STRCOLL
AC_FUNC_STRFTIME
AC_FUNC_VFORK
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(\
ether_ntoa \
ether_ntohost \
kvm_getswapinfo \
gethostname gethostid \
getutid \
memcpy memset \
re_comp regcmp regcomp \
strtol strtoll \
uname asctime \
statfs statvfs \
putenv setenv \
strchr strrchr \
strdup strerror snprintf strcasecmp \
swapctl setreuid sysconf \
llseek lseek64 \
wait4 waitpid \
)

dnl
dnl Linux says it has nlist, but it really doesn't
dnl
if test "$osname" != "linux"; then
AC_CHECK_FUNCS(\
nlist nlist64 knlist \
)
fi

dnl
dnl Special checks for sysinfo() since some systems have
dnl a sysinfo() which is not what we expect
dnl
AC_MSG_CHECKING(for sysinfo)
have_sysinfo="no"
have_systeminfo_h="no"
AC_TRY_CPP([#include <sys/systeminfo.h>], have_systeminfo_h="yes")
if test $have_systeminfo_h = "yes"; then
	AC_TRY_COMPILE([
#include <sys/systeminfo.h>
],
[
char b[100];
sysinfo(SI_RELEASE, b, 100);
], 
	have_sysinfo="yes")
fi

if test $have_sysinfo = "yes"; then
	AC_DEFINE(HAVE_SYSINFO)
fi
AC_MSG_RESULT($have_sysinfo)

AC_MSG_CHECKING(for struct in_ifaddr)
ans="yes"
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
], struct in_ifaddr f, AC_DEFINE(HAVE_IN_IFADDR), ans=no)
AC_MSG_RESULT($ans)

AC_MSG_CHECKING(for struct ifnet)
ans="yes"
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
], 
[
struct ifnet f;
struct ifaddr ifa;
ifa.ifa_addr.sa_family = (sa_family_t) 0;
], AC_DEFINE(HAVE_IFNET), ans=no)
AC_MSG_RESULT($ans)

AC_MSG_CHECKING(for struct ifnet->if_version)
ans="yes"
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
], [
struct ifnet f; 
char *s;
s = f.if_version;
], AC_DEFINE(HAVE_IF_VERSION), ans=no)
AC_MSG_RESULT($ans)

AC_MSG_CHECKING(for struct ether_addr)
ans="yes"
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
], struct ether_addr ea, AC_DEFINE(HAVE_ETHER_ADDR), ans=no)
AC_MSG_RESULT($ans)

AC_MSG_CHECKING(for struct anoninfo)
ans="yes"
AC_TRY_COMPILE([
#include <sys/types.h>
#include <vm/anon.h>
], struct anoninfo f, AC_DEFINE(HAVE_ANONINFO), ans=no)
AC_MSG_RESULT($ans)

AC_MSG_CHECKING(if printf supports "%lld")
ans="no"
cat > conftest.c <<EOF
int main()
{
printf("%lld\n", 1);
return 0;
}
EOF
${CC-cc} $CFLAGS $CPPFLAGS conftest.c -o conftest > /dev/null 2>&1 && \
	result=`./conftest`
if test "$result" = "1"; then
AC_DEFINE(HAVE_PRINTF_LL)
ans=yes
fi
rm -f conftest conftest.c
AC_MSG_RESULT($ans)

dnl
dnl Cleanup
dnl
rm -f core

dnl
dnl Some makefile variables we use
dnl
AC_SUBST_FILE(VarsMF)
VarsMF=$srcdir/mf/Vars.mf
AC_SUBST_FILE(RulesMF)
RulesMF=$srcdir/mf/Rules.mf

dnl
dnl Other variables
dnl
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(LIBS)

AC_CONFIG_HEADER(include/autoconfig.h)
AC_OUTPUT([
Makefile
config/Makefile
doc/Makefile
doc/mcsysinfo.man
doc/sysinfo.man
doc/sysinfo.cf.man
include/Makefile
lib/Makefile
mcd/Makefile
sysinfo/Makefile
sysinfo/sysinfowrapper
configvars.sh
], 
echo timestamp > stamp-h)
