dnl Process this file with autoconf to produce a configure script.
AC_INIT([chmlib],[0.40])
dnl,[Jed Wing <jedwin@ugcs.caltech.edu>])
AC_CONFIG_SRCDIR(src/chm_lib.c)
AM_INIT_AUTOMAKE
AM_MAINTAINER_MODE


dnl Checks for programs.
AC_PROG_CC
AC_PROG_LD
AM_PROG_LIBTOOL
AC_PROG_INSTALL

dnl Checks for header files.
AC_CHECK_HEADERS(string.h strings.h unistd.h)

dnl Enable developer code (e.g. core dumps)
ac_devel_cflags="-g -Wall"

AC_ARG_ENABLE(devel,
  [AC_HELP_STRING(
    [--enable-devel],
    [enable code of interest to developers])
  ],
  [
    if test x"$enableval" = xyes ; then
      devel="yes"
    else
      devel="no"
    fi
  ])

dnl default defines
CHM_MT="-DCHM_MT"
CHM_USE_PREAD="-DCHM_USE_PREAD"
CHM_USE_IO64="-DCHM_USE_IO64"
DMALLOC_DISABLE="-DDMALLOC_DISABLE"

dnl With chmLib
EXAMPLES="test_chmLib enum_chmLib enumdir_chmLib extract_chmLib"
LDFLAGS="$LDFLAGS"
AC_ARG_ENABLE(pthread,
  [AC_HELP_STRING(
    [--disable-pthread],
    [disable pthreads and do not build thread safe library])
  ],
  [
   if test "$enableval" = "yes"; then
      AC_CHECK_LIB(pthread,pthread_mutex_init)
      EXAMPLES="$EXAMPLES chm_http"
      LDFLAGS="$LDFLAGS -lpthread"
     else
      CHM_MT=""
      AC_MSG_WARN(Thread safe mode turned off. chm_http will not be built)
   fi
  ],
  [
   AC_CHECK_LIB(pthread,pthread_mutex_init)
   EXAMPLES="$EXAMPLES chm_http"
   LDFLAGS="$LDFLAGS -lpthread"
  ])

AC_ARG_ENABLE(io64,
  [AC_HELP_STRING(
    [--disable-io64],
    [do not build chm_lib with support of 64-bit file I/O])
  ],
  [
   if test "$enableval" = "no"; then
      CHM_USE_IO64=""
   fi
  ])

AC_ARG_ENABLE(pread,
  [AC_HELP_STRING(
    [--disable-pread],
    [do not build chm_lib to use pread/pread64 for all I/O])
  ],
  [
   if test "$enableval" = "no"; then
      CHM_USE_PREAD=""
   fi
  ])

AC_ARG_ENABLE(dmalloc,
  [AC_HELP_STRING(
    [--enable-dmalloc],
    [enable dmalloc])
  ],
  [
   if test "$enableval" = "yes"; then
      DMALLOC_DISABLE=""
   fi
  ])


dnl check for availability of the functions we need
if test -n "$CHM_USE_IO64"; then
    AC_CHECK_FUNC(lseek64,,[
            AC_MSG_WARN([64-bit file offsets not available, huge files won't be supported.])
            CHM_USE_IO64=""])
fi
if test -n "$CHM_USE_PREAD"; then
    if test -n "$CHM_USE_IO64"; then
        pread_func="pread64"
    else
        pread_func="pread"
    fi

    AC_CHECK_FUNC($pread_func,,[
            AC_MSG_WARN([pread() not available, falling back to less efficient replacement])
            CHM_USE_PREAD=""])
fi

AC_ARG_ENABLE([examples],
[  --enable-examples        Build example programs that use chmlib],
[case "${enableval}" in
 yes) build_examples=true;;
 no) build_examples=false;;
 *) AC_MSG_ERROR([bad value ${enableval} for --enable-examples]);;
  esac], [build_examples=false])
AM_CONDITIONAL([EXAMPLES],[test "x$build_examples" = xtrue])
AC_SUBST(EXAMPLES)
AC_SUBST(CHM_MT)
AC_SUBST(CHM_USE_PREAD)
AC_SUBST(CHM_USE_IO64)
AC_SUBST(DMALLOC_DISABLE)


dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T

dnl Checks for library functions.
AC_FUNC_MALLOC

dnl Checks for installation user/group

if test x"$install_user" = x; then
  if test "x$target_os" = "xcygwin"; then
    install_user=Administrator
  else
    install_user=root
  fi
fi
if test x"$install_group" = x ; then
        install_group=`sed -n '/.*:.*:0:/{s/^\(.*\):.*:0:.*/\1/p;q;}' /etc/group 2>/dev/null`
fi

AC_SUBST(install_user)
AC_SUBST(install_group)

if test x"$devel" = xyes ; then
  AC_DEFINE(USE_DEVEL)

  if test x"$GCC" = xyes; then

    dnl Remove any optimization flags from CFLAGS
    CFLAGS=`echo "$CFLAGS" | sed -e 's/\-O2//'`
    CFLAGS="$CFLAGS $ac_devel_cflags"

    dnl Some C compilers (e.g. older gcc versions) may not accept these
    dnl options.  Check if they are supported.  They will be added to
    dnl CFLAGS if supported.
    dnl PR_CHECK_CC_OPT(Wfloat-equal)
  fi

  dnl Do not strip symboles from developer object files.
  INSTALL_STRIP=""
else

  dnl Make sure to strip symbols from non-developer object files.
  INSTALL_STRIP="-s"
fi

CFLAGS="$CFLAGS $CHM_MT $CHM_USE_PREAD $CHM_USE_IO64"

dnl AC_PROG_LIBTOOL
dnl AC_SUBST(LIBTOOL)

dnl AC_SUBST(LD)

AC_SUBST(INSTALL_STRIP)

dnl top_builddir=`pwd`
dnl AC_SUBST(top_builddir)

dnl AC_OUTPUT_COMMANDS([if test ! -d src; then mkdir src; fi])
AC_OUTPUT([Makefile src/Makefile])
