dnl Process this file with autoconf to produce a configure script.

AC_PREREQ([2.53])

AC_INIT(include/dpkg.h.in)
AC_CONFIG_HEADER(config.h)

ALSO_STATIC=
AC_ARG_WITH(static-progs,
[  --with-static-progs     compile static versions of certain binaries as well],
[case "$withval" in
  "true" | "yes" )
    ALSO_STATIC=1
    ;;
 esac])
AC_SUBST(ALSO_STATIC)

DSELECTDIR="dselect"
AC_ARG_WITH(dselect,
[  --with-dselect          the dselect package-management frontend],
[case "$withval" in
  "false" | "no" )
    DSELECTDIR=""
    ;;
 esac])
AC_SUBST(DSELECTDIR)

USE_START_STOP_DAEMON="true"
AC_ARG_WITH(start-stop-daemon,
[  --with-start-stop-daemon
                          start and stop system daemons],
[case "$withval" in
  "false" | "no" )
    USE_START_STOP_DAEMON=""
    ;;
 esac])
AC_SUBST(USE_START_STOP_DAEMON)
if test "$USE_START_STOP_DAEMON" != ""; then
    AC_DEFINE(USE_START_STOP_DAEMON,,[Define if start-stop-daemon is compiled.])
fi

USE_SGML_DOC="true"
AC_ARG_WITH(sgml-doc,
[  --with-sgml-doc         SGML documentation converted to HTML],
[case "$withval" in
  "false" | "no" )
    USE_SGML_DOC=""
    ;;
 esac])
AC_SUBST(USE_SGML_DOC)

admindir="$libdir/db"
AC_ARG_WITH(admindir,
[  --with-admindir=DIR     store dpkg database in DIR [LIBDIR/db]],
[case "$withval" in
  "" )
    AC_MSG_ERROR(invalid admindir specified)
    ;;
  * )
    admindir="$withval"
    ;;
 esac])


AC_CHECK_TOOL_PREFIX
AC_CANONICAL_SYSTEM

AC_CHECK_TOOL(CC, gcc)
AC_PROG_CC
AC_PROG_CXX
AM_CONDITIONAL(HAVE_CPLUSPLUS, [test "$CXX" != ""])

AC_CHECK_TOOL(LD, ld, ld)

PACKAGE=dpkg
AC_SUBST(PACKAGE)
VERSION=`cat $srcdir/version-nr`
AC_SUBST(VERSION)

AC_SYS_LARGEFILE

dnl test to see if srcdir already configured
if test "`cd $srcdir && pwd`" != "`pwd`" && test -f $srcdir/config.status; then
  AC_MSG_ERROR([source directory already configured; run "make distclean" there first])
fi
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of package])
AC_DEFINE_UNQUOTED(VERSION, "$VERSION", [Version number of package])
AC_ARG_PROGRAM
AC_PROG_MAKE_SET

AC_MSG_CHECKING(dpkg version)
AC_MSG_RESULT($VERSION)

AC_PREFIX_DEFAULT(/usr)

# OpenBSD has decided to be odd and includ a version number
# in the final field of the GNU system type.
# This code strips it out in a generic way.
os_type=''
AC_MSG_CHECKING(Operating system type)
case $target_os in 
        *linux-gnu* )
          os_type="linux-gnu";;
        *linux* )
          os_type="linux-gnu";;
        *openbsd* )
          os_type="openbsd";;
        *knetbsd*-gnu* )
          os_type="knetbsd-gnu";;
        *netbsd* )
          os_type="netbsd";;
        *kfreebsd*-gnu* )
          os_type="kfreebsd-gnu";;
        *freebsd* )
          os_type="freebsd";;
        gnu )
          os_type="hurd";;
esac
if test "x$os_type" = "x"; then
 os_type=$target_os 
fi
AC_MSG_RESULT([$os_type])
AC_SUBST(os_type)

dpkg_archset=''
AC_MSG_CHECKING(Debian architecture)
dpkg_archset="`awk '$1 == "'$target_cpu-$os_type'" { print $2 }' $srcdir/archtable`"
# Finish off
if test "x$dpkg_archset" = "x"; then
 AC_MSG_RESULT([$target_cpu-$os_type, but not found in archtable])
 dpkg_archset=$target_cpu-$os_type
 else
 AC_MSG_RESULT($dpkg_archset)
fi
AC_DEFINE_UNQUOTED(ARCHITECTURE, "${dpkg_archset}", [Set this to the canonical Debian architecture string for this CPU type.])

dnl gettext

ALL_LINGUAS="ca cs da de el en es fr gl he it ja ko nl nn pl pt pt_BR ru sv zh_CN"
AM_GNU_GETTEXT([external])
AM_PO_SUBDIRS
if test "x$HAVE_LOCALE_H" = "x"; then
 AC_MSG_CHECKING([whether setlocale() is available])
 HAVE_SETLOCALE=''
 AC_TRY_COMPILE([#include <locale.h>],[ setlocale(0, "bar");], HAVE_SETLOCALE=1;
  AC_DEFINE(HAVE_SETLOCALE,1,[Define if setlocale is available.])
 )
 if test "x$HAVE_SETLOCALE" = x; then
  AC_MSG_RESULT(no)
 else
  AC_MSG_RESULT(yes)
 fi
fi

dnl Other stuff

AC_STDC_HEADERS
AC_PROG_RANLIB
AC_PROG_LN_S
AC_CHECK_PROG(RM,rm,rm -f)
AC_CHECK_PROG(SED,sed,sed)
AC_PROG_INSTALL
AC_PATH_PROG(PERL,perl,/usr/bin/perl)
AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_VPRINTF
AC_C_CONST
AC_C_BIGENDIAN
AC_C_INLINE
AC_CHECK_SIZEOF(unsigned long)
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_TYPE(ptrdiff_t,int)
AC_CHECK_FUNCS(unsetenv alphasort scandir strerror strsignal strtoul setsid)
AC_CHECK_FUNCS(vsnprintf lchown snprintf)
AC_CHECK_HEADERS(sys/cdefs.h syslog.h stddef.h)
AC_CHECK_HEADERS(error.h locale.h)
AC_DECL_SYS_SIGLIST
AC_CHECK_LIB(ihash, ihash_create, SSD_LIBS="-lihash $SSD_LIBS")
AC_CHECK_LIB(ps, proc_stat_list_create, SSD_LIBS="-lps $SSD_LIBS")
AC_CHECK_LIB(shouldbeinlibc, fmt_past_time, SSD_LIBS="-lshouldbeinlibc $SSD_LIBS")
AC_CHECK_LIB(kvm, kvm_openfiles, SSD_LIBS="-lkvm $SSD_LIBS")
AC_SUBST(SSD_LIBS)

# check for the proper curses library. This can be either
# -lcurses or -lncurses, we need to check for either one.
AC_CHECK_LIB(ncurses,initscr, CURSES_LIBS="-lncurses $CURSES_LIBS", AC_CHECK_LIB(curses,initscr, CURSES_LIBS="-lcurses $CURSES_LIBS"))
if test "x$CURSES_LIBS" = "x"; then
 AC_MSG_WARN(no curses library found)
fi
AC_SUBST(CURSES_LIBS)

DPKG_CHECK_DEFINE(TIOCNOTTY,sys/ioctl.h)

ZLIB_CFLAGS=
ZLIB_LIBS=
ZLIB_LIBS_ALSO_STATIC=
AC_ARG_WITH(zlib,
[  --with-zlib             use zlib for decompression of some gzip files],
	[use_zlib=$withval],
	[uze_zlib=no])
if test "$use_zlib" != "no" ; then
	ZLIB_CFLAGS=-DUSE_ZLIB
	if test "$use_zlib" = "static"  ; then
		ZLIB_LIBS="-Wl,-Bstatic -lz -Wl,-Bdynamic"
		if test "$ALSO_STATIC"; then
			ZLIB_LIBS_ALSO_STATIC="-lz"
		fi
	else
		ZLIB_LIBS=-lz
		ZLIB_LIBS_ALSO_STATIC="-lz"
	fi
fi
AC_SUBST(ZLIB_CFLAGS)
AC_SUBST(ZLIB_LIBS)
AC_SUBST(ZLIB_LIBS_ALSO_STATIC)

BZ2LIB_CFLAGS=
BZ2LIB_LIBS=
BZ2LIB_LIBS_ALSO_STATIC=
AC_ARG_WITH(bz2lib,
[  --with-bz2lib           use bz2lib for decompression of some bzip2 files],
	[use_bz2lib=$withval],
	[uze_bz2lib=no])
if test "$use_bz2lib" != "no" ; then
	BZ2LIB_CFLAGS=-DUSE_BZ2LIB
	if test "$use_bz2lib" = "static"  ; then
		BZ2LIB_LIBS="-Wl,-Bstatic -lbz2 -Wl,-Bdynamic"
		if test "$ALSO_STATIC"; then
			BZ2LIB_LIBS_ALSO_STATIC="-lbz2"
		fi
	else
		BZ2LIB_LIBS=-lbz2
		BZ2LIB_LIBS_ALSO_STATIC="-lbz2"
	fi
fi
AC_SUBST(BZ2LIB_CFLAGS)
AC_SUBST(BZ2LIB_LIBS)
AC_SUBST(BZ2LIB_LIBS_ALSO_STATIC)

AC_TRY_COMPILE([
#include <sys/types.h>
#include <dirent.h>
], alphasort, AC_DEFINE(HAVE_ALPHASORT_DECLARATION,,[Define if alphasort is declared.]))

AC_TRY_COMPILE(,[
} inline int foo (int x) {], AC_DEFINE(HAVE_INLINE,,[Define if inline functions a la GCC are available.]))

AC_MSG_CHECKING([for va_copy])
# OpenBSD passes AC_TRY_COMPILE for va_copy even though 
# it doesn't seem to exist, which is odd. We need to use
# AC_TRY_RUN.
AC_TRY_RUN([
#include <stdarg.h>
main(){
va_list v1,v2;
va_copy(v1, v2);
exit(0);}
], [AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_VA_COPY,,[Whether the va_copy macro exists])],[AC_MSG_RESULT(no)
AC_MSG_CHECKING([for va_list assignment copy])
AC_TRY_COMPILE([
#include <stdarg.h>
],[
va_list v1,v2;
v1 = v2;
], AC_MSG_RESULT(yes),AC_MSG_ERROR(no))])

DPKG_C_GCC_ATTRIBUTE([,,],supported,[int x],[,,],ATTRIB,[Define if function attributes a la GCC 2.5 and higher are available.],
  DPKG_C_GCC_ATTRIBUTE(noreturn,noreturn,[int x],noreturn,NORETURN,[Define if nonreturning functions a la GCC 2.5 and higher are available.])
  DPKG_C_GCC_ATTRIBUTE(const,const,[int x],const,CONST,[Define if constant functions a la GCC 2.5 and higher are available.])
  DPKG_C_GCC_ATTRIBUTE(unused,unused,[int x],unused,UNUSED,[Define if unused variables la GCC 2.5 and higher are available.])
  DPKG_C_GCC_ATTRIBUTE(format...,format,[char *y, ...],[format(printf,1,2)],PRINTFFORMAT,[Define if printf-format argument lists a la GCC are available.]))

AC_MSG_CHECKING(whether to use -Werror)
AC_ARG_WITH(Werror,
[  --with-Werror           use -Werror when compiling],
	[warn=-Werror]
	AC_MSG_RESULT(yes)
	DPKG_C_GCC_TRY_WARNS(-Werror, dpkg_cv_c_gcc_warn_error),
	[warn=all]
	AC_MSG_RESULT(no))

DPKG_C_GCC_TRY_WARNS(-Wall , dpkg_cv_c_gcc_warn_all)
DPKG_C_GCC_TRY_WARNS(-Wwrite-strings, dpkg_cv_c_gcc_warn_writestrings)
DPKG_C_GCC_TRY_WARNS(-Wpointer-arith, dpkg_cv_c_gcc_warn_pointerarith)
DPKG_C_GCC_TRY_WARNS(-Wimplicit -Wnested-externs, dpkg_cv_c_gcc_warn_implicit)
DPKG_C_GCC_TRY_WARNS(-Wstrict-prototypes, dpkg_cv_c_gcc_strict_prototypes)
DPKG_C_GCC_TRY_WARNS(-Wmissing-prototypes, dpkg_cv_c_gcc_missing_prototypes)
DPKG_C_GCC_TRY_WARNS(-Wmissing-declarations, dpkg_cv_c_gcc_missing_declarations)
DPKG_C_GCC_TRY_WARNS(-Wbad-function-cast, dpkg_cv_c_gcc_func_cast)
DPKG_C_GCC_TRY_WARNS(-Wcast-qual -Wcast-align, dpkg_cv_c_gcc_cast_qual)
DPKG_C_GCC_TRY_WARNS(-Winline, dpkg_cv_c_gcc_inline)
DPKG_C_GCC_TRY_WARNS(-Wmissing-noreturn, dpkg_cv_c_gcc_noreturn)
DPKG_C_GCC_TRY_WARNS(-Wsign-compare, dpkg_cv_c_gcc_comp_conv)
AC_SUBST(CWARNS)

dnl fix up patchs. We can not use variable references paths since 
dnl that breaks DocBook
expandvar() { echo $(eval echo $1) ; }

test "x$prefix" = xNONE && prefix="$ac_default_prefix"
test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
LLIBDIR=`eval echo $libdir`
LLIBDIR=`eval echo $LLIBDIR`
LOCALSTATEDIR="$(expandvar $localstatedir)"
AC_DEFINE_UNQUOTED(LLIBDIR, "$LLIBDIR", [Defined to the $(libdir) location])
AC_DEFINE_UNQUOTED(LOCALSTATEDIR, "$LOCALSTATEDIR", [Defined to $(localstatedir) dir location])

admindir=$(expandvar "$admindir")
bindir=$(expandvar "$bindir")
datadir=$(expandvar "$datadir")
includedirdir=$(expandvar "$includedirdir")
libdir=$(expandvar "$libdir")
libexecdir=$(expandvar "$libexecdir")
localstatedir=$(expandvar "$localstatedir")
mandir=$(expandvar "$mandir")
sbindir=$(expandvar "$sbindir")
sharedstatedir=$(expandvar "$sharedstatedir")
sysconfdir=$(expandvar "$sysconfdir")
infodir=$(expandvar "$infodir")

AC_SUBST(admindir)

AH_BOTTOM([#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif

/* Use the definitions: */

/* Give us an unsigned 32-bit data type. */
#if SIZEOF_UNSIGNED_LONG==4
#define UWORD32 unsigned long
#elif SIZEOF_UNSIGNED_INT==4
#define UWORD32 unsigned int
#else
#error I do not know what to use for a UWORD32.
#endif

/* The maximum length of a #! interpreter displayed by dpkg-deb. */
#ifdef PATH_MAX
#define INTERPRETER_MAX PATH_MAX
#else
#define INTERPRETER_MAX 1024
#endif

/* GNU C attributes. */
#ifndef FUNCATTR
#ifdef HAVE_GNUC25_ATTRIB
#define FUNCATTR(x) __attribute__(x)
#else
#define FUNCATTR(x)
#endif
#endif

/* GNU C printf formats, or null. */
#ifndef ATTRPRINTF
#ifdef HAVE_GNUC25_PRINTFFORMAT
#define ATTRPRINTF(si,tc) format(printf,si,tc)
#else
#define ATTRPRINTF(si,tc)
#endif
#endif
#ifndef PRINTFFORMAT
#define PRINTFFORMAT(si,tc) FUNCATTR((ATTRPRINTF(si,tc)))
#endif

/* GNU C nonreturning functions, or null. */
#ifndef ATTRNORETURN
#ifdef HAVE_GNUC25_NORETURN
#define ATTRNORETURN noreturn
#else /* ! HAVE_GNUC25_NORETURN */
#define ATTRNORETURN
#endif /* HAVE_GNUC25_NORETURN */
#endif /* ATTRNORETURN */

#ifndef NONRETURNING
#define NONRETURNING FUNCATTR((ATTRNORETURN))
#endif /* NONRETURNING */

/* Combination of both the above. */
#ifndef NONRETURNPRINTFFORMAT
#define NONRETURNPRINTFFORMAT(si,tc) FUNCATTR((ATTRPRINTF(si,tc),ATTRNORETURN))
#endif

/* GNU C constant functions, or null. */
#ifndef ATTRCONST
#ifdef HAVE_GNUC25_CONST
#define ATTRCONST const
#else
#define ATTRCONST
#endif
#endif
#ifndef CONSTANT
#define CONSTANT FUNCATTR((ATTRCONST))
#endif

/* GNU C unused functions, or null. */
#ifndef ATTRUNUSED
#ifdef HAVE_GNUC25_UNUSED
#define ATTRUNUSED
#else
#define ATTRUNUSED
#endif
#endif
#ifndef UNUSED
#define UNUSED FUNCATTR((ATTRUNUSED))
#endif

/* Declare strerror if we don't have it already. */
#ifndef HAVE_STRERROR
const char *strerror(int);
#endif

/* Declare strsignal if we don't have it already. */
#ifndef HAVE_STRSIGNAL
const char *strsignal(int);
#endif

/* Declare scandir if we don't have it already. */
#ifndef HAVE_SCANDIR
struct dirent;
int scandir(const char *dir, struct dirent ***namelist,
            int (*select)(const struct dirent *),
            int (*compar)(const void*, const void*));
#endif

/* Declare alphasort if we don't have it already. */
#if !defined(HAVE_ALPHASORT) || !defined(HAVE_ALPHASORT_DECLARATION)
struct dirent;
int alphasort(const struct dirent *a, const struct dirent *b);
#endif

/* Declare unsetenv if we don't have it already. */
#ifndef HAVE_UNSETENV
void unsetenv(const char *x);
#endif

/* Define strtoul if we don't have it already. */
#ifndef HAVE_STRTOUL
#define strtoul strtol
#endif

#ifndef HAVE_VA_COPY
#define va_copy(dest,src)	(dest) = (src)
#endif

/* Define WCOREDUMP if we don't have it already - coredumps won't be
 * detected, though.
 */
#ifndef WCOREDUMP
#define WCOREDUMP(x) 0
#endif

/* Set BUILDOLDPKGFORMAT to 1 to build old-format archives by default.
 */
#ifndef BUILDOLDPKGFORMAT
#define BUILDOLDPKGFORMAT 0
#endif

/* Take care of NLS matters.  */

#if HAVE_LOCALE_H
# include <locale.h>
#endif
#if !HAVE_SETLOCALE
# define setlocale(Category, Locale) /* empty */
#endif

#if ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
# define N_(Text) Text
#else
# undef bindtextdomain
# define bindtextdomain(Domain, Directory) /* empty */
# undef textdomain
# define textdomain(Domain) /* empty */ 
# define _(Text) Text
# define N_(Text) Text
# define gettext(Text) Text
#endif

])

AC_OUTPUT( po/Makefile.in
Makefile.conf
Makefile
include/Makefile
dpkg-deb/Makefile
split/Makefile
lib/Makefile
optlib/Makefile
doc/Makefile
man/Makefile
man/paths.ent
man/de/Makefile
man/en/Makefile
man/es/Makefile
man/fr/Makefile
man/ja/Makefile
man/pt_BR/Makefile
man/ru/Makefile
man/sv/Makefile
scripts/Makefile
main/Makefile
dselect/Makefile
methods/Makefile
utils/Makefile)
