dnl Process this file with autoconf to produce a configure script.
AC_PREREQ(2.50)
AC_INIT(common/GUI-QT/main.cpp)

AM_INIT_AUTOMAKE(drm,1.12b)

AM_CONFIG_HEADER(config.h)

dnl Checks for programs
AC_PROG_LIBTOOL
AC_PROG_CC
AC_PROG_CXX
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CHECK_PROGS(RPMBUILD, rpmbuild, rpm)
AC_SUBST(LIBTOOL_DEPS)
PKG_PROG_PKG_CONFIG


dnl Configuration Arguments

AC_ARG_ENABLE( simulation,[  --enable-simulation     simulation mode], enable_simulation=$enableval, enable_simulation=no)

AC_ARG_ENABLE( oss,[  --enable-oss        open sound system support], enable_oss=$enableval, enable_oss=yes)

AC_ARG_ENABLE( alsa, [  --enable-alsa           use alsa sound lib], enable_alsa=$enableval, enable_alsa=yes)

AC_ARG_ENABLE( portaudio,[  --enable-portaudio   open sound system support], enable_portaudio=$enableval, enable_portaudio=yes)

AC_ARG_ENABLE( jack,[  --enable-jack   open sound system support], enable_jack=$enableval, enable_jack=no)

AC_ARG_ENABLE( qt,[  --enable-qt     use QT3 GUI], enable_qt=$enableval, enable_qt=yes)

AC_ARG_ENABLE( faad2,[  --enable-faad2     use faad2 library for decoding AAC audio], enable_faad2=$enableval, enable_faad2=yes)

AC_ARG_ENABLE( faac,[  --enable-faac     use faac library for encoding AAC audio], enable_faac=$enableval, enable_faac=yes)

AC_ARG_ENABLE( hamlib,[  --enable-hamlib     use hamlib library], enable_hamlib=$enableval, enable_hamlib=yes)

AC_ARG_ENABLE( pcap,[  --enable-pcap     use pcap library], enable_pcap=$enableval, enable_pcap=yes)

AC_ARG_ENABLE( sndfile,[  --enable-sndfile     use sndfile library], enable_sndfile=$enableval, enable_sndfile=yes)

if test "$enable_simulation" = "yes"; then
	dnl Only minimal configuration for simulation
	enable_alsa=no
	enable_oss=no
	enable_portaudio=no
	enable_jack=no
	enable_gps=no
	enable_qt=no
	enable_faad2=no
	enable_faac=no
	enable_hamlib=no
	enable_pcap=no
	enable_sndfile=no
fi

PKG_CHECK_MODULES(alsa, alsa, , enable_alsa=no)
PKG_CHECK_MODULES(portaudio, portaudio-2.0, , enable_portaudio=no)
PKG_CHECK_MODULES(jack, jack, , enable_jack=no)
AC_CHECK_HEADER(sys/soundcard.h, , enable_oss=no)

if test "x$enable_jack" = "xyes"; then
	AC_DEFINE(USE_JACK, 1, [Define if you want to use the jack audio lib])
	AM_CONDITIONAL(USE_JACK, true)
	LIBS="$LIBS $jack_LIBS"
	enable_portaudio=no
	enable_alsa=no
	enable_oss=no
else
	AM_CONDITIONAL(USE_JACK, false)
fi

if test "x$enable_portaudio" = "xyes"; then
	AC_DEFINE(USE_PORTAUDIO, 1, [Define if you want to use the portaudio audio lib])
	AM_CONDITIONAL(USE_PORTAUDIO, true)
	LIBS="$LIBS $portaudio_LIBS"
	enable_alsa=no
	enable_oss=no
else
	AM_CONDITIONAL(USE_PORTAUDIO, false)
fi

if test "x$enable_alsa" = "xyes"; then
	AC_DEFINE(USE_ALSA, 1, [Define if you want to use the ALSA audio lib])
	AM_CONDITIONAL(USE_ALSA, true)
	LIBS="$LIBS $alsa_LIBS"
	enable_oss=no
else
	AM_CONDITIONAL(USE_ALSA, false)
fi

if test "x$enable_oss" = "xyes"; then
	AC_DEFINE(USE_OSS, 1, [Define if you want to use the OSS audio lib])
	AM_CONDITIONAL(USE_OSS, true)
else
	AM_CONDITIONAL(USE_OSS, false)
fi

dnl Checks for header files.
AC_HEADER_STDC
AC_LANG_CPLUSPLUS

dnl Checks for typedefs, structures, and compiler characteristics.

dnl Checks for some external libraries that need to be installed
LIBS="$LIBS -L$PWD/libs"
CXXFLAGS="$CXXFLAGS -I$PWD/libs -I$PWD/linux/moc"
CFLAGS="$CFLAGS -I$PWD/libs -I$PWD/linux/moc"

AC_LANG(C++)

dnl libz ------------------------------------------------------------------------
AC_CHECK_LIB(z, inflateEnd)

dnl librt ------------------------------------------------------------------------
AC_CHECK_LIB(rt, clock_gettime)

dnl fftw ------------------------------------------------------------------------
AC_SEARCH_LIBS([fftw_create_plan],[dfftw fftw], , exit 1)
AC_SEARCH_LIBS([rfftw],[drfftw rfftw], , exit 1)
AC_CHECK_HEADERS(dfftw.h fftw.h)
AC_CHECK_HEADERS(drfftw.h rfftw.h)

dnl QT --------------------------------------------------------------------------
PKG_CHECK_MODULES(qt, qt-mt, , enable_qt=no)
if test "x$enable_qt" = "xyes"; then
	QTDIR=`$PKG_CONFIG --variable=exec_prefix qt-mt`
	AC_SUBST(QTDIR)
	AC_PATH_PROG(MOC, moc,, $QTDIR/bin)
	AC_PATH_PROG(UIC, uic,, $QTDIR/bin)
	AM_CONDITIONAL(USE_QT, true)
	CFLAGS="$CFLAGS $qt_CFLAGS"
	CXXFLAGS="$CXXFLAGS $qt_CFLAGS"
	LIBS="$LIBS $qt_LIBS"

	dnl qwt

	AC_HAVE_LIBRARY(qwt, ,exit 1)

	dnl check for at least version 4.2
	AC_CHECK_HEADER(qwt/qwt_dial_needle.h, qwt_version_ok=yes, qwt_version_ok=no)
	if test "x$qwt_version_ok" = "xno"; then
		AC_MSG_ERROR( "Your version of qwt is too old!")
	fi
	dnl check we don't have qwt 5 by mistake
	AC_CHECK_HEADER(qwt/qwt_scldiv.h, qwt_version_ok=yes, qwt_version_ok=no)
	if test "x$qwt_version_ok" = "xno"; then
		AC_MSG_ERROR( "Your version of qwt is too new!")
	fi

	AC_DEFINE(USE_QT_GUI, 1, [Define if you want to use the QT GUI])
	AM_CONDITIONAL(USE_QT, true)
else
	AM_CONDITIONAL(USE_QT, false)
fi

dnl Hamlib (optional) -----------------------------------------------------------
if test "x$enable_hamlib" = "xyes"; then
	PKG_CHECK_MODULES(hamlib, hamlib, , enable_hamlib=no)
	if test "x$enable_hamlib" = "xyes"; then
		AC_DEFINE(HAVE_LIBHAMLIB, 1, [Define if you have Hamlib])
		LIBS="$LIBS $hamlib_LIBS"
		hamlib121=`$PKG_CONFIG --exists --print-errors 'hamlib >= 1.2.1' 2>&1`
		if test "x$hamlib121" = x; then
			hamlib121=yes
			AC_DEFINE(HAVE_RIG_PARSE_MODE, 1, [Define if you have Hamlib >= 1.2.1])
		else
			hamlib121=no
		fi
	else
		hamlib121=no
	fi
else
	hamlib121=no
fi

dnl faad2 -----------------------------------------------------------------------
AC_CHECK_HEADER(neaacdec.h, , enable_faad2=no)
AC_CHECK_LIB(faad,NeAACDecOpen, , enable_faad2=no, -lm )
if test "x$enable_faad2" = xyes; then
	AC_CHECK_LIB(faad,NeAACDecInitDRM, , enable_faad2=no, -lm )
	if test "x$enable_faad2" = "xno"; then
		AC_MSG_ERROR( "*** libfaad build without DRM support ***" )
	fi
	AC_DEFINE(USE_FAAD2_LIBRARY, 1, [Define if you want to use the faad2 library])
fi

dnl faac (optional) -------------------------------------------------------------
AC_HAVE_LIBRARY(faac, , enable_faac=no)
if test "x$enable_faac" = xyes; then
	AC_DEFINE(USE_FAAC_LIBRARY, 1, [Define if you want to use the faac library])
fi

dnl libpcap (optional) ----------------------------------------------------------
AC_CHECK_HEADER(pcap.h, , enable_pcap=no)
AC_CHECK_LIB(pcap,pcap_next_ex, , enable_pcap=no)

dnl libsndfile (optional) -------------------------------------------------------
PKG_CHECK_MODULES(libsndfile, sndfile,,enable_sndfile=no)
if test "x$enable_sndfile" = xyes; then
	AC_DEFINE(HAVE_LIBSNDFILE, 1, [Define if you have libsndfile])
	LIBS="$LIBS $libsndfile_LIBS"
fi

AC_CONFIG_FILES(Makefile linux/Makefile drm.spec)
AC_OUTPUT
echo
echo
echo " 	simulation mode selected:			$enable_simulation"
echo "	jack sound driver selected:			$enable_jack (alpha)"
echo "	portaudio sound driver selected:		$enable_portaudio"
echo " 	alsa sound driver selected:			$enable_alsa"
echo "	oss sound driver selected: 			$enable_oss"
echo "	QT GUI selected:				$enable_qt"
echo "	rig control via hamlib supported:	 	$enable_hamlib"
echo "	hamlib with rigparse mode:			$hamlib121"
echo "	AAC decoding supported:				$enable_faad2"
echo "	AAC encoding supported:				$enable_faac"
echo "	pcap file format supported:			$enable_pcap"
echo "	libsndfile supported:				$enable_sndfile"
echo
echo
