################################################################################
##
##    Copyright 1999-2001 Sistina Software, Inc.
##
##    This is free software released under the GNU General Public License.
##    There is no warranty for this software.  See the file COPYING for
##    details.
##
##    See the file CONTRIBUTORS for a list of contributors.
##
##    File name: configure.in
##
##    Description: Input file for autoconf.  Generates the configure script 
##                 that tries to keep everything nice and portable.  It also
##                 simplifies distribution package building considerably.
################################################################################

dnl Process this file with autoconf to produce a configure script.
AC_INIT(lib/libdevmapper.h)

dnl setup the directory where autoconf has auxilary files
AC_CONFIG_AUX_DIR(autoconf) 

dnl Checks for programs.
AC_PROG_AWK
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(ctype.h errno.h fcntl.h stdio.h sys/ioctl.h sys/stat.h sys/types.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_STRUCT_ST_RDEV
AC_HEADER_TIME

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_ARG_WITH(user,
  [  --with-user=USER        Set the owner of installed files ],
  [ OWNER="$withval" ],
  [ OWNER="root" ])

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

AC_ARG_ENABLE(jobs, [  --enable-jobs=NUM       Number of jobs to run simultaneously], JOBS=-j$enableval, JOBS=)

dnl Enable Debugging
AC_ARG_ENABLE(debug,    [  --enable-debug          Enable debugging],  \
DEBUG=yes, DEBUG=no)

dnl Disable Compatibility mode
AC_ARG_ENABLE(compat,   [  --disable-compat        Disable support for old device-mapper versions],  \
COMPAT=no, COMPAT=yes)

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 Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(mkdir rmdir uname)

echo $ac_n "checking for kernel directory""... $ac_c" 1>&6
dnl -- Where the linux src tree is
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
echo "$ac_t""$kerneldir" 1>&6

if test "x${kerneldir}" = x;  then
  missingkernel=yes
else
  test -d "${kerneldir}" || { echo "configure: WARNING: kernel dir $kerneldir not found" 1>&2; missingkernel=yes ; }
fi

echo $ac_n "checking for kernel version""... $ac_c" 1>&6
dnl -- Kernel version string
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
echo "$ac_t""$kernelvsn" 1>&6

dnl -- Temporary directory for kernel diffs
AC_ARG_WITH(tmp-dir,
  [  --with-tmp-dir=DIR      temp directory 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

echo $ac_n "checking for kernel interface choice""... $ac_c" 1>&6
dnl -- which kernel interface to use (ioctl or fs)
AC_ARG_WITH(interface,
  [  --with-interface=IFACE  Choose kernel interface (ioctl or fs) [ioctl]],
  [ interface="$withval" ],
  [ interface=ioctl ])
if [[ "x$interface" != xfs -a "x$interface" != xioctl ]];
then
  echo "configure: error: --with-interface=ioctl or --with-interface=fs required" 1>&2
  exit 1;
fi
echo "$ac_t""$interface" 1>&6

if test "-f VERSION" ; then
  DM_LIB_VERSION="\"`cat VERSION|sed -e \"s/[[0-9.]]*/\0-$interface/\"`\""
else
  DM_LIB_VERSION="Unknown version ($interface)"
fi


AC_SUBST(JOBS)
AC_SUBST(OWNER)
AC_SUBST(GROUP)
AC_SUBST(LIBS)
AC_SUBST(interface)
AC_SUBST(kerneldir)
AC_SUBST(missingkernel)
AC_SUBST(kernelvsn)
AC_SUBST(tmpdir)
AC_SUBST(DEBUG)
AC_SUBST(DM_LIB_VERSION)
AC_SUBST(COMPAT)
dnl First and last lines should not contain files to generate in order to 
dnl keep utility scripts running properly
AC_OUTPUT( 								\
Makefile								\
make.tmpl                                                               \
include/Makefile                                                        \
dmsetup/Makefile							\
lib/Makefile							 	\
lib/libdm-common.h						 	\
kernel/Makefile								\
man/Makefile								\
)

echo

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


