dnl ---------------------------------------------------------------------
dnl Process this file with autoconf to produce a configure script.

AC_INIT(amsn)
AC_CONFIG_HEADER(utils/linux/capture/config.h)


APPNAME=amsn
APPVERSION=0.98.3
APPRELEASE=1
APPSTABLE=yes

AC_SUBST(APPVERSION)
AC_SUBST(APPNAME)
AC_SUBST(APPRELEASE)
AC_SUBST(APPSTABLE)

dnl ---------------------------------------------------------------------
dnl compile time options

AC_ARG_ENABLE(debug,
  [  --enable-debug          enable some debugging information],[enable_debug=$enableval],[enable_debug=no])

if test "$enable_debug" = "yes" ; then
    DEBUG="yes"
else
    DEBUG="no"
fi
AC_SUBST(DEBUG)

AC_ARG_ENABLE(static,
  [  --enable-static         enable static link of libstdc++],[enable_static=$enableval],[enable_static=no])

if test "$enable_static" = "yes" ; then
    STATIC="yes"
else
    STATIC="no"
fi
AC_SUBST(STATIC)

dnl subst configure command
AC_SUBST(CONFIGURE_ARGS)
CONFIGURE_ARGS=`echo $@`

dnl Check for OS
#`uname -s`
case `uname -s` in
	Linux)
		FOUND_OS="linux"
		;;
	OpenBSD | FreeBSD | NetBSD)
		# *BSD has important stuff (from ports)
		# in /usr/local ...
		CFLAGS="$CFLAGS -I/usr/local/include -L/usr/local/lib"
		LDFLAGS="$LDFLAGS -L/usr/local/lib"
		FOUND_OS="bsd"
		;;
	Darwin)
		FOUND_OS="mac"
		CFLAGS="$CFLAGS -DMAC_OSX_TK=1 -I${prefix}/include -L${prefix}/lib"
		CXXFLAGS="$CXXFLAGS -I${prefix}/include -L${prefix}/lib"
		LDFLAGS="$LDFLAGS -I${prefix}/include -L/usr/lib -L${prefix}/lib"
		
		# Directories in which the Tcl core can be found
		TCL_INC_SPEC="-I/Library/Frameworks/Tcl.framework/Headers -DUSE_TCL_STUBS"
		TCL_STUB_LIB_SPEC="-L/Library/Frameworks/Tcl.framework -ltclstub8.5"
		
		# Directories in which the Tk core can be found
		TK_INC_SPEC="-I/Library/Frameworks/Tk.framework/Headers -DUSE_TK_STUBS"
		TK_STUB_LIB_SPEC="-L/Library/Frameworks/Tk.framework -ltkstub8.5"
		;;
	SunOS)
		FOUND_OS=solaris
		;;
	*)
		AC_MSG_CHECKING(if amsn will build on `uname -s`)
		AC_MSG_RESULT(maybe)
		FOUND_OS="unknown"
		;;
esac

dnl ---------------------------------------------------------------------
dnl Checks for TCL and TK.

AC_PREFIX_PROGRAM(wish)

TCLSH=`which tclsh`
VERSION=`echo "puts $::tcl_version ; exit" | ${TCLSH}`
AC_ARG_ENABLE(version-check, [  --disable-version-check Disable checking that linked tcl/tk version is the same version that bin program tclsh ],    [version_ok=$enableval], [version_ok=yes])

OLDV=`expr $VERSION \< 8.4`

if test "x${version_ok}" = "xyes" -a $OLDV = 1; then
    AC_MSG_ERROR(Your current Tcl/Tk installation has a version number of $VERSION. The minimal version required for aMSN to run is Tcl/Tk 8.4)
fi



dnl ---------------------------------------------------------------------
dnl Checks for programs.

AC_PROG_CC

CC_INSTALLED=`which $CC`
if test "x${CC_INSTALLED}" = "x"; then
    AC_MSG_ERROR(You need a C compiler (gcc) to compiler aMSN. Please install gcc and try again) 
fi

AC_PROG_CXX

CXX_INSTALLED=`which $CXX`
if test "x${CXX_INSTALLED}" = "x"; then
     AC_MSG_ERROR(You need a C++ compiler (g++) to compiler aMSN. Please install g++ and try again) 
fi


BUILD_DIR=`pwd`
cd ${srcdir}
TOP_DIR=`pwd`
LIBDIR=`pwd`
cd "${BUILD_DIR}"

AC_SUBST(TCLSH)
AC_SUBST(TOP_DIR)
AC_SUBST(LIBDIR)
AC_SUBST(BUILD_DIR)


if test $FOUND_OS != "mac"; then
	#--------------------------------------------------------------------
	#	Read in configuration information generated by Tcl for shared
	#	libraries, and arrange for it to be substituted into our
	#	Makefile.
	#--------------------------------------------------------------------
	
	AC_MSG_CHECKING(tcl build dir)
	TCL_LIB_DIR=$exec_prefix/lib
	dirs="	/usr/lib/tcl8.* \
		/usr/local/lib/tcl8.* \
		/Library/Frameworks/Tcl.framework \
		~/Library/Frameworks/Tcl.framework \
		/Network/Library/Frameworks/Tcl.framework \
		/System/Library/Frameworks/Tcl.framework \
		/usr/local/lib \
		/usr/lib \
		/usr/local/lib64 \
		/usr/lib64 \
		/usr/lib64/tcl8.* \
		/usr/local/lib64/tcl8.* \
		`locate tclConfig.sh`"
	
	for i in $dirs ; do
	
		if test -f "$i"; then
			i=`dirname $i`
		fi
	
		if test -d "$i" -a -f "$i/tclConfig.sh"; then
			file=$i/tclConfig.sh
			. $file
		
			if test "x${version_ok}" != "xyes"; then
			VERSION=${TCL_VERSION}
			fi
		
				if  test "x${TCL_INC_DIR}" != "x" -o "x${TCL_INC_SPEC}" != "x" \
					-o "x${TCL_INCLUDE_DIR}" != "x" -o "x${TCL_INCLUDE_SPEC}" != "x"; then
				if test "x${TCL_VERSION}" = "x${VERSION}" ; then
				TCL_LIB_DIR=`cd $i; pwd`
				break;
				fi
			elif test -f ${TCL_PREFIX}/include/tcl.h; then
				TCL_LIB_DIR=`cd $i; pwd`
				break;
			fi
	
		fi
	
	done
	AC_ARG_WITH(tcl, [  --with-tcl=DIR          use Tcl binaries from DIR], TCL_LIB_DIR=$withval,)
	if test -d $TCL_LIB_DIR; then
		AC_MSG_RESULT([using tcl library in $TCL_LIB_DIR])
	else
		AC_MSG_ERROR(Unable to find Tcl directory or Tcl package is not tcl-dev)
	fi
	
	cd ${TCL_LIB_DIR}/..
	TCL_TOP_DIR=`pwd`
	cd "${BUILD_DIR}"
	
	#--------------------------------------------------------------------
	
	file=$TCL_LIB_DIR/tclConfig.sh
	. $file
	#CC=$TCL_CC
	#CXX=$TCL_CXX
	SHLIB_CFLAGS=`echo "$TCL_SHLIB_CFLAGS" |sed -e "s|PIC|pic|" -e "s|+Z|+z|"`
	SHLIB_LD=$TCL_SHLIB_LD
	SHLIB_SUFFIX=$TCL_SHLIB_SUFFIX
	DL_LIBS=$TCL_DL_LIBS
	#LD_FLAGS=$TCL_LD_FLAGS
	
	TCL_BASE=`echo ${TCL_LIB_SPEC} |sed -e "s|^.*-l||"`
	TCL_STUB_BASE=`echo ${TCL_STUB_LIB_SPEC} |sed -e "s|^.*-l||"`
	
	AC_SUBST(TCL_LIB_DIR)
	
	if test "x${TCL_INC_SPEC}" != "x" ; then
	TCL_INC_DIR=$TCL_INC_SPEC
	fi
	
	
	if  test "x${TCL_INCLUDE_SPEC}" != "x"; then
	TCL_INC_DIR=$TCL_INCLUDE_SPEC
	fi
	
	if test "x${TCL_INC_DIR}" = "x"; then
	TCL_INC_DIR=$TCL_INCLUDE_DIR
	fi
	
	if test "x${TCL_INC_DIR}" = "x" -a -f ${TCL_PREFIX}/include/tcl.h; then
	TCL_INC_DIR=${TCL_PREFIX}/include
	fi
	
	if test "x${TCL_INC_DIR}" = "x"; then
	AC_MSG_ERROR(You dot not have a devel package of Tcl)
	fi
	
	case $TCL_INC_DIR in
		-I*) ;;
		*) TCL_INC_DIR=-I$TCL_INC_DIR;;
	esac
	
	TCL_INC_SPEC=$TCL_INC_DIR
	
	framework=`echo "${TCL_BASE}" | grep -- "-framework"`
	if test "x${framework}" = "x"; then
	TCL_LIB_SPEC="-L${TCL_LIB_DIR} -l${TCL_BASE}"
	fi
	TCL_STUB_LIB_SPEC="-L${TCL_LIB_DIR} -l${TCL_STUB_BASE}"
	eval "TCL_UNSHARED_LIB_FILE=lib${TCL_BASE}${TCL_UNSHARED_LIB_SUFFIX}"
	eval "TCL_SHARED_LIB_FILE=lib${TCL_BASE}${TCL_SHARED_LIB_SUFFIX}"
	

	#--------------------------------------------------------------------
	#	Read in configuration information generated by TK, and 
	#	arrange for it to be substituted into our Makefile.
	#--------------------------------------------------------------------
	
	AC_MSG_CHECKING(tk build dir)
	TK_LIB_DIR=$prefix/lib
	if test -f "${TCL_LIB_DIR}/tkConfig.sh"; then
		TK_LIB_DIR=$TCL_LIB_DIR
	else
		TK_LIB_DIR=$prefix/lib
		dirs="/usr/lib/tk8.* \
		/usr/local/lib/tk8.* \
		/Library/Frameworks/Tk.framework \
		~/Library/Frameworks/Tk.framework \
		/Network/Library/Frameworks/Tk.framework \
		/System/Library/Frameworks/Tk.framework \
		/usr/local/lib \
		/usr/lib \
		/usr/local/lib64 \
		/usr/lib64 \
		/usr/lib64/tk8.* \
		/usr/local/lib64/tk8.* \
		`locate tkConfig.sh`"
	
	
		for i in $dirs ; do
	
		if test -f "$i"; then
		i=`dirname $i`
		fi
	
		if test -d "$i" -a -f "$i/tkConfig.sh"; then
		file=$i/tkConfig.sh
		. $file
	
		if test "x${version_ok}" != "xyes"; then
			VERSION=${TK_VERSION}
		fi
	
		if  test "x${TK_INC_DIR}" != "x" -o "x${TK_INC_SPEC}" != "x" \
				-o "x${TK_INCLUDE_DIR}" != "x" -o "x${TK_INCLUDE_SPEC}" != "x"; then
				if test "x${TK_VERSION}" = "x${VERSION}"; then
			TK_LIB_DIR=`cd $i; pwd`
			break;
			fi
		elif test -f ${TK_PREFIX}/include/tk.h; then
			TK_LIB_DIR=`cd $i; pwd`
			break;
		fi
		fi
	
		done
	
	fi
	AC_ARG_WITH(tk, [  --with-tk=DIR           use Tk binaries from DIR], TK_LIB_DIR=$withval,)
	if test -d $TK_LIB_DIR; then
		AC_MSG_RESULT([using tk library in $TK_LIB_DIR])
	else
		AC_MSG_ERROR(Unable to find Tk directory or Tk package is not tk-dev)
	fi
	
	cd ${TK_LIB_DIR}/..
	TK_TOP_DIR=`pwd`
	cd "${BUILD_DIR}"
	
	file=$TK_LIB_DIR/tkConfig.sh
	. $file
	AC_SUBST(TK_VERSION)
	AC_SUBST(TK_LIB)
	AC_SUBST(TK_LIBS)
	
	AC_SUBST(TK_LIB_DIR)
	
	if test "x${TK_INC_SPEC}" != "x" ; then
	TK_INC_DIR=$TK_INC_SPEC
	fi
	
	if  test "x${TK_INCLUDE_SPEC}" != "x"; then
	TK_INC_DIR=$TK_INCLUDE_SPEC
	fi
	
	if test "x${TK_INC_DIR}" = "x"; then
	TK_INC_DIR=$TK_INCLUDE_DIR
	fi
	
	if test "x${TK_INC_DIR}" = "x" -a -f ${TK_PREFIX}/include/tk.h; then
	TK_INC_DIR=${TK_PREFIX}/include
	fi
	
	if test "x${TK_INC_DIR}" = "x"; then
	AC_MSG_ERROR(You dot not have a devel package of Tk)
	fi
	
	case $TK_INC_DIR in
		-I*) ;;
		*) TK_INC_DIR=-I$TK_INC_DIR;;
	esac
	
	TK_INC_SPEC=$TK_INC_DIR
fi

AC_SUBST(TCL_INC_SPEC)
AC_SUBST(TK_INC_SPEC)

AC_SUBST(TCL_STUB_LIB_SPEC)
AC_SUBST(TK_STUB_LIB_SPEC)

AC_SUBST(TCL_VERSION)
AC_SUBST(TK_VERSION)

dnl ---------------------------------------------------------------------
dnl do some OS specific stuff here

dnl Check for libstdc++
AC_CHECK_LIB(stdc++,main,CXX_LIB="-lstdc++", AC_MSG_ERROR(stdc++ library not found) )
AC_SUBST(CXX_LIB)

AC_SUBST(LDLIBS)
AC_SUBST(FOUND_X11)
AC_SUBST(FOUND_OS)
FOUND_X11="no"


dnl Check for X11

AC_PATH_XTRA
if test "$no_x" != "yes"; then
  FOUND_X11="yes"
  X_LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
else
   AC_MSG_RESULT("Could not find X11 devel package, traydock will not be compiled")
fi

if test "$FOUND_OS" = "solaris"; then
	# If possible, build webcam support.  Otherwise, default to
	# Linux for other goodies.
	AC_CHECK_HEADERS(sys/videodev2.h,FOUND_OS=solaris,FOUND_OS=linux)
fi

#---------------------------------------------------------------------------------------------

dnl ---------------------------------------------------------------------
dnl Check for required libs for TkCxImage

dnl Check for png

AC_CHECK_LIB(png, png_read_info,
       [AC_CHECK_HEADER(png.h,
         ,
         AC_MSG_ERROR([png headers not found]))],
       AC_MSG_ERROR([libpng is required]), -lz -lm)

dnl Check for jpeg

AC_CHECK_LIB(jpeg, jpeg_CreateDecompress,
       [AC_CHECK_HEADER(jpeglib.h,
         [AC_CHECK_HEADER(jerror.h,
           ,
           AC_MSG_ERROR([jerror headers not found]))],
         AC_MSG_ERROR([jpeg headers not found]))],
       AC_MSG_ERROR([libjpeg is required]))


dnl ---------------------------------------------------------------------
dnl Checks for functions

AC_CHECK_FUNCS(ftello fseeko getpt strcasestr memmem dlopen )

AC_CHECK_LIB(pthread,pthread_create,LIBPTHREAD="-lpthread")
if test "$LIBPTHREAD" = ""; then
  AC_CHECK_LIB(c_r,pthread_create,LIBPTHREAD="-lc_r")
fi

AC_SUBST(LIBPTHREAD)


AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)

dnl ---------------------------------------------------------------------
dnl deinterlace plugins
AC_MSG_CHECKING(if mmx should be used)
AC_SUBST(USE_MMX)
if test "$enable_mmx" = "no" -o "$enable_mmx" = "yes"; then
	USE_MMX="$enable_mmx"
else
	case "`uname -m`" in
		i586 | i686)
			USE_MMX="yes"
			;;
		*)
			USE_MMX="no"
			;;
	esac
fi
AC_MSG_RESULT($USE_MMX)

HAVE_FARSIGHT=no
GST_REQUIRED=0.10.22
GST_MAJORMINOR=0.10
FARSIGHT_REQUIRED=0.0.9
 
dnl Check for pkgconfig first
AC_CHECK_PROG(HAVE_PKGCONFIG, pkg-config, yes, no)


dnl Give error and exit if we don't have pkgconfig
if test "x$HAVE_PKGCONFIG" = "xyes"; then
  PKG_CHECK_MODULES(GLIB, \
    glib-2.0 >= 2.14,
    HAVE_GLIB=yes,HAVE_GLIB=no)

  dnl Give error and exit if we don't have gstreamer
  if test "x$HAVE_GLIB" = "xyes"; then
    PKG_CHECK_MODULES(GST, \
      gstreamer-$GST_MAJORMINOR >= $GST_REQUIRED,
      HAVE_GST=yes,HAVE_GST=no)

    PKG_CHECK_MODULES(GST_INTERFACES, \
      gstreamer-interfaces-$GST_MAJORMINOR >= $GST_REQUIRED,
      HAVE_GST_INTERFACES=yes,HAVE_GST_INTERFACES=no)

    dnl Give error and exit if we don't have gstreamer
    if test "x$HAVE_GST" = "xyes"; then
         PKG_CHECK_MODULES(FARSIGHT2, \
             farsight2-$GST_MAJORMINOR >= $FARSIGHT_REQUIRED,
            HAVE_FARSIGHT=yes,HAVE_FARSIGHT=no)
    fi
  fi

  AC_ARG_ENABLE([libv4l],
        AC_HELP_STRING([--disable-libv4l], [Disable libv4l support]),
        [case "${enableval}" in
            yes) WANT_V4L=yes ;;
            no)  WANT_V4L=no ;;
            *) AC_MSG_ERROR(bad value ${enableval} for --enable-libv4l) ;;
        esac],
        WANT_V4L=test)

  HAVE_LIBV4L=no
  if test "x$WANT_V4L" != "xno"; then
    dnl Use libv4l if we have it
    PKG_CHECK_MODULES(LIBV4L, \
      libv4l2 >= 0.5.0,
      HAVE_LIBV4L=yes,HAVE_LIBV4L=no)
  fi
  AC_SUBST(HAVE_LIBV4L)

  AC_ARG_ENABLE([upnp],
        AC_HELP_STRING([--disable-upnp], [Disable upnp support]),
        [case "${enableval}" in
            yes) WANT_UPNP=yes ;;
            no)  WANT_UPNP=no ;;
            *) AC_MSG_ERROR(bad value ${enableval} for --enable-upnp) ;;
        esac],
        WANT_UPNP=test)
  HAVE_GUPNP=no
  if test "x$WANT_UPNP" != "xno"; then
    PKG_CHECK_MODULES(GUPNP, [ gupnp-igd-1.0 ],
      [ HAVE_GUPNP=yes ],
      [ HAVE_GUPNP=no ])
  fi
  AC_SUBST(HAVE_GUPNP)

fi

AC_SUBST(GST_CFLAGS)
AC_SUBST(GST_LIBS)
AC_SUBST(GST_INTERFACES_CFLAGS)
AC_SUBST(GST_INTERFACES_LIBS)

AC_SUBST(FARSIGHT2_CFLAGS)
AC_SUBST(FARSIGHT2_LIBS)

AC_SUBST(HAVE_FARSIGHT)

dnl ---------------------------------------------------------------------
dnl write out stuff
AC_OUTPUT(Makefile)
dnl ---------------------------------------------------------------------
dnl compile time options summary

cat <<EOF

compile time options summary
============================

    X11          : $FOUND_X11
    Tcl		 : $TCL_VERSION
    TK 		 : $TK_VERSION
    DEBUG        : $DEBUG
    STATIC       : $STATIC
    FARSIGHT     : $HAVE_FARSIGHT
    LIBV4L       : $HAVE_LIBV4L
    GUPNP-IGD    : $HAVE_GUPNP

EOF

if test x"$HAVE_FARSIGHT" = "xno"; then
   echo "*** You do not seem to have gstreamer and farsight2 installed."
   echo "*** You will not be able to build the required component for audio conversations."
   echo "*** Read this for more information : http://amsn-project.net/wiki/Farsight"
fi

exit 0
