dnl Welcome to the Sphinx automated build system.
dnl try not to hurt yourself ;)

AC_INIT(pocketsphinx, 0.7)
AM_INIT_AUTOMAKE([no-define])
AC_SUBST(ACLOCAL_AMFLAGS, "-I m4")

CFLAGS=${CFLAGS:--g -O2 -Wall}

AC_CANONICAL_HOST

dnl
dnl Set proper compiler flags and such for some platforms
dnl
case $host in
     arm-wince-pe*)
	CFLAGS=${CFLAGS:--march=armv4 -mapcs-32 -malignment-traps}
	LIBS='-lc -lgcc -lwinsock -lcoredll'
	CFLAGS="$CFLAGS -DNEWLIB -DSARM -DWIN32 -DGNUWINCE"
	;;
     *apple*)
        LIBS="$LIBS -framework CoreAudio -framework AudioToolbox"
        ;;
     *uclinux*)
	# uClinux needs special "flat" binaries
	LDFLAGS="$LDFLAGS -Wl,-elf2flt"
	;;
     *)
     ;;
esac     

AC_PROG_CC
AM_PROG_LIBTOOL
AC_WORDS_BIGENDIAN
AC_CHECK_TYPES(long long)
AC_CHECK_SIZEOF(long long)
AC_CHECK_FUNCS(dup2)

dnl
dnl Check for pkgconfig
dnl
AC_CHECK_PROG(HAVE_PKGCONFIG, pkg-config, yes, no)

dnl
dnl Check for Doxygen, and build dox if present
dnl
AC_CHECK_PROG(HAVE_DOXYGEN, doxygen, yes, no)
AM_CONDITIONAL(BUILD_DOXYGEN, test "x$HAVE_DOXYGEN" = "xyes")

dnl
dnl Check for Python, and build python module if present
dnl
use_python=true
PYTHON=python
AC_ARG_WITH(python,
	AS_HELP_STRING([--without-python],
		       [Disable Python extension]),[
if test x$withval = xno; then
   use_python=false
   PYTHON=bogus
elif test x$withval = xyes; then
   # No specific python binary was given, so check for it in PATH
   use_python=true
   PYTHON=python
else
   # A python was given, assume it is in the user's PATH or is fully qualified
   use_python=true
   PYTHON="$withval"
   PYTHON_CONFIG="$withval"-config
fi
])
if test "x$use_python" = xtrue -a "x$PYTHON" = xpython; then
   AC_PATH_PROG(PYTHON, python, bogus)
   AC_PATH_PROG(PYTHON_CONFIG, python-config, bogus)
fi
if test "x$use_python" = xtrue -a "x$PYTHON" != xbogus -a "x$PYTHON_CONFIG" != xbogus; then
    PYTHON_INCLUDES=`"$PYTHON_CONFIG" --includes` 
    CPPFLAGS="$CPPFLAGS $PYTHON_INCLUDES"
    AC_CHECK_HEADER(Python.h,use_python=true,use_python=false)
else
    use_python=false
    PYTHON=bogus
fi
if test "x$use_python" != xtrue ; then
    AC_WARN([Disabling python since development headers were not found])
fi
AM_CONDITIONAL(BUILD_PYTHON, test "x$use_python" = "xtrue")
AC_SUBST(PYTHON)

dnl
dnl Check for Cython, and rebuild python module if present
dnl
if test "x$PYTHON" != "xbogus"; then
   AC_CHECK_PROG(HAVE_CYTHON, cython, yes, no)
fi
AM_CONDITIONAL(BUILD_CYTHON, test "x$HAVE_CYTHON" = "xyes")

dnl
dnl Now check for GStreamer, and build the plugin if it's available
dnl
GST_MAJORMINOR=0.10
PKG_CHECK_MODULES(GStreamer, [gstreamer-$GST_MAJORMINOR >= 0.10.0
		       gstreamer-base-$GST_MAJORMINOR >= 0.10.0
		       gstreamer-plugins-base-$GST_MAJORMINOR >= 0.10.0],
                  HAVE_GST=yes, HAVE_GST=no)
dnl Don't build GStreamer when cross-compiling
AM_CONDITIONAL(BUILD_GST, test x$cross_compiling != xyes && test "x$HAVE_GST" = "xyes")
GST_CFLAGS="$GStreamer_CFLAGS $GStreamer_ERROR"
GST_LIBS="$GStreamer_LIBS"
AC_SUBST(GST_MAJORMINOR)
AC_SUBST(GST_CFLAGS)
AC_SUBST(GST_LIBS)

dnl set the plugindir where plugins should be installed
if test "x${prefix}" = "x$HOME"; then
  plugindir="$HOME/.gstreamer-$GST_MAJORMINOR/plugins"
else
  plugindir="\$(libdir)/gstreamer-$GST_MAJORMINOR"
fi
AC_SUBST(plugindir)

dnl set proper LDFLAGS for plugins
GST_PLUGIN_LDFLAGS='-module -avoid-version -export-symbols-regex [_]*\(gst_\|Gst\|GST_\).*'
AC_SUBST(GST_PLUGIN_LDFLAGS)

dnl
dnl Get SphinxBase source from command line if given
dnl
AC_ARG_WITH(sphinxbase,
	AS_HELP_STRING([--with-sphinxbase=DIRECTORY],
			[Look for SphinxBase source files in DIRECTORY.  If this is 'auto',
			 the system-wide installation will be used.]),
			sphinxbase=$withval)

dnl
dnl Get SphinxBase build directory from command line if given
dnl
AC_ARG_WITH(sphinxbase-build,
	AS_HELP_STRING([--with-sphinxbase-build=DIRECTORY],
			[Look for SphinxBase object files in DIRECTORY]),
	sphinxbasebuild=$withval)


dnl
dnl Check for SphinxBase in parent directories
dnl
if test x$sphinxbase = x; then
   dn=`dirname $0`
   case "$dn" in
	.)
	sbdir="`pwd`/.."
	;;
	[\\/]* | ?:[\\/]*)
	sbdir="$dn/.."
	;;
	*)
	sbdir="`pwd`/$dn/.."
	;;
   esac
   # Look for sphinxbase in the parent directory
   for sb in "$sbdir/sphinxbase" \
	  "$sbdir/sphinxbase"*; do
       AC_MSG_CHECKING([for sphinxbase in $sb])
       if test -f "$sb/src/libsphinxbase/libsphinxbase.la"; then
          sphinxbase="$sb"
	  AC_MSG_RESULT(yes)
	  break
       else
	  AC_MSG_RESULT(no)
       fi
   done
fi

dnl
dnl Check for system SphinxBase if none was passed to us
dnl Also allow --with-sphinxbase=auto to use system one explicitly
dnl
if test x$sphinxbase = x || test x$sphinxbase = xauto; then
   sphinxbase=
   if test "x$HAVE_PKGCONFIG" = "xno"; then
      SphinxBase_CFLAGS = "-Iusr/include/sphinxbase -I/usr/local/include/sphinxbase"
      SphinxBase_LIBS = "-lsphinxbase"
   else
      PKG_CHECK_MODULES(SphinxBase, [sphinxbase],,[
      AC_MSG_FAILURE(dnl
[SphinxBase was not found on your system.
Make sure that you have installed it and that the
PKG_CONFIG_PATH environment variable is set correctly, if
it was installed in a non-standard prefix.])])
   fi
   LIBS="$LIBS $SphinxBase_LIBS"
   CPPFLAGS="$CPPFLAGS $SphinxBase_CFLAGS"
   AC_CHECK_HEADER(sphinx_config.h,,[
   AC_MSG_FAILURE(dnl
[SphinxBase was not found on your system.])])
fi

dnl
dnl Now verify SphinxBase if defined
dnl Sadly, this doesn't work when cross-compiling (for some dumb reason...)
dnl
: ${sphinxbasebuild=$sphinxbase}
if test x$sphinxbase != x && test x$cross_compiling = xyes; then
   CPPFLAGS="-I$sphinxbase/include -I$sphinxbasebuild/include $CPPFLAGS"
   LIBS="$LIBS -lsphinxbase"
   # We include libsphinxbase/.libs because of stupid broken versions of libtool
   LDFLAGS="$LDFLAGS -L$sphinxbasebuild/src/libsphinxad \
		     -L$sphinxbasebuild/src/libsphinxbase \
		     -L$sphinxbasebuild/src/libsphinxbase/.libs"
fi
if test x$sphinxbase != x && test x$cross_compiling != xyes; then
   AC_CHECK_FILE($sphinxbase/include/sphinxbase/prim_type.h,
		 CPPFLAGS="-I$sphinxbase/include $CPPFLAGS",
   AC_ERROR(
[SphinxBase headers not found in $sphinxbase.  Please use the
--with-sphinxbase option to `configure' to specify the location of
SphinxBase.  Run $0 --help for more information.]))
fi
# We include libsphinxbase/.libs because of stupid broken versions of libtool
if test x$sphinxbasebuild != x && test x$cross_compiling != xyes; then
   AC_CHECK_FILE($sphinxbasebuild/src/libsphinxbase/libsphinxbase.la,
[CPPFLAGS="-I$sphinxbasebuild/include $CPPFLAGS"
LIBS="$LIBS -lsphinxbase"
LDFLAGS="$LDFLAGS -L$sphinxbasebuild/src/libsphinxad \
		  -L$sphinxbasebuild/src/libsphinxbase \
		  -L$sphinxbasebuild/src/libsphinxbase/.libs"],
		   AC_ERROR(
[SphinxBase libraries were not found in $sphinxbasebuild.
Use the --with-sphinxbase-build option to `configure' to specify
the build directory for SphinxBase.  Run $0 --help for more information.]))
fi

AC_SUBST(sphinxbase)
AC_SUBST(sphinxbasebuild)

AC_OUTPUT([
pocketsphinx.pc
Makefile
include/Makefile
python/Makefile
python/setup.py
src/Makefile
src/libpocketsphinx/Makefile
src/programs/Makefile
src/gst-plugin/Makefile
doc/Makefile
doc/doxyfile
model/Makefile
model/hmm/Makefile
model/lm/Makefile
scripts/Makefile
test/Makefile
test/testfuncs.sh
test/unit/Makefile
test/regression/Makefile
])
