dnl Process this file with autoconf to produce a configure script.
AC_INIT(pbd)

AC_CONFIG_AUX_DIR(.)
AC_CANONICAL_HOST
AC_CANONICAL_TARGET
AC_VALIDATE_CACHED_SYSTEM_TUPLE()

LIBPBD_MAJOR_VERSION=2
LIBPBD_MINOR_VERSION=6
LIBPBD_MICRO_VERSION=0

AC_SUBST(LIBPBD_MAJOR_VERSION)
AC_SUBST(LIBPBD_MINOR_VERSION)
AC_SUBST(LIBPBD_MICRO_VERSION)

LIBPBD_VERSION=$LIBPBD_MAJOR_VERSION.$LIBPBD_MINOR_VERSION.${LIBPBD_MICRO_VERSION}
LIBPBD_RELEASE=$LIBPBD_MAJOR_VERSION-$LIBPBD_MINOR_VERSION-${LIBPBD_MICRO_VERSION}

AC_SUBST(LIBPBD_SO_VERSION)
AC_SUBST(LIBPBD_VERSION)
AC_SUBST(LIBPBD_RELEASE)

AM_INIT_AUTOMAKE(libpbd,${LIBPBD_VERSION})

AC_CONFIG_HEADERS(config.h)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
if test "$ac_cv_prog_cxx" = "no" ; then
   AC_MSG_ERROR([*** libpbd is C++. You don't appear to have a C++ compiler])
fi

AM_OPT_FLAGS

AC_ARG_ENABLE(optimize,
     [  --disable-optimize       avoid optimizations to allow for gdb debugging.],
     [ if test "x$enable_optimize" != "xno" ; then CXXFLAGS="$OPT_CXXFLAGS" ; fi ],
     [ CXXFLAGS="$OPT_CXXFLAGS" ])

AC_ARG_ENABLE(profiling,
     [  --enable-profiling       build for profiling using gprof.],
     [ if test "x$enable_profiling" = "xyes" ; then CXXFLAGS="$CXXFLAGS $PROF_FLAGS" ; fi ])

AC_OBJEXT
AC_PROG_RANLIB

AC_LANG_CPLUSPLUS

dnl We may need C++-11 for some versions of sigc
AX_CXX_COMPILE_STDCXX_11([noext], [mandatory])

AC_MSG_CHECKING([for --with-macosx-sdk])
AC_ARG_WITH(macosx-sdk,    [  --with-macosx-sdk=PATH  use an OS X SDK at PATH], [
        wxUSE_MACOSX_SDK=$withval
        wx_cv_use_macosx_sdk="wxUSE_MACOSX_SDK=$withval"
        #echo "wxUSE_MACOSX_SDK=$wxUSE_MACOSX_SDK" >> ${wx_arg_cache_file}.tmp
    ], [
        # NOTE: Empty string on purpose so that --without-macosx-sdk (thus no)
        # will override --enable-universal_binary's automatic SDK usage.
        wxUSE_MACOSX_SDK=
        #LINE=`grep "^wxUSE_MACOSX_SDK=" ${wx_arg_cache_file}`
        #if test "x$LINE" != x ; then
        #    eval "$LINE"
        #    echo "$LINE" >> ${wx_arg_cache_file}.tmp
        #fi
    ])
AC_MSG_RESULT([$wxUSE_MACOSX_SDK])

AC_MSG_CHECKING([for --with-macosx-version-min])
AC_ARG_WITH(macosx-version-min,    [  --with-macosx-version-min=VER   build binaries which require at least this OS X version], [
        wxUSE_MACOSX_VERSION_MIN=$withval
        wx_cv_use_macosx_version_min="wxUSE_MACOSX_VERSION_MIN=$withval"
        #echo "wxUSE_MACOSX_VERSION_MIN=$wxUSE_MACOSX_VERSION_MIN" >> ${wx_arg_cache_file}.tmp
    ], [
        # default "yes" because when not using an SDK it means the same as no
        # but when using an SDK it means to use the SDK version
        wxUSE_MACOSX_VERSION_MIN=yes
        # See if there is a value in the cache
        #LINE=`grep "^wxUSE_MACOSX_VERSION_MIN=" ${wx_arg_cache_file}`
        #if test "x$LINE" != x ; then
        #    eval "$LINE"
        #    echo "$LINE" >> ${wx_arg_cache_file}.tmp
        #fi
    ])
AC_MSG_RESULT([$wxUSE_MACOSX_VERSION_MIN])


dnl Set up the Mac OS X SDK.  We do this early so configure tests will occur
dnl with the SDK in place.
dnl NOTE: We clobber wxUSE_MACOSX_SDK with the SDK path
if test "x$wxUSE_MACOSX_SDK" = "xno"; then
    wxUSE_MACOSX_SDK=
elif test "x$wxUSE_MACOSX_SDK" = "xyes"; then
    # TODO: Search for most recent SDK and use it.
    wxUSE_MACOSX_SDK="/Developer/SDKs/MacOSX10.4u.sdk"
fi


if test "x$wxUSE_MACOSX_SDK" != "x"; then
    AC_MSG_CHECKING([for SDK directory $wxUSE_MACOSX_SDK])
    if ! test -d "$wxUSE_MACOSX_SDK"; then
        AC_MSG_FAILURE([not found])
    else
        AC_MSG_RESULT([exists])
    fi
    MACOSX_SDK_OPTS="-isysroot $wxUSE_MACOSX_SDK"
    eval "CC=\"$CC $MACOSX_SDK_OPTS\""
    eval "CXX=\"$CXX $MACOSX_SDK_OPTS\""
    eval "LD=\"$LD $MACOSX_SDK_OPTS\""
    retest_macosx_linking=yes
    dnl NOTE: When libtool is used in lieu of AR/RANLIB (i.e. in static mode)
    dnl the syslibroot makes no difference.  We aren't using libtool now but
    dnl if we ever did, be aware that you don't need to worry about it.
fi

dnl Set up the deployment target
dnl No   : Don't specify a min version even if using an SDK
dnl Yes  : Use the version from the SDK if used, otherwise same as no
dnl Param: Use the specified version
if test "x$wxUSE_MACOSX_VERSION_MIN" = "xno"; then
    wxUSE_MACOSX_VERSION_MIN=
elif test "x$wxUSE_MACOSX_VERSION_MIN" = "xyes"; then
    if test "x$wxUSE_MACOSX_SDK" != "x"; then
        AC_MSG_CHECKING([SDK deployment version])
dnl We need to quote the next line where we don't need macros and do need [] in the regex
[
        MACOSX_SDK_PLIST_VERSION_MIN=`defaults read "$wxUSE_MACOSX_SDK/SDKSettings" buildSettings | grep '^ *"\{0,1\}MACOSX_DEPLOYMENT_TARGET"\{0,1\} *= *"\{0,1\}[^"]*"\{0,1\}; *$' | sed 's/^ *"\{0,1\}MACOSX_DEPLOYMENT_TARGET"\{0,1\} *= *"\{0,1\}\([^"]*\)"\{0,1\} *; *$/\1/'`
]
        # If that failed, try again with the new key
        if test "x$MACOSX_SDK_PLIST_VERSION_MIN" == "x"; then
[
            MACOSX_SDK_PLIST_VERSION_MIN=`defaults read "$wxUSE_MACOSX_SDK/SDKSettings" DefaultProperties | grep '^ *"\{0,1\}MACOSX_DEPLOYMENT_TARGET"\{0,1\} *= *"\{0,1\}[^"]*"\{0,1\}; *$' | sed 's/^ *"\{0,1\}MACOSX_DEPLOYMENT_TARGET"\{0,1\} *= *"\{0,1\}\([^"]*\)"\{0,1\} *; *$/\1/'`
]
        fi

        if test "x$MACOSX_SDK_PLIST_VERSION_MIN" != "x"; then
            wxUSE_MACOSX_VERSION_MIN=$MACOSX_SDK_PLIST_VERSION_MIN
            AC_MSG_RESULT([$wxUSE_MACOSX_VERSION_MIN])
        else
            AC_MSG_WARN([Could not determine deployment target from SDKSettings.plist])
            wxUSE_MACOSX_VERSION_MIN=
        fi
    else
        wxUSE_MACOSX_VERSION_MIN=
    fi
fi

if test "x$wxUSE_MACOSX_VERSION_MIN" != "x"; then
    MACOSX_VERSION_MIN_OPTS="-mmacosx-version-min=$wxUSE_MACOSX_VERSION_MIN"
    eval "CC=\"$CC $MACOSX_VERSION_MIN_OPTS\""
    eval "CXX=\"$CXX $MACOSX_VERSION_MIN_OPTS\""
    eval "LD=\"$LD $MACOSX_VERSION_MIN_OPTS\""
    retest_macosx_linking=yes
fi

dnl If either an SDK or a version option was added, make sure that we can
dnl still compile and link both C and C++.  If we didn't do this, then most
dnl of the remaining tests would fail.
if test "x$retest_macosx_linking" = "xyes"; then
    AC_LANG_PUSH(C)
    AC_MSG_CHECKING([if C compiler works with SDK/version options])
    AC_TRY_LINK([],[],[AC_MSG_RESULT([yes])],[AC_MSG_FAILURE([no.  Try a different SDK]); exit 1])
    AC_LANG_POP()

    AC_LANG_PUSH(C++)
    AC_MSG_CHECKING([if C++ compiler works with SDK/version options])
    AC_TRY_LINK([],[],[AC_MSG_RESULT([yes])],[AC_MSG_FAILURE([no.  Try a different SDK]); exit 1])
    AC_LANG_POP()
fi


dnl Checks for libraries.

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h sys/time.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
dnl AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_PID_T
dnl AC_TYPE_SIZE_T
AC_HEADER_TIME

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_FUNC_GETMNTENT
AC_CHECK_FUNCS(regcomp select strdup strerror strtol)
AC_CHECK_LIB(pthread, pthread_create, ,[AC_MSG_ERROR([you have no POSIX thread support])])

dnl PKG_CHECK_MODULES(ICU, icu-i18n, , [AC_MSG_WARN([ICU not found, no problem])])

PKG_CHECK_MODULES(XML, libxml-2.0 >= 2.5)

dnl sigc++
PKG_CHECK_MODULES(SIGCPP, sigc++-2.0 >= 2.2.10)


AM_BUILD_ENVIRONMENT

CFLAGS="$CFLAGS $XML_CFLAGS $SIGCPP_CFLAGS $ICU_CLFAGS"
CXXFLAGS="$CXXFLAGS $XML_CFLAGS $SIGCPP_CFLAGS $ICU_CFLAGS"
LIBS="$LIBS $XML_LIBS $SIGCPP_LIBS $ICU_LIBS"

AC_OUTPUT([Makefile
           version.cc
           pbd/Makefile
])
