# Copyright (C) 2006 Michael Halcrow <mhalcrow@us.ibm.com>
#  
# 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()
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(ecryptfs-kernel, 2.6.19)
AC_PROG_CC
AC_MINIX
AC_PROG_LIBTOOL

echo "checking kernel version; applying code modifiers..."

AC_DEFUN([ECFS_WITH_KERNELVER],
         [AC_ARG_WITH(kernelver,
                      AC_HELP_STRING([--with-kernelver=VER],
                                     [Specify kernel version to be in VER]),
                                     [with_kernelver=$withval])
          if test "x$with_kernelver" = "x" ; then
                  kernelver=`uname -r`
          else
                  kernelver=$with_kernelver
          fi
          echo " * Kernel version: [[$kernelver]]"
          AC_SUBST(kernelver)])

ECFS_WITH_KERNELVER

kernel_major_version=`echo $kernelver | \
                      sed 's/^\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\).*/\1/'`
echo "  * Major version detected: [[$kernel_major_version]]"
kernel_minor_version=`echo $kernelver | \
                      sed 's/^\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\).*/\2/'`
echo "  * Minor version detected: [[$kernel_minor_version]]"
kernel_micro_version=`echo $kernelver | \
                      sed 's/^\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\).*/\3/'`
echo "  * Micro version detected: [[$kernel_micro_version]]"
kernel_rc_version=`echo $kernelver | \
                   sed 's/^\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)-\(rc[[0-9]]*\).*/\4/'`
kernel_mm_version=`echo $kernelver | \
                   sed 's/^\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)-\(rc[[0-9]]*\)-\(mm[[0-9]]*\).*/\5/'`

echo $kernel_rc_version | grep "^rc" &> /dev/null
if test $? == 0; then
  echo "  * -rc version detected: [[$kernel_rc_version]]"
  echo $kernel_mm_version | grep "^mm" &> /dev/null
  if test $? == 0; then
    echo "  * -mm version detected: [[$kernel_mm_version]]"
  else
    kernel_mm_version=""
  fi
else
  kernel_rc_version=""
fi

if test $kernel_major_version != 2; then
  echo " * Invalid kernel major version number; expected 2; detected $kernel_major_version"
  exit 1
else
  if test $kernel_minor_version != 6; then
    echo " * Invalid minor version number; expected 6; detected $kernel_minor_version"
    exit 1
  else
    if test $kernel_micro_version -le 21; then
      AC_SUBST(LOOKUP_ONE_LEN_ND_2_6_21)
      LOOKUP_ONE_LEN_ND_2_6_21="#define LOOKUP_ONE_LEN_ND_2_6_21"
      echo " * Applying 2.6.21 lookup_one_len_nd() code (eCryptfs on NFS is broken for this kernel)"
      AC_SUBST(ZERO_USER_PAGE_2_6_21)
      ZERO_USER_PAGE_2_6_21="#define ZERO_USER_PAGE_2_6_21"
      echo " * Applying 2.6.21 zero_user_page() code"
    fi
    if test $kernel_micro_version -le 20; then
      AC_SUBST(CRYPTO_TFM_MODE_CBC_2_6_20)
      CRYPTO_TFM_MODE_CBC_2_6_20="#define CRYPTO_TFM_MODE_CBC_2_6_20"
      echo " * Applying 2.6.20 CRYPTO_TFM_MODE_CBC code"
    fi
    if test $kernel_micro_version -le 19; then
      AC_SUBST(FS_STACK_2_6_19)
      FS_STACK_2_6_19="#define FS_STACK_2_6_19"
      echo " * Applying 2.6.19 fs_stack code"
      AC_SUBST(FILE_PATH_2_6_19)
      FILE_PATH_2_6_19="#define FILE_PATH_2_6_19"
      echo " * Applying 2.6.19 file path code"
      AC_SUBST(CRYPTO_API_2_6_19)
      CRYPTO_API_2_6_19="#define CRYPTO_API_2_6_19"
      echo " * Applying 2.6.19 crypto API code"
      AC_SUBST(POSTPONE_CIPHER_INIT_ERR_2_6_19)
      POSTPONE_CIPHER_INIT_ERR_2_6_19="#define POSTPONE_CIPHER_INIT_ERR_2_6_19"
      echo " * Applying 2.6.19 cipher init err postpone code"
      AC_SUBST(SYNC_PAGE_2_6_19)
      SYNC_PAGE_2_6_19="#define SYNC_PAGE_2_6_19"
      echo " * Applying 2.6.19 sync page code"
    fi
    if test $kernel_micro_version -le 18; then
      AC_SUBST(READ_UPDATE_ATIME_2_6_18)
      READ_UPDATE_ATIME_2_6_18="#define READ_UPDATE_ATIME_2_6_18"
      echo " * Applying 2.6.18 ecryptfs_read_update_atime() code"
      AC_SUBST(FILLDIR_U64_INODE_2_6_18)
      FILLDIR_U64_INODE_2_6_18="#define FILLDIR_U64_INODE_2_6_18"
      echo " * Applying 2.6.18 ecryptfs_filldir() code"
    fi
    if test $kernel_micro_version -le 17; then
      AC_SUBST(READ_UPDATE_ATIME_2_6_17)
      READ_UPDATE_ATIME_2_6_17="#define READ_UPDATE_ATIME_2_6_17"
      echo " * Applying 2.6.17 ecryptfs_read_update_atime() code"
      AC_SUBST(FLUSH_2_6_17)
      FLUSH_2_6_17="#define FLUSH_2_6_17"
      echo " * Applying 2.6.17 ecryptfs_flush() code"
      AC_SUBST(MAIN_FOPS_2_6_17)
      MAIN_FOPS_2_6_17="#define MAIN_FOPS_2_6_17"
      echo " * Applying 2.6.17 ecryptfs_main_fops code"
      AC_SUBST(GET_SB_2_6_17)
      GET_SB_2_6_17="#define GET_SB_2_6_17"
      echo " * Applying 2.6.17 ecryptfs_get_sb() code"
      AC_SUBST(STATFS_2_6_17)
      STATFS_2_6_17="#define STATFS_2_6_17"
      echo " * Applying 2.6.17 ecryptfs_statfs() code"
      AC_SUBST(MUTEX_LOCK_NESTED_2_6_17)
      MUTEX_LOCK_NESTED_2_6_17="#define MUTEX_LOCK_NESTED_2_6_17"
      echo " * Applying 2.6.17 mutex_lock_nested() code"
      AC_SUBST(READ_MAPPING_PAGE_2_6_17)
      READ_MAPPING_PAGE_2_6_17="#define READ_MAPPING_PAGE_2_6_17"
      echo " * Applying 2.6.17 read_mapping_page() code"
    fi
    if test $kernel_micro_version -le 16; then
      AC_SUBST(SYNC_PAGE_2_6_16)
      SYNC_PAGE_2_6_16="#define SYNC_PAGE_2_6_16"
      echo " * Applying 2.6.16 ecryptfs_sync_page() code"
      AC_SUBST(ZALLOC_2_6_16)
      ZALLOC_2_6_16="#define ZALLOC_2_6_16"
      echo " * Applying 2.6.16 zalloc code"
      AC_SUBST(CONST_FOPS_2_6_16)
      CONST_FOPS_2_6_16="#define CONST_FOPS_2_6_16"
      echo " * Applying 2.6.16 const fops code"
    fi
  fi
fi

AC_DEFUN([ECFS_WITH_MODULESDIR],
         [AC_ARG_WITH(modulesdir,
                      AC_HELP_STRING([--with-modulesdir=DIR],
                                     [Specify modules to be in DIR]),
                                     [with_modulesdir=$withval])
          if test "x$with_modulesdir" = "x" ; then
                  modulesdir=/lib/modules/$kernelver
          else
                  modulesdir=$with_modulesdir
          fi
          if [[ -d $modulesdir ]]; then
                  echo "Modules directory: [[$modulesdir]]"
          else
                  echo
                  echo "Modules directory non-existent: [[$modulesdir]]"
                  echo "Pass a valid modules directory via --with-modulesdir"
                  echo
                  exit 1
          fi
          AC_SUBST(modulesdir)])

ECFS_WITH_MODULESDIR

AC_ISC_POSIX
AC_STDC_HEADERS
AC_PROG_RANLIB
AC_OUTPUT(Makefile ecryptfs/Makefile ecryptfs/config.h)
