#!/bin/bash
#
# simple configure script for softdevice
#
# $Id: configure,v 1.50 2008/09/21 12:55:57 lucke Exp $
#

TMPDIR1="/tmp"
TMPC="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}.c"
TMPE="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}"
TMPF="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}.makefile"
TMPH="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}.h"
TMPM="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}.mak"

if test -z "$PKG_CONFIG_PATH" ; then
  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
else
  PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
fi

set -u
: ${CFLAGS=""}

system="Linux"
cpu_arch="i386"
use_pkgconfig="yes"
ffmpeg="yes"
ffmpeg_local="no"
libswscale="no"
ffmpeg_use_path="no"
dfb="yes"
libpostproc="no"
libpostproc_const_uint8="yes"
cc="g++"
xv="yes"
with_shm="yes"
xinerama="yes"
vidix="yes"
vidix_path="/usr/local"
ffmpeg_path="/usr/local/include/ffmpeg"
with_fb="yes"
with_subplugins="yes"
with_mmx="yes"
with_mmx2="yes"
with_altivec="no"
cle266="yes"
alsa="yes"
oss="yes"
yaepg="auto"
suspendkey="yes"
cpu_bigendian="no"

function help () {
  echo "Usage: configure [options]"
  echo "available options are:"
  echo "  --disable-vidix"
  echo "  --disable-fb"
  echo "  --disable-dfb"
  echo "  --disable-cle266"
  echo "  --disable-xv"
  echo "  --disable-shm"
  echo "  --disable-subplugins"
  echo "  --disable-mmx"
  echo "  --disable-mmx2"
  echo "  --disable-alsa"
  echo "  --disable-xinerama"
  echo "  --disable-oss"
  echo "  --disable-suspendkey"
  echo "  --disable-yaepg / --enable-yaepg (yaepg patch support)"
  echo "  --with-ffmpeg-path YOUR_FFMPEG_PATH"
  echo "  --with-vidix-path YOUR_VIDIX_PATH"
  echo "  --help"
}

echo "Parameters '$@'" >config.log
echo "PKG_CONFIG_PATH '$PKG_CONFIG_PATH'" >> config.log

while [ $# -gt 0 ]
do
  case $1 in
    --disable-vidix) shift; vidix="no" ;;
    --disable-fb) shift; with_fb="no" ;;
    --disable-dfb) shift; dfb="no" ;;
    --disable-cle266) shift; cle266="no" ;;
    --disable-xv) shift; xv="no" ;;
    --disable-shm) shift; with_shm="no" ;;
    --disable-subplugins) shift; with_subplugins="no" ;;
    --disable-mmx) shift; with_mmx="no";;
    --disable-mmx2) shift; with_mmx2="no";;
    --disable-alsa) shift; alsa="no";;
    --disable-xinerama) shift; xinerama="no";;
    --disable-oss) shift; oss="no";;
    --disable-suspendkey) shift; suspendkey="no";;
    --disable-yaepg) shift; yaepg="no";;
    --enable-yaepg) shift; yaepg="yes";;
    --with-ffmpeg-path) shift;
      ffmpeg_path=$1 ;
      ffmpeg_use_path="yes" ;
      echo "ffmpeg path set to: $ffmpeg_path" ;
      shift ;;
    --with-vidix-path) shift;
      vidix_path=$1 ;
      echo "vidix path set to: $vidix_path" ;
      shift ;;
    --help) help; exit ;;
    *) echo "ERROR: unknown parameter $1"; help; exit ;;
  esac
done

#########################################################
# detect host system and cpu type
#
echo -n "Testing system and cpu type... "
echo "Testing system and cpu type.----------------------------" >> config.log
echo "uname -a: `uname -a`" >> config.log
echo "$cc -dumpmache: `$cc -dumpmachine `" >> config.log

system=`uname -s`
case "$system" in
  Linux) system="Linux";;
  Darwin)
        system="Darwin"
        #with_subplugins="no"
        ;;
  *) system="unknown";;
esac

cpu_arch=`uname -m`
case "$cpu_arch" in
  i[3-6]86*|x86_64)
        cpu_arch="i386"
        ;;
  ppc|"Power Macintosh")
        cpu_arch="PowerPC"
        with_mmx="no"
        with_mmx2="no"
        with_altivec="yes"
        cpu_bigendian="yes"
        ;;
  *)
        echo "'$cpu_arch' is unknown..."
        cpu_arch="unknown"
        with_mmx="no"
        with_mmx2="no"
        ;;
esac

echo "found $system on $cpu_arch cpu."

#########################################################
# start pkg-config test
#
if test "${use_pkgconfig}" = "yes" ; then
  echo -n "Checking for pkg-config... "
  echo  "Checking for pkg-config.-------------------------------" >> config.log

  pkg-config --version >> config.log 2>&1 || use_pkgconfig="no";

  if test "${use_pkgconfig}" = "no" ; then
    echo "Not found."
    echo "Please consider installing pkg-config. Many parts of this configure"
    echo "rely on pkg-config to find the parameters of the needed libraries."
  else
    echo "Found."
  fi
fi

#########################################################
# start ffmpeg test
#
echo -n "Checking for ffmpeg... "
echo  "Checking for ffmpeg.------------------------------------" >> config.log

#########################################################
test_ffmpeg()
{
  ffmpeg="yes";
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
extern "C" {
#include <stdlib.h>
#include <avcodec.h>
#include <avformat.h>
}
int main(void) {
  avcodec_init();
  avcodec_register_all();

 return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags -o $TMPE $TMPC $ffmpeg_libs  >> config.log 2>&1 || ffmpeg="no"
};

#########################################################
test_ffmpeg_local()
{
  ffmpeg="yes";
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
extern "C" {
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
int main(void) {
  avcodec_init();
  avcodec_register_all();

 return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags -o $TMPE $TMPC $ffmpeg_libs  >> config.log 2>&1 || ffmpeg="no"
};

ffmpeg="no" # = not yet configured
if test "${use_pkgconfig}" = "yes" ; then
  if test "${ffmpeg_use_path}" = "no" ; then

echo "try to use pkg-config." >> config.log
ffmpeg_l1="libavformat libavcodec"

pkg-config --libs libpostproc >> config.log 2>&1 && { ffmpeg_l1="$ffmpeg_l1 libpostproc";libpostproc="yes"; }

ffmpeg_libs=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --libs $ffmpeg_l1 2>> config.log` || ffmpeg="no"
ffmpeg_cflags=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --cflags $ffmpeg_l1 2>> config.log`

# test if it works..
test_ffmpeg

  fi
fi

if test "${ffmpeg}" = "no" ; then
  # try the latest ffmpeg change
  test_ffmpeg_local
  if test "${ffmpeg}" = "yes" ; then
    ffmpeg_local="yes"
  fi
fi

if test "${ffmpeg}" = "no" ; then
  echo "pkg-config failed, try the default/parameter path." >> config.log
  ffmpeg="yes"
  ffmpeg_libs="-L${ffmpeg_path}/ -L${ffmpeg_path}/libavcodec/ -L${ffmpeg_path}/libavformat/ -lavformat -lavcodec -lz"
  ffmpeg_cflags="-I${ffmpeg_path}/ -I${ffmpeg_path}/libavcodec/ -I${ffmpeg_path}/libavformat/ "

  test_ffmpeg

  if test "${ffmpeg}" = "no" ; then
    echo "Still failed. Maybe avutils is needed?" >> config.log
    ffmpeg_libs="$ffmpeg_libs -L${ffmpeg_path}/libavutil/ -lavutil"
    ffmpeg_cflags="$ffmpeg_cflags -I${ffmpeg_path}/libavutil/"

    test_ffmpeg
  fi

  if test "${ffmpeg}" = "no" ; then
    echo "No. Sorry giving up." >> config.log
    echo " Not found."
    echo "No usable ffmpeg library found in ${ffmpeg_path}."
    echo "Specify the path to your ffmpeg installation using --with-ffmpeg-path"
    echo "or use a more recent (svn) version of ffmpeg and use/install pkg-config."
    echo "For details check config.log."
    exit 8;
  fi
fi

echo "Ok."

####################################################################################
echo -n "Checking for ffmpeg/libswscale... "
echo  "Checking for ffmpeg/libswscale.------------------------------------" >> config.log

test_swscale()
{
  swscale="yes";
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
#include <stdlib.h>
extern "C" {
#include <avcodec.h>
#include <avformat.h>
#include <swscale.h>
}
int main(void) {
  avcodec_init();
  avcodec_register_all();
  struct SwsContext *img_convert_ctx = sws_getContext(120, 120,
                                PIX_FMT_YUV420P,
                                100, 100,
                                PIX_FMT_YUV420P,
                                SWS_BICUBIC, NULL, NULL, NULL);

 return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags $swscale_cflags -o $TMPE $TMPC $swscale_libs $ffmpeg_libs >> config.log 2>&1 || swscale="no"
};

test_swscale_local()
{
  swscale="yes";
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
#include <stdlib.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(void) {
  avcodec_init();
  avcodec_register_all();
  struct SwsContext *img_convert_ctx = sws_getContext(120, 120,
                                PIX_FMT_YUV420P,
                                100, 100,
                                PIX_FMT_YUV420P,
                                SWS_BICUBIC, NULL, NULL, NULL);

 return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags $swscale_cflags -o $TMPE $TMPC $swscale_libs $ffmpeg_libs >> config.log 2>&1 || swscale="no"
};

swscale_libs=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --libs libswscale 2>> config.log` || libswscale="no"
swscale_cflags=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --cflags libswscale 2>> config.log`

#test if it works
if test "${ffmpeg_local}" = "no" ; then
  test_swscale
else
  test_swscale_local
fi

if test "${swscale}" = "no" ; then
  echo "pkg-config failed, try the default/parameter path." >> config.log
  swscale_libs="-L${ffmpeg_path}/libswscale/ -lswscale -lz"
  swscale_cflags="-I${ffmpeg_path}/libswscale/ "

  test_swscale
fi

if test "${swscale}" = "yes" ; then
echo "Using libswscale."
else
echo "Not Found."
swscale_libs=""
swscale_cflags=""
fi

test_pp_const_uint8()
{
  libpostproc_const_uint8="yes";
  if test $1 = "loc" ; then
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
#include <stdlib.h>
extern "C" {
#include <libpostproc/postprocess.h>
}
EOF
  else
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
#include <stdlib.h>
extern "C" {
#include <postprocess.h>
}
EOF
  fi
cat >> ${TMPC} << EOF
int main(void) {
    uint8_t *data[3];
    int     num = 100;
    pp_postprocess((const uint8_t **) data, &num, data, &num,
                   100, 100, NULL, 0, 0, NULL, 0);
 return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags -o $TMPE $TMPC $ffmpeg_libs >> config.log 2>&1 || libpostproc_const_uint8="no"
};

########################################################
# check for cast to (const char **)
if test "${libpostproc}" = "yes" ; then
  echo -n "Checking for pp_postprocess 1st arg ..."
  echo "Checking for pp_postprocess 1st arg. -----" >> config.log

  if test "${ffmpeg_local}" = "yes" ; then
    test_pp_const_uint8 loc
  else
    test_pp_const_uint8 non_loc
  fi
  if test "${libpostproc_const_uint8}" = "yes" ; then
    echo " is: const uint8_t **"
  else
    echo " is: uint8_t **"
  fi
fi

########################################################
echo -n "Checking ffmpeg for member of AVCodecContext: error_recognition ... "
echo  "Checking ffmpeg for member of AVCodecContext: error_recognition ----" >> config.log

test_error_recognition()
{
  has_error_recognition="yes";
cat > ${TMPC} << EOF
#define __STDC_CONSTANT_MACROS
#include <stdlib.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(void) {
    AVCodecContext *context;

  if (context -> error_recognition)
    return 1;
  return 0;
}
EOF
  $cc $CFLAGS $ffmpeg_cflags $swscale_cflags -o $TMPE $TMPC $swscale_libs $ffmpeg_libs >> config.log 2>&1 || has_error_recognition="no"
  $TMPE
};

########################################################
# check for error_recognition
test_error_recognition
if test "${has_error_recognition}" = "yes" ; then
  echo " 'error_recognition' found"
else
  echo " 'error_recognition' NOT found"
fi


########################################################
# check for ALSA
echo -n "Checking for ALSA sound... "
echo "Checking for ALSA sound.------------------" >> config.log

if test "${alsa}" = "yes" ; then
cat > ${TMPC} << EOF
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>
int main(void) {
  snd_pcm_t         *handle;
  char device[]="hw:0,1";
  snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0);
  return 0;
}
EOF
$cc $CFLAGS -lasound -o $TMPE $TMPC >> config.log 2>&1 || alsa="no"
fi

if test "${alsa}" = "yes" ; then
echo "Enabled audio-alsa."
else
echo "Not Found."
fi

########################################################
# check for ALSA
echo -n "Checking for OSS sound... "
echo "Checking for OSS sound.------------------" >> config.log

if test "${oss}" = "yes" ; then
cat > ${TMPC} << EOF
#include <unistd.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#define DSP_DEVICE "/dev/dsp"

int main(void) {
        int fdDSP;
        if (-1 == (fdDSP = open(DSP_DEVICE, O_WRONLY))) {
                exit(-1);
        }
        ioctl(fdDSP, SNDCTL_DSP_RESET, 0);
        close(fdDSP);
        return 0;
}
EOF
$cc $CFLAGS -o $TMPE $TMPC >> config.log 2>&1 || oss="no"
fi

if test "${oss}" = "yes" ; then
echo "Enabled audio-oss."
else
echo "Not Found."
fi

########################################################
# check for linux fb
echo -n "Checking for linux frambuffer... "
echo "Checking for linux framebuffer.------------------" >> config.log

if test "${with_fb}" = "yes" ; then
cat > ${TMPC} << EOF
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
        int fbdev;
        struct fb_fix_screeninfo fb_finfo;
        struct fb_var_screeninfo fb_orig_vinfo;
        struct fb_var_screeninfo fb_vinfo;
        if ((fbdev = open("/dev/fb0", O_RDWR)) == -1) {
                printf("[video-fb] cant open framebuffer %s\n", "/dev/fb0");
                exit(1);
        }

        if (ioctl(fbdev, FBIOGET_VSCREENINFO, &fb_vinfo)) {
                printf("[video-fb] Can't get VSCREENINFO\n");
                exit(1);
        }
        if (ioctl(fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) {
                printf("[video-fb] Can't get FSCREENINFO\n");
                exit(1);
        }

        return 0;
}
EOF
$cc $CFLAGS  -o $TMPE $TMPC >> config.log 2>&1 || with_fb="no"
fi

if test "${with_fb}" = "yes" ; then
echo "Enabled video-fb."
else
echo "Not Found."
fi

########################################################
# check for yaepg patch
if test "${yaepg}" = "auto" ; then
echo -n "Checking for yaepg patch... "
echo "Checking for yaepg patch.------------------" >> config.log

yaepg="no";
VDR_DIR=../../..

cat > ${TMPF} << EOF
DEFINES += -D_GNU_SOURCE
CFLAGS = -O2 -g -Wall
-include ../../../Make.config
${TMPE}: ${TMPC}
	$cc \$(CFLAGS)  \$(DEFINES) -I${VDR_DIR} -c -o $TMPE $TMPC >> config.log 2>&1
EOF

cat > ${TMPC} << EOF
#include <osd.h>
int test_func(cOsd *osd) {
	return (osd -> vidWin. bpp) ? 1 : 0;
}
EOF

rm ${TMPE}
make -f ${TMPF} >> config.log 2>&1 && yaepg="yes"

if test "${yaepg}" = "yes" ; then
echo "Found yaepg patch."
else
echo "Not Found."
fi

fi

#########################################################
# start of DirectFB specific tests
#
echo -n "Checking for DirectFB and DFB++... "
echo "Checking for DirectFB and DFB++.------------------" >> config.log
if test "${dfb}" = "yes" ; then

dfb_cflags=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --cflags directfb dfb++ 2>> config.log`|| dfb="no"
#dfb_cflags=`pkg-config --cflags directfb dfb++ 2>> config.log` || dfb="no"

if test "${dfb}" = "yes" ; then

dfb_libs=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --libs directfb dfb++ 2>> config.log`
dfb_opts="${dfb_cflags} ${dfb_libs}"
dfb_device_desc="yes"
dfb_sourcerectangle="yes"
dfb_dscaps_double="yes"
dfb_dief_repeat="yes"

cat > ${TMPC} << EOF
#include <directfb.h>
int main(void) {
    DFBGraphicsDeviceDescription caps;
  return 0;
}
EOF
$cc $CFLAGS $dfb_opts -o $TMPE $TMPC >> config.log 2>&1 || dfb_device_desc="no"

cat > ${TMPC} << EOF
#include <stdio.h>
#include <dfb++.h>
#include <directfb.h>
int main(void) {
    IDirectFBDisplayLayer *videoLayer;
  videoLayer->SetSourceRectangle (0, 0, 128, 128);
  return 0;
}
EOF
$cc $CFLAGS $dfb_opts -o $TMPE $TMPC >> config.log 2>&1 || dfb_sourcerectangle="no"

cat > ${TMPC} << EOF
#include <stdio.h>
#include <dfb++.h>
#include <directfb.h>
int main(void) {

    DFBDisplayLayerConfig       dlc;

  dlc.flags = (DFBDisplayLayerConfigFlags)
                  ((int) dlc.flags | DLCONF_SURFACE_CAPS);
  dlc.surface_caps  = DSCAPS_DOUBLE;

  return 0;
}
EOF
$cc $CFLAGS $dfb_opts -o $TMPE $TMPC >> config.log 2>&1 || dfb_dscaps_double="no"

cat > ${TMPC} << EOF
#include <stdio.h>
#include <dfb++.h>
#include <directfb.h>
int main(void) {
    DFBInputEvent event;
  event.flags = DIEF_REPEAT;
  return 0;
}
EOF
$cc $CFLAGS $dfb_opts -o $TMPE $TMPC >> config.log 2>&1 || dfb_dief_repeat="no"

cat > ${TMPC} << EOF
#include <stdio.h>
#include <dfb++.h>
#include <directfb.h>
int main(void) {
  IDirectFBSurface  *surf;
  int               i;
  surf->GetFramebufferOffset(&i);
  return 0;
}
EOF
$cc $CFLAGS $dfb_opts -o $TMPE $TMPC >> config.log 2>&1 || cle266="no"

  fi
fi

if test "${dfb}" = "yes" ; then
echo "Enabled video-dfb."
else
echo "Not Found."
fi

# end of DirectFB specific tests

#########################################################
# vidix: check if we could compile with specified options
#
echo -n "Checking for vidix... "
echo "Checking for vidix.------------------------" >> config.log

if test "${vidix}" = "yes" ; then
  vidix_cflags="-I${vidix_path}/include/vidix"
  vidix_libs="-L${vidix_path}/lib -lvidix"
  cat > ${TMPC} << EOF
#include <stdio.h>
#include "vidixlib.h"
int main(void) {
    vidix_capability_t vidix_cap;
  return 0;
}
EOF
$cc $CFLAGS $vidix_cflags $vidix_libs -o $TMPE $TMPC >> config.log 2>&1 || vidix="no"
fi

if test "${vidix}" = "yes" ; then
echo "Enabled video-out vidix."
else
echo "Not found."
fi

#########################################################
# libcle266mpegdec: check if required version is present
#
if test "${dfb}" = "yes" ; then
  echo -n "Checking for libcle266mpegdec ... "
  pkg-config --atleast-version=0.2 libcle266mpegdec 2>>config.log || cle266="no"
  if test "${cle266}" = "yes" ; then
    cle266_cflags=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --cflags libcle266mpegdec`
    cle266_libs=`PKG_CONFIG_PATH=$PKG_CONFIG_PATH pkg-config --libs libcle266mpegdec`
    cle266_opts="${cle266_cflags} ${cle266_libs}"
    echo "Enabled cle266 hardware decoding."
  else
    echo "Not found, not supported by DirectFB or disabled by argument."
  fi
else
  cle266="no"
fi

#########################################################
# X11: check if X11 is present
#
echo -n "Checking for Xv... "
echo  "Checking for Xv.-----------------------------" >> config.log

test_xv()
{
  xv="yes"
  cat > ${TMPC} << EOF
#include <stdio.h>
#include <X11/Xlib.h>
int main(void) {
    Display       *dpy;
  dpy = XOpenDisplay(NULL);
  return 0;
}
EOF
  $cc $CFLAGS $xv_libs -o $TMPE $TMPC >> config.log 2>&1 || xv="no"
};

if test "${xv}" = "yes" ; then
  xv_libs="-L/usr/X11R6/lib -lXext -lX11 -lm -lXv"
  test_xv

  if test "${xv}" = "no" ; then
    # do we need libXi at all?
    echo "Xv test failed. Maybe -lXi is needed?" >> config.log
    xv_libs+=" -lXi"
    test_xv
  fi
fi

if test "${xv}" = "yes" ; then
echo "Enabled video-xv."
else
echo "Not found."
fi

#########################################################
# X11: check if we can use Xinerama extension
#
echo -n "Checking for Xinerama... "
echo "Checking for Xinerama.-----------------------------" >> config.log

if test "${xv}" = "yes" -a "${xinerama}" = "yes" ; then
  xinerama_libs="-lXinerama"
  cat > ${TMPC} << EOF
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
int main(void) {
    Display             *dpy;
    XineramaScreenInfo  *screens;
    int                 numScreens;
  dpy = XOpenDisplay(NULL);
  screens = XineramaQueryScreens (dpy, &numScreens);
  return 0;
}
EOF
$cc $CFLAGS $xv_libs $xinerama_libs -o $TMPE $TMPC >> config.log 2>&1 || xinerama="no"
fi

if test "${xv}" = "yes" -a "${xinerama}" = "yes" ; then
echo "Enabled Xinerama."
else
echo "Not found."
fi

###########

echo "Creating temporary config.h and config.mak"
echo "/* Generated by configure - do not modify */" > $TMPH
echo "# Generated by configure - do not modify" > $TMPM
echo "WITH_CONFIGURE = 1" >> $TMPM


###############################################################################
# generating some global defines
#
if test "${with_subplugins}" = "yes" ; then
  echo "USE_SUBPLUGINS = 1" >> $TMPM
  echo "#define USE_SUBPLUGINS                  1" >> $TMPH
fi

if test "${with_mmx}" = "yes" ; then
  echo "#define USE_MMX                         1" >> $TMPH

  if test "${with_mmx2}" = "yes" ; then
    echo "#define USE_MMX2                        1" >> $TMPH
  fi
fi

if test "${with_altivec}" = "yes" ; then
  echo "#define USE_ALTIVEC                     1" >> $TMPH
  echo "ADD_CXXFLAGS += -faltivec" >> $TMPM
fi

if test "${suspendkey}" = "yes" ; then
  echo "#define SUSPEND_BY_KEY                  1" >> $TMPH
fi

if test "${cpu_bigendian}" = "yes" ; then
  echo "#define CPU_BIGENDIAN                   1" >> $TMPH
fi

if test "${yaepg}" = "yes" ; then
  echo "#define HAVE_YAEPGPATCH                 1" >> $TMPH
fi

if test "${system}" = "Linux"; then
  echo "#define LINUX_RTC                       1" >> $TMPH
fi

if test "${ffmpeg_local}" = "yes"; then
  echo "#define HAVE_FFMPEG_LOCAL               1" >> $TMPH
else
  echo "#define HAVE_FFMPEG_LOCAL               0" >> $TMPH
fi

###############################################################################
# Darwin defines
#
if test "${system}" = "Darwin"; then
  echo "SHARED_FLAG = -dynamiclib -Wl,-single_module,-undefined,dynamic_lookup " >> $TMPM
  echo "QUARTZ_SUPPORT = 1" >> $TMPM
  echo "#define QUARTZ_SUPPORT                  1" >> $TMPH
  echo "MACOSAO_SUPPORT = 1" >> $TMPM
  echo "#define MACOSAO_SUPPORT                 1" >> $TMPH
  echo "MACVDRCLIENT_SUPPORT = 1" >> $TMPM
fi

###############################################################################
# alsa defines
#
if test "${alsa}" = "yes" ; then
  echo "ALSA_SUPPORT = 1" >> $TMPM
  echo "#define ALSA_SUPPORT                    1" >> $TMPH
fi

###############################################################################
# oss defines
#
if test "${oss}" = "yes" ; then
  echo "OSS_SUPPORT = 1" >> $TMPM
  echo "#define OSS_SUPPORT                     1" >> $TMPH
fi

###############################################################################
# generating DirectFB specific defines
#
if test "${dfb}" = "yes" ; then
  echo "DFB_SUPPORT=1" >> $TMPM
  echo "#define DFB_SUPPORT                     1" >> $TMPH
  echo "DFB_LIBS = ${dfb_libs}" >> $TMPM
  echo "DFB_CFLAGS = ${dfb_cflags}" >> $TMPM

  if test "${dfb_sourcerectangle}" = "yes" ; then
    echo "#define HAVE_SetSourceRectangle         1" >> $TMPH
  else
    echo "#define HAVE_SetSourceRectangle         0" >> $TMPH
  fi

  if test "${dfb_device_desc}" = "yes" ; then
    echo "#define HAVE_GraphicsDeviceDescription  1" >> $TMPH
  else
    echo "#define HAVE_GraphicsDeviceDescription  0" >> $TMPH
  fi

  if test "${dfb_dscaps_double}" = "yes" ; then
    echo "#define HAVE_DSCAPS_DOUBLE              1" >> $TMPH
  else
    echo "#define HAVE_DSCAPS_DOUBLE              0" >> $TMPH
  fi

  if test "${dfb_dief_repeat}" = "yes" ; then
    echo "#define HAVE_DIEF_REPEAT                1" >> $TMPH
  else
    echo "#define HAVE_DIEF_REPEAT                0" >> $TMPH
  fi

fi

###############################################################################
# generating DirectFB specific defines
#
if test "${cle266}" = "yes" ; then
  echo "CLE266_LIBS = ${cle266_libs}" >> $TMPM
  echo "CLE266_CFLAGS = -DHAVE_CLE266_MPEG_DECODER ${cle266_cflags}" >> $TMPM
fi

###############################################################################
# generating vidix specific defines
#
if test "${vidix}" = "yes" ; then
  echo "VIDIX_SUPPORT = 1" >> $TMPM
  echo "VIDIX_DIR     = ${vidix_path}" >> $TMPM
  echo "VIDIX_LIBS    = ${vidix_libs}" >> $TMPM
  echo "VIDIX_CFLAGS  = ${vidix_cflags}" >> $TMPM
  echo "#define VIDIX_SUPPORT 1" >> $TMPH
  echo "#define VIDIX_DIR     \"${vidix_path}/lib/vidix/\"" >> $TMPH
fi

###############################################################################
# generating X11 specific defines
#
if test "${xv}" = "yes" ; then
  echo "XV_SUPPORT       = 1" >> $TMPM
  if test "${with_shm}" = "yes" ; then
    echo "SHMCLIENT_SUPPORT = 1" >> $TMPM
  fi
  if test "${xinerama}" = "yes" ; then
    echo "XINERAMA_SUPPORT = 1" >> $TMPM
    echo "#define XINERAMA_SUPPORT       1" >> $TMPH
    xv_libs="${xv_libs} ${xinerama_libs}"
  fi
  echo "LIBXDPMS_SUPPORT = 1" >> $TMPM
  echo "XV_LIBS          = ${xv_libs}"  >> $TMPM
  echo "#define XV_SUPPORT       1" >> $TMPH
  echo "#define LIBXDPMS_SUPPORT 1" >> $TMPH
fi

###############################################################################
# generate defines for shared memory server
#
if test "${with_shm}" = "yes" ; then
  echo "SHM_SUPPORT = 1" >> $TMPM
fi

###############################################################################
# generating framebuffer specific defines
#
if test "${with_fb}" = "yes" ; then
  echo "FB_SUPPORT = 1" >> $TMPM
  echo "#define FB_SUPPORT  1" >> $TMPH
fi

###############################################################################
# generating ffmpeg and libswscale specific defines
#
if test "${libpostproc}" = "yes" ; then
  echo "#define PP_LIBAVCODEC" >> $TMPH
  if test "${libpostproc_const_uint8}" = "yes" ; then
    echo "#define PP_WITH_CONST_UINT8             1" >> $TMPH
  else
    echo "#define PP_WITH_CONST_UINT8             0" >> $TMPH
  fi
else
  echo "#undef PP_LIBAVCODEC" >> $TMPH
fi

if test "${swscale}" = "yes"; then
  echo "#define USE_SWSCALE" >> $TMPH
fi

if test "${has_error_recognition}" = "yes" ; then
  echo "#define HAS_ERROR_RECOGNITION           1" >> $TMPH
else
  echo "#define HAS_ERROR_RECOGNITION           0" >> $TMPH
fi

echo "FFMPEGLIBS = ${swscale_libs} ${ffmpeg_libs}" >> $TMPM
echo "FFMPEGCFLAGS = ${swscale_cflags} ${ffmpeg_cflags}" >> $TMPM

###############################################################################
# checking for differences to previous results
#
clean_dep_file="no"

diff $TMPH config.h >/dev/null 2>&1
if test $? -ne 0 ; then
  mv -f $TMPH config.h
  clean_dep_file="yes"
  echo "updated config.h"
else
  echo "config.h is unchanged"
fi

diff $TMPM config.mak >/dev/null 2>&1
if test $? -ne 0 ; then
  mv -f $TMPM config.mak
  clean_dep_file="yes"
  echo "updated config.mak"
else
  echo "config.mak is unchanged"
fi

if test "${clean_dep_file}" = "yes" ; then
  rm -f .dependencies
fi

echo "Configure is finished. Please check config.log in case of problems."

rm -f $TMPC $TMPE $TMPH $TMPM
