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

AC_INIT(config.h.in)
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(forked-daapd, 0.15)

AC_USE_SYSTEM_EXTENSIONS

dnl Checks for programs.
AC_PROG_CC
AM_PROG_CC_C_O
LT_INIT([disable-static])

AC_CHECK_PROG(GPERF, [gperf], [gperf])
if test "x$GPERF" = x; then
	AC_MSG_ERROR([GNU gperf not found, please install it])
fi
AC_SUBST(GPERF)

AC_CHECK_PROG(ANTLR, [antlr3], [antlr3])
if test "x$ANTLR" = x; then
	if test -d $srcdir/src/pregen; then
		for f in $srcdir/src/pregen/*; do
			bf=`basename $f`
			ln -sf pregen/$bf $srcdir/src/$bf
		done
		AC_MSG_NOTICE([antlr3 wrapper not found, using pre-generated files])
	else
		AC_MSG_ERROR([antlr3 wrapper not found and pre-generated files not available])
	fi
fi
AC_SUBST(ANTLR)
AM_CONDITIONAL(COND_ANTLR, test "x$ANTLR" != x)

CFLAGS="$CFLAGS -Wall -D_LARGEFILE_SOURCE"

AC_CHECK_HEADERS([sys/wait.h])
AC_CHECK_HEADERS([sys/param.h])
AC_CHECK_HEADERS([sys/select.h])
AC_CHECK_HEADERS([dirent.h])
AC_CHECK_FUNCS(posix_fadvise)
AC_CHECK_FUNCS(strptime)
AC_CHECK_FUNCS(strtok_r)
AC_CHECK_FUNCS(timegm)

dnl Large File Support (LFS)
AC_SYS_LARGEFILE
AC_TYPE_OFF_T

AC_ARG_ENABLE(flac, AC_HELP_STRING([--enable-flac], [Enable FLAC support]),
                      use_flac=true;
                      CPPFLAGS="${CPPFLAGS} -DFLAC")

AC_ARG_ENABLE(musepack, AC_HELP_STRING([--enable-musepack], [Enable Musepack support]),
                      use_musepack=true;
                      CPPFLAGS="${CPPFLAGS} -DMUSEPACK")

AC_ARG_ENABLE(itunes, AC_HELP_STRING([--enable-itunes], [Enable iTunes library support]),
                      use_itunes=true;
                      CPPFLAGS="${CPPFLAGS} -DITUNES")

AM_CONDITIONAL(COND_FLAC, test x$use_flac = xtrue)
AM_CONDITIONAL(COND_MUSEPACK, test x$use_musepack = xtrue)
AM_CONDITIONAL(COND_ITUNES, test x$use_itunes = xtrue)

AC_ARG_WITH(oss4, AC_HELP_STRING([--with-oss4=includedir], [Use OSS4 with soundcard.h in includedir (default /usr/lib/oss/include/sys)]),
  [ case "$withval" in
    no)
    case "$host" in
      *-*-linux-*)
      use_oss4=false
      ;;
      *-*-kfreebsd*-*|*-*-freebsd-*)
      AC_MSG_ERROR([OSS4 must be enabled on FreeBSD systems])
      ;;
    esac
    ;;
    yes)
    use_oss4=true
    oss4_includedir=/usr/lib/oss/include/sys
    ;;
    [[\\/$]]* | ?:[[\\/]]* )
    use_oss4=true
    oss4_includedir="$withval"
    ;;
    *)
    AC_MSG_ERROR([expected an absolute directory name for --with-oss4: $withval])
    ;;
    esac ],
  [ case "$host" in
    *-*-linux-*)
    use_oss4=false
    ;;
    *-*-kfreebsd*-*|*-*-freebsd-*)
    use_oss4=true
    oss4_includedir=/usr/lib/oss/include/sys
    ;;
    esac ])

dnl Checks for libraries.
gl_LIBUNISTRING

if test x$HAVE_LIBUNISTRING != xyes; then
   AC_MSG_ERROR([GNU libunistring is required])
fi

PKG_CHECK_MODULES(ZLIB, [ zlib ])
PKG_CHECK_MODULES(CONFUSE, [ libconfuse ])
PKG_CHECK_MODULES(AVAHI, [ avahi-client >= 0.6.24 ])
PKG_CHECK_MODULES(SQLITE3, [ sqlite3 >= 3.5.0 ])

save_LIBS="$LIBS"
LIBS="$SQLITE3_LIBS"
dnl Check that SQLite3 has the unlock notify API built-in
AC_CHECK_LIB([sqlite3], [sqlite3_unlock_notify], [], AC_MSG_ERROR([SQLite3 was built without unlock notify support]))
dnl Check that SQLite3 has been built with threadsafe operations
AC_MSG_CHECKING([if SQLite3 was built with threadsafe operations support])
AC_LANG_PUSH([C])
AC_RUN_IFELSE(
  [AC_LANG_PROGRAM([dnl
    #include <sqlite3.h>
    ], [dnl
    int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
    if (ret != SQLITE_OK)
      return 1;
    return 0;])],
  [AC_MSG_RESULT([yes])], [AC_MSG_ERROR([SQLite3 was not built with threadsafe operations support])],
  [AC_MSG_RESULT([runtime will tell])])
AC_LANG_POP([C])
LIBS="$save_LIBS"

PKG_CHECK_MODULES(FFMPEG, [ libavcodec libavformat libswscale libavutil ])
dnl Check for av_lock_manager (ffmpeg >= 0.5.1)
save_LIBS="$LIBS"
AC_CHECK_LIB([avcodec], [av_lockmgr_register], , AC_MSG_ERROR([ffmpeg >= 0.5.1 required]))
LIBS="$save_LIBS"

PKG_CHECK_MODULES(MINIXML, [ mxml ])

AC_CHECK_HEADER(event.h, , AC_MSG_ERROR([event.h not found]))
AC_CHECK_LIB([event_core], [event_init], [LIBEVENT_LIBS="-levent_core"], AC_MSG_ERROR([libevent not found]))
AC_SUBST(LIBEVENT_LIBS)

AC_CHECK_HEADER(avl.h, , AC_MSG_ERROR([avl.h not found]))
AC_CHECK_LIB([avl], [avl_alloc_tree], [LIBAVL_LIBS="-lavl"], AC_MSG_ERROR([libavl not found]))
AC_SUBST(LIBAVL_LIBS)

AC_CHECK_HEADER(antlr3.h, , AC_MSG_ERROR([antlr3.h not found]))
AC_CHECK_LIB([antlr3c], [antlr3BaseRecognizerNew], [ANTLR3C_LIBS="-lantlr3c"], AC_MSG_ERROR([ANTLR3 C runtime (libantlr3c) not found]))
AC_SUBST(ANTLR3C_LIBS)

AM_PATH_LIBGCRYPT([1:1.2.0], , AC_MSG_ERROR([libgcrypt not found]))
AM_PATH_GPG_ERROR([1.6], , AC_MSG_ERROR([libgpg-error not found]))

if test x$use_flac = xtrue; then
	PKG_CHECK_MODULES(FLAC, [ flac ])
fi

if test x$use_musepack = xtrue; then
	PKG_CHECK_MODULES(TAGLIB, [ taglib_c ])
fi

if test x$use_itunes = xtrue; then
	PKG_CHECK_MODULES(LIBPLIST, [ libplist >= 0.16 ])
fi

case "$host" in
  *-*-linux-*)
    if test x$use_oss4 != xtrue; then
        use_alsa=true
	PKG_CHECK_MODULES(ALSA, [ alsa ])
	AC_DEFINE(LAUDIO_USE_ALSA, 1, [define if local audio output uses ALSA])
    fi

    AC_CHECK_HEADERS([sys/eventfd.h])
    AC_CHECK_FUNCS(eventfd)

    AC_CHECK_HEADER(sys/signalfd.h, , AC_MSG_ERROR([signalfd required; glibc 2.9+ recommended]))

    AC_CHECK_HEADER(sys/timerfd.h, , AC_MSG_ERROR([timerfd required; glibc 2.8+ recommended]))
    AC_CHECK_FUNC(timerfd_create, , AC_MSG_ERROR([timerfd required; glibc 2.8+ recommended]))
    ;;
esac

if test x$use_oss4 = xtrue; then
   OSS4CPPFLAGS="-I$oss4_includedir"

   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$OSS4CPPFLAGS"

   AC_CHECK_HEADER(soundcard.h, , AC_MSG_ERROR([soundcard.h not found in $oss4_includedir]))

   CPPFLAGS="$save_CPPFLAGS"
fi

AM_CONDITIONAL(COND_ALSA, test x$use_alsa = xtrue)
AM_CONDITIONAL(COND_OSS4, test x$use_oss4 = xtrue)
AC_SUBST(OSS4CPPFLAGS)

AC_CHECK_SIZEOF(void *)

AC_CHECK_HEADERS(getopt.h,,)
AC_CHECK_HEADERS(stdint.h,,)

dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT

AC_OUTPUT(src/Makefile sqlext/Makefile Makefile)