dnl Process this file with autoconf to produce a configure script.
AC_INIT([Gtk Wave Cleaner], [0.22-04], [http://gwc.sourceforge.net])
AM_INIT_AUTOMAKE([1.12 foreign no-define tar-ustar])

PKG_CHECK_MODULES(SNDFILE, sndfile >= 1.0.0,
                   HAVE_LIBSNDFILE1="yes", HAVE_LIBSNDFILE1="no")

if test "x$HAVE_LIBSNDFILE1" = xno ; then
    AC_MSG_ERROR([
Libsndfile 1.0.2 or greater needed, find it at http://www.zip.com.au/~erikd/libsndfile/
OR perhaps configure can not file libsndfile, in which you can try 1 of 3 things:
1) set the environment variable PKG_CONFIG_PATH
   to find the package file for libsndfile for example:
   export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
2) re-install libsndfile in /usr instead of /usr/local, where it installs by default.
3) There are some packages for libsndfile now, be sure to install libsndfile-devel
])
fi

AC_ARG_ENABLE(debug,
AS_HELP_STRING([--enable-debug],
               [enable debugging, default: no]),
[case "${enable_debug}" in
             yes) debug=true ;;
             no)  debug=false ;;
             *)   AC_MSG_ERROR([bad value ${enable_debug} for --enable-debug]) ;;
esac],
[debug=false])

AM_CONDITIONAL(DEBUG, test x"$debug" = x"true")

PKG_CHECK_MODULES(PULSEAUDIO, libpulse-simple >= 0.9.0,
                   HAVE_PULSE_AUDIO="yes", HAVE_PULSE_AUDIO="no")

dnl Checks for programs.
AC_PROG_CC
AC_LANG(C)

dnl Checks for libraries.

dnl Replace `main' with a function in -lm:
AC_CHECK_LIB(m, cos)

dnl setup macros variables to be replaced in Makefile.in
AC_SUBST(FFTWLIB)
AC_SUBST(FFTWHDR)
AC_SUBST(FFTWPREC)
AC_SUBST(OSXDEF)
AC_SUBST(PALIB)
AC_SUBST(PAHDR)
AC_SUBST(ALSALIB)
AC_SUBST(ALSADEF)
AC_SUBST(ALSACFLAGS)
AC_SUBST(OGGLIB)
AC_SUBST(OGGHDR)
AC_SUBST(MP3LIB)
AC_SUBST(MP3HDR)
AC_SUBST(OSXLIBS)

dnl enable ogg/vorbis libs on user request and check installation
OGGLIB=""
OGGHDR=""

AC_ARG_ENABLE(ogg, [  --enable-ogg           compile experimental Ogg file reading support (default disabled)])

AC_MSG_CHECKING([for ogg])
if test "$enable_ogg" = "yes"
then
    ogg="yes";OGGLIB="-lvorbis -logg -lvorbisfile";OGGHDR="-DHAVE_OGG"
else
    ogg="disabled"
fi
AC_MSG_RESULT($ogg)

dnl enable mp3 libs on user request and check installation
MP3LIB=""
MP3HDR=""

AC_ARG_ENABLE(mp3, [  --enable-mp3           compile experimental mp3 file reading support (default disabled)])

AC_MSG_CHECKING([for mp3])
if test "$enable_mp3" = "yes"
then
    mp3="yes";MP3LIB="-lmpg123";MP3HDR="-DHAVE_MP3"
else
    mp3="disabled"
fi
AC_MSG_RESULT($mp3)

dnl enable alsa on user request and check installation
ALSALIB=""
ALSADEF=""
alsa="enabled"

AC_ARG_ENABLE(alsa, [  --disable-alsa           disable ALSA support (default enabled)])

if test "$enable_alsa" != "no"
then
    PKG_CHECK_MODULES(ALSA, alsa >= 0.9.0, alsa="yes";ALSALIB=$ALSA_LIBS;ALSADEF="-DHAVE_ALSA";ALSACFLAGS=$ALSA_CFLAGS, alsa="disabled")
else
    alsa="disabled"
    AC_MSG_CHECKING([for ALSA])
    AC_MSG_RESULT($alsa)
fi

dnl enable pulseaudio on user request and check installation
PALIB=""
PAHDR=""

AC_ARG_ENABLE(pa, [  --enable-pa           compile Pulse Audio support (default disabled)],enablepa="yes",enablepa="no")


AC_MSG_CHECKING([Pulse Audio enabled])
if test "$enablepa" == "yes"
then
    if test "x$HAVE_PULSE_AUDIO" = xyes ; then
        pa="yes";
	PALIB=$PULSEAUDIO_LIBS
	PAHDR="-DHAVE_PULSE_AUDIO" ;
	alsa="disabled";
	ALSALIB="";
	ALSADEF="";
	AC_MSG_NOTICE([
***********************!!!!!!! Notice !!!!!!!**********************
The Pulseaudio libraries were found and you have chosen to use the
Pulseaudio driver.  Note that ALSA is recommended as a better sound
driver to use, but on some systems (e.g. Ubuntu), getting ALSA to
work may be complicated.
*******************************************************************
])
    fi
else
    AC_MSG_RESULT($enablepa)
    pa="disabled"
fi


dnl enable single precision FFTW3 if installed
AC_ARG_ENABLE(single-fftw3,
            [  --enable-single-fftw3    compile single precision FFTW3 support (default double precision)],
            FFTWLIB="-lfftw3f";FFTWHDR="-DHAVE_FFTW3";FFTWPREC="-DFFTWPREC=1")

if test "$FFTWLIB" = ""
then
dnl Detect which version of fftw libs exist, if any
FFTWPREC=""
AC_CHECK_LIB(fftw3, fftw_plan_r2r_1d,FFTWLIB="-lfftw3";FFTWHDR="-DHAVE_FFTW3";FFTWPREC="-DFFTWPREC=2",
    AC_CHECK_LIB(fftw3f, fftwf_plan_r2r_1d,FFTWLIB="-lfftw3f";FFTWHDR="-DHAVE_FFTW3";FFTWPREC="-DFFTWPREC=1",
        AC_CHECK_LIB(fftw, fftw_create_plan, ,
	    AC_CHECK_LIB(dfftw, fftw_create_plan))))

if test "$FFTWLIB" = ""
then
AC_CHECK_LIB(rfftw, rfftw_one, FFTWLIB="-lrfftw -lfftw";FFTWHDR="-DHAVE_FFTW",
    AC_CHECK_LIB(drfftw, rfftw_one, FFTWLIB="-ldrfftw -ldfftw";FFTWHDR="-DHAVE_DFFTW",
	AC_MSG_ERROR("You need to install the fftw 3.x libraries from www.fftw.org")))
fi
fi


dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sys/ioctl.h sys/time.h unistd.h sndfile.h)
AC_HEADER_TIME

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

dnl refer to comments in gwc.c where these versions are required:
PKG_CHECK_MODULES(GTK, [ gtk+-2.0 >= 2.16 glib-2.0 >= 2.30 ])

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(gettimeofday)

dnl check for osx platform
AC_MSG_CHECKING([for Mac OS X platform])
case `uname -s` in
  Darwin)
    platform_osx=yes
    ;;
  *)
    platform_osx=no
    ;;
esac
AC_MSG_RESULT([$platform_osx])
if [test x$platform_osx = xyes]; then
  OSXDEF="-DMAC_OS_X"
  if test "$enablepa" == "yes"; then
    # pulseaudio is enabled (recommended until issues are fixed with coreaudio backend)
    OSXLIBS="$LIBS -lgtkmacintegration-gtk2"
  else
    # pulseaudio is not enabled so we need to build CoreAudio backend instead
    AC_MSG_NOTICE([
**********************!!!!!!! Warning !!!!!!!**********************
You are building on Apple OSX with the CoreAudio backend.
This currently does not seem to work.
Use --enable-pa to build with the pulseaudio backend instead.
*******************************************************************
])
    OSXLIBS="$LIBS -framework CoreAudio -lgtkmacintegration-gtk2" 
  fi
fi

AC_OUTPUT(Makefile)
