#!/bin/bash
#
# simple configure script for softdevice
#
# $Id: configure,v 1.8 2005/10/07 13:37:53 lucke Exp $
#

TMPDIR1="/tmp"
TMPC="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}.c"
TMPE="${TMPDIR1}/softdevice-conf-${RANDOM}-$$-${RANDOM}"
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=""}

dfb="yes"
libpostproc="no"
cc="g++"
xv="yes"
vidix="yes"
vidix_path="/usr/local"
with_fb="yes"
fb_dev_name="/dev/fb0"
with_subplugins="yes"
with_mmx="yes"
with_mmx2="yes"

function help () {
  echo "Usage: configure [options]"
  echo "available options are:"
  echo "  --disable-vidix"
  echo "  --disable-fb"
  echo "  --disable-dfb"
  echo "  --disable-xv"
  echo "  --disable-subplugins"
  echo "  --with-vidix-path YOUR_VIDIX_PATH"
  echo "  --help"
}

while [ $# -gt 0 ]
do
  case $1 in
    --disable-vidix) shift; vidix="no" ;;
    --disable-fb) shift; with_fb="no" ;;
    --disable-dfb) shift; dfb="no" ;;
    --disable-xv) shift; xv="no" ;;
    --disable-subplugins) shift; with_subplugins="no" ;;
    --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

#########################################################
# start ffmpeg test
#
ffmpeg_l1="libavcodec libavformat"

pkg-config --libs libpostproc >/dev/null 2>&1 && { ffmpeg_l1="$ffmpeg_l1 libpostproc";libpostproc="yes"; }

ffmpeg_libs=`pkg-config --libs $ffmpeg_l1`
ffmpeg_cflags=`pkg-config --cflags $ffmpeg_l1`

#########################################################
# start of DirectFB specific tests
#
if test "${dfb}" = "yes" ; then

dfb_cflags=`pkg-config --cflags directfb dfb++` || dfb="no"

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

dfb_libs=`pkg-config --libs directfb dfb++`
dfb_opts="${dfb_cflags} ${dfb_libs}"
dfb_device_desc="yes"
dfb_sourcelocation="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 > /dev/null 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 > /dev/null 2>&1 || dfb_sourcelocation="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 > /dev/null 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 > /dev/null 2>&1 || dfb_dief_repeat="no"

  fi
fi
# end of DirectFB specific tests

#########################################################
# vidix: check if we could compile with specified options
#
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 > /dev/null 2>&1 || vidix="no"
fi

#########################################################
# X11: check if X11 is present
#
if test "${xv}" = "yes" ; then
  xv_libs="-L/usr/X11R6/lib -lXi -lXext -lX11 -lm -lXv"
  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 > /dev/null 2>&1 || xv="no"
fi

#########################################################
broken_gcc_cpp="no"
cat > ${TMPC} << EOF

#include <stdio.h>
#define BLOCK_SIZE 4096

#ifdef __x86_64__
# define  REG_a "rax"
#else
# define  REG_a "eax"
#endif

#define CONFUSION_FACTOR 0

int main(void) {
    void    *to, *from;
    size_t  i;

  asm volatile(
    "xor %%"REG_a", %%"REG_a"	\n\t"
    "xor %%"REG_a", %%"REG_a"	\n\t"
    "add %3, %0		\n\t"
    "add %3, %1		\n\t"
    "sub %4, %2		\n\t"
    "cmp %4, %2		\n\t"
      : "+r" (from),
        "+r" (to),
        "+r" (i)
      : "r" ((long)BLOCK_SIZE),
        "i" (BLOCK_SIZE/64),
        "i" ((long)CONFUSION_FACTOR)
      : "%"REG_a,
        "%ebx"
    );
  return 0;
}
EOF
$cc $CFLAGS -o $TMPE $TMPC > /dev/null 2>&1 || broken_gcc_cpp="yes"
###########

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
fi

if test "${with_mmx2}" = "yes" ; then
  echo "#define USE_MMX2 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_sourcelocation}" = "yes" ; then
    echo "#define HAVE_SetSourceLocation          1" >> $TMPH
  else
    echo "#define HAVE_SetSourceLocation          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 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
  echo "#define FBDEV         \"${fb_dev_name}\"" >> $TMPH
fi

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

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

###############################################################################
# generating utils.c specific specific defines
#
if test "${broken_gcc_cpp}" = "yes" ; then
  echo "#define HAVE_BROKEN_GCC_CPP  1" >> $TMPH
else
  echo "#define HAVE_BROKEN_GCC_CPP  0" >> $TMPH
fi

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

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

###############################################################################
# checking for differences to previous results
#
diff $TMPH config.h >/dev/null 2>&1
if test $? -ne 0 ; then
  mv -f $TMPH config.h
  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
  echo "updated config.mak"
else
  echo "config.mak is unchanged"
fi

rm -f $TMPC $TMPE $TMPH $TMPM
