dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/ziproxy.c)
AC_CONFIG_AUX_DIR(config)
AM_INIT_AUTOMAKE(ziproxy, 3.3.2)
AC_PREFIX_DEFAULT(/usr/local)
AM_CONFIG_HEADER(src/config.h)

dnl Checks for programs.
AC_PROG_CC
AC_LANG_C

dnl Checks for libraries.
ETR_SOCKET_NSL
LIBS="$LIBS $ETR_SOCKET_LIBS"

dnl zlib
AC_CHECK_LIB(z, gzopen,, AC_MSG_ERROR([ZLib not found.]))

dnl math library
AC_CHECK_LIB(m, pow,, AC_MSG_ERROR([Math library not found.]))

dnl USE_LIB(gif) 
AC_SEARCH_LIBS(DGifSlurp, gif ungif,,
	       AC_SEARCH_LIBS(DGifSlurp, gif ungif, "-L/usr/X11R6/lib -lX11",
	       [AC_MSG_ERROR([Both libgif and libungif not found.])]))

dnl USE_LIB(jpeg)
AC_CHECK_LIB(jpeg, jpeg_start_decompress,, AC_MSG_ERROR([libjpeg not found.]))

dnl USE_LIB(png)
AC_CHECK_LIB(png, png_sig_cmp,, AC_MSG_ERROR([libpng not found.]), -lz -lm)

dnl USE_LIB(pthread)
AC_CHECK_LIB(pthread, pthread_create,, AC_MSG_ERROR([libpthread not found.]), -lpthread)

dnl optional libjasper
AC_ARG_WITH([jasper],
	[AS_HELP_STRING([--with-jasper], [Enable JPEG 2000 support @<:@default=yes@:>@])],
	[],
	[with_jasper=yes])
with_jasper_bool=false
AS_IF([test "x$with_jasper" != xno],
	[AC_CHECK_LIB([jasper], [jas_init],
		[
			AC_CHECK_HEADERS([jasper/jasper.h], [
				LIBS="$LIBS -ljasper"
				AC_DEFINE([JP2K],[1],[JP2K support])
				with_jasper_bool=true
			], AC_MSG_FAILURE([no jasper headers found]))
		],
		AC_MSG_FAILURE([libjasper not found])
	)])
AM_CONDITIONAL(COMPILE_JP2_SUPPORT, $with_jasper_bool)

dnl optional libsasl2
AC_ARG_WITH([sasl2],
	[AS_HELP_STRING([--with-sasl2], [Enable SASL support @<:@default=yes@:>@])],
	[],
	[with_sasl2=yes])
with_sasl2_bool=false
AS_IF([test "x$with_sasl2" != xno],
	[AC_CHECK_LIB([sasl2], [sasl_set_path],
		[
			AC_CHECK_HEADERS([sasl/sasl.h], [
				LIBS="$LIBS -lsasl2"
				AC_DEFINE([SASL],[1],[SASL support])
				with_sasl2_bool=true
			], AC_MSG_FAILURE([no sasl headers found]))
		], AC_MSG_FAILURE([libsasl2 not found])
	)])
AM_CONDITIONAL(COMPILE_SASL_SUPPORT, $with_sasl2_bool)

dnl optional nameservers support
AC_ARG_ENABLE([nameservers],
    AS_HELP_STRING([--enable-nameservers], [Enable Nameservers option support @<:@default=yes@:>@]))
enable_nameservers_bool=false
AS_IF([test "x$enable_nameservers" != xno],
	[
		enable_nameservers_bool=true
		AC_DEFINE([EN_NAMESERVERS],[1],[Nameservers support])
	])
AM_CONDITIONAL(COMPILE_NAMSERVERS_SUPPORT, $enable_nameservers_bool)

dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT

dnl Those are all the mandatory headers in *.c and *.h files
AC_CHECK_HEADERS(arpa/inet.h arpa/nameser.h assert.h errno.h fcntl.h getopt.h gif_lib.h grp.h jpeglib.h math.h netdb.h netinet/in.h png.h pthread.h pwd.h resolv.h signal.h stdarg.h stdio.h stdlib.h string.h syslog.h sys/select.h sys/socket.h sys/stat.h sys/time.h sys/types.h sys/wait.h time.h unistd.h zlib.h,,AC_MSG_FAILURE([required header absent]))

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_CHECK_SIZEOF([int])
AC_CHECK_SIZEOF([long int])
AC_CHECK_SIZEOF([long long int])

dnl Checks for library functions.
AC_FUNC_SETVBUF_REVERSED
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(gettimeofday select socket strdup strndup strstr strtol)
      
AC_ARG_WITH(cfgfile, [  --with-cfgfile=/dir/ziproxy.conf	Set /dir/ziproxy.conf as the default configuration file.],
	    [AC_DEFINE_UNQUOTED(DefaultCfgLocation,["$withval"],[Default configuration file])])

dnl AC_CYGWIN
AC_OUTPUT(Makefile src/Makefile src/tools/Makefile man/Makefile)
