dnl Process this file with autoconf to produce a configure script.

AC_INIT(remind, 06.03.01, , , https://dianne.skoll.ca/projects/remind/)
AC_CONFIG_SRCDIR([src/queue.c])

cat <<'EOF'

**********************
*                    *
* Configuring REMIND *
*                    *
**********************

EOF

AC_LANG([C])
AC_CONFIG_HEADERS([src/config.h])

AC_ARG_ENABLE(perl-build-artifacts,
[  --disable-perl-build-artifacts
                          Disable perllocal.pod and .packlist generation], ac_cv_perlartifacts=$enableval, ac_cv_perlartifacts=yes)

AH_BOTTOM([#include <custom.h>])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PATH_PROG([PERL], [perl])
AC_PATH_PROG([TCLSH], [tclsh])
AC_PATH_PROG([WISH], [wish])

dnl if neither tclsh nor wish is found, look for versioned variants
if test -z "$TCLSH" -a -z "$WISH" ; then
    for version in 9.2 9.1 9.0 8.6 8.5 ; do
        AC_PATH_PROG([TCLSH], [tclsh$version])
        AC_PATH_PROG([WISH], [wish$version])
        if test -n "$WISH" -o -n "$TCLSH" ; then
            break
        fi
    done
fi

dnl If we don't find tclsh, use wish instead
if test -z "$TCLSH" -a -n "$WISH" ; then
    TCLSH="`basename $WISH`"
elif test -n "$TCLSH" ; then
    TCLSH="`basename $TCLSH`"
else
    TCLSH=tclsh
fi

dnl Checks for libraries.
AC_CHECK_LIB(m, sqrt)
AC_CHECK_LIB(readline, readline)
AC_CHECK_HEADERS_ONCE([sys/time.h sys/termios.h stdint.h readline/readline.h readline/history.h])

dnl Integer sizes
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_SIZEOF(unsigned long)
AC_CHECK_SIZEOF(time_t)

dnl Checks for header files.
AC_CHECK_HEADERS(strings.h sys/types.h glob.h wctype.h locale.h langinfo.h sys/inotify.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_STRUCT_TM
U="`uname -s`"

# LTO fails on Solaris for some reason
if test "$U" != "SunOS" -a "$GCC" = yes; then
   	CFLAGS="$CFLAGS -Wall -Wextra -Wstrict-prototypes"
        # Check for link-time optimization support
        OLDCFLAGS="$CFLAGS"
        CFLAGS="$CFLAGS -flto=auto"
        AC_MSG_CHECKING([whether $CC supports -flto=auto])
        AC_LINK_IFELSE(
            [AC_LANG_PROGRAM(
               [[int x = 1;]],
               [[]])],
               [lto=yes],
               [lto=no]
               )
        AC_MSG_RESULT([$lto])
        if test "x$lto" != "xyes" ; then
	    CFLAGS="$OLDCFLAGS"
        else
            AC_MSG_CHECKING([whether $CC supports -ffat-lto-object])
            OLDCFLAGS="$CFLAGS"
            CFLAGS="$CFLAGS -Werror -ffat-lto-objects"
            AC_LINK_IFELSE(
                [AC_LANG_PROGRAM(
                    [[int x = 1;]],
                    [[]])],
                [lto=yes],
                [lto=no]
                )
            AC_MSG_RESULT([$lto])
            if test "x$lto" != "xyes" ; then
                CFLAGS="$OLDCFLAGS"
            else
                CFLAGS="$OLDCFLAGS -ffat-lto-objects"
            fi
        fi
fi

dnl If sizeof(time_t) is 4, try to get 64-bit time_t
if test "$ac_cv_sizeof_time_t" = "4" ; then
    AC_MSG_NOTICE([time_t is 32-bits on this system; attempting to use 64-bit time_t])
    CFLAGS="$CFLAGS -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64"
fi

if test "$ac_cv_perlartifacts" = "yes" ; then
    PERLARTIFACTS=
else
    PERLARTIFACTS='NO_PACKLIST=1 NO_PERLLOCAL=1'
fi

RELEASE_DATE=`grep '[[0-9]][[0-9]][[0-9]][[0-9]]-[[0-9]][[0-9]]-[[0-9]][[0-9]]' docs/WHATSNEW  | head -n 1 | awk '{print $NF}'`

# Sanity-check release date
echo "$RELEASE_DATE" | grep '^....-..-..$' > /dev/null 2>&1
if test "$?" != 0 ; then
    echo "*** COULD NOT DETERMINE RELEASE DATE: docs/WHATSNEW is incorrect!"
    exit 1
fi

if test "$ac_cv_header_wctype_h" != "yes" ; then
   echo "*** Remind requires the <wctype.h> header"
   exit 1
fi

AC_CHECK_FUNCS(setenv unsetenv glob mbstowcs setlocale initgroups inotify_init1 readline)

if test "$ac_cv_func_mbstowcs" != "yes"; then
   echo "*** Remind requires the mbstowcs function"
   exit 1
fi

VERSION=$PACKAGE_VERSION
CONFIG_CMD="$0$ac_configure_args_raw"
CONFIG_CMD=`echo "$CONFIG_CMD" | sed -e 's/"/\\\\"/g'`
AC_DEFINE_UNQUOTED([CONFIG_CMD], ["$CONFIG_CMD"], [Configuration command used to build Remind])
AC_SUBST(CONFIG_CMD)
AC_SUBST(VERSION)
AC_SUBST(PERL)
AC_SUBST(TCLSH)
AC_SUBST(PERLARTIFACTS)
AC_SUBST(RELEASE_DATE)
AC_CONFIG_FILES([src/Makefile www/Makefile src/version.h rem2html/Makefile rem2html/rem2html rem2pdf/Makefile.PL rem2pdf/Makefile.top rem2pdf/bin/rem2pdf man/rem2ps.1 man/remind.1 man/tkremind.1 scripts/tkremind])
AC_OUTPUT
chmod a+x rem2pdf/bin/rem2pdf
chmod a+x scripts/tkremind
