###############################################################################
## Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
## Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
##
## This file is part of the device-mapper userspace tools.
##
## This copyrighted material is made available to anyone wishing to use,
## modify, copy, or redistribute it subject to the terms and conditions
## of the GNU General Public License v.2.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software Foundation,
## Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
################################################################################

AC_PREREQ(2.57)
################################################################################
dnl -- Process this file with autoconf to produce a configure script.
AC_INIT
AC_CONFIG_SRCDIR([lib/libdevmapper.h])
AC_CONFIG_HEADERS(include/configure.h)

################################################################################
dnl -- Setup the directory where autoconf has auxilary files
AC_CONFIG_AUX_DIR(autoconf)

################################################################################
dnl -- Get system type
AC_CANONICAL_TARGET([])

case "$host_os" in
	linux*)
		COPTIMISE_FLAG="-O2"
		CLDFLAGS="$CLDFLAGS -Wl,--version-script,.export.sym"
		CLDWHOLEARCHIVE="-Wl,-whole-archive"
		CLDNOWHOLEARCHIVE="-Wl,-no-whole-archive"
		LDDEPS="$LDDEPS .export.sym"
		LIB_SUFFIX=so
		DEVMAPPER=yes
		ODIRECT=yes
		DM_IOCTLS=yes
		SELINUX=yes
		REALTIME=yes
		CLUSTER=internal
		FSADM=no
		;;
	darwin*)
		CFLAGS="$CFLAGS -no-cpp-precomp -fno-common"
		COPTIMISE_FLAG="-O2"
		CLDFLAGS="$CLDFLAGS"
		CLDWHOLEARCHIVE="-all_load"
		CLDNOWHOLEARCHIVE=
		LIB_SUFFIX=dylib
		DEVMAPPER=yes
		ODIRECT=no
		DM_IOCTLS=no
		SELINUX=no
		REALTIME=no
		CLUSTER=none
		FSADM=no
		;;
esac

################################################################################
dnl -- Additional library location
usrlibdir='${prefix}/lib'

################################################################################
dnl -- Check for programs.
AC_PROG_AWK
AC_PROG_CC

dnl probably no longer needed in 2008, but...
AC_PROG_GCC_TRADITIONAL
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_PATH_PROG(CFLOW_CMD, cflow)
AC_PATH_PROG(CSCOPE_CMD, cscope)

################################################################################
dnl -- Check for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_HEADER_TIME

AC_CHECK_HEADERS([ctype.h dirent.h errno.h fcntl.h getopt.h inttypes.h limits.h \
  stdarg.h stdio.h stdlib.h string.h sys/ioctl.h sys/param.h sys/stat.h \
  sys/types.h unistd.h], , [AC_MSG_ERROR(bailing out)])
AC_CHECK_HEADERS(termios.h sys/statvfs.h)

################################################################################
dnl -- Check for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_CHECK_MEMBERS([struct stat.st_rdev])
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIGNAL
AC_TYPE_SIZE_T
AC_TYPE_MODE_T
AC_CHECK_MEMBERS([struct stat.st_rdev])
AC_STRUCT_TM

################################################################################
dnl -- Check for functions
AC_CHECK_FUNCS([gethostname getpagesize memset mkdir rmdir munmap setlocale \
  strcasecmp strchr strdup strncasecmp strerror strrchr strstr strtol strtoul \
  uname], , [AC_MSG_ERROR(bailing out)])
AC_FUNC_ALLOCA
AC_FUNC_CLOSEDIR_VOID
AC_FUNC_FORK
AC_FUNC_LSTAT
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_MMAP
AC_FUNC_STAT
AC_FUNC_STRTOD
AC_FUNC_VPRINTF

################################################################################
dnl -- Prefix is /usr by default, the exec_prefix default is setup later
AC_PREFIX_DEFAULT(/usr)

################################################################################
dnl -- Setup the ownership of the files
AC_MSG_CHECKING(file owner)
OWNER="root"

AC_ARG_WITH(user,
  [  --with-user=USER        Set the owner of installed files [[USER=root]] ],
  [ OWNER="$withval" ])
AC_MSG_RESULT($OWNER)

if test x$OWNER != x; then
	OWNER="-o $OWNER"
fi

################################################################################
dnl -- Setup the group ownership of the files
AC_MSG_CHECKING(group owner)
GROUP="root"
AC_ARG_WITH(group,
  [  --with-group=GROUP      Set the group owner of installed files [[GROUP=root]] ],
  [ GROUP="$withval" ])
AC_MSG_RESULT($GROUP)

if test x$GROUP != x; then
	GROUP="-g $GROUP"
fi

################################################################################
dnl -- Setup device node ownership
AC_MSG_CHECKING(device node uid)

AC_ARG_WITH(device-uid,
  [  --with-device-uid=UID   Set the owner used for new device nodes [[UID=0]] ],
  [ DM_DEVICE_UID="$withval" ], [ DM_DEVICE_UID="0" ] )
AC_MSG_RESULT($DM_DEVICE_UID)

################################################################################
dnl -- Setup device group ownership
AC_MSG_CHECKING(device node gid)

AC_ARG_WITH(device-gid,
  [  --with-device-gid=UID   Set the group used for new device nodes [[GID=0]] ],
  [ DM_DEVICE_GID="$withval" ], [ DM_DEVICE_GID="0" ] )
AC_MSG_RESULT($DM_DEVICE_GID)

################################################################################
dnl -- Setup device mode
AC_MSG_CHECKING(device node mode)

AC_ARG_WITH(device-mode,
  [  --with-device-mode=MODE Set the mode used for new device nodes [[MODE=0600]] ],
  [ DM_DEVICE_MODE="$withval" ], [ DM_DEVICE_MODE="0600" ] )
AC_MSG_RESULT($DM_DEVICE_MODE)

################################################################################
dnl -- Enable debugging
AC_MSG_CHECKING(whether to enable debugging)
AC_ARG_ENABLE(debug,    [  --enable-debug          Enable debugging],
  DEBUG=$enableval, DEBUG=no)
AC_MSG_RESULT($DEBUG)

dnl -- Normally turn off optimisation for debug builds
if test x$DEBUG = xyes; then
	COPTIMISE_FLAG=
else
	CSCOPE_CMD=
fi

################################################################################
dnl -- Override optimisation
AC_MSG_CHECKING(for C optimisation flag)
AC_ARG_WITH(optimisation,
  [  --with-optimisation=OPT C optimisation flag [[OPT=-O2]] ],
  [ COPTIMISE_FLAG="$withval" ])
AC_MSG_RESULT($COPTIMISE_FLAG)

################################################################################
dnl -- Compatibility mode
AC_ARG_ENABLE(compat,   [  --enable-compat         Enable support for old device-mapper versions],
  DM_COMPAT=$enableval, DM_COMPAT=no)

################################################################################
dnl -- Disable ioctl
AC_ARG_ENABLE(ioctl,   [  --disable-driver        Disable calls to device-mapper in the kernel],
  DM_IOCTLS=$enableval)

################################################################################
dnl -- Enable dmeventd
AC_ARG_ENABLE(dmeventd,   [  --enable-dmeventd       Build the new event daemon],
  BUILD_DMEVENTD=$enableval, BUILD_DMEVENTD=no)

################################################################################
dnl -- Enable pkg-config
AC_ARG_ENABLE(pkgconfig,   [  --enable-pkgconfig      Install pkgconfig support],
  PKGCONFIG=$enableval, PKGCONFIG=no)

################################################################################
dnl -- Clear default exec_prefix - install into /sbin rather than /usr/sbin
if [[ "x$exec_prefix" = xNONE -a "x$prefix" = xNONE ]];
 then  exec_prefix="";
fi;

################################################################################
dnl -- getline included in recent libc

AC_CHECK_LIB(c, getline, AC_DEFINE([HAVE_GETLINE], 1,
  [Define to 1 if getline is available.]))

################################################################################
dnl -- canonicalize_file_name included in recent libc

AC_CHECK_LIB(c, canonicalize_file_name,
  AC_DEFINE([HAVE_CANONICALIZE_FILE_NAME], 1,
    [Define to 1 if canonicalize_file_name is available.]))

################################################################################
dnl -- Enables statically-linked tools
AC_MSG_CHECKING(whether to use static linking)
AC_ARG_ENABLE(static_link,
  [  --enable-static_link    Use this to link the tools to their libraries
                          statically.  Default is dynamic linking],
  STATIC_LINK=$enableval, STATIC_LINK=no)
AC_MSG_RESULT($STATIC_LINK)

################################################################################
dnl -- Disable selinux
AC_MSG_CHECKING(whether to enable selinux support)
AC_ARG_ENABLE(selinux, [  --disable-selinux       Disable selinux support],
  SELINUX=$enableval)
AC_MSG_RESULT($SELINUX)

################################################################################
dnl -- Check for selinux
if test x$SELINUX = xyes; then
	AC_CHECK_LIB(sepol, sepol_check_context, HAVE_SEPOL=yes, HAVE_SEPOL=no)

	if test x$HAVE_SEPOL = xyes; then
		AC_DEFINE([HAVE_SEPOL], 1,
		  [Define to 1 if sepol_check_context is available.])
		LIBS="-lsepol $LIBS"
	fi

	AC_CHECK_LIB(selinux, is_selinux_enabled, HAVE_SELINUX=yes, HAVE_SELINUX=no)

	if test x$HAVE_SELINUX = xyes; then
		AC_DEFINE([HAVE_SELINUX], 1, [Define to 1 to include support for selinux.])
		LIBS="-lselinux $LIBS"
	else
		AC_MSG_WARN(Disabling selinux)
	fi

	# With --enable-static_link and selinux enabled, linking
	# fails on at least Debian unstable due to unsatisfied references
	# to pthread_mutex_lock and _unlock.  See if we need -lpthread.
	if test "$STATIC_LINK-$HAVE_SELINUX" = yes-yes; then
		lvm_saved_libs=$LIBS
		LIBS="$LIBS -static"
		AC_SEARCH_LIBS([pthread_mutex_lock], [pthread],
		  [test "$ac_cv_search_pthread_mutex_lock" = "none required" ||
				LIB_PTHREAD=-lpthread])
		LIBS=$lvm_saved_libs
	fi
fi

################################################################################
dnl -- Check for getopt
AC_CHECK_HEADERS(getopt.h, AC_DEFINE([HAVE_GETOPTLONG], 1, [Define to 1 if getopt_long is available.]))

################################################################################
dnl -- Check for readline (Shamelessly copied from parted 1.4.17)
if test x$READLINE = xyes; then
	AC_CHECK_LIB(readline, readline, ,
		AC_MSG_ERROR(
GNU Readline could not be found which is required for the
--enable-readline option (which is enabled by default).  Either disable readline
support with --disable-readline or download and install readline from:
	ftp.gnu.org/gnu/readline
Note: if you are using precompiled packages you will also need the development
package as well (which may be called readline-devel or something similar).
)
	)
	AC_CHECK_FUNC(rl_completion_matches, AC_DEFINE([HAVE_RL_COMPLETION_MATCHES], 1, [Define to 1 if rl_completion_matches() is available.]))
fi

################################################################################
dnl -- Internationalisation stuff
AC_MSG_CHECKING(whether to enable internationalisation)
AC_ARG_ENABLE(nls, [  --enable-nls            Enable Native Language Support],
		INTL=$enableval, INTL=no)
AC_MSG_RESULT($INTL)

if test x$INTL = xyes; then
	INTL_PACKAGE="device-mapper"
	AC_PATH_PROG(MSGFMT, msgfmt)
	if [[ "x$MSGFMT" == x ]];
		then  AC_MSG_ERROR(
		msgfmt not found in path $PATH
		)
	fi;

	AC_ARG_WITH(localedir,
		    [  --with-localedir=DIR    Translation files in DIR [[PREFIX/share/locale]] ],
		    [ LOCALEDIR="$withval" ],
		    [ LOCALEDIR='${prefix}/share/locale' ])
fi

################################################################################
dnl -- Where the linux src tree is
AC_MSG_CHECKING(for kernel directory)
AC_ARG_WITH(kerneldir,
  [  --with-kernel-dir=DIR   linux kernel source in DIR []],
  [ kerneldir="$withval" ] )
if test "${with_kernel_dir+set}" = set; then
   kerneldir="$with_kernel_dir"
fi
if test "${with_kernel-dir+set}" = set; then
   kerneldir="$with_kerneldir"
fi
if test "${with_kernel-src+set}" = set; then
   kerneldir="$with_kernel-src"
fi
if test "${with_kernel_src+set}" = set; then
   kerneldir="$with_kernel_src"
fi
if test "${with_kernel+set}" = set; then
   kerneldir="$with_kernel"
fi
AC_MSG_RESULT($kerneldir)

if test "x${kerneldir}" = x;  then
  missingkernel=yes
else
  test -d "${kerneldir}" || { AC_MSG_WARN(kernel dir $kerneldir not found); missingkernel=yes ; }
fi

################################################################################
dnl -- Kernel version string
AC_MSG_CHECKING(for kernel version)
AC_ARG_WITH(kernel-version,
  [  --with-kernel-version=VERSION   linux kernel version] )

if test "${with_kernel-version+set}" = set; then
   kernelvsn="$with_kernel-version"
fi
if test "${with_kernelvsn+set}" = set; then
   kernelvsn="$with_kernelvsn"
fi
if test "${with_kernel_version+set}" = set; then
   kernelvsn="$with_kernel_version"
fi
if test "${with_kernelversion+set}" = set; then
   kernelvsn="$with_kernelversion"
fi

if test "x${kernelvsn}" = x; then
  if test "x${missingkernel}" = "x"; then
    kernelvsn=`awk -F ' = ' '/^VERSION/ {v=$2} /^PATCH/ {p=$2} /^SUBLEVEL/ {s=$2} /^EXTRAVERSION/ {e=$2} END {printf "%d.%d.%d%s",v,p,s,e}' $kerneldir/Makefile`
  else
    kernelvsn="UNKNOWN"
  fi
fi
AC_MSG_RESULT($kernelvsn)

################################################################################
dnl -- Temporary directory for kernel diffs
AC_ARG_WITH(tmp-dir,
  [  --with-tmp-dir=DIR      temp dir to make kernel patches [[/tmp/kerndiff]] ],
  [ tmpdir="$withval" ],
  [ tmpdir=/tmp/kerndiff ])
if test "${with_tmp_dir+set}" = set; then
   tmpdir="$with_tmp_dir"
fi
if test "${with_tmpdir+set}" = set; then
   tmpdir="$with_tmpdir"
fi

################################################################################
dnl -- which kernel interface to use (ioctl only)
AC_MSG_CHECKING(for kernel interface choice)
AC_ARG_WITH(interface,
  [  --with-interface=IFACE  Choose kernel interface (ioctl) [[ioctl]] ],
  [ interface="$withval" ],
  [ interface=ioctl ])
if [[ "x$interface" != xioctl ]];
then
  AC_MSG_ERROR(--with-interface=ioctl required. fs no longer supported.)
fi
AC_MSG_RESULT($interface)

DM_LIB_VERSION="\"`cat VERSION 2>/dev/null || echo Unknown`\""

AC_DEFINE_UNQUOTED(DM_LIB_VERSION, $DM_LIB_VERSION, [Library version])

################################################################################
dnl -- dmeventd pidfile and executable path
AH_TEMPLATE(DMEVENTD_PIDFILE, [Path to dmeventd pidfile.])
if test "$BUILD_DMEVENTD" = yes; then
	AC_ARG_WITH(dmeventd-pidfile,
		    [  --with-dmeventd-pidfile=PATH    dmeventd pidfile [[/var/run/dmeventd.pid]] ],
		    [ AC_DEFINE_UNQUOTED(DMEVENTD_PIDFILE,"$withval") ],
		    [ AC_DEFINE_UNQUOTED(DMEVENTD_PIDFILE,"/var/run/dmeventd.pid") ])
fi

AH_TEMPLATE(DMEVENTD_PATH, [Path to dmeventd binary.])
if test "$BUILD_DMEVENTD" = yes; then
        dmeventd_prefix="$exec_prefix"
        if test "x$dmeventd_prefix" = "xNONE"; then
                dmeventd_prefix="$prefix"
        fi
        if test "x$dmeventd_prefix" = "xNONE"; then
                dmeventd_prefix=""
        fi
	AC_ARG_WITH(dmeventd-path,
		    [  --with-dmeventd-path=PATH       dmeventd path [[${exec_prefix}/sbin/dmeventd]] ],
		    [ AC_DEFINE_UNQUOTED(DMEVENTD_PATH,"$withval") ],
		    [ AC_DEFINE_UNQUOTED(DMEVENTD_PATH,"$dmeventd_prefix/sbin/dmeventd") ])
fi

################################################################################
AC_SUBST(BUILD_DMEVENTD)
AC_SUBST(CFLAGS)
AC_SUBST(CFLOW_CMD)
AC_SUBST(CLDFLAGS)
AC_SUBST(CLDNOWHOLEARCHIVE)
AC_SUBST(CLDWHOLEARCHIVE)
AC_SUBST(CLUSTER)
AC_SUBST(CLVMD)
AC_SUBST(CMDLIB)
AC_SUBST(COPTIMISE_FLAG)
AC_SUBST(CSCOPE_CMD)
AC_SUBST(DEBUG)
AC_SUBST(DEVMAPPER)
AC_SUBST(DMDIR)
AC_SUBST(DM_COMPAT)
AC_SUBST(DM_DEVICE_GID)
AC_SUBST(DM_DEVICE_MODE)
AC_SUBST(DM_DEVICE_UID)
AC_SUBST(DM_IOCTLS)
AC_SUBST(DM_LIB_VERSION)
AC_SUBST(FSADM)
AC_SUBST(GROUP)
AC_SUBST(HAVE_LIBDL)
AC_SUBST(HAVE_REALTIME)
AC_SUBST(HAVE_SELINUX)
AC_SUBST(INTL)
AC_SUBST(INTL_PACKAGE)
AC_SUBST(JOBS)
AC_SUBST(LDDEPS)
AC_SUBST(LIBS)
AC_SUBST(LIB_SUFFIX)
AC_SUBST(LOCALEDIR)
AC_SUBST(LVM1)
AC_SUBST(LVM1_FALLBACK)
AC_SUBST(LVM_CONF_DIR)
AC_SUBST(LVM_VERSION)
AC_SUBST(MIRRORS)
AC_SUBST(MSGFMT)
AC_SUBST(OWNER)
AC_SUBST(PKGCONFIG)
AC_SUBST(POOL)
AC_SUBST(SNAPSHOTS)
AC_SUBST(STATICDIR)
AC_SUBST(STATIC_LINK)
AC_SUBST([LIB_PTHREAD])
AC_SUBST(interface)
AC_SUBST(kerneldir)
AC_SUBST(missingkernel)
AC_SUBST(kernelvsn)
AC_SUBST(tmpdir)
AC_SUBST(usrlibdir)

################################################################################
dnl -- First and last lines should not contain files to generate in order to
dnl -- keep utility scripts running properly
AC_CONFIG_FILES([\
Makefile								\
make.tmpl                                                               \
include/Makefile                                                        \
dmsetup/Makefile							\
lib/Makefile								\
lib/libdevmapper.pc							\
dmeventd/Makefile							\
dmeventd/libdevmapper-event.pc						\
kernel/Makefile								\
man/Makefile								\
po/Makefile								\
])
AC_OUTPUT

if test "x${kerneldir}" != "x" ; then
  if test -d "${kerneldir}"; then
    if test ! -f "${kerneldir}/include/linux/dm-ioctl.h"; then
      AC_MSG_WARN(Your kernel source in ${kerneldir} needs patching)
      if test "x${kernelvsn}" != "xUNKNOWN"; then
        AC_MSG_WARN([For supported kernels, try 'make apply-patches' next to do this, or apply the
device-mapper patches by hand.
])
      fi
    fi
  else
    AC_MSG_WARN(kernel directory $kerneldir not found)
  fi
  if test "x${kernelvsn}" = "xUNKNOWN"; then
    AC_MSG_WARN([kernel version not detected: 'make apply-patches' won't work.
If your kernel already contains device-mapper it may be OK,
otherwise you'll need to apply the device-mapper patches by hand.
])
  fi
fi


