# Copyright (C) 2006-2007 International Business Machines Corp.
#  
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without 
# modifications, as long as this notice is preserved.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


AC_INIT(reconf)
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(ecryptfs, 0.4.0)
AC_PROG_CC
AC_MINIX
AC_PROG_LIBTOOL
AC_PATH_PROG(POD2MAN, pod2man)
if test "x$POD2MAN" = x
then
        #
        # The alternative is not to build the man pages....
        #
        AC_MSG_ERROR(I couldn't find pod2man; make sure it's installed and in your path)
fi

AC_SUBST(POD2MAN)
# Verify gcrypt
AC_CHECK_LIB(gcrypt, gcry_md_open, [], [
		     echo "eCryptfs userspace tools require Libgcrypt."
		     echo "You might find the package at:"
		     echo "http://directory.fsf.org/security/libgcrypt.html"
		     exit -1
		     ])

AC_CHECK_HEADER(gcrypt.h)

# Verify keyutil version 1.0 or higher
AC_CHECK_LIB(keyutils, keyctl_search, [], [
		      echo "eCryptfs userspace tools require Keyutils version 1.0 or higher."
		      echo "You might find the package at:"
		      echo "http://people.redhat.com/~dhowells/keyutils/"
		      exit -1
		      ])
AC_CHECK_HEADER(keyutils.h)

if test "x$prefix" = xNONE; then
    prefix=$ac_default_prefix
fi

test_ock=true
test_openssl=true
test_tspi=true
test_pam=true

OCK_LIBLIBRARIES_STR=libecryptfs_pki_opencryptoki.la
OCK_INSTALL_STR='$(INSTALL_DATA) .libs/libecryptfs_pki_opencryptoki.* $(DESTDIR)/$(libdir)/ecryptfs/'
OCK_SCRUB_STR='rm -f $(DESTDIR)/$(libdir)/ecryptfs/libecryptfs_pki_opencryptoki*[[0ai]]'

OPENSSL_LIBLIBRARIES_STR=libecryptfs_pki_openssl.la
OPENSSL_INSTALL_STR='$(INSTALL_DATA) .libs/libecryptfs_pki_openssl.* $(DESTDIR)/$(libdir)/ecryptfs/'
OPENSSL_SCRUB_STR='rm -f $(DESTDIR)/$(libdir)/ecryptfs/libecryptfs_pki_openssl*[[0ai]]'

TSPI_LIBLIBRARIES_STR=libecryptfs_pki_tspi.la
TSPI_INSTALL_STR='$(INSTALL_DATA) .libs/libecryptfs_pki_tspi.* $(DESTDIR)/$(libdir)/ecryptfs/'
TSPI_SCRUB_STR='rm -f $(DESTDIR)/$(libdir)/ecryptfs/libecryptfs_pki_tspi*[[0ai]]'

PASSPHRASE_LIBLIBRARIES_STR=libecryptfs_pki_passphrase.la
PASSPHRASE_INSTALL_STR='$(INSTALL_DATA) .libs/libecryptfs_pki_passphrase.* $(DESTDIR)/$(libdir)/ecryptfs/'
PASSPHRASE_SCRUB_STR='rm -f $(DESTDIR)/$(libdir)/ecryptfs/libecryptfs_pki_passphrase*[[0ai]]'

AC_SUBST(PASSPHRASE_LIBLIBRARIES_STR)
AC_SUBST(PASSPHRASE_INSTALL_STR)
AC_SUBST(PASSPHRASE_SCRUB_STR)

AC_ARG_ENABLE(opencryptoki,
              AC_HELP_STRING([--enable-opencryptoki],
                             [Build openCryptoki key module]),
              [if test x"${enableval}" == x"yes" ; then
                   test_ock=true
               else
                   test_ock=false
               fi])

if test "x$test_ock" = "xtrue" ; then
    AC_CHECK_LIB(opencryptoki, C_Login,
                 [AC_CHECK_HEADER(opencryptoki/pkcs11types.h,
                                  [opencryptoki=true])])
fi

if test "x$opencryptoki" = "xtrue" ; then
   AM_CONDITIONAL(BUILD_OCK, true)
else
   AM_CONDITIONAL(BUILD_OCK, false)
   OCK_LIBLIBRARIES_STR=
   OCK_INSTALL_STR=
   OCK_SCRUB_STR=
fi

AC_SUBST(OCK_LIBLIBRARIES_STR)
AC_SUBST(OCK_INSTALL_STR)
AC_SUBST(OCK_SCRUB_STR)

AC_ARG_ENABLE(openssl,
              AC_HELP_STRING([--enable-openssl],
                             [Build OpenSSL key module]),
              [if test x"${enableval}" == x"yes" ; then
                   test_openssl=true
               else
                   test_openssl=false
               fi])

if test "x$test_openssl" = "xtrue" ; then
    AC_CHECK_LIB(ssl, SSL_new,
                 [AC_CHECK_HEADER(openssl/rsa.h,
                                  [openssl=true])])
fi

if test "x$openssl" = "xtrue" ; then
   AM_CONDITIONAL(BUILD_OPENSSL, true)
else
   AM_CONDITIONAL(BUILD_OPENSSL, false)
   OPENSSL_LIBLIBRARIES_STR=
   OPENSSL_INSTALL_STR=
   OPENSSL_SCRUB_STR=
fi

AC_SUBST(OPENSSL_LIBLIBRARIES_STR)
AC_SUBST(OPENSSL_INSTALL_STR)
AC_SUBST(OPENSSL_SCRUB_STR)

AC_ARG_ENABLE(tspi,
              AC_HELP_STRING([--enable-tspi],
                             [Build TrouSerS key module]),
              [if test x"${enableval}" == x"yes" ; then
                   test_tspi=true
               else
                   test_tspi=false
               fi])

if test "x$test_tspi" = "xtrue" ; then
    AC_CHECK_LIB(tspi, Tspi_Context_Create,
                 [AC_CHECK_HEADER(trousers/tss.h,
                                  [tspi=true])])
fi

if test "x$tspi" = "xtrue" ; then
   AM_CONDITIONAL(BUILD_TSPI, true)
else
   AM_CONDITIONAL(BUILD_TSPI, false)
   TSPI_LIBLIBRARIES_STR=
   TSPI_INSTALL_STR=
   TSPI_SCRUB_STR=
fi

AC_SUBST(TSPI_LIBLIBRARIES_STR)
AC_SUBST(TSPI_INSTALL_STR)
AC_SUBST(TSPI_SCRUB_STR)

AC_ARG_ENABLE(pam,
              AC_HELP_STRING([--enable-pam],
                             [Build PAM module]),
              [if test x"${enableval}" == x"yes" ; then
                   test_pam=true
               else
                   test_pam=false
               fi])

if test "x$test_pam" = "xtrue" ; then
    AC_CHECK_LIB(pam, pam_authenticate,
                 [AC_CHECK_HEADER(security/pam_modules.h,
                                  [pam=true])])
    AC_CHECK_LIB(pam_misc, pam_misc_setenv, [pam_misc=true])
fi

if test "x$pam" = "xtrue" ; then
   if test "x$pam_misc" = "xtrue" ; then
      AM_CONDITIONAL(BUILD_PAM, true)
   else
      AM_CONDITIONAL(BUILD_PAM, false)
   fi
else
   AM_CONDITIONAL(BUILD_PAM, false)
fi

case $target in
        *64* | *s390x)
                if test "x$prefix" != "x/usr" ; then
                        PAMDIR="$prefix/lib64/security"
		else
                        PAMDIR="/lib64/security"
		fi
		;;
        *)
                if test "x$prefix" != "x/usr" ; then
                        PAMDIR="$prefix/lib/security"
		else
                        PAMDIR="/lib/security"
		fi
		;;
esac

AC_ARG_WITH([pamdir],
            AC_HELP_STRING([--with-pamdir=PATH],
                           [Specify the directory where PAM modules are stored]),
            [PAMDIR=$withval],
	    [case $target in
                 *64* | *s390x)
                     if test "x$prefix" != "x/usr" ; then
                         PAMDIR="$prefix/lib64/security"
 	             else
                         PAMDIR="/lib64/security"
 		     fi
 		     ;;
                 *)
                     if test "x$prefix" != "x/usr" ; then
                         PAMDIR="$prefix/lib/security"
		     else
                         PAMDIR="/lib/security"
		     fi
		     ;;
             esac])

if test "x$pam" = "xtrue" ; then
   echo "PAM directory: [[$PAMDIR]]"
else
   echo "PAM module disabled"
fi
AC_SUBST(PAMDIR)

AC_DEFINE(_GNU_SOURCE)

AC_ARG_ENABLE(gui,
              AC_HELP_STRING([------------------------
  --enable-gui            Enable building of GUI components
  --disable-gui           Disable building of GUI components
  ------------------------]),
              AC_DEFINE(ENABLE_GUI, 1, [Enable building of GUI components]))

if test "x$enable_gui" != "xno"; then
dnl        echo "GUI not disabled"
	gtk_ver=`gtk-config --version`
	gtk_cflags=`gtk-config --cflags`
	gtk_libs=`gtk-config --libs`
	if test $? == 0; then
                gtk_major_version=`gtk-config --version | \
                                   sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
                gtk_minor_version=`gtk-config --version | \
                                   sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
                gtk_micro_version=`gtk-config --version | \
                                   sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
                if test $gtk_major_version != 1; then
                        echo "Invalid major version number; expected 1; detected $gtk_major_version"
                        enable_gui="no"
                else
                        if test $gtk_minor_version != 2; then
                                echo "Invalid minor version number; expected 2; detected $gtk_minor_version"
                                enable_gui="no"
                        fi
                fi
        else
                echo "GTK not found"
        fi
else
        enable_gui="no"
fi

dnl if test "x$enable_gui" != "xno"; then
dnl TODO: include src/gui in Makefile
dnl        echo "GUI disabled"
dnl fi

AC_ISC_POSIX
AC_STDC_HEADERS
AC_PROG_RANLIB

case $target in
	*64* | *s390x)
		CFLAGS="$CFLAGS -m64 -DECRYPTFS_DEFAULT_PKI_DIR=\\\"$prefix/lib64/ecryptfs\\\""
		LDFLAGS="$LDFLAGS -m64" ;;
	*)
		CFLAGS="$CFLAGS -DECRYPTFS_DEFAULT_PKI_DIR=\\\"$prefix/lib/ecryptfs\\\""
		;;
esac

AC_ARG_ENABLE(mudflap,
              AC_HELP_STRING([--enable-mudflap],
                             [Build with -fmudflap gcc option]),
              [if test x"${enableval}" == x"yes" ; then
                   mudflap=true
               else
                   mudflap=false
               fi])

if test "x$mudflap" = "xtrue" ; then
   CFLAGS="$CFLAGS -fmudflap"
   LDFLAGS="$LDFLAGS -lmudflap"
fi

AC_OUTPUT(Makefile doc/Makefile m4/Makefile src/Makefile \
	  src/libecryptfs/Makefile src/utils/Makefile src/pki/Makefile \
	  src/daemon/Makefile src/include/Makefile doc/manpage/Makefile \
	  src/pam_ecryptfs/Makefile)

if test "x$prefix" != "x/usr";
then
	echo
	echo "Warning: install prefix is $prefix"
	echo
	echo "Most mount applications expect the mount helper to install to"
	echo "/sbin. You can install the mount helper to that location by "
	echo "running the configure script again with --prefix=/usr:"
	echo
	echo "./configure --prefix=/usr"
	echo
fi
