+AC_PROG_INSTALL
+AC_LIBTOOL_DLOPEN
+CFLAGS="$cflags_reset"
+
+msg_pkg_config_missing="'pkg-config' is missing - adapter detection will not be available"
+msg_pthread_missing="required library 'pthread' is missing"
+msg_dl_missing="required library 'dl' is missing"
+msg_udev_missing="library 'udev' is missing - adapter detection will not be available"
+msg_dirent_missing="dirent.h header is missing - adapter detection will not be available"
+msg_lockdev_missing="required library 'liblockdev' is missing"
+msg_required_header_missing="required header is missing"
+
+## debugging symbols
+AC_ARG_ENABLE([debug],
+ [AS_HELP_STRING([--enable-debug],
+ [include debug symbols (default is no)])],
+ [use_debug=$enableval],
+ [use_debug=no])
+
+## optimisation
+AC_ARG_ENABLE([optimisation],
+ [AS_HELP_STRING([--enable-optimisation],
+ [optimisation flag (default is yes)])],
+ [use_optimisation=$enableval],
+ [use_optimisation=yes])
+
+## add the top dir and include to the include path, so we can include config.h and cec.h
+CPPFLAGS="$CPPFLAGS -I\$(abs_top_srcdir)/src -I\$(abs_top_srcdir)/include"
+
+## search for pkg-config
+AC_CHECK_PROG(HAVE_PKG_CONFIG, pkg-config, yes)
+if test "x$HAVE_PKG_CONFIG" != "xyes" ; then
+ AC_MSG_WARN($msg_pkg_config_missing)
+fi
+
+## search for pthread, required by all targets
+AC_SEARCH_LIBS([pthread_create],[pthread],,AC_MSG_ERROR($msg_pthread_missing))
+AC_CHECK_FUNCS([pthread_mutexattr_init pthread_cond_init pthread_cond_destroy pthread_cond_signal pthread_cond_broadcast pthread_cond_wait pthread_cond_timedwait])
+
+## search for dlopen, required by all targets
+AC_SEARCH_LIBS([dlopen], [dl],
+ [test "$ac_cv_search_dlopen" = "none required" || LIBS_DL=$ac_cv_search_dlopen],
+ AC_MSG_ERROR($msg_dl_missing))
+AC_CHECK_FUNCS([dlopen dlcose dlsym])
+
+## platform specific libs, required by all targets
+case "${host}" in
+ *-*-linux*)
+ # for timeutils
+ LIBS="$LIBS -lrt"
+ ;;
+ *-apple-darwin*)
+ LIBS="$LIBS -framework CoreVideo -framework IOKit"
+ ;;
+esac
+
+## we found all the libs and headers that we need for the client applications
+libs_client="$LIBS"
+
+## search for udev, lockdev and the RPi API, only required by libCEC
+use_udev="no"
+use_adapter_detection="yes"
+case "${host}" in
+ *-*-linux*)
+ ## search for udev if pkg-config was found
+ if test "x$HAVE_PKG_CONFIG" = "xyes" ; then
+ PKG_CHECK_MODULES([UDEV],[libudev],use_udev="yes",AC_MSG_WARN($msg_udev_missing))
+ fi
+
+ ## we need dirent.h on linux too
+ if test "$use_udev" = "yes"; then
+ AC_CHECK_HEADER(dirent.h,,[use_udev="no";AC_MSG_WARN($msg_dirent_missing)])
+ fi
+
+ if test "$use_udev" != "yes"; then
+ use_adapter_detection="no"
+ fi
+
+ ## search for lockdev
+ AC_CHECK_HEADER(lockdev.h,,AC_MSG_ERROR($msg_lockdev_missing))
+ AC_CHECK_LIB(lockdev,dev_unlock,,AC_MSG_ERROR($msg_lockdev_missing))
+
+ AC_CHECK_HEADER(time.h,,AC_MSG_ERROR($msg_required_header_missing))
+ AC_CHECK_HEADER(sys/prctl.h,,AC_MSG_ERROR($msg_required_header_missing))
+ ;;
+ *-apple-darwin*)
+ AC_CHECK_HEADER(mach/mach_time.h,,AC_MSG_ERROR($msg_required_header_missing))
+ AC_CHECK_HEADER(CoreVideo/CVHostTime.h,,AC_MSG_ERROR($msg_required_header_missing))
+ ;;
+esac
+
+## define the build info
+LIB_INFO="host: ${host}, features:"
+
+features="Configured features:\n Pulse-Eight CEC Adapter :\t\tyes"
+LIB_INFO="$LIB_INFO 'P8 USB'"
+AC_DEFINE([HAVE_P8_USB],[1],[Define to 1 to include support for the Pulse-Eight USB-CEC Adapter])
+AM_CONDITIONAL(USE_P8_USB, true)
+
+## mark adapter detection as available if the required deps were found
+if test "x$use_adapter_detection" = "xyes"; then
+ ## mark udev as available if it was found
+ if test "x$use_udev" = "xyes"; then
+ INCLUDES="$INCLUDES $UDEV_CFLAGS"
+ LIBS="$LIBS $UDEV_LIBS"
+ AC_DEFINE([HAVE_LIBUDEV],[1],[Define to 1 if libudev is installed])
+ REQUIRES="$REQUIRES udev"
+ fi
+
+ AC_DEFINE([HAVE_P8_USB_DETECT],[1],[Define to 1 to include autodetection support for the Pulse-Eight USB-CEC Adapter])
+ AM_CONDITIONAL(USE_P8_USB_DETECT, true)