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

AC_INIT(libsxmp, m4_esyscmd([tr -d '\n' < VERSION.sxmp]))
AC_SUBST([LIBSXT_VERSION], m4_esyscmd([tr -d '\n' < sxt/VERSION]))
AC_SUBST([LIBTDATA_VERSION], m4_esyscmd([tr -d '\n' < tdata/VERSION]))
AC_SUBST([LIBSEXPR_VERSION], m4_esyscmd([tr -d '\n' < sexpr/VERSION]))
AC_SUBST([LIBYDAEMON_VERSION], m4_esyscmd([tr -d '\n' < ydaemon/VERSION]))

AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE([1.11])

AM_SILENT_RULES([yes])

AC_PROG_CC

LT_INIT

# Checks for pointer size.
# TODO: Later this is irrelevant, and we should just bail on 32-bit platforms always
AC_CHECK_SIZEOF([uintptr_t])
if test "x$ac_cv_sizeof_uintptr_t" == "x"; then
  AC_ERROR([Cannot determine size of uintptr_t])
fi
AC_SUBST(ac_cv_sizeof_uintptr_t)

if test "${ac_cv_sizeof_uintptr_t}" = "4"; then
   AC_DEFINE([BUILD_HOST_32BIT], 1, [Define to 1 if host is 32bit])
fi

dnl  **************************************
dnl ***** tests for compiler built-ins *****
dnl  **************************************

AC_CACHE_CHECK([for __sync_bool_compare_and_swap_8],
[ctrie_cv_func___sync_bool_compare_and_swap_8],
[AC_LINK_IFELSE([
typedef unsigned int uint64  __attribute__ ((mode (DI)));
uint64 i;
int main() { return __sync_bool_compare_and_swap (&i, 0, 1); }
],
[ctrie_cv_func___sync_bool_compare_and_swap_8=yes],
[ctrie_cv_func___sync_bool_compare_and_swap_8=no])])
if test "$ctrie_cv_func___sync_bool_compare_and_swap_8" = "yes"; then
  AC_DEFINE([HAVE__SYNC_BOOL_COMPARE_AND_SWAP_8], 1,
    [Define to 1 if the compiler provides the __sync_bool_compare_and_swap function for uint64])
fi

dnl  *****************
dnl ***** options *****
dnl  *****************

dnl Build basic examples option
AC_ARG_ENABLE([build_examples],
    AS_HELP_STRING([--enable-build-examples], [Enable examples build]))

AS_IF([test "x$enable_build_examples" = "xyes"], [
  AC_DEFINE([BUILD_EXAMPLES], 1, [build of examples enabled])
])

AM_CONDITIONAL(BUILD_EXAMPLES, test "x$enable_build_examples" = "xyes")

dnl Build smpf example daemon and client option
AC_ARG_ENABLE([build_smpf_example],
    AS_HELP_STRING([--enable-build-smpf-example], [Enable to build smpf advanced example, requires readline]))

AS_IF([test "x$enable_build_smpf_example" = "xyes"], [
  AC_DEFINE([BUILD_SMPF_EXAMPLE], 1, [build of smpf example enabled])
])

AM_CONDITIONAL(BUILD_SMPF_EXAMPLE, test "x$enable_build_smpf_example" = "xyes")


dnl checking fpr dependencies

PKG_CHECK_MODULES(OPENSSL, [openssl])

case $host_os in 
	linux*) 	WIN32=no
				LINUX=yes
	 			PKG_CHECK_MODULES(LIBUUID, [uuid]);;
	mingw*) 	WIN32=yes 
				LINUX=no;;
	msys*) 		WIN32=yes 
				LINUX=no;;
esac

case $enable_build_smpf_example in
     yes)
        sic_save_LIBS=$LIBS
        AC_CHECK_LIB(readline, readline)
        if test x"${ac_cv_lib_readline_readline}" = xno; then
           AC_MSG_ERROR(libreadline not found)
        fi
        LIBS=$sic_save_LIBS
        ;;
esac

AM_CONDITIONAL(COND_WIN32, test "x$WIN32" = "xyes")

dnl AM_CONDITIONAL(BUILD_WIN32, test "x$enable_win32_build" = "xyes")


AC_OUTPUT([
Makefile
sxt/libsxt.pc
sxt/Makefile
sxmp/libsxmp.pc
sxmp/Makefile
sexpr/Makefile
sexpr/libsexpr.pc
tdata/Makefile
tdata/libtdata.pc
ydaemon/Makefile
ydaemon/libydaemon.pc
include/Makefile
man/Makefile
examples/Makefile])