AC_INIT(include/h323.h)

AC_PROG_CXX

dnl ########################################################################
dnl set the OpenH323 directory to the current directory

OPENH323DIR=`pwd`
AC_SUBST(OPENH323DIR)

dnl ########################################################################
dnl set the PREFIX accordingly
if test "x$prefix" = "xNONE"; then
   INSTALLPREFIX="/usr/local"
else
   INSTALLPREFIX="${prefix}"
fi

AC_SUBST(INSTALLPREFIX)


dnl ########################################################################
dnl set LIBDIR accordingly
LIBDIR="${libdir}"

AC_SUBST(LIBDIR)


dnl ########################################################################
dnl extract the OpenH323 version
MAJOR_VERSION=`cat ${OPENH323DIR}/version.h | grep MAJOR_VERSION | cut -f3 -d' '`
MINOR_VERSION=`cat ${OPENH323DIR}/version.h | grep MINOR_VERSION | cut -f3 -d' '`
BUILD_NUMBER=`cat ${OPENH323DIR}/version.h | grep BUILD_NUMBER | cut -f3 -d' '`
OPENH323_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${BUILD_NUMBER}"
AC_SUBST(OPENH323_VERSION)

echo "OpenH323 version is ${OPENH323_VERSION}"


dnl ########################################################################
dnl look for ptlib, use a preference order of explicit PWLIBDIR, directory
dnl at same level, home directory, /usr/local or /usr.

if test "${PWLIBDIR:-unset}" != "unset" ; then
  AC_CHECK_FILE(${PWLIBDIR}/version.h, HAS_PTLIB=1)
fi
if test "${HAS_PTLIB:-unset}" = "unset" ; then
  AC_CHECK_FILE(${OPENH323DIR}/../pwlib/version.h, HAS_PTLIB=1)
  if test "${HAS_PTLIB:-unset}" != "unset" ; then
    PWLIBDIR="${OPENH323DIR}/../pwlib"
  else
    AC_CHECK_FILE(${HOME}/pwlib/include/ptlib.h, HAS_PTLIB=1)
    if test "${HAS_PTLIB:-unset}" != "unset" ; then
      PWLIBDIR="${HOME}/pwlib"
    else
      AC_CHECK_FILE(/usr/local/include/ptlib.h, HAS_PTLIB=1)
      if test "${HAS_PTLIB:-unset}" != "unset" ; then
        AC_PATH_PROG(PTLIB_CONFIG, ptlib-config, , /usr/local/bin)
      else
        AC_CHECK_FILE(/usr/include/ptlib.h, HAS_PTLIB=1)
        if test "${HAS_PTLIB:-unset}" != "unset" ; then
          AC_PATH_PROG(PTLIB_CONFIG, ptlib-config, , /usr/share/pwlib/make/)
        fi
      fi
    fi
  fi
fi

if test "${HAS_PTLIB:-unset}" = "unset" ; then
  echo "Cannot find pwlib - please install or set PWLIBDIR and try again"
  exit
fi

if test "${PWLIBDIR:-unset}" = "unset" ; then
  if test "${PTLIB_CONFIG:-unset}" = "unset" ; then
    echo "Cannot find ptlib-config - please install and try again"
    exit
  fi
  PWLIBDIR=`$PTLIB_CONFIG --prefix`
fi

if test "x$PWLIBDIR" = "x/usr" -o "x$PWLIBDIR" = "x/usr/"; then
  PWLIBDIR="/usr/share/pwlib/"
fi
if test "xPWLIBDIR" = "x/usr/local" -o "x$PWLIBDIR" = "x/usr/"; then
  PWLIBDIR="/usr/local/share/pwlib/"
fi

echo "PWLib prefix set to.... $PWLIBDIR"

AC_SUBST(PWLIBDIR)


dnl ########################################################################
dnl look for avcodec from library.

dnl MSWIN ffmpeg AVCODEC Library,avcodec,H323_AVCODEC,1,@H323_AVCODEC_DIR@,avcodec.h,,..\ffmpeg\libavcodec\,..\external\ffmpeg\libavcodec\,\ffmpeg\libavcodec\,c:\ffmpeg\libavcodec\

AC_CHECK_HEADERS(avcodec.h, H323_AVCODEC=1)

if test "${H323_AVCODEC:-unset}" != "unset" ; then
  AC_CHECK_LIB(avcodec, avcodec_open, H323_AVCODEC=1, H323_AVCODEC=)
fi

if test "${H323_AVCODEC:-unset}" != "unset" ; then
  AC_CHECK_FILE(/usr/local/include/ffmpeg/avcodec.h, H323_AVCODEC=1)
fi

if test "${H323_AVCODEC:-unset}" != "unset" ; then
  AC_SUBST(H323_AVCODEC, 1)
  AC_DEFINE(H323_AVCODEC, 1, "ffmpeg based avodec found")
  AVCODECINCLUDEDIR=/usr/local/include/ffmpeg
  AVCODECLIBDIR=/usr/local/lib
  STDCCFLAGS="$STDCCFLAGS -I${AVCODECINCLUDEDIR}"

  echo  "                      H263 codec on FFmpeg library  enabled"
fi

AC_SUBST(H323_AVCODEC)

dnl ########################################################################
dnl look for vich263 library

dnl MSWIN vich263 VICH263 Library,vich263,H323_VICH263,1,@H323_VICH263_DIR@,encoder-h263.h,,..\vich263\,..\external\vich263\,\vich263\,c:\vich263\

AC_CHECK_HEADERS(encoder-h263.h, H323_VICH263=1)

if test "${H323_VICH263:-unset}" != "unset" ; then
  AC_CHECK_FILE(/usr/local/lib/libvich263.so, H323_VICH263=1, H323_VICH263=)
fi

if test "${H323_VICH263:-unset}" != "unset" ; then
  AC_CHECK_FILE(/usr/local/include/vich263/encoder-h263.h, H323_VICH263=1)
fi

if test "${H323_VICH263:-unset}" != "unset" ; then
  AC_SUBST(H323_VICH263, 1)
  AC_DEFINE(H323_VICH263, 1, "vic H263 codec found")
  VICH263INCLUDEDIR=/usr/local/include/vich263
  VICH263LIBDIR=/usr/local/lib
  STDCCFLAGS="$STDCCFLAGS -I${VICH263INCLUDEDIR}"

  echo  "                      H263 codec on libvich263 enabled"
fi

AC_SUBST(H323_VICH263)


dnl ########################################################################
dnl Look for G.729 codec

dnl MSWIN Voice Age G.729A,vag729,VOICE_AGE_G729A,1,@VOICE_AGE_DIR@,va_g729a.h,,..\va_g729\,..\external\va_g729,\va_g729,c:\va_g729


dnl ########################################################################
dnl make directives

AC_SUBST(STDCCFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(ENDLDLIBS)

AC_CONFIG_FILES(openh323u.mak)
AC_CONFIG_FILES(Makefile)


dnl ########################################################################
dnl Header file

AC_CONFIG_HEADERS(include/openh323buildopts.h)


dnl ########################################################################
dnl Output configured files

AC_OUTPUT()

