dnl Process this file with autoconf or autoreconf to produce a configure script.
AC_INIT([sarg],[2.3.6])
AC_CONFIG_SRCDIR([log.c])
AC_CONFIG_AUX_DIR(cfgaux)

AC_CANONICAL_HOST

AC_ARG_WITH(gcc,
AS_HELP_STRING([--without-gcc],[use CC to compile]))

test -n "$CC" && cc_specified=yes
case ${with_gcc} in
  yes ) CC=gcc ;;
dnl yes ) CC=g++ ;;
  no  ) CC=cc ;;
  *   ) AC_PROG_CC;;
dnl  *   ) AC_PROG_CXX ;;
esac

dnl The purpose of the following condition is unknown but it is obsolete as it is.
dnl The -Aa option produces an error claiming that some argument are missing.
dnl if test "${CC}" = "cc" ; then
dnl    CFLAGS="${CFLAGS} -g -O2 -Aa"
dnl fi

dnl C99 support is required to define LLONG_MAX (at least on CentOS 5.7)
AC_PROG_CC_C99

# Report more warnings to improve code quality.
CFLAGS="${CFLAGS} -Wall -Wno-sign-compare"

dnl Check for supported compiler options

AC_MSG_CHECKING([for extra warnings flag in $CC])
saved_CFLAGS="${CFLAGS}"
CFLAGS="${CFLAGS} -Wextra -Wno-unused-parameter"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_extra_warnings="yes"],[have_extra_warnings="no"])
AC_MSG_RESULT($have_extra_warnings)
if test "$have_extra_warnings" == "no" ; then
    CFLAGS="${saved_CFLAGS}"
fi

AC_MSG_CHECKING([for implicit-function-declaration error flag in $CC])
saved_CFLAGS="${CFLAGS}"
CFLAGS="${CFLAGS} -Werror=implicit-function-declaration"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_implicit_function_declaration="yes"],[have_implicit_function_declaration="no"])
AC_MSG_RESULT($have_implicit_function_declaration)
if test "$have_implicit_function_declaration" == "no" ; then
    CFLAGS="${saved_CFLAGS}"
fi

AC_MSG_CHECKING([for format error flag in $CC])
saved_CFLAGS="${CFLAGS}"
CFLAGS="${CFLAGS} -Werror=format"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_error_format="yes"],[have_error_format="no"])
AC_MSG_RESULT($have_error_format)
if test "$have_error_format" == "no" ; then
    CFLAGS="${saved_CFLAGS}"
fi

case "$host" in
   *-solaris*)
   LDFLAGS="${LDFLAGS} -lsocket -lnsl"
   CFLAGS="-DSOLARIS ${CFLAGS}"
   ;;
esac

#dnl Checks for programs.
#AC_PROG_CC

dnl Check for headers
AC_HEADER_DIRENT
AC_HEADER_STDC


AC_CHECK_HEADERS(stdio.h stdlib.h string.h strings.h sys/time.h time.h unistd.h sys/dirent.h \
		dirent.h sys/types.h sys/socket.h netdb.h arpa/inet.h netinet/in.h sys/stat.h \
		ctype.h errno.h sys/resource.h sys/wait.h stdarg.h inttypes.h limits.h locale.h \
		execinfo.h math.h libintl.h libgen.h stdbool.h getopt.h fcntl.h)

if test $ac_cv_header_getopt_h = "no" ; then
   AC_MSG_ERROR("getopt.h is required to compile sarg")
fi

# Build with gd
AC_ARG_WITH([gd],
AS_HELP_STRING([--with-gd],[Compile with support for the graphical gd library]),
[],[with_gd=check])
if ( test "x$with_gd" != "xno" ) ; then
	AC_CHECK_HEADERS(gd.h gdfontl.h gdfontt.h gdfonts.h gdfontmb.h gdfontg.h)
	if ( test "x$ac_cv_header_gd_h" == "xyes" ) ; then
		AC_CHECK_LIB(gd, gdImagePng,LIBS="-lgd ${LIBS}"; HAVE_GD_LIB="yes", HAVE_GD_LIB="")
		if ( test "x$HAVE_GD_LIB" != "xyes" ) ; then
			AC_MSG_ERROR([ligbd is required to compile sarg with gd])
		fi
	else
		gd_status="not found"
	fi
else
	gd_status="disabled"
fi

# Build with LDAP
AC_ARG_WITH([ldap],
AS_HELP_STRING([--with-ldap],[Compile with LDAP support]),
[],[with_ldap=check])
if ( test "x$with_ldap" != "xno" ) ; then
	AC_CHECK_HEADERS(ldap.h)
	AC_CHECK_LIB(ldap, ldap_init,LIBS="-lldap ${LIBS}"; HAVE_LDAP="yes", HAVE_LDAP="")
else
	AC_MSG_NOTICE([Not building with LDAP support as requested on the configuration command line])
fi

# Build with iconv
AC_ARG_WITH([iconv],
AS_HELP_STRING([--with-iconv],[Compile with support for iconv]),
[],[with_iconv=check])
if ( test "x$with_iconv" != "xno" ) ; then
	dnl Check for iconv
	AM_ICONV
	if test -n "$LIBICONV" ; then
	  LIBS="$LIBS $LIBICONV"
	fi
	AC_CHECK_HEADERS(iconv.h)
else
	AC_MSG_NOTICE([Not building with iconv as requested on the configuration command line])
fi

# Build with pcre
AC_ARG_WITH([pcre],
AS_HELP_STRING([--with-pcre],[Compile with support for the Perl Compatible Regular Expressions library]),
[],[with_pcre=check])
if ( test "x$with_pcre" != "xno" ) ; then
	AC_CHECK_HEADERS(pcre.h)
	if ( test "x$ac_cv_header_pcre_h" == "xyes" ) ; then
		LDFLAGS="$LDFLAGS $(pcre-config --libs)"
		CFLAGS="$CFLAGS $(pcre-config --cflags)"
	else
		pcre_status="not found"
	fi
else
	pcre_status="disabled"
fi

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_STRUCT_TM

# Change the gettext version according to the available version on your system.
# It should not be necessary to change it unless you run autoreconf -fi.
AM_GNU_GETTEXT_VERSION([0.18])
AM_GNU_GETTEXT([external])

AC_SYS_LARGEFILE

# Check whether fopen64 is available and whether _LARGEFILE64_SOURCE
# needs to be defined for it
AC_MSG_CHECKING([for fopen64])
AC_CACHE_VAL(bu_cv_have_fopen64,
[noerror_CFLAGS=$CFLAGS
 CFLAGS="$CFLAGS -Werror"
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[FILE *f = fopen64 ("/tmp/foo","r");fclose(f);]])],[bu_cv_have_fopen64=yes],
 [CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE"
 AC_TRY_LINK([#include <stdio.h>], [FILE *f = fopen64 ("/tmp/foo","r");fclose(f);],
bu_cv_have_fopen64="need -D_LARGEFILE64_SOURCE",
 CFLAGS=$noerror_CFLAGS
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[FILE *f = fopen64 ("/tmp/foo","r");fclose(f);]])],[bu_cv_have_fopen64="yes without -Werror"
 werror_status="fail"],
 [CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE"
 AC_TRY_LINK([#include <stdio.h>], [FILE *f = fopen64 ("/tmp/foo","r");fclose(f);],
bu_cv_have_fopen64="need -D_LARGEFILE64_SOURCE and no -Werror"
werror_status="fail",
bu_cv_have_fopen64=no)]))
 ])
 CFLAGS=$noerror_CFLAGS])
AC_MSG_RESULT($bu_cv_have_fopen64)
if test "$bu_cv_have_fopen64" != no; then
  AC_DEFINE([HAVE_FOPEN64], 1,
            [Is fopen64 available?])
  if test "$bu_cv_have_fopen64" = "need -D_LARGEFILE64_SOURCE"; then
    AC_DEFINE([_LARGEFILE64_SOURCE], 1,
              [Enable LFS])
  fi
fi

dnl check for functions
AC_CHECK_FUNCS(bzero)
AC_CHECK_FUNCS(backtrace)
AC_CHECK_FUNCS(symlink)
AC_CHECK_FUNCS(lstat)
AC_CHECK_FUNCS(getnameinfo)
AC_CHECK_FUNCS(getaddrinfo)
AC_CHECK_FUNCS(mkstemp)

dnl check for structure members
AC_CHECK_MEMBER([struct sockaddr_storage.ss_len],[AC_DEFINE([HAVE_SOCKADDR_SA_LEN],1,[ss_len in sockaddr_storage])])

dnl check for the rlim_t size
AC_CHECK_SIZEOF(rlim_t,1,[#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
])
if test $ac_cv_sizeof_rlim_t = "4"; then
    AC_DEFINE(RLIM_STRING, "%d", [Rlim string])
elif test $ac_cv_sizeof_rlim_t = "8"; then
    AC_DEFINE(RLIM_STRING, "%lli", [Rlim string])
else
    AC_MSG_WARN([can not detect the size of your system\'s rlim_t type])
fi

dnl check for the long long int max constant
AC_CHECK_DECL(LLONG_MAX,HAVE_LLONG_MAX="yes",HAVE_LLONG_MAX="no",[[#include <limits.h>]])
if ( test "x$ac_cv_have_decl_LLONG_MAX" != "xyes" ) ; then
	AC_MSG_ERROR([LLONG_MAX is not defined on your system.])
fi

dnl Select sarg-php directory
AC_ARG_ENABLE(sargphp,
AS_HELP_STRING([--enable-sargphp=sargphpdir],
[Select sargphpdir as the directory to install sarg-php into]),
[
    if test "$enableval" -a "x$enableval" != "xno" ; then
        SARGPHPDIR=$enableval
    fi
],[SARGPHPDIR="/var/www/html"])
if test "$SARGPHPDIR" ; then
   echo "using $SARGPHPDIR as the directory to install sarg-php"
   AC_SUBST(SARGPHPDIR)
fi

dnl Select fonts dir
AC_ARG_ENABLE(fontdir,
AS_HELP_STRING([--enable-fontdir=fontdir],
[Select fontdir as the directory with the fonts to use in the reports]),
[
    if test "$enableval" -a "x$enableval" != "xno" ; then
        FONTDIR=$enableval
    fi
],[FONTDIR="${datarootdir}/sarg/fonts"])
if test "$FONTDIR" ; then
   if test "x$prefix" == "xNONE" ; then
      tempfullpath=`prefix=$ac_default_prefix ; eval "echo $FONTDIR"`
   else
      tempfullpath=`eval "echo $FONTDIR"`
   fi
   echo "using $tempfullpath as the directory of the fonts"
   AC_SUBST(FONTDIR)
fi

dnl Select images dir
AC_ARG_ENABLE(imagedir,
AS_HELP_STRING([--enable-imagedir=imagedir],
[Select imagedir as the directory with the images to use in the reports]),
[
    if test "$enableval"; then
        IMAGEDIR=$enableval
    fi
],[IMAGEDIR="${datarootdir}/sarg/images"])
if test "x$prefix" == "xNONE" ; then
   tempfullpath=`prefix=$ac_default_prefix ; eval "echo $IMAGEDIR"`
else
   tempfullpath=`eval "echo $IMAGEDIR"`
fi
echo "using $tempfullpath as the directory of the images"
AC_SUBST(IMAGEDIR)

dnl Enable extra compile and run time protection
AC_ARG_ENABLE(extraprotection,
AS_HELP_STRING([--enable-extraprotection],
[Enable compile and runtime extra protections]),
[
    if test "$enableval"; then
       CFLAGS="${CFLAGS} -fstack-protector -D_FORTIFY_SOURCE=2"
       if test "x$werror_status" != "xfail" ; then
          CFLAGS="${CFLAGS} -Werror"
       fi

       AC_MSG_CHECKING([for format security flag in $CC])
       saved_CFLAGS="${CFLAGS}"
       CFLAGS="${CFLAGS} -Wformat -Werror=format-security"
       AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_format_security="yes"],[have_format_security="no"])
       AC_MSG_RESULT($have_format_security)
       if test "$have_format_security" == "no" ; then
          CFLAGS="${saved_CFLAGS}"
       fi

       AC_MSG_CHECKING([for empty body flag in $CC])
       saved_CFLAGS="${CFLAGS}"
       CFLAGS="${CFLAGS} -Wempty-body"
       AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_empty_body="yes"],[have_empty_body="no"])
       AC_MSG_RESULT($have_empty_body)
       if test "$have_empty_body" == "no" ; then
          CFLAGS="${saved_CFLAGS}"
       fi
    fi
])

dnl Enable double check of the data written in the reports
AC_ARG_ENABLE(doublecheck,
AS_HELP_STRING([--enable-doublecheck],
[Make sarg double check the data it manipulates and output a warning if an error is found]),
[
   if test "$enableval"; then
      AC_DEFINE(ENABLE_DOUBLE_CHECK_DATA)
   fi
])

dnl Get the xsl stylesheet to produce the manpage
AC_ARG_ENABLE(xsl-man,
AS_HELP_STRING([--enable-xsl-man=man-xsl-style-sheet],
[Select the directory containing the the XSL stylesheets to convert DocBook into man page]),
[
	if test "$enableval"; then
		XSL_MAN_STYLESHEET=$enableval
		AC_SUBST(XSL_MAN_STYLESHEET)
	fi
],
AC_CHECK_FILES([/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl],
[
	# debian
	XSL_MAN_STYLESHEET="/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl"
	AC_SUBST(XSL_MAN_STYLESHEET)
],
[
AC_CHECK_FILES([/usr/share/sgml/docbook/xsl-stylesheets/manpages/docbook.xsl],
[
	# gentoo
	XSL_MAN_STYLESHEET="/usr/share/sgml/docbook/xsl-stylesheets/manpages/docbook.xsl"
	AC_SUBST(XSL_MAN_STYLESHEET)
],)
]))

dnl Get the xsl stylesheet to produce the html page
AC_ARG_ENABLE(xsl-html,
AS_HELP_STRING([--enable-xsl-html=html-xsl-style-sheet],
[Select the directory containing the the XSL stylesheets to convert DocBook into html page]),
[
	if test "$enableval"; then
		XSL_HTML_STYLESHEET=$enableval
		AC_SUBST(XSL_HTML_STYLESHEET)
	fi
],
AC_CHECK_FILES([/usr/share/sgml/docbook/stylesheet/xsl/docbook-xsl/html/onechunk.xsl],
[
	# debian
	XSL_HTML_STYLESHEET="/usr/share/sgml/docbook/stylesheet/xsl/docbook-xsl/html/onechunk.xsl"
	AC_SUBST(XSL_HTML_STYLESHEET)
],
[
AC_CHECK_FILES([/usr/share/sgml/docbook/xsl-stylesheets/html/onechunk.xsl],
[
	# gentoo
	XSL_HTML_STYLESHEET="/usr/share/sgml/docbook/xsl-stylesheets/html/onechunk.xsl"
	AC_SUBST(XSL_HTML_STYLESHEET)
],)
]))

AC_SUBST(PACKAGE,"$PACKAGE_NAME")
AC_SUBST(VERSION,"$PACKAGE_VERSION")

AC_CONFIG_FILES([Makefile po/Makefile.in])
AC_OUTPUT

if ( test "x$gd_status" == "xdisabled" ) ; then
	AC_MSG_NOTICE([Not building with gd as requested on the configuration command line])
elif ( test "x$gd_status" == "xnot found" ) ; then
	AC_MSG_NOTICE([gd.h was not found so the graphs won't be available in the report])
fi

if ( test "x$pcre_status" == "xdisabled" ) ; then
	AC_MSG_NOTICE([Not building with pcre as requested on the configuration command line])
elif ( test "x$pcre_status" == "xnot found" ) ; then
	AC_MSG_NOTICE([pcre.h was not found so the regexp won't be available in the hostalias])
fi
