AC_INIT(dump/dump.h)
AC_PREREQ(2.57)

MCONFIG=./MCONFIG
AC_SUBST_FILE(MCONFIG)

AC_CONFIG_HEADER(config.h)

dnl
dnl Check for programs
dnl
AC_PROG_MAKE_SET
AC_PROG_LN_S
AC_PATH_PROG(CP, cp, cp)
AC_PATH_PROG(MV, mv, mv)
AC_PATH_PROG(RM, rm, rm)
AC_CHECK_TOOL(AR, ar, ar)
AC_CHECK_TOOL(RANLIB, ranlib, :)
AC_CHECK_TOOL(PATCH, patch, :)
AC_PROG_CC
AC_PROG_INSTALL

AC_CHECK_HEADERS([sys/types.h])

CPPFLAGS="-D_BSD_SOURCE -D_USE_BSD_SIGNAL ${CPPFLAGS}"

dnl
dnl Handle --enable-debug
dnl
AC_ARG_ENABLE([debug],
[  --enable-debug             include debugging code (default is NO)],
if test "$enableval" = "no"
then
	DUMPDEBUG=""
	RESTOREDEBUG=""
	echo "Not including debugging code"
else
	DUMPDEBUG="-DFDEBUG -DTDEBUG -DWRITEDEBUG -DDIRDEBUG"
	RESTOREDEBUG="-DDIRDEBUG"
	echo "Including debugging code"
fi,
DUMPDEBUG=""
RESTOREDEBUG=""
echo "Not including debugging code by default"
)
AC_SUBST(DUMPDEBUG)
AC_SUBST(RESTOREDEBUG)

dnl
dnl Handle --enable-static
dnl
AC_ARG_ENABLE([static],
[  --enable-static            link dump and restore statically (default is NO)],
if test "$enableval" = "no"
then
	STATIC=""
	echo "Linking dump and restore dynamically"
else
	STATIC="-static"
	echo "Linking dump and restore statically"
fi
,
STATIC=""
echo "Linking dump and restore dynamically by default"
)
AC_SUBST(STATIC)

dnl
dnl Handle --enable-staticz
dnl
AC_ARG_ENABLE([staticz],
[  --enable-staticz           link libz and libbz2 statically (default is NO)],
if test "$enableval" = "no"
then
	STATICZ="no"
	echo "Linking libz and libbz2 dynamically"
else
	STATICZ="yes"
	echo "Linking libz and libbz2 statically"
fi
,
STATICZ="no"
echo "Linking libz and libbz2 dynamically by default"
)

dnl
dnl Handle --enable-rmt
dnl
AC_ARG_ENABLE([rmt],
[  --enable-rmt               compile and install rmt (default is YES)],
if test "$enableval" = "no"
then
	RMTDIR=""
	RMTMAKEFILE=""
	echo "Not compiling rmt"
else
	RMTDIR="rmt"
	RMTMAKEFILE="rmt/Makefile"
	echo "Compiling rmt"
fi
,
RMTDIR="rmt"
RMTMAKEFILE="rmt/Makefile"
echo "Compiling rmt by default"
)
AC_SUBST(RMTDIR)

dnl
dnl Handle --enable-ermt
dnl
AC_ARG_ENABLE([ermt],
[  --enable-ermt              compile ermt, an encrypting version of rmt (default is NO)],
if test "$enableval" = "no"
then
	ERMT=""
	echo "Not compiling ermt"
else
	if test "$RMTDIR" = ""
	then
		AC_MSG_ERROR(ermt requires --enable-rmt)
	fi
	ERMT="ermt"
	echo "Compiling ermt"
fi
,
ERMT=""
echo "Not compiling ermt by default"
)
AC_SUBST(ERMT)

dnl
dnl Handle --enable-kerberos
dnl
AC_ARG_ENABLE([kerberos],
[  --enable-kerberos          compile kerberos extensions (default is NO)],
if test "$enableval" = "yes"
then
	OPTDEFS="-DKERBEROS"
	echo "Compiling kerberos extensions"
else
	OPTDEFS=""
	echo "Not compiling kerberos extensions"
fi
,
OPTDEFS=""
echo "Not compiling kerberos extensions by default"
)
AC_SUBST(OPTDEFS)

dnl
dnl Handle --enable-readline
dnl
AC_ARG_ENABLE([readline],
[  --enable-readline          enable readline support in restore (default is YES)],
if test "$enableval" = "no"
then
	READLINE=""
	echo "Not including readline support"
else
	READLINE="yes"
	AC_DEFINE([HAVE_READLINE],1,[Define if you want to include readline support.])
	echo "Including readline support"
fi
,
READLINE="yes"
AC_DEFINE([HAVE_READLINE],1,[Define if you want to include readline support.])
echo "Including readline support by default"
)

dnl
dnl Handle --enable-oldsylefscript
dnl
AC_ARG_ENABLE([oldstylefscript],
[  --enable-oldstylefscript   enable old style F script (no arguments) (default is NO)],
if test "$enableval" = "yes"
then
	AC_DEFINE([OLD_STYLE_FSCRIPT],1,[Define this is you want old style F script (no arguments).])
	echo "Using old style F script"
else
	echo "Using new style F script"
fi
,
echo "Using new style F script by default"
)

dnl
dnl Handle --enable-largefile
dnl
AC_ARG_ENABLE([largefile],
[  --enable-largefile         enable Large File System support (default is YES)],
if test "$enableval" = "yes"
then
	AC_DEFINE([USE_LFS],1,[Define this if you want Large File System support.])
	echo "Enabling Large File System support"
else
	echo "Not enabling Large File System support"
fi
,
AC_DEFINE([USE_LFS],1,[Define this if you want Large File System support.])
echo "Enabling Large File System support by default"
)

dnl
dnl Handle --enable-qfa
dnl
AC_ARG_ENABLE([qfa],
[  --enable-qfa               enable Quick File Access support (default is YES)],
if test "$enableval" = "yes"
then
	AC_DEFINE([USE_QFA],1,[Define this if you want Quick File Access support.])
	echo "Enabling Quick File Access support"
else
	echo "Not enabling Quick File Access support"
fi
,
AC_DEFINE([USE_QFA],1,[Define this if you want Quick File Access support.])
echo "Enabling Quick File Access support by default"
)

dnl
dnl Handle --enable-qfadebug
dnl
AC_ARG_ENABLE([qfadebug],
[  --enable-qfadebug          include Quick File Access debugging code (default is NO)],
if test "$enableval" = "yes"
then
	AC_DEFINE([DEBUG_QFA],1,[Define this if you want to include Quick File Access debugging code.])
	echo "Including Quick File Access debugging code"
else
	echo "Not including Quick File Access debugging code"
fi
,
echo "Not including Quick File Access debugging code by default"
)

dnl
dnl Handle --enable-macosx
dnl
AC_ARG_ENABLE([macosx],
[  --enable-macosx            include Mac OSX restore compatibility (default is NO)],
if test "$enableval" = "yes"
then
	AC_DEFINE([DUMP_MACOSX],1,[Define this if you want to include Mac OSX restore compatibility.])
	echo "Including Mac OSX restore compatibility code"
else
	echo "Not including Mac OSX restore compatibility code"
fi
,
echo "Not including Mac OSX restore compatibility code by default"
)

dnl
dnl Handle --enable-transselinux
dnl
AC_ARG_ENABLE([transselinux],
[  --enable-transselinux      restore can translate SELinux EAs (default is YES)],
if test "$enableval" = "yes"
then
	LTRANSSELINUX="-lselinux"
	CCTRANSSELINUX="-DTRANSSELINUX"
	echo "restore can translate SELinux EAs"
else
	LTRANSSELINUX=""
	CCTRANSSELINUX=""
	echo "restore can not translate SELinux EAs"
fi
,
LTRANSSELINUX="-lselinux"
CCTRANSSELINUX="-DTRANSSELINUX"
echo "restore can translate SELinux EAs by default"
)
AC_SUBST(LTRANSSELINUX)
AC_SUBST(CCTRANSSELINUX)


dnl
dnl set $(CC) from --with-cc=value
dnl
AC_ARG_WITH([cc],
[  --with-cc=COMPILER         select compiler to use],
AC_MSG_RESULT(CC=$withval)
CC=$withval,
if test -z "$CC" ; then CC=cc; fi
[AC_MSG_RESULT(CC defaults to $CC)])dnl
export CC
AC_SUBST([CC])

dnl
dnl set $(LD) from --with-linker=value
dnl
AC_ARG_WITH([linker],
[  --with-linker=LINKER       select linker to use],
AC_MSG_RESULT(LD=$withval)
LD=$withval,
if test -z "$LD" ; then LD=$CC; fi
[AC_MSG_RESULT(LD defaults to $LD)])dnl
export LD
AC_SUBST([LD])

dnl
dnl set $(CCOPTS) from --with-ccopts=value
dnl
AC_ARG_WITH([ccopts],
[  --with-ccopts=CCOPTS       select compiler command line options],
AC_MSG_RESULT(CCOPTS is $withval)
CCOPTS=$withval
CFLAGS="$CFLAGS $withval",
CCOPTS=)dnl
AC_SUBST(CCOPTS)

dnl
dnl set $(LDFLAGS) from --with-ldopts=value
dnl
AC_ARG_WITH([ldopts],
[  --with-ldopts=LDOPTS       select linker command line options],
AC_MSG_RESULT(LDFLAGS is $withval)
LDOPTS=$withval
LDFLAGS="$LDFLAGS $withval",
LDOPTS=)dnl
AC_SUBST(LDOPTS)

dnl
dnl set $(BINOWNER) from --with-binowner
dnl
AC_ARG_WITH([binowner],
[  --with-binowner=USER       select owner for binaries],
AC_MSG_RESULT(BINOWNER is $withval)
BINOWNER=$withval,
BINOWNER=root
echo "BINOWNER defaults to $BINOWNER"
)dnl
AC_SUBST(BINOWNER)

dnl
dnl set $(BINGRP) from --with-bingrp
dnl
AC_ARG_WITH([bingrp],
[  --with-bingrp=GROUP        select group for binaries],
AC_MSG_RESULT(BINGRP is $withval)
BINGRP=$withval,
BINGRP=tty
echo "BINGRP defaults to $BINGRP"
)dnl
AC_SUBST(BINGRP)

dnl
dnl set $(BINMODE) from --with-binmode
dnl
AC_ARG_WITH([binmode],
[  --with-binmode=MODE        select mode for binaries],
AC_MSG_RESULT(BINMODE is $withval)
BINMODE=$withval,
BINMODE=0755
echo "BINMODE defaults to $BINMODE"
)dnl
AC_SUBST(BINMODE)

dnl
dnl set $(MANOWNER) from --with-manowner
dnl
AC_ARG_WITH([manowner],
[  --with-manowner=USER       select owner for manual pages],
AC_MSG_RESULT(MANOWNER is $withval)
MANOWNER=$withval,
MANOWNER=man
echo "MANOWNER defaults to $MANOWNER"
)dnl
AC_SUBST(MANOWNER)

dnl
dnl set $(MANGRP) from --with-mangrp
dnl
AC_ARG_WITH([mangrp],
[  --with-mangrp=GROUP        select group for manual pages],
AC_MSG_RESULT(MANGRP is $withval)
MANGRP=$withval,
MANGRP=tty
echo "MANGRP defaults to $MANGRP"
)dnl
AC_SUBST(MANGRP)

dnl
dnl set $(MANMODE) from --with-manmode
dnl
AC_ARG_WITH([manmode],
[  --with-manmode=MODE        select mode for manual pages],
AC_MSG_RESULT(MANMODE is $withval)
MANMODE=$withval,
MANMODE=0644
echo "MANMODE defaults to $MANMODE"
)dnl
AC_SUBST(MANMODE)

dnl
dnl set $(DUMPDATESPATH) from --with-dumpdatespath
dnl
AC_ARG_WITH([dumpdatespath],
[  --with-dumpdatespath=PATH  select path for dumpdates file],
AC_MSG_RESULT(DUMPDATESPATH is $withval)
DUMPDATESPATH=$withval,
DUMPDATESPATH="${sysconfdir}/dumpdates"
echo "DUMPDATESPATH defaults to $DUMPDATESPATH"
)dnl
AC_SUBST(DUMPDATESPATH)

dnl
dnl Check for Ext2fs headers and libraries
dnl
AC_CHECK_HEADER(ext2fs/ext2fs.h, [ext2fs_h=yes], [ext2fs_h=no], [-])
PKG_CHECK_MODULES(EXT2FS, [ext2fs])
if test -n "$STATIC" ; then
	EXT2FS_LIBS=`$PKG_CONFIG --libs --static ext2fs`
fi
if test "$ext2fs_h" = no -o "x$EXT2FS_LIBS" = "x"; then
	AC_MSG_ERROR(You need to install the Ext2fs libraries from the E2fsprogs distribution first - hint: make install-libs)
fi

dnl
dnl Check for ext2fs_read_inode_full
dnl
AC_CHECK_LIB(ext2fs, ext2fs_read_inode_full, [rif=yes], [rif=no], [-lcom_err])
if test "$rif" = yes; then
	AC_DEFINE([HAVE_EXT2FS_READ_INODE_FULL],1,[Define this if your ext2fs libs have the ext2fs_read_inode_full function.])
fi

dnl
dnl Try to use ext2_fs.h header from libext2fs instead of from the kernel
dnl
AC_CHECK_HEADERS(ext2fs/ext2_fs.h, [], [], [-])

dnl
dnl Check for ext2_ino_t type
dnl
AC_MSG_CHECKING(for ext2_ino_t type in libext2fs headers)
AC_TRY_COMPILE([#include <stdio.h>
#ifdef HAVE_EXT2FS_EXT2_FS_H
#include <ext2fs/ext2_fs.h>
#else
#include <linux/ext2_fs.h>
#endif
#include <ext2fs/ext2fs.h>],
[ext2_ino_t ino = 0;], 
[AC_DEFINE([HAVE_EXT2_INO_T],1,[Define if we have the ext2_ino_t type (from e2fsprogs 1.20+).])
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))

dnl
dnl Check for s_journal_inum field in ext2_super_block struct
dnl
AC_MSG_CHECKING(for s_journal_inum field in ext2_super_block struct)
AC_TRY_COMPILE([#include <stdio.h>
#ifdef HAVE_EXT2FS_EXT2_FS_H
#include <ext2fs/ext2_fs.h>
#else
#include <linux/ext2_fs.h>
#endif
#include <ext2fs/ext2fs.h>],
[struct ext2_super_block es; es.s_journal_inum = 0;],
[AC_DEFINE([HAVE_EXT2_JOURNAL_INUM],1,[Define if we have the s_journal_inum field in struct ext2_super_block.])
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))

dnl
dnl Check for blkid headers libraries
dnl
AC_CHECK_HEADER(blkid/blkid.h, [blkid_h=yes], [blkid_h=no], [-])
PKG_CHECK_EXISTS([blkid],
	[
	if test -n $STATIC ; then
		BLKID=`$PKG_CONFIG --libs --static blkid`
	else
		BLKID=`$PKG_CONFIG --libs blkid`
	fi
	if test "$blkid_h" = yes ; then
		AC_DEFINE([HAVE_BLKID],1,[Define this if you have the blkid library.])
	fi
	],[BLKID=""])
AC_SUBST(BLKID)

dnl
dnl Check for ncurses or termcap libraries
dnl
AC_CHECK_LIB(ncurses, tgetent, [ncurses_lib=yes], [ncurses_lib=no])
AC_CHECK_LIB(termcap, tgetent, [termcap_lib=yes], [termcap_lib=no])
AC_CHECK_LIB(tinfo, tgetent, [tinfo_lib=yes], [tinfo_lib=no])

if test "$ncurses_lib" = no -a "$termcap_lib" = no -a "$tinfo_lib" = no; then
	if test "$READLINE" = "yes"; then
		AC_MSG_ERROR(You need to install the ncurses or termcap library or configure without --enable-readline)
	fi
fi
if test "$tinfo_lib" = yes; then
	rdllib="-ltinfo"
elif test "$ncurses_lib" = yes; then
	rdllib="-lncurses"
elif test "$termcap_lib" = yes; then
	rdllib="-ltermcap"
fi

dnl
dnl Check for readline headers and libraries
dnl
AC_CHECK_HEADER(readline/readline.h, [readline_h=yes], [readline_h=no], [-])
AC_CHECK_LIB(readline, readline, [readline_lib=yes], [readline_lib=no], $rdllib)
if test "$readline_h" = no -o "$readline_lib" = no; then
	if test "$READLINE" = "yes"; then
		AC_MSG_ERROR(You need to install the GNU readline library or configure without --enable-readline)
	fi
fi
if test "$READLINE" = yes; then
	READLINE="-lreadline $rdllib"
fi
AC_SUBST(READLINE)

dnl
dnl Check for rl_completion_matches
dnl
AC_CHECK_LIB(readline, rl_completion_matches, [rlcm=yes], [rlcm=no], "-ltermcap")
if test "$rlcm" = yes; then
	AC_DEFINE([HAVE_READLINE_RLCM],1,[Define this if your readline libs have the rl_completion_matches library.])
fi

dnl
dnl Check for rl_completion_append_character
dnl
AC_CHECK_LIB(readline, rl_completion_append_character, [rcac=yes], [rcac=no], "-ltermcap")
if test "$rcac" = yes; then
	AC_DEFINE([HAVE_READLINE_CAC],1,[Define this if your readline libs have the rl_completion_append_character variable.])
fi

dnl
dnl Check for zlib headers and libraries
dnl
AC_CHECK_HEADER(zlib.h, [zlib_h=yes], [zlib_h=no], [-])
AC_CHECK_LIB(z, zlibVersion, [zlib_lib=yes], [zlib_lib=no])
if test "$zlib_h" = yes -a "$zlib_lib" = yes; then
	if test "$STATICZ" = yes; then
		ZLIB="-Wl,-Bstatic -lz -Wl,-Bdynamic"
	else
		ZLIB="-lz"
	fi
	AC_DEFINE([HAVE_ZLIB],1,[Define this if you have zlib compression library.])
else
	ZLIB=""
fi
AC_SUBST(ZLIB)

dnl
dnl Check for bzlib headers and libraries
dnl
AC_CHECK_HEADER(bzlib.h, [bzlib_h=yes], [bzlib_h=no], [-])
AC_CHECK_LIB(bz2, BZ2_bzBuffToBuffCompress, [bzlib_lib=yes], [bzlib_lib=no])
if test "$bzlib_h" = yes -a "$bzlib_lib" = yes; then
	if test "$STATICZ" = yes; then
		BZLIB="-Wl,-Bstatic -lbz2 -Wl,-Bdynamic"
	else
		BZLIB="-lbz2"
	fi
	AC_DEFINE([HAVE_BZLIB],1,[Define this if you have bzlib compression library.])
else
	BZLIB=""
fi
AC_SUBST(BZLIB)

dnl
dnl Check for library functions
dnl
AC_CHECK_FUNCS(err errx verr verrx vwarn vwarnx warn warnx realpath lchown)
AC_CHECK_FUNC(glob)

dnl
dnl Check for GLOB_ALTDIRFUNC
dnl
AC_MSG_CHECKING(for extended glob routines)
if test "$ac_cv_func_glob" = "yes"; then
	AC_EGREP_CPP(yes, 
	[
#	include <glob.h>
#	ifdef GLOB_ALTDIRFUNC
	yes
#	endif
	], 
	[
	AC_DEFINE([HAVE_GLOB],1,[Define if you have the glob function.])
	AC_MSG_RESULT(yes)
	],
	[
	AC_MSG_RESULT(no)
	echo "Your system does not support extended glob, will use the internal routines"
	])
fi

dnl
dnl Check for OpenSSL, for ermt
dnl
if test "$ERMT" != ""; then
	AC_CHECK_HEADER(openssl/evp.h, [evp_h=yes], [evp_h=no])
	AC_CHECK_LIB(crypto, EVP_CIPHER_CTX_set_padding, [crypto_lib=yes], [crypto_lib=no])
	PKG_CHECK_EXISTS([libcrypto],
		[
		if test -n $STATIC ; then
			CRYPTO=`$PKG_CONFIG --libs --static libcrypto`
		else
			CRYPTO=`$PKG_CONFIG --libs libcrypto`
		fi
		],[CRYPTO=""])
	if test "$evp_h" = no -o "x$CRYPTO" = "x"; then
		AC_MSG_ERROR(You need to install the OpenSSL library (version 0.9.7a or later), or configure without --enable-ermt)
	fi
fi
AC_SUBST(CRYPTO)

dnl
dnl Check for types
dnl
AC_CHECK_TYPE(quad_t, int64_t)
AC_CHECK_TYPE(u_quad_t, uint64_t)

dnl
dnl Compute top_buildir
dnl
top_builddir=`cd .; pwd`
AC_SUBST(top_builddir)

dnl
dnl Create directories
dnl
test -d compat || mkdir compat
test -d compat/lib || mkdir compat/lib

dnl
dnl Output files
dnl
AC_OUTPUT(MCONFIG Makefile common/Makefile compat/include/Makefile compat/lib/Makefile dump/Makefile restore/Makefile $RMTMAKEFILE)
