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

DATE=27-May-2010
AC_SUBST(DATE)
VERSION=0.10
VERSIONREGEXP=0[[.]]10
AC_SUBST(VERSION)
AC_SUBST(VERSIONREGEXP)

AC_INIT(src/numbers.c)

AC_CONFIG_HEADER([config.h])

# Checks for package options.
# Normally, configure will choose the longest precision supported,
# but the user can configure a particular choice with any of these
# mutually-exclusive options:
#	--with-gmp
#	--with-double
#	--with-long-double

fp_package=

AC_ARG_WITH(gmp,
	[  --with-gmp              use GNU multiple-precision floating-point library],
	[fp_package=gmp])

AC_ARG_WITH(double,
	[  --with-double           use double floating-point precision],
	[fp_package=dbl])

AC_ARG_WITH(long-double,
	[  --with-long-double      use long double floating-point precision],
	[fp_package=ldb])

#
# Setup CC and CFLAGS
#

# Check if user request its CC and CFLAGS
if test -n "$CFLAGS" || test -n "$CC" ; then
 user_redefine_cc=yes
fi
# Autoconf detection
AC_PROG_CC
AC_LANG(C)

AC_HEADER_STDC
AC_HEADER_STDBOOL
AC_HEADER_DIRENT

# Checks for header files.
AC_CHECK_HEADERS([archive.h archive_entry.h float.h fnmatch.h inttypes.h limits.h stddef.h stdlib.h string.h sys/param.h unistd.h])

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

AC_TYPE_UINT8_T
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T

AC_C_CONST

# Checks for library functions.
AC_FUNC_CLOSEDIR_VOID
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_REALLOC
AC_FUNC_STAT
AC_FUNC_STRTOD
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([getcwd regcomp strstr strtol])

AC_SEARCH_LIBS(ceil, m)

if test -z "$fp_package"
then
	AC_CACHE_CHECK([GNU multiple-precision library (gmp)],ac_cv_have_gmp,
		OLDCFLAGS="$CFLAGS"
		OLDLIBS=$LIBS
		if test "x$prefix" != xNONE
		then
			CFLAGS="$CFLAGS -I$prefix/include"
			LIBS="$LIBS -L$prefix/lib -lgmp"
		else
			CFLAGS="$CFLAGS -I$ac_default_prefix/include"
			LIBS="$LIBS -L$ac_default_prefix/lib -lgmp"
		fi
		AC_TRY_LINK(
			[#include <gmp.h>],
			[mpf_t x; mpf_init(x);],
			[ac_cv_have_gmp=yes],
			[ac_cv_have_gmp=no])
		if test "$ac_cv_have_gmp" = "yes"
		then
			ac_cv_c_long_double=no
			AC_DEFINE(HAVE_GMP)
			fp_package=gmp
		else
			CFLAGS=$OLDCFLAGS
			LIBS=$OLDLIBS
		fi
	)

	# The built-in test for long double fails for C++ compilers because
	# of a missing exit() prototype, sigh..., so we create our own,
	# using very similar code.  However, we change the < to <= in the
	# return statement, so as to catch the several implementations that
	# treat long double like double, so as not to confuse the user who
	# believes the code has been built for quadruple precision.
	# AC_C_LONG_DOUBLE
	if test "$ac_cv_have_gmp" = "no"
	then
		AC_MSG_CHECKING(for long double)
		AC_TRY_RUN(
		    [
			#include "confdefs.h"
			int main() 
			{
			    /* The Stardent Vistra knows sizeof(long double), but does not support it.  */
			    long double foo = 0.0;
			    /* On Ultrix 4.3 cc, long double is 4 and double is 8.  */
			    return (sizeof(long double) <= sizeof(double)); 
			}
		    ],
		    [ac_cv_c_long_double=yes],[ac_cv_c_long_double=no]
		)
		if test $ac_cv_c_long_double = yes
		then
		    fp_package=ldb
		    AC_DEFINE(HAVE_LONG_DOUBLE)
		fi
		AC_MSG_RESULT($ac_cv_c_long_double)
	fi
elif test $fp_package = gmp
then
	AC_DEFINE(HAVE_GMP)
elif test $fp_package = ldb
then
	AC_DEFINE(HAVE_LONG_DOUBLE)
fi

if test -z "$fp_package"
then
	fp_package=dbl
elif test $fp_package = gmp
then
	LIBS="$LIBS -lgmp"
fi

# TODO Need to explicitly do an availability check
LIBS="$LIBS -larchive"

CHECKOKAY=okay-$fp_package
AC_SUBST(CHECKOKAY)

# Remove -g from CFLAGS gratuitously inserted by configure.
CFLAGS="`echo $CFLAGS | sed -e 's/-g //g' -e 's/ -g$//g' -e 's/^-g$//'`"

# Remove -O2 from CFLAGS gratuitously inserted by configure.
CFLAGS="`echo $CFLAGS | sed -e 's/-O2 //g' -e 's/ -O2$//g' -e 's/^-O2$//'`"

if test -z "$GCC"; then
 : ;
# Do something
else
   CFLAGS="$CFLAGS -Wall  -Wextra -Wshadow -Wno-char-subscripts -DFULL_POSIX -g"
fi

# Remove -g from CFLAGS gratuitously inserted by configure.

AC_OUTPUT(Makefile src/Makefile hash/Makefile)

# Make all generated files, non-writable
chmod 444 Makefile
