dnl Copyright (c) 1991-2002 Doshita Lab. Speech Group, Kyoto University
dnl Copyright (c) 2000-2002 Speech and Acoustics Processing Lab., NAIST
dnl    All rights reserved
dnl
dnl  $Id: configure.in,v 1.9 2002/11/19 04:03:29 ri Exp $
dnl

dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/phmm/outprob.c)
AC_CONFIG_HEADER(include/sent/config.h)
AC_CONFIG_AUX_DIR(../support)

VERSION=3.3-multipath

dnl Checks for options
# specify mic type
AC_ARG_WITH(mictype,
[  --with-mictype=TYPE     specify mic I/O (oss|alsa|freebsd|sol2|sun4|irix)],,with_mictype=auto)

# NetAudio support
AC_ARG_WITH(netaudio-dir,
[  --with-netaudio-dir=DIR DatLink/NetAudio include/lib are in DIR],,with_netaudio_dir=auto)

dnl disable pthread
AC_ARG_ENABLE(pthread,
[  --disable-pthread       [debug] do not use pthread for speech input],
want_pthread=$enableval
,want_pthread=yes)

AC_ARG_ENABLE(addarray,
[  --disable-addarray      [debug] do not use addlog_array() function],
if test $enableval = yes; then
	AC_DEFINE(USE_ADDLOG_ARRAY)
fi
,AC_DEFINE(USE_ADDLOG_ARRAY))

dnl extend to int
AC_ARG_ENABLE(words-int,
[  --enable-words-int      use integer instead of unsigned short for word ID
                           to extend vocabulary limit to 2^31=2G words],
if test $enableval = yes; then
	AC_DEFINE(WORDS_INT)
fi)

AC_SUBST(VERSION)

dnl Checks for system.
AC_CANONICAL_HOST

dnl Checks for optimization flag
AC_MSG_CHECKING([default optimization flag])
if test -z "$CFLAGS" ; then
    OPTFLAG=../support/cflags.${host_cpu}-${host_vendor}-${host_os}
    if test -f $OPTFLAG ; then
	. $OPTFLAG
	AC_MSG_RESULT([$OPTFLAG])
    else
	AC_MSG_RESULT([no])
    fi
else
    AC_MSG_RESULT([skipped])
fi

dnl Checks for compiler.
AC_PROG_CC
AC_PROG_CPP

dnl Checks for programs.
AC_PATH_PROG(RM, rm)
AC_PATH_PROG(AR, ar)
AC_PROG_RANLIB

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_BIGENDIAN
AC_C_CONST
dnl AC_TYPE_SIZE_T
AC_MSG_CHECKING([for socklen_t])
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
],[socklen_t dummy;],
AC_DEFINE(HAVE_SOCKLEN_T)
AC_MSG_RESULT([yes]),
AC_MSG_RESULT([no]))

dnl Checks for library functions.
dnl AC_PROG_GCC_TRADITIONAL
dnl AC_FUNC_VPRINTF
dnl AC_CHECK_FUNCS(strdup strstr)
AC_CHECK_FUNC(gethostbyname,,AC_CHECK_LIB(nsl,gethostbyname))
AC_CHECK_FUNC(connect,,AC_CHECK_LIB(socket, connect))

dnl Checks for audio input libraries (order is important)
if test $with_mictype = auto; then
  AC_MSG_CHECKING(for microphone type)
  altype=no
  case "$host_os" in
  linux-*)
#	# ALSA
#	AC_TRY_CPP([#include <sys/asoundlib.h>],altype=alsa)
#	if test $altype = no; then
	  # OSS
	  AC_TRY_CPP([#include <sys/soundcard.h>],altype=oss)
#	fi
	;;
  freebsd*)
	# FreeBSD (snd driver)
	AC_TRY_CPP([#include <machine/soundcard.h>],altype=freebsd)
	;;
  solaris2*)
	# Solaris2.x Built-in Audio
	AC_TRY_CPP([#include <sys/audioio.h>],altype=sol2)
	;;
  sunos4*)
	# SunOS4 Audio
	AC_TRY_CPP([#include <multimedia/libaudio.h>],altype=sun4)
	;;
  irix6*)
 	# O2 (SGI IRIX6.3)
	AC_TRY_CPP([#include <audio.h>],altype=irix)
	;;
#  osf*)
#	# Digital Unix 4.0 --- not yet
#	AC_TRY_CPP([#include <mme/mme_api.h>],altype=osf1)
#	;;
  esac
else
  altype=$with_mictype
fi
case $altype in
    oss)
	aldesc="Open Sound System compatible"
	ADINOBJ=src/adin/adin_mic_linux_oss.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB=""
	;;
    alsa)
	aldesc="Advanced Linux Sound Architecture"
	ADINOBJ=src/adin/adin_mic_linux_alsa.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB="-lasound"
	;;
    freebsd)
	aldesc="FreeBSD snd driver"
	ADINOBJ=src/adin/adin_mic_freebsd.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB=""
	;;
    sol2)
	aldesc="Solaris2.x audio interface"
	ADINOBJ=src/adin/adin_mic_sol2.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB=""
	;;
    sun4)
	aldesc="SunOS4.x audio interface"
	ADINOBJ=src/adin/adin_mic_sun4.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB="-laudio"
	;;
    irix)
	aldesc="IRIX6.x audio interface"
	ADINOBJ=src/adin/adin_mic_o2.o
	AC_DEFINE(USE_MIC)
	SOUNDLIB="-laudio" ;;
#    osf1)
#	aldesc="Digital/Unix 4.0"
#	ADINOBJ=src/adin/adin_mic_osf1.o
#	AC_DEFINE(USE_MIC)
#	SOUNDLIB="-L/usr/opt/MME242/lib -lmme")
#	;;
    *)
	aldesc="no support"
	;;
esac
AC_MSG_RESULT($altype)

# NetAudio
if test $with_netaudio_dir = auto ; then
	AC_MSG_CHECKING([for DatLink/NetAudio support])
else
	AC_MSG_CHECKING([for DatLink/NetAudio support on ${with_netaudio_dir}])
fi
TMPCPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS -I${with_netaudio_dir}/include"
use_netaudio=no
AC_TRY_CPP([#include <netaudio.h>],
	use_netaudio=yes
	AC_DEFINE(USE_NETAUDIO)
	SOUNDINC="-I${with_netaudio_dir}/include"
	ADINOBJ="$ADINOBJ src/adin/adin_netaudio.o src/adin/adin_na.o "
	SOUNDLIB="$SOUNDLIB -L${with_netaudio_dir}/lib -lnetaudio -lsupport")
AC_MSG_RESULT($use_netaudio)
CPPFLAGS="$TMPCPPFLAGS"
EXTRAOBJ="$EXTRAOBJ $ADINOBJ"

AC_SUBST(altype)
AC_SUBST(aldesc)
AC_SUBST(use_netaudio)

dnl file decompress method check
use_zlib=no
# AC_CHECK_LIB(z, deflate,
#   AC_CHECK_HEADERS(zlib.h,
#     use_zlib=yes
#     EXTRALIB="$EXTRALIB -lz"
#     AC_DEFINE(HAVE_ZLIB)))
if test $use_zlib = no; then
  AC_CHECK_PROG(GZIP, gzip, found, no)
  if test $GZIP = no; then
    AC_MSG_WARN([not found, reading compressed file disabled])
  else
    AC_DEFINE(ZCAT, ["gzip -d -c"])
  fi
fi

dnl thread support
if test $want_pthread = yes; then
  case "$host_os" in
  freebsd*)
	# FreeBSD
	AC_MSG_CHECKING([for linking POSIX threaded process])
 	ac_save_CFLAGS="$CFLAGS"
 	CFLAGS="$CFLAGS -pthread"
 	AC_TRY_LINK([#include <pthread.h>],[pthread_equal(NULL,NULL);],
     	    use_pthread=yes
     	    AC_DEFINE(HAVE_PTHREAD)
	    SOUNDINC="$SOUNDINC -pthread",
   	  use_pthread=no
	)
	CFLAGS="$ac_save_CFLAGS"
	;;
  *)
	# other with libpthread
	AC_MSG_CHECKING([for POSIX thread library in -lpthread])
	ac_save_LIBS="$LIBS"
	LIBS="$LIBS -lpthread"
	AC_TRY_LINK([#include <pthread.h>],[pthread_equal(NULL,NULL);],
	    use_pthread=yes
	    AC_DEFINE(HAVE_PTHREAD)
	    EXTRALIB="$EXTRALIB -lpthread",
	  use_pthread=no
	)
	LIBS="$ac_save_LIBS"
  esac
  AC_MSG_RESULT("$use_pthread")
else
  use_pthread=no
fi
AC_SUBST(use_pthread)

dnl libsndfile
wavefile_support="RAW and WAV only"
have_libsndfile=no
AC_CHECK_LIB(sndfile, sf_open,
  AC_CHECK_HEADERS(sndfile.h,
    wavefile_support='a large number of formats by libsndfile ver.1'
    AC_DEFINE(HAVE_LIBSNDFILE)
    AC_DEFINE(HAVE_LIBSNDFILE_VER1)
    EXTRALIB="$EXTRALIB -lsndfile"
    have_libsndfile=yes))
if test $have_libsndfile = no; then
  AC_CHECK_LIB(sndfile, sf_open_read,
    AC_CHECK_HEADERS(sndfile.h,
      wavefile_support='a large number of formats by libsndfile ver.0'
      AC_DEFINE(HAVE_LIBSNDFILE)
      EXTRALIB="$EXTRALIB -lsndfile"
      have_libsndfile=yes))
fi
if test $have_libsndfile = no; then
  AC_MSG_WARN([libsndfile enables AIFF AU SND NIST reading.
It's available at http://www.zip.com.au/~erikd/libsndfile/])
fi
AC_SUBST(wavefile_support)

AC_SUBST(EXTRAOBJ)
AC_SUBST(EXTRALIB)
AC_SUBST(SOUNDINC)
AC_SUBST(SOUNDLIB)

AC_OUTPUT_COMMANDS(
[chmod +x libsent-config
./libsent-config --info
],
altype=$altype
aldesc="$aldesc"
use_netaudio=$use_netaudio
use_pthread=$use_pthread
wavefile_support="$wavefile_support"
)
AC_OUTPUT(Makefile libsent-config)
