# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.57)
#
# Note to self: always bump version number when there are changes
# in setup window or avview_shell
#
AC_INIT([AVview], [0.80.7], [gatos-devel@lists.sf.net])
#
# For now I'll be happier without NEWS and Changelog files.
#
AM_INIT_AUTOMAKE(foreign)
AM_MAINTAINER_MODE

AC_CONFIG_SRCDIR([frequencies.c])
AM_CONFIG_HEADER([config.h])


# Checks for programs.
AC_PROG_CC
AC_GNU_SOURCE
AC_SYS_LARGEFILE
AC_C_BIGENDIAN

# Checks for header files.
AC_PATH_XTRA
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/ioctl.h sys/time.h unistd.h])

AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME

# Checks for library functions.
AC_FUNC_ALLOCA
AC_PROG_GCC_TRADITIONAL
AC_FUNC_SELECT_ARGTYPES
AC_CHECK_FUNCS([gettimeofday memmove memset rint select strcasecmp strdup strerror])

AC_ARG_WITH(ffmpeg, [  --with-ffmpeg=path      Path to compiled ffmpeg source tree])
AC_ARG_ENABLE(alsa, [  --disable-alsa          Do not use ALSA sound library])
AC_ARG_ENABLE(lirc, [  --disable-lirc          Disable LIRC support])

AC_CHECK_LIB([dl], [dlopen])
AC_CHECK_LIB([z], [uncompress])
AC_CHECK_LIB([m], [sin])
AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_LIB([zvbi], [vbi_capture_v4l_new])
AC_CHECK_LIB([asound], [snd_card_get_name])
AC_CHECK_LIB([lirc_client], [lirc_init])

echo "The libraries mp3lame, ogg, vorbis and vorbisenc might be required by ffmpeg"
AC_CHECK_LIB([mp3lame], [lame_encode_buffer])
AC_CHECK_LIB([ogg], [oggpack_write])
AC_CHECK_LIB([vorbis], [vorbis_book_init_encode])
AC_CHECK_LIB([vorbisenc], [vorbis_encode_init])

#
#
# Default values
#
#AC_DEFINE(USE_FFMPEG, 1, [use ffmpeg libraries])
#AC_DEFINE(USE_ALSA, 1, [use alsa libraries])
#
# paths
#
INTERPRETER_PATH=$bindir
SCRIPT_PATH=$datadir/AVview
AC_SUBST(INTERPRETER_PATH)
AC_SUBST(SCRIPT_PATH)

ALSA_PRESENT=no
if ! test "$enable_alsa" = "no" ; then

  if test "$ac_cv_lib_asound_snd_card_get_name" = "yes" ; then
  AC_TRY_LINK([#include <alsa/asoundlib.h> 
	     /* #if (SND_LIB_MAJOR != 0) || (SND_LIB_MINOR < 9)  */
	     #if (SND_LIB_MAJOR != 1) 
	     #error "Avview requires ALSA version 1.0.x" 
	     #endif], ,AC_DEFINE(USE_ALSA,1,[use alsa libraries]) ALSA_PRESENT=yes,)
  fi

  if test "$ALSA_PRESENT" = "no" ; then
  echo "** Avview requires ALSA version 0.9.x"
  fi

else

ALSA_PRESENT=disabled

fi

LIRC_PRESENT=no

if ! test "$enable_lirc" = "no" ; then

  if test "$ac_cv_lib_lirc_client_lirc_init" = "yes" ; then
    AC_DEFINE(USE_LIRC, 1, [use lirc libraries])
    LIRC_PRESENT=yes
  fi

else

  LIRC_PRESENT=disabled

fi

AC_CHECK_HEADER(linux/videodev.h, ,echo "Avview requires video4linux support. You may want to upgrade to Linux 2.4.x."; exit)

AC_CHECK_HEADER(libzvbi.h, , echo "Avview requires libzvbi 0.2.3 or later" ; exit )

use_xinerama=0
AC_CHECK_HEADER(X11/extensions/Xinerama.h, 
	AC_DEFINE(USE_XINERAMA, 1, [Is Xinerama present])
	use_xinerama=1 ,
	AC_DEFINE(USE_XINERAMA, 0, [Is Xinerama present])
	,[
	#include <X11/X.h>
	#include <X11/Xlib.h>
	])

AC_SUBST(USE_XINERAMA)

LDFLAGS="$LDFLAGS $X_LIBS"

AC_CHECK_LIB([X11], [XCreateSimpleWindow],,,[$X_LIBS])
AC_CHECK_LIB([Xext], [XInitExtension],,,[$X_LIBS])
AC_CHECK_LIB([Xv], [XvPutVideo],,,[$X_LIBS -lX11 -lXext])
AC_CHECK_LIB([Xinerama], [XineramaIsActive],,,[$X_LIBS -lX11 -lXext])



A_CFLAGS="$CFLAGS"

# Sets TCL_BIN_DIR
SC2_PATH_TCLCONFIG
# Sets TK_BIN_DIR
SC2_PATH_TKCONFIG
# Sources tclConfig.sh, substs TCL_{BIN,SRC,LIB}_DIR
SC2_LOAD_TCLCONFIG
# Sources tkConfig.sh, substs TK_BIN_DIR
SC2_LOAD_TKCONFIG

AC_SUBST([TCL_LIB_SPEC])
AC_SUBST([TCL_INC_DIR])
AC_SUBST([TK_LIB_SPEC])
AC_SUBST([TK_INC_DIR])
AC_SUBST([TK_XLIBSW])

AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(X_EXTRA_LIBS)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(LIBS)

if ! test "$with_ffmpeg" = "" ; then
AC_DEFINE(USE_FFMPEG, 1, [use ffmpeg libraries])
FFMPEG_LIBS="$with_ffmpeg/libavformat/libavformat.a $with_ffmpeg/libavcodec/libavcodec.a"
FFMPEG_CFLAGS="-I$with_ffmpeg/libavformat -I$with_ffmpeg/libavcodec"
echo -n "checking ffmpeg version..."
CFLAGS="$CFLAGS $FFMPEG_CFLAGS"
#LDFLAGS="$LDFLAGS $FFMPEG_LIBS"
LIBS="$LIBS $FFMPEG_LIBS"
AC_RUN_IFELSE([
	#include <stdio.h>
	#include "avcodec.h"
	#include "avformat.h"
	
	#if LIBAVCODEC_VERSION_INT != LIBAVFORMAT_VERSION_INT
		#error "FFMPEG libraries mismatch"
	#endif
	
	#if LIBAVCODEC_VERSION_INT != 0x00.4.9
		#error "AVview now requires ffmpeg 0.4.9"
	#endif
	
	int main(void)
	{
	if(avcodec_version()!=LIBAVCODEC_VERSION_INT){
		fprintf(stderr,"AVcodec header and library mismatch detected\n");
		exit(-1);
		}
	#if 0 
	/* no check for avformat version yet */
	if(avformat_version()!=LIBAVFORMAT_VERSION_INT){
		fprintf(stderr,"AVcodec header and library mismatch detected\n");
		exit(-1);
		}
	#endif
	exit(0);
	}
	],,[ 
	echo
	echo
	echo "*** AVview requires ffmpeg 0.4.9"
	echo "If you are sure you are using correct version of ffmpeg check config.log for details"
	exit])
echo " ok"
else
FFMPEG_LIBS=
FFMPEG_CFLAGS=
fi

AC_SUBST(FFMPEG_LIBS)
AC_SUBST(FFMPEG_CFLAGS)

AC_HEADER_STDC
AC_CHECK_HEADERS([errno.h fcntl.h stdlib.h string.h sys/ioctl.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

# Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_CHECK_FUNCS([memset strdup strerror])

AC_CONFIG_FILES([Makefile start_avview.no_install])
AC_OUTPUT

if test "$no_x" = "yes" ; then
	AC_MSG_ERROR([can not find X11])
fi

if ! test "$TCL_VERSION $TK_VERSION" = "8.4 8.4" ; then
	AC_MSG_ERROR([AVview requires Tcl/Tk 8.4.x])
fi

echo ""
echo "------------------- Configuration -------------------"
echo ""
echo "  X11 libraries                       $X_LIBS"
echo "  X11 cflags                          $X_CFLAGS"
echo "  TCL location                        $TCL_PREFIX"
echo "  TCL version                         $TCL_VERSION$TCL_PATCH_LEVEL"
echo "  TK location                         $TK_PREFIX"
echo "  TCL version                         $TK_VERSION$TK_PATCH_LEVEL"
echo "  ALSA support                        $ALSA_PRESENT"
if test "$with_ffmpeg" = "" ; then
echo "  Movie recording                     disabled"
echo "         to enable specify --with-ffmpeg=/path/to/compiled/ffmpeg/source"
else
echo "  Movie recording                     enabled"
echo "  Path to compiled ffmeg source       $with_ffmpeg"
echo ""
echo "  Binaries will be installed in       $INTERPRETER_PATH"
echo "  Script files will be installed in   $SCRIPT_PATH"
fi
echo ""

if test "$with_ffmpeg" = "yes" || test "$with_ffmpeg" = "no"; then
echo ""
echo "   **** POSSIBLE MISCONFIGURATION DETECTED ****"
echo ""
echo "The proper way to specify path to ffmpeg tree is by using"
echo "              --with-ffmpeg=/path/to/ffmpeg/tree"
echo ""
echo "while what you used is likely: --with-ffmpeg /path/to/ffmpeg/tree"
echo ""
echo "Unless this tree is really in directory named \"yes\" or \"no\" please"
echo "restart ./configure with the correct parameter syntax"
echo ""
fi

chmod +x start_avview.no_install
