dnl configure.in for RPM
dnl
dnl Don't add checks for things we can't work around, unless those checks
dnl failing causes the script to bomb out with a message. If we can't fix
dnl it, why check it?

AC_INIT(rpm.c)
AC_CONFIG_HEADER(config.h)

dnl default to not using NLS
XGETTEXT=""
LIBINTL=""
PO=""
GETTEXTSTUB="gettextstub.o"

AC_CANONICAL_SYSTEM

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_GCC_TRADITIONAL
AC_PROG_INSTALL

AC_MSG_CHECKING(POSIX chmod...)
touch foo.chmodtest
chmod 744 foo.chmodtest
chmod +X foo.chmodtest 2>/dev/null
a=`ls -l foo.chmodtest | awk '{print $1}'`
rm -f foo.chmodtest
if test "$a" = "-rwxr-xr-x"; then
    AC_MSG_RESULT(yes)
    FIXPERMS=a+rX,g-w,o-w 
else
    AC_MSG_RESULT(no (tell your OS vendor about GNU fileutils))
    FIXPERMS=a+r,g-w,o-w 
fi

AC_AIX
AC_MINIX
AC_ISC_POSIX

AC_PROG_AWK
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_CHECK_TOOL(AR, ar, :)

if test "$cross_compiling" != "yes"; then
    AC_MSG_CHECKING(GNU gzip...)
    if test -z "$GZIPBIN"; then
	    oldifs="$IFS"
	    IFS=:
	    for n in $PATH:/opt/gnu/bin; do
		    if test -f $n/gzip; then
			    GZIPBIN="$n/gzip";
			    break
		    fi
	    done
	    IFS="$oldifs"
    fi

    if test -z "$GZIPBIN"; then
	    GZIPBIN="gzip"
	    AC_MSG_RESULT(no)
	    AC_MSG_WARN(RPM will not work without GNU gzip.)
    else
	    AC_MSG_RESULT(yes)
    fi
fi

topdir=`pwd`

if test $prefix != "/usr" -a $prefix != "/usr/local"; then
	AC_MSG_CHECKING("$prefix/lib... $ac_c")
	if test -d $prefix/lib ; then
		LIBS="$LIBS -L$prefix/lib"
		AC_MSG_RESULT(yes)
	else
		AC_MSG_RESULT(no)
	fi

	echo $ac_n "checking for $prefix/include... $ac_c"
	if test -d $prefix/include ; then
		INCPATH="$INCPATH -I$prefix/include"
		AC_MSG_RESULT(yes)
	else
		AC_MSG_RESULT(no)
	fi
fi

AC_MSG_CHECKING(/usr/local/lib...)
if test -d /usr/local/lib ; then
	LIBS="$LIBS -L/usr/local/lib"
	
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(/usr/local/include...)
if test -d /usr/local/include ; then
	INCPATH="$INCPATH -I/usr/local/include"
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(/usr/ucblib...)
if test -d /usr/ucblib ; then
	LIBS="$LIBS -L/usr/ucblib"
	
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

dnl Checks for libraries.

AC_CHECK_FUNC(strcasecmp, [], [
    AC_CHECK_LIB(ucb, strcasecmp, [LIBS="$LIBS -lucb" USEUCB=y])
])

AC_CHECK_FUNC(random, [], [
    AC_CHECK_LIB(random, random, [LIBS="$LIBS -lm"], [
	if test -z "$USEUCB"; then
	    AC_CHECK_LIB(ucb, random, [LIBS="$LIBS -lucb" USEUCB=y])
	fi
    ])
])

dnl
dnl handle --disable-nls
dnl
AC_ARG_ENABLE([nls],
[  --disable-nls           do not use National Language Support], 
	USE_NLS=$enableval, USE_NLS=yes)

if test "$USE_NLS" != "no"; then
    AC_CHECK_FUNC(gettext, [HAVEGETTEXT="yes"], 
      AC_CHECK_LIB(intl, gettext, [LIBINTL="-lintl" HAVEGETTEXT="y"], 
		   AC_MSG_WARN([sorry this package needs libintl.a (from the 
    gettext package)])
		 )
		)

    AC_CHECK_PROG(XGETTEXT, xgettext, xgettext)

    if test "$XGETTEXT" != ""; then 
	if xgettext --help 2>&1 | grep illegal >/dev/null ; then
	    echo "xgettext isn't GNU version"
	    XGETTEXT=""
	fi
    fi

    if test "$XGETTEXT" != "" -a "$HAVEGETTEXT" != ""; then
	echo "xgettext and gettext() exist; will build i18n support"
	PO=po
	GETTEXTSTUB=""
    else
	echo "xgettext and libintl.a don't both exist; will not build i18n support"
    fi
else
    echo "user specificed no gettext; will not build i18n support"
fi

AC_CHECK_FUNC(getdomainname, [], [
    AC_CHECK_LIB(nsl, getdomainname, [LIBS="$LIBS -lnsl"])
])
AC_CHECK_FUNC(socket, [], [
    AC_CHECK_LIB(socket, socket, [LIBS="$LIBS -lsocket"])
])

AC_CHECK_FUNC(dbopen, [],
  AC_CHECK_LIB(db, dbopen, [], AC_MSG_WARN([sorry this package needs libdb.a 
(from the db package)]))
           )

AC_CHECK_FUNC(fork, [], [echo "using vfork() instead of fork()";
	MISCOBJS=fakefork.o])

dnl AmigaOS and IXEmul have a fork() dummy
    case "$target" in
	m68k-*-amigaos ) 
		echo "Building for AmigaOS: using vfork() instead of fork()"; 
		CFLAGS="$CFLAGS -Dfork=vfork" 
		;;
esac


ZLIB=
for zlib in z gz ; do
   AC_CHECK_LIB(${zlib}, gzread, 
	[LIBS="$LIBS -l${zlib}"; break], 
	[if test ${zlib} = gz; then 
	    AC_MSG_WARN([sorry this package needs libz.a or libgz.a (from the zlib package)]) 
	 fi]
	       )
done

dnl Checks for header files we can live without.
AC_HEADER_STDC
AC_HEADER_MAJOR
AC_CHECK_HEADERS(netinet/in_systm.h limits.h)
AC_CHECK_HEADERS(alloca.h dirent.h sys/socket.h sys/select.h)
AC_CHECK_HEADERS(machine/types.h string.h)
AC_CHECK_HEADERS(mntent.h sys/mnttab.h sys/systemcfg.h)
AC_CHECK_HEADERS(glob.h,,MISCOBJS="$MISCOBJS glob.o")
AC_CHECK_HEADERS(fnmatch.h,,MISCOBJS="$MISCOBJS fnmatch.o")

if test "$PO" = po; then
    AC_CHECK_HEADERS(libintl.h)
fi

AC_C_BIGENDIAN

dnl look for libc features
PROVIDES_ERRNO=no
AC_MSG_CHECKING(if <netdb.h> defines h_errno...)
AC_TRY_LINK([#include <netdb.h>],printf("%d",h_errno),PROVIDES_ERRNO=yes)
AC_MSG_RESULT($PROVIDES_ERRNO)
if test $PROVIDES_ERRNO = yes; then
	AC_DEFINE(HAVE_HERRNO)
fi

dnl If a system doesn't have S_IFSOCK, define it as 0 which will
dnl make S_ISSOCK always return false (nice, eh?)
AC_MSG_CHECKING(if <sys/stat.h> defines S_IFSOCK...)
AC_TRY_LINK([#include <sys/stat.h>],printf("%d", S_IFSOCK),
	HAS_S_IFSOCK=yes,HAS_S_IFSOCK=no)
AC_MSG_RESULT($HAS_S_IFSOCK)
if test $HAS_S_IFSOCK = yes; then
	AC_DEFINE(HAVE_S_IFSOCK)
fi

dnl Some Unix's are missing S_ISLNK, S_ISSOCK
AC_MSG_CHECKING(if <sys/stat.h> defines S_ISLNK...)
AC_TRY_LINK([#include <sys/stat.h>],printf("%d", S_ISLNK(0755)),
	HAS_S_ISLNK=yes,HAS_S_ISLNK=no)
AC_MSG_RESULT($HAS_S_ISLNK)
if test $HAS_S_ISLNK = yes; then
	AC_DEFINE(HAVE_S_ISLNK)
fi

AC_MSG_CHECKING(if <sys/stat.h> defines S_ISSOCK...)
AC_TRY_LINK([#include <sys/stat.h>],printf("%d", S_ISSOCK(0755)),
	HAS_S_ISSOCK=yes,HAS_S_ISSOCK=no)
AC_MSG_RESULT($HAS_S_ISSOCK)
if test $HAS_S_ISSOCK = yes; then
	AC_DEFINE(HAVE_S_ISSOCK)
fi

AC_MSG_CHECKING(if timezone is defined...)
AC_TRY_LINK([#include <time.h>],printf("%ld", timezone),
      HAS_TIMEZONE=yes,HAS_TIMEZONE=no)
AC_MSG_RESULT($HAS_TIMEZONE)
if test $HAS_TIMEZONE = no; then
      AC_DEFINE(NEED_TIMEZONE)
fi

dnl Checks for library functions.
AC_CHECK_FUNC(inet_aton, AC_DEFINE(HAVE_INET_ATON), MISCOBJS="$MISCOBJS inet_aton.o")
AC_CHECK_FUNC(realpath, AC_DEFINE(HAVE_REALPATH), MISCOBJS="$MISCOBJS realpath.o")
AC_CHECK_FUNC(getmntent, AC_DEFINE(HAVE_GETMNTENT), [
  AC_CHECK_FUNC(mntctl, AC_DEFINE(HAVE_MNTCTL),[
    AC_CHECK_FUNC(getmntinfo_r, AC_DEFINE(HAVE_GETMNTINFO_R), [
      AC_CHECK_LIB(c_r, getmntinfo_r, [LIBS="$LIBS -lc_r"; 
					AC_DEFINE(HAVE_GETMNTINFO_R)],
                 MISCOBJS="$MISCOBJS getmntent.o")])])])
AC_CHECK_FUNC(strerror, [], MISCOBJS="$MISCOBJS strerror.o")
AC_CHECK_FUNC(strtol, [], MISCOBJS="$MISCOBJS strtol.o")
AC_CHECK_FUNC(strtoul, [], MISCOBJS="$MISCOBJS strtoul.o")
LCHOWN=no
AC_CHECK_FUNC(lchown, [LCHOWN=yes; AC_DEFINE(HAVE_LCHOWN)])

if test "$LCHOWN" = no; then 
    dnl Does chown() follow symlinks? This should be a good enough test.
    AC_MSG_CHECKING(whether chown() follows symlinks...)
    AC_ARG_ENABLE([broken-chown],
    [  --enable-broken-chown  this system's chown follows symbolic links], 
	    result=yes, result=unknown)
    if echo "$build" | egrep "(aix)|(hpux)" > /dev/null ; then
	result=yes
    elif echo "$build" | egrep "(nextstep)" > /dev/null ; then
	result=no
    fi
    if test $result = unknown; then
	if test `id | cut -f 2 -d \= | cut -f 1 -d \(` = 0; then
	    rm -f foo bar
	    touch foo
	    ln -s foo bar
	    chown 10 bar
	    if test `ls -l foo | awk '{print $3}'` != "root"; then
		result=yes
	    else
		result=no
	    fi
	    rm -f foo bar
	else
	    AC_MSG_CHECKING((cannot check by non-root user))
	    result=no
	fi
    fi

    AC_MSG_RESULT($result)
    if test $result = yes; then
	    AC_DEFINE(CHOWN_FOLLOWS_SYMLINK)
    fi
fi

if test -n "$MISCOBJS"; then
	LIBMISC=-lmisc
fi

if test "x$varprefix" = "x"; then
    varprefix=`echo $prefix | sed 's/usr/var/'`
    test "x$prefix" = xNONE && varprefix=`echo $ac_default_prefix | sed 's/usr/var/'`
fi

if test "x$tmpdir" = "x"; then
    if test -d $varprefix/tmp; then
	tmpdir=$varprefix/tmp
    else
	if test -d /var/tmp; then
	    tmpdir=/var/tmp
	else
	    tmpdir=/tmp
	fi
    fi
fi

# Solaris 2.5.1 seems to confuse things
if echo "$build" | grep "solaris2.5" > /dev/null ; then
	echo "Hacking things up for Solaris 2.5.x"
	build_os="solaris"
fi

RPM=rpm
# Solaris needs -ldl to use -lnsl, and it can't build static
# binaries if you use -ldl
if echo "$build_os" | grep solaris >/dev/null; then
	echo "hacking things up for solaris"
	RPM=rpm.shared
	LIBDL=-ldl
elif echo "$build_os" | grep sco > /dev/null; then
	echo "hacking things up for sco"
	RPM=rpm.shared
	AC_DEFINE(NEED_STRINGS_H)
	AC_DEFINE(HAVE_STRUCT_MNTTAB)
elif echo "$build_os" | grep sunos > /dev/null; then
	echo "hacking things up for sunos"
	CFLAGS="$CFLAGS -D__USE_FIXED_PROTOTYPES__"
	AC_DEFINE(NEED_STRINGS_H)
	AC_DEFINE(NEED_MYREALLOC)
	MISCOBJS="$MISCOBJS myrealloc.o"
elif echo "$build_os" | grep hpux > /dev/null; then
	RPM=rpm.shared
	echo "hacking things up for hpux"
fi

rm -f ./find-provides.sh
if test -f autodeps/${build_os}.prov ; then
	echo "using autodeps/$build_os.prov for automatic provides generation"
	ln -s autodeps/${build_os}.prov ./find-provides.sh
else
	echo "*** no default provides information is available for $build_os"
	ln -s autodeps/none ./find-provides.sh
fi

rm -f ./find-requires.sh
if test -f autodeps/${build_os}.req ; then
	echo "using autodeps/$build_os.req for automatic requires generation"
	ln -s autodeps/$build_os.req ./find-requires.sh
else
	echo "*** no default requires information is available for $build_os"
	ln -s autodeps/none ./find-requires.sh
fi

AC_SUBST(MISCOBJS)
AC_SUBST(INCPATH)
AC_SUBST(LIBMISC)
AC_SUBST(LIBINTL)
AC_SUBST(varprefix)
AC_SUBST(tmpdir)
AC_SUBST(topdir)
AC_SUBST(INTLDEF)
AC_SUBST(XGETTEXT)
AC_SUBST(LIBINTL)
AC_SUBST(LIBS)
AC_SUBST(LIBDL)
AC_SUBST(PO)
AC_SUBST(GETTEXTSTUB)
AC_SUBST(RPM)
AC_SUBST(GZIPBIN)
AC_SUBST(FIXPERMS)

AC_OUTPUT(Makefile Makefile.inc lib-rpmrc lib/Makefile build/Makefile tools/Makefile po/Makefile misc/Makefile)
