dnl
dnl  @configure_input@
dnl
dnl  Configuration input file for Hapy
dnl

AC_INIT(Hapy, 0.0.4,, Hapy)
AC_CONFIG_SRCDIR([src/Pree.cc])
AC_CONFIG_AUX_DIR(cfgaux)

dnl save pre-set values (if any) for variables that are
dnl created by autoconf
PRESET_CXXFLAGS="$CXXFLAGS"
PRESET_LDFLAGS="$LDFLAGS"

AM_INIT_AUTOMAKE([no-define nostdinc])
AC_CONFIG_HEADERS(src/include/Hapy/config.h)
AC_PREFIX_DEFAULT(/usr/local)

AM_MAINTAINER_MODE

AC_LANG([C++])

dnl user can overwrite this default with --enable-shared
AC_DISABLE_SHARED
dnl do not configure for Fortran
AC_LIBTOOL_TAGS([CXX])
AC_PROG_LIBTOOL

dnl check if alleged C++ compiler understands C++
AC_MSG_CHECKING(whether $CXX compiler understands C++)
AC_TRY_COMPILE([
	#define Concat(a,b) a ## b
	struct T { 
		virtual ~T();
		virtual int m() const = 0;
		mutable bool a;
	};

	template <class P>
	struct C: public P {
		C(int): c(0) {}
		int m() const;
		int c;
	};

	template <class P>
	int C<P>::m() const { Concat(re,turn) c; }

	namespace N {
		struct Spaced {
			Spaced();
			int m;
		};
	}

	N::Spaced::Spaced() {
	}

	using N::Spaced;

	inline int test_inline() { return 0; }
],[
	// this is main()'s body
	static C<T> ct(1);
	Spaced spaced;
	return sizeof(spaced) > 0 ? 0 : 1;
],[
	AC_MSG_RESULT(yes)
],[
	AC_MSG_RESULT(no)
	AC_MSG_ERROR(the compiler ($CXX) failed to interpret straightforward C++; check config.log for details)
])


dnl adjust program options

OLD_CXXFLAGS=$CXXFLAGS
if test -z "$PRESET_CXXFLAGS"
then
	if test "x$GXX" = xyes
	then [

		# reset to preferred options
		# replace -O? with -O3
		CXXFLAGS=`echo $CXXFLAGS | sed 's/-O[0-9]*/-O3/'`;
		# enable useful warnings
		CXXFLAGS="$CXXFLAGS -Wall -Wwrite-strings -Woverloaded-virtual"

		# custom host-dependent tuning
		case "$host" in
		alpha-*)
			# we get random coredumps with g++ -O on alpha-dec-osf4
			# g++ -O3 warns about being buggy on alpha-*-freebsd4.*
			echo deleting -On on $host
			CXXFLAGS=`echo $CXXFLAGS | sed -e 's/-O[0-9]* *//'`
			;;
		*-linux-*)
			# -O2,3 seems to produce coredumps on RH and MDK linux
			echo enforcing -O1 on $host
			CXXFLAGS=`echo $CXXFLAGS | sed -e 's/-O[0-9]* */-O1 /'`
			;;
		esac
	] fi
else
	AC_MSG_WARN(CXXFLAGS manualy set to "$PRESET_CXXFLAGS", will not overwrite)
fi

if test "x$PRESET_CXXFLAGS" != "x$CXXFLAGS"
then
	AC_MSG_CHECKING(whether custom $CXX flags work)
	AC_TRY_COMPILE(
		[
		],[
			return 0;
		],[
			AC_MSG_RESULT(probably)
			echo "changing $CXX flags to $CXXFLAGS"
		],[
			AC_MSG_RESULT(no)
			CXXFLAGS=$OLD_CXXFLAGS
			echo "leaving  $CXX flags at $CXXFLAGS"
		]
	)
fi


dnl check for libraries
AC_CHECK_LIB(nsl, main)


dnl checks for header files
AC_CHECK_HEADERS(\
	limits \
	climits \
	limits.h \
)

AC_CACHE_CHECK(for non-standard std::string::compare profile,
	ac_cv_have_spn_string_compare, [
        AC_TRY_COMPILE([
                #include <string>
        ],[
                const string s1 = "abcd";
                const string s2 = "bc";
		return (s1.compare(s2, 1, s2.size()) == 0) ? 0 : 255;
        ],[
                ac_cv_have_spn_string_compare="yes";
        ],[
                ac_cv_have_spn_string_compare="no";
        ])
])
if test "x$ac_cv_have_spn_string_compare" = xyes; then
        AC_DEFINE(HAVE_SPN_STRING_COMPARE, 1,
		[Define to 1 if you have non-standard std::string::compare profile])
fi

AC_CACHE_CHECK(for std::numeric_limits,
	ac_cv_have_numeric_limits, [
        AC_TRY_COMPILE([
                #include <limits>
		// try #including non-standard ones too?
        ],[
		return std::numeric_limits<unsigned long>::max() == 0;
        ],[
                ac_cv_have_numeric_limits="yes";
        ],[
                ac_cv_have_numeric_limits="no";
        ])
])
if test "x$ac_cv_have_numeric_limits" = xyes; then
        AC_DEFINE(HAVE_NUMERIC_LIMITS, 1,
		[Define to 1 if you have std::numeric_limits template])
fi

AC_CONFIG_FILES([\
        Makefile \
	src/Makefile \
	doc/Makefile \
	tests/Makefile \
])
AC_OUTPUT
