AC_PREREQ(2.59)
AC_INIT(libpst,0.6.25,carl@five-ten-sg.com)
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADER([config.h])

AM_INIT_AUTOMAKE($PACKAGE_NAME,$PACKAGE_VERSION)


# Checks for programs.
my_build_dii=yes
AC_PATH_PROG(CONVERT, convert)
if test "x$CONVERT" = "x" ; then
    AC_MSG_WARN([convert not found.  pst2dii disabled])
    my_build_dii=no
else
    # The following lines adds the --enable-dii option to configure:
    #
    # Give the user the choice to enter one of these:
    # --enable-dii
    # --enable-dii=yes
    # --enable-dii=no
    #
    AC_MSG_CHECKING([whether we are enabling dii utility])
    AC_ARG_ENABLE(dii,
          AC_HELP_STRING([--enable-dii], [enable dii utility]),
          [if test "${enable_dii}" = "no" ; then
              AC_MSG_RESULT([no])
              my_build_dii=no
          else
              AC_MSG_RESULT([yes])
          fi],
          # Default value for configure
          AC_MSG_RESULT([yes])
          )
fi
AM_CONDITIONAL(BUILD_DII, test $my_build_dii = yes)


# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_LIBTOOL
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_SYS_LARGEFILE
AC_CHECK_SIZEOF(off_t)

# Checks for header files.
AC_CHECK_HEADER([unistd.h],
    AM_CONDITIONAL(NEED_XGETOPT, [test yes = no ]),
    AM_CONDITIONAL(NEED_XGETOPT, [test yes = yes])
    )
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h stdint.h stdlib.h string.h sys/param.h unistd.h wchar.h])
if test "$my_build_dii" = "yes"; then
    AC_CHECK_HEADERS([gd.h])
fi

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_STRUCT_TM

# Checks for library functions.
AC_FUNC_FSEEKO
AC_FUNC_LSTAT
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_REALLOC
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([memchr memmove memset strcasecmp strchr strdup strerror strpbrk strrchr strstr strtol])

# iconv on mac is a library
SAVELIBS="${LIBS}"
AC_SEARCH_LIBS([iconv_open], [iconv])
if test "x${SAVELIBS}" != "x${LIBS}"; then
    all_libraries="-L/usr/lib $all_libraries"
    AC_SUBST(all_libraries)
    AC_MSG_WARN([This should be building on Mac OSX, adding /usr/lib to the library path])
fi

AC_MSG_CHECKING([if iconv uses const arguments])
AC_LANG_PUSH([C++])
AC_COMPILE_IFELSE([
        #include <iconv.h>
        int main(int argc, char **argv)
        {
            size_t  inlen = 100;
            char buffer[[inlen]];
            char *p = buffer;
            size_t utf8_len = 2 * inlen + 1;
            char utf8_buffer[[utf8_len]];
            char *utf8_p = utf8_buffer;
            iconv_t cd = 0;
            iconv(cd, (const char**)&p, &inlen, &utf8_p, &utf8_len);
        }
    ],
    [AC_DEFINE(ICONV_CONST,const,[do we have const headers])
     AC_MSG_RESULT([yes])
    ],
    [AC_DEFINE(ICONV_CONST,,[do we have const headers])
     AC_MSG_RESULT([no])
    ])
AC_LANG_POP([C++])


# The following lines adds the --enable-pst-debug option to configure:
#
# Give the user the choice to enter one of these:
# --enable-pst-debug
# --enable-pst-debug=yes
# --enable-pst-debug=no
#
AC_MSG_CHECKING([whether we are forcing debug dump file creation])
AC_ARG_ENABLE(pst-debug,
      AC_HELP_STRING([--enable-pst-debug], [force debug dump file creation]),
      [if test "${enable_pst_debug}" = "no" ; then
          AC_MSG_RESULT([no])
      else
          AC_DEFINE(DEBUG_ALL, 1, Define to 1 to force debug dump file creation)
          AC_MSG_RESULT([yes])
      fi],
      # Default value for configure
      AC_MSG_RESULT([no])
      )


# The following lines adds the --enable-libpst-shared option to configure:
#
# Give the user the choice to enter one of these:
# --enable-libpst-shared
# --enable-libpst-shared=yes
# --enable-libpst-shared=no
#
AC_MSG_CHECKING([whether we are building libpst shared object])
AC_ARG_ENABLE(libpst-shared,
      AC_HELP_STRING([--enable-libpst-shared], [build libpst shared object]),
      [if test "${enable_libpst_shared}" = "no" ; then
          AC_MSG_RESULT([no])
          AM_CONDITIONAL(BUILD_LIBPST_SHARED, test 0 = 1)
      else
          AC_MSG_RESULT([yes])
          AM_CONDITIONAL(BUILD_LIBPST_SHARED, test 1 = 1)
      fi],
      # Default value for configure
      [AC_MSG_RESULT([no])
       AM_CONDITIONAL(BUILD_LIBPST_SHARED, test 0 = 1)]
      )


AC_OUTPUT(                  \
    Makefile                \
    debian/changelog        \
    debian/Makefile         \
    libpst.spec             \
    libpst.pc               \
    html/Makefile           \
    info/Makefile           \
    man/Makefile            \
    src/Makefile            \
    src/version.h           \
    src/pst2dii.cpp         \
    xml/Makefile            \
    xml/libpst              \
    )
