#! /bin/sh

TMPLOG="configure.log"
rm -f "$TMPLOG"

cc_check() {
  echo >> "$TMPLOG"
  cat "$TMPC" >> "$TMPLOG"
  echo >> "$TMPLOG"
  echo "$_cc $_inc_extra $_ld_static $_ld_extra $TMPC -o $TMPO $@" >> "$TMPLOG"
  rm -f "$TMPO"
  ( "$_cc" $_inc_extra $_ld_static $_ld_extra "$TMPC" -o "$TMPO" "$@" ) >> "$TMPLOG" 2>&1
  TMP="$?"
  echo >> "$TMPLOG"
  echo "ldd $TMPO" >> "$TMPLOG"
  ( ldd "$TMPO" ) >> "$TMPLOG" 2>&1
  echo >> "$TMPLOG"
  return "$TMP"
}

die () {
  echo
  echo "Error: $@" >&2
  echo >&2
  exit 1
}

# Use this before starting a check
echocheck() {
  echo "============ Checking for $@ ============" >> "$TMPLOG"
  echo ${_echo_n} "Checking for $@ ... ${_echo_c}"
}

# Use this to echo the results of a check
echores() {
  echo "Result is: $@" >> "$TMPLOG"
  echo "##########################################" >> "$TMPLOG"
  echo "" >> "$TMPLOG"
  echo "$@"
}

for I in "$TMPDIR" "$TEMPDIR" "/tmp" ; do
  test "$I" && break
done

for parm in "$@" ; do
  if test "$parm" = "--help" || test "$parm" = "-help" || test "$parm" = "-h" ; then
    cat << EOF

Usage: $0 [OPTIONS]...

Configuration:
  -h, --help             display this help and exit

Installation directories:
  --prefix=DIR           use this prefix for installing mms [/usr/local]

Options:
  --disable-audio	 Disable all music support
  --disable-audio-hd	 Disable music playback from harddrive
  --disable-radio	 Disable internet radio playback
  --enable-bttv-radio	 Enable radio tuner hardware playback (/dev/radio) 
			 note: radio support must be enabled
  --disable-fancy-audio	 Disable fancy audio features like id3 tags,
			 audio covers etc. 
  --disable-xidle	 Disable Xidle for fancy audio
  --enable-gst-audio	 Use Gstreamer as audio player instead of Alsaplayer
  --enable-xine-audio	 Use Xine as audio player instead of Alsaplayer

  --disable-movie	 Disable all movie support (mplayer)
  --disable-movie-hd	 Disable movie playback from harddrive (mplayer)
  --disable-fancy-movie  Disable fancy movie features like downloading
  			 movie information from imdb. 

  --disable-picture	 Disable all picture support
  --disable-picture-hd	 Disable picture viewing from harddrive

  --disable-epg		 Disable Electronic Program Guide
  --enable-picture-epg	 Enable pictures of channels instead of names 
  			 for Electronic Program Guide

  --enable-game	         Enable game emulator support
  --disable-game-hd	 Disable game emulation from harddrive
  --disable-fancy-game   Disable fancy game features such as covers

  --enable-tv            Enable simple TV entry in startmenu

  --enable-lirc	         Enable lirc input
  --enable-evdev         Enable evdev input

  --disable-sdl		 Disable sdl output
  --enable-dxr3	         Enable dxr3 output
  --enable-dvb	         Enable dvb output
  --enable-vgagl	 Enable vga gl output
  --enable-mpeg	         Enable writing the output to a mpeg file on disc
  --enable-fbdev	 Enable framebuffer output (deprecated, use SDL)

  --use-internal-ffmpeg  Uses internal ffmpeg instead of external ffmpeg

  --disable-cd		 Disable audio/movie/pictures features from cd/dvd

  --enable-eject-tray	 Enables open/close button within the startmenu

  --disable-nls		 Disable gettext

  --enable-benchmark	 Enable benchmarking, which can be used to
			 search for bottlenecks
  --enable-debug	 Compile mms with debugging symbols (for gdb)
			 and debugging messages
  --disable-optimization Built MMS for i386 instead of specific architecture

EOF
    exit 0
  fi
done # for parm in ...

cc=cc

# Checking CC version...
for _cc in "$_cc" gcc gcc-3.4 gcc-3.3 gcc-3.2 gcc-3.1 gcc3 gcc-3.0 cc ; do
 cc_name=`( $_cc -v ) 2>&1 | tail -n 1 | cut -d ' ' -f 1`
 cc_version=`( $_cc -dumpversion ) 2>&1 | cut -f1,2 -d.`
 if test "$?" -gt 0; then
   cc_version="not found"
 fi
 case $cc_version in
   '')
     cc_version="v. ?.??, bad"
     cc_verc_fail=yes
     ;;
   2.95|2.95.[2-9]|2.95.[2-9][-.][0-9]|3.[0-9]|3.[0-9].[0-9]|4.[0-1])
     cc_version="$cc_version"
     cc_verc_fail=no
     ;;
   'not found')
     cc_verc_fail=yes
     ;;
   *)
     cc_version="$cc_version, bad"
     cc_verc_fail=yes
     ;;
 esac
 (test "$cc_verc_fail" = "no") && break
done 
 if test "$cc_verc_fail" = yes ; then
   cat <<EOF

*** Please downgrade/upgrade C compiler to version gcc-2.95.x or gcc-3.x! ***

Gcc 2.96 is well known for being broken. Please use a supported compiler!

EOF
die "Bad gcc version"
  fi

CC=$_cc

if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)'
   echo ' yes '
   echo '#endif'; } | ${CC} -E - | grep yes > /dev/null; then
 _gcc_version_major="3"
else
 _gcc_version_major="2"
fi

if { echo '#if __GNUC__ > 2 && (__GNUC_MINOR__ >= 4)'
   echo ' yes '
   echo '#endif'; } | ${CC} -E - | grep yes > /dev/null; then
 _gcc_version_minor="4"
elif { echo '#if __GNUC__ > 2 && (__GNUC_MINOR__ >= 3)'
   echo ' yes '
   echo '#endif'; } | ${CC} -E - | grep yes > /dev/null; then
 _gcc_version_minor="3"
else
 _gcc_version_minor="2"
fi

# cpu detection also from mplayer

cpu_detect() {
  _cpuinfo="cat /proc/cpuinfo"

  pname=`$_cpuinfo | grep 'model name' | cut -d ':' -f 2 | head -n 1`
  pvendor=`$_cpuinfo | grep 'vendor_id' | cut -d ':' -f 2  | cut -d ' ' -f 2 | head -n 1`
  pfamily=`$_cpuinfo | grep 'cpu family' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -n 1`
  pmodel=`$_cpuinfo | grep -v 'model name' | grep 'model' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -n 1`
  pstepping=`$_cpuinfo | grep 'stepping' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -n 1`

  pparam=`$_cpuinfo | grep 'features' | cut -d ':' -f 2 | head -n 1`
  if test -z "$pparam" ; then
    pparam=`$_cpuinfo | grep 'flags' | cut -d ':' -f 2 | head -n 1`
  fi

  _mmx=no
  _3dnow=no
  _3dnowex=no
  _mmx2=no
  _sse=no
  _sse2=no
  _mtrr=no

  for i in $pparam ; do
    case "$i" in
    3dnow)        _3dnow=yes               ;;
    3dnowext)     _3dnow=yes  _3dnowex=yes ;;
    mmx)          _mmx=yes                 ;;
    mmxext)       _mmx2=yes                ;;
    mtrr|k6_mtrr|cyrix_arr)   _mtrr=yes    ;;
    xmm|sse|kni)  _sse=yes    _mmx2=yes    ;;
    sse2)         _sse2=yes                ;;
    esac
  done

  echocheck "CPU vendor"
  echores "$pvendor ($pfamily:$pmodel:$pstepping)"

  echocheck "CPU type"
  echores "$pname"

  case "$pvendor" in
  AuthenticAMD)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    5) iproc=586       # LGB: models are: K5/SSA5 K5 K5 K5 ? ? K6 K6 K6-2 K6-3
	# K6 model 13 are the K6-2+ and K6-III+, only differing in cache size.
	if test "$_gcc_version_major" = 2; then
		proc=i586
		iproc=586
	elif test "$pmodel" -eq 9 -o "$pmodel" -eq 13; then
		proc=k6-3
	elif test "$pmodel" -ge 8; then
		proc=k6-2
	elif test "$pmodel" -ge 6; then
		proc=k6
	else
		proc=i586
		iproc=586
	fi
	;;
    6) iproc=686
	if test "$_gcc_version_major" = 2; then
		proc=i686
		iproc=686
	elif test "$pmodel" -ge 7; then
		proc=athlon-4
	elif test "$pmodel" -ge 6; then
		# only Athlon XP supports ssem MP, Duron etc not
		# but most of them are CPUID 666, so check if sse detected
		# btw. there is also athlon-mp opt, but we need extended
		# CPUID to detect if CPU is SMP capable -> athlon-mp ::atmos
		if test "$_sse" = yes && test "$pstepping" -ge 2; then
			proc=athlon-xp
		else
			proc=athlon-4
		fi
	elif test "$pmodel" -ge 4; then
		proc=athlon-tbird
	else
		proc=athlon # TODO: should the Duron Spitfire be considered a Thunderbird instead?
	fi
	;;
    
    15) proc=k8 iproc=686 ;; # amd86 support
    *) proc=athlon-xp iproc=686 ;;
    esac
    ;;
  GenuineIntel)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    5) iproc=586
	if test "$_gcc_version_major" = 2; then
		proc=i586
	elif test "$pmodel" -eq 4 || test "$pmodel" -eq 8; then
		proc=pentium-mmx # 4 is desktop, 8 is mobile
	else
		proc=i586
	fi
	;;
    6) iproc=686
	if test "$_gcc_version_major" = 2; then
		proc=i686
	elif test "$pmodel" -eq 9 -o "$pmodel" -eq 13; then
                proc=pentium-m
	elif test "$pmodel" -eq 15; then
		proc=nocona
	elif test "$pmodel" -ge 7; then
		proc=pentium3
	elif test "$pmodel" -ge 3; then
		proc=pentium2
	else
		proc=i686
	fi
	;;
    15) proc=pentium4 iproc=686 ;;
    *) proc=pentium4 iproc=686 ;;
    esac
    ;;
  unknown)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    *) proc=i586 iproc=586 ;;
    esac
    ;;
  *)
    proc=i586 iproc=586 ;;
  esac

  _optimizing="$proc"
}

##########################################
# iconv probe
# 
# Taken from mplayers configure script.
cat > test.c << EOF
#include <stdio.h>
#include <unistd.h>
#include <iconv.h>
#define INBUFSIZE 1024
#define OUTBUFSIZE 4096

char inbuffer[INBUFSIZE];
char outbuffer[OUTBUFSIZE];

int main(void) {
  size_t numread;
  iconv_t icdsc;
  char *tocode="UTF-8";
  char *fromcode="cp1250";
  if ((icdsc = iconv_open (tocode, fromcode)) != (iconv_t)(-1)) {
    while ((numread = read (0, inbuffer, INBUFSIZE))) {
      char *iptr=inbuffer;
      char *optr=outbuffer;
      size_t inleft=numread;
      size_t outleft=OUTBUFSIZE;
      if (iconv(icdsc, (char **)&iptr, &inleft, &optr, &outleft)
          != (size_t)(-1)) {
        write (1, outbuffer, OUTBUFSIZE - outleft);
      }
    }
    if (iconv_close(icdsc) == -1)
      ;
  }
}
EOF
if gcc -o test.o test.c ; then
  iconv='#define use_iconv 1'
  rm test.o
else
  iconv='#undef use_iconv'
fi
rm test.c

##########################################
# imlib probe
# 
# Taken from ffmpeg's configure script.

cat > test.c << EOF
#define X_DISPLAY_MISSING
#include <Imlib2.h>
int main( void ) { return (int) imlib_load_font("foo"); }
EOF

imlib2=no
if gcc -o test.o test.c -lImlib2 -lm > /dev/null 2>&1 ; then
imlib2=yes
rm test.o
fi
rm test.c

if test $imlib2 = "no" ; then
die "Please install imlib2!"
fi

##########################################
# ncurses probe
#

cat > test.c << EOF
#include <termios.h>
int main( void ) { struct termios orig_tty; return 0; }
EOF

ncurses=no
if gcc -o test.o test.c -lncurses > /dev/null 2>&1 ; then
ncurses=yes
rm test.o
fi
rm test.c

if test $ncurses = "no" ; then
die "Please install ncurses!"
fi

##########################################
# ffmpeg probe
#

ffmpeg_external=no

ffmpeg_check() {

if test $_external_ffmpeg = "no"; then
echo "Skipping external ffmpeg check, using internal"
echo ""
return
fi

echo "Checking for external ffmpeg"

cat > test.c << EOF
#include <ffmpeg/avcodec.h>
int main(void) { avcodec_find_encoder_by_name(""); return 0; }
EOF

if pkg-config --exists libavcodec && gcc -o test.o test.c `pkg-config --libs libavcodec` > /dev/null 2>&1 ; then
rm test.o
ffmpeg_external=yes
fi
rm test.c

if test $ffmpeg_external = "yes"; then
echo "- test ok"
echo ""
else
echo "- test failed, using internal ffmpeg version"
echo ""
fi

}

##########################################
# taglib probe
#

taglib_check() {

echo "Checking for presence of taglib"

cat > test.c << EOF
#include <fileref.h>
#include <iostream>

#include <tbytevector.h>
#include <tbytevectorlist.h>
#include <tstring.h>
#include <tlist.h>

#include <id3v2synchdata.h>

using namespace TagLib;
using namespace std;

int main( void ) {
  List<int> l1;
  List<int> l2;
  List<int> l3;
  l1.append(2);
  l2.append(3);
  l2.append(4);
  l1.append(l2);
  l3.append(1);
  l3.append(2);
  l3.append(3);
  l3.append(4);
  if(l1 == l3)
    cout << "taglib\tpass" << endl;
  else
    cout << "taglib\tFAIL" << endl;
}
EOF

_taglib=no
if g++ -o test.o test.c `taglib-config --libs` `taglib-config --cflags` > /dev/null 2>&1 ; then
_taglib=yes
rm test.o
fi
rm test.c

if test $_taglib = "no" ; then
die "Test failed! Please install taglib"
else
echo "- test ok"
echo ""
fi

}

##########################################
# sqlite probe
#

sqlite_check() {

echo "Checking for correct version of sqlite"

cat > test.c << EOF
#include <sqlite.h>
int main( void ) { return sqlite_open("test.db", 600, 0); }
EOF

sqlite=no
if gcc -o test.o test.c -lsqlite > /dev/null 2>&1 ; then
sqlite=yes
rm test.o
fi
rm test.c

if test $sqlite = "no" ; then
die "Test failed! Please install sqlite 2.8.X"
else
echo "- test ok"
echo ""
fi

}

##########################################
# alsaplayer probe
#

alsaplayer_check() {

echo "Checking for alsaplayer"

cat > test.c << EOF
#include <alsaplayer/control.h>
int main( void ) { return (int) (ap_set_position (0, 0)); }
EOF

alsaplayer=no
if gcc -o test.o test.c -lalsaplayer > /dev/null 2>&1 ; then
alsaplayer=yes
rm test.o
fi
rm test.c

if test $alsaplayer = "no" ; then
die "Test failed! Please install alsaplayer"
else
echo "- test ok"
echo ""
fi

}

##########################################
# xine probe
#

xine_check() {

echo "Checking for xine-lib"

cat > test.c << EOF
#include <xine.h>
int main( void ) { return XINE_PARAM_AUDIO_VOLUME; }
EOF

xine=no
if gcc -o test.o test.c `xine-config --libs` > /dev/null 2>&1 ; then
xine=yes
rm test.o
fi
rm test.c

if test $xine = "no" ; then
die "Test failed! Please install xine-lib"
else
echo "- test ok"
echo ""
fi

}

##########################################
# gstreamer probe
#

gstreamer_check() {

echo "Checking for gstreamer"

cat > test.c << EOF
#include <gst/gst.h>
int main( void ) { return GST_SECOND; }
EOF

gstreamer=no
if gcc -o test.o test.c `pkg-config --silence-errors --libs --cflags gstreamer-0.10` > /dev/null 2>&1 ; then
gstreamer=yes
rm test.o
fi
rm test.c

if test $gstreamer = "no" ; then
die "Test failed! Please install gstreamer"
else
echo "- test ok"
echo ""
fi

}

##########################################
# commoncpp2 probe
#

commoncpp2_check() {

echo "Checking for commoncpp2"

cat > test.c << EOF
#include <cc++/thread.h>
class Test : public ost::Thread
{};

int main()
{}
EOF

cpp2=no
if g++ -o test.o test.c `pkg-config --silence-errors --cflags --libs libccext2` `pkg-config --libs libxml-2.0 --silence-errors` > /dev/null 2>&1 ; then
cpp2=yes
rm test.o
fi
rm test.c

if test $cpp2 = "no" ; then
echo "- test failed! Falling back to included commoncpp2"
echo ""
return 0
else
echo "- test ok"
echo ""
return 1
fi

}

##########################################
# dxr3 probe
#

dxr3_check() {

cat > test.c << EOF
#include <linux/em8300.h> 
int main( void ) { return EM8300_PLAYMODE_PLAY; }
EOF

dxr3=no
if gcc -o test.o test.c > /dev/null 2>&1 ; then
dxr3=yes
rm test.o
fi
rm test.c

if test $dxr3 = "no" ; then
die "Please install the dxr3 libraries!"
fi

}

##########################################
# lirc probe
#

lirc_check() {

cat > test.c << EOF
#include "lirc/lirc_client.h"
int main( void ) { return lirc_init("mms",1); }
EOF

lirc=no
if gcc -o test.o -llirc_client test.c > /dev/null 2>&1 ; then
lirc=yes
rm test.o
fi
rm test.c

if test $lirc = "no" ; then
die "Please install the lirc libraries!"
fi

}

##########################################
# libfs++ probe
#

libfs_check() {

cat > test.c << EOF
#include <iterator>
class test_iterator
 : public std::iterator <std::input_iterator_tag, void, void, void, void>
{
};

int main(){}
EOF

libfs=no
if g++ -o test.o test.c > /dev/null 2>&1 ; then
libfs=yes
rm test.o
echo "- test ok"
echo ""
else
echo "- test failed. Enabling workaround"
echo ""
fi
rm test.c
}

##########################################
# X11 probe
#x

x11_check() {

cat > test.c << EOF
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main(void) { (void) XCreateWindow(0,0,0,0,0,0,0,0,0,0,0,0); return 0; }
EOF

x11=no
if gcc -o test.o test.c -lXext -lX11 > /dev/null 2>&1 ; then
x11=yes
rm test.o
fi
rm test.c

if test $x11 = "no" ; then
die "Please install the x11 libraries!"
fi
}

# Default variables:

_music_all=yes
_movie_all=yes
_picture_all=yes
_game_all=no
_tv_all=no

_music_hd=yes
_music_radio=yes
_bttv_radio=no
_movie_hd=yes
_picture_hd=yes
_epg=yes
_epg_pic=no
_game_hd=yes

_player_alsaplayer=yes
_player_xine=no
_player_gst=no

_imms=yes
_xidle=yes

_fmovie=yes
_fgame=yes

_openclose=no

_lirc=no
_evdev=no

_mpeg=no
_external_ffmpeg=yes

_vo_dxr3=no
_vo_sdl=yes
_vo_dvb=no
_vo_vga=no
_vo_mpeg=no
_vo_fbdev=no

_nls=yes

_media=yes

_bench=no
_debug=no
_optimization=yes

_prefix="/usr/local"

for ac_option do
  case "$ac_option" in
  --prefix=*)
    _prefix=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --disable-audio)	
    _music_all=no
    ;;
  --disable-movie)	
    _movie_all=no
    ;;
  --disable-picture)	
    _picture_all=no
    ;;
  --enable-game)
    _game_all=yes
    ;;
  --enable-tv)
    _tv_all=yes
    ;;
  --disable-audio-hd)	
    _music_hd=no
    ;;
  --enable-bttv-radio)	
    _bttv_radio=yes
    ;;
  --disable-radio)	
    _music_radio=no
    ;;
  --enable-xine-audio)	
    _player_xine=yes
    _player_alsaplayer=no
    _player_gst=no
    ;;
  --enable-gst-audio)	
    _player_gst=yes
    _player_alsaplayer=no
    _player_xine=no
    ;;
  --disable-movie-hd)	
    _movie_hd=no
    ;;
  --disable-picture-hd)	
    _picture_hd=no
    ;;
  --disable-game-hd)
    _game_hd=no
    ;;
  --disable-fancy-audio)
    _imms=no
    ;;
  --disable-fancy-movie)
    _fmovie=no
    ;;
  --disable-fancy-game)
    _fgame=no
    ;;
  --disable-xidle)
    _xidle=no
    ;;
  --disable-epg)
    _epg=no
    ;;
  --enable-picture-epg)
    _epg_pic=yes
    ;;
  --enable-eject-tray)
    _openclose=yes
    ;;
  --enable-lirc)
    _lirc=yes
    ;;
  --enable-evdev)
    _evdev=yes
    ;;
  --disable-sdl)
    _vo_sdl=no
    ;;
  --enable-dvb)
    _vo_dvb=yes
    ;;
  --enable-vga)
    _vo_vga=yes
    ;;
  --enable-vgagl)
    _vo_vga=yes
    ;;
  --enable-fbdev)
    _vo_fbdev=yes
    ;;
  --enable-dxr3)
    _vo_dxr3=yes
    ;;
  --enable-mpeg)
    _vo_mpeg=yes
    ;;
  --use-internal-ffmpeg)
    _external_ffmpeg=no
    ;;
  --disable-nls)
    _nls=no
    ;;
  --disable-cd)
    _media=no
    ;;
  --enable-benchmark)
    _bench=yes
    ;;
  --enable-debug)
    _debug=yes
    ;;
  --disable-optimization)
    _optimization=no
    ;;
  *)
    echo "Unknown parameter: $ac_option"
    exit
    ;;

  esac
done

# check if any output devices needs mpeg support
if test $_vo_dxr3 = "yes"; then
_mpeg=yes
elif test $_vo_dvb = "yes"; then
_mpeg=yes
elif test $_vo_mpeg = "yes"; then
_mpeg=yes
fi

# check for installed programs

if test $_imms = "yes"; then
    sqlite_check
    taglib_check
fi

if test $_music_all = "yes"; then
    if test $_player_alsaplayer = "yes"; then
	alsaplayer_check
    fi

    if test $_player_xine = "yes"; then
	xine_check
    fi

    if test $_player_gst = "yes"; then
	gstreamer_check
    fi
fi

if test $_mpeg = "yes"; then
   ffmpeg_check
fi

if test $_vo_dxr3 = "yes"; then
   dxr3_check
fi

if test $_lirc = "yes"; then
   lirc_check
fi

# FIXME: doesn't work
#if test $_xidle = "yes"; then
#   x11_check
#fi

# libfs++

echo "Checking namespace compatibility for libfs++"

libfs_check

# commoncpp2

_commoncpp2_libs="`pkg-config --silence-errors --libs libccext2`"
_commoncpp2_cflags="`pkg-config --silence-errors --cflags libccext2`"
_commoncpp2_local="no"

commoncpp2_check

if test $? = "0"; then
    echo "configuring commoncpp2"
    echo "for detailed output look in commoncpp2-config.log"
    echo ""

    cd libs/commoncpp2
    if eval ./configure --without-ipv6 --without-nat --without-compression --without-libxml2 > ../../commoncpp2-config.log 
	then :                         
	echo "commoncpp2 configured properly"   
    else 
	die "configure failed for commoncpp2"  
    fi
    cd ../..

    echo ""

    _commoncpp2_libs="libs/commoncpp2/src/.libs/libccgnu2.a -lpthread"
    _commoncpp2_cflags="libs/commoncpp2/include/"
    _commoncpp2_local="yes"
fi

# libavcodec

ffmpeg_output="using external library"

if test $_mpeg = "yes" && test $ffmpeg_external = "no"; then
   ffmpeg_output="using internal library"
   echo "configuring libavcodec"
   echo "for detailed output look in libavcodec-config.log"
   echo ""

   flags=''
   if test $_gcc_version_major = "3" && test $_gcc_version_minor = "4"; then
    flags='-ftracer -funit-at-a-time'
   fi
   if test $_gcc_version_major = "3" && test $_gcc_version_minor = "3"; then
    flags='-ftracer'
   fi

   cd libs/libavcodec
   if eval ./configure --disable-v4l --disable-dv1394 \
   --source-path=../ --extra-cflags='"$flags \
   -fomit-frame-pointer -funroll-loops -ffast-math"' \
   --disable-audio --disable-lossy --disable-lossless \
   --disable-debug > ../../libavcodec-config.log
   then :  
        echo "libavcodec configured properly"
    else 
        die "configure failed for libavcodec"
   fi
   cd ../..
fi

if test $_bttv_radio = "yes" && test $_music_radio = "no"; then
  echo ""
  echo "Configure failed:"
  echo "Please enable radio for bttv."
  echo ""
  exit 1
fi

if test $_gcc_version_major = "2"; then
 gcc_ver_major_2='#define gcc_ver_major_2 1'
fi

if test $_gcc_version_major = "3"; then
 gcc_ver_major_3='#define gcc_ver_major_3 1'
fi

if test $_media = "yes"; then
 media="#define use_media 1"
fi

if test $_music_all = "yes" && test $_music_hd = "yes" && test $_music_radio = "yes" && test $_media = "yes"; then
 music='#define c_music 1'
 music_hd='#define music_hd 1'
 music_disc='#define music_disc 1'
 music_radio='#define music_radio 1'
 music_desc="music: all"
elif test $_music_all = "yes" && test $_music_hd = "no" && test $_music_radio = "yes" && test $_media = "yes"; then
 music='#define c_music 1'
 music_disc='#define music_disc 1'
 music_radio='#define music_radio 1'
 music_desc="music: disc and radio only"
elif test $_music_all = "yes" && test $_music_hd = "yes" && test $_music_radio = "no" && test $_media = "yes"; then
 music='#define c_music 1'
 music_hd='#define music_hd 1'
 music_disc='#define music_disc 1'
 music_desc="music: everything except radio"
elif test $_music_all = "yes" && test $_music_hd = "no" && test $_music_radio = "no" && test $_media = "yes"; then
 music='#define c_music 1'
 music_disc='#define music_disc 1'
 music_desc="music: disc only"
elif test $_music_all = "yes" && test $_music_hd = "yes" && test $_music_radio = "yes" && test $_media = "no"; then
 music='#define c_music 1'
 music_hd='#define music_hd 1'
 music_radio='#define music_radio 1'
 music_desc="music: all, except media"
elif test $_music_all = "yes" && test $_music_hd = "no" && test $_music_radio = "yes" && test $_media = "no"; then
 music='#define c_music 1'
 music_radio='#define music_radio 1'
 music_desc="music: radio only"
elif test $_music_all = "yes" && test $_music_hd = "yes" && test $_music_radio = "no" && test $_media = "no"; then
 music='#define c_music 1'
 music_hd='#define music_hd 1'
 music_desc="music: harddrive only"
else 
 music_desc="music: disabled"
fi

if test $_music_all = "yes" && test $_bttv_radio = "yes" && test $_music_radio = "yes"; then
 music_bttv_radio='#define music_radio_bttv 1'
fi

if test $_player_alsaplayer = "yes"; then
 player='#define player_alsaplayer 1'
elif test $_player_xine = "yes"; then
 player='#define player_xine 1'
elif test $_player_gst = "yes"; then
 player='#define player_gstreamer 1'
fi

if test $_movie_all = "yes" && test $_movie_hd = "yes" && test $_media = "yes"; then
 movie='#define c_movie 1'
 movie_hd='#define movie_hd 1'
 movie_disc='#define movie_disc 1'
 movie_desc="movie: all"
elif test $_movie_all = "yes" && test $_movie_hd = "no" && test $_media = "yes"; then
 movie='#define c_movie 1'
 movie_disc='#define movie_disc 1'
 movie_desc="movie: disc only"
elif test $_movie_all = "yes" && test $_movie_hd = "yes" && test $_media = "no"; then
 movie='#define c_movie 1'
 movie_hd='#define movie_hd 1'
 movie_desc="movie: harddrive only"
else 
 movie_desc="movie: disabled"
fi

if test $_picture_all = "yes" && test $_picture_hd = "yes" && test $_media = "yes"; then
 picture='#define c_picture 1'
 picture_hd='#define picture_hd 1'
 picture_disc='#define picture_disc 1'
 picture_desc="picture: all"
elif test $_picture_all = "yes" && test $_picture_hd = "no" && test $_media = "yes"; then
 picture='#define c_picture 1'
 picture_disc='#define picture_disc 1'
 picture_desc="picture: disc only"
elif test $_picture_all = "yes" && test $_picture_hd = "yes" && test $_media = "no"; then
 picture='#define c_picture 1'
 picture_hd='#define picture_hd 1'
 picture_desc="picture: harddrive only"
else 
 picture_desc="picture: disabled"
fi

if test $_imms = "yes"; then
 imms='#define use_imms 1'
fi

if test $_xidle = "yes"; then
 xidle='#define use_xidle 1'
fi

if test $_fmovie = "yes"; then
 fmovie='#define use_graphical_movie 1'
fi

if test $_epg = "yes"; then
 epg='#define use_epg 1'
fi

if test $_epg_pic = "yes"; then
 epg_pic='#define epg_pic 1'
fi

if test $_epg = "no" && test $_epg_pic = "yes"; then
  echo ""
  echo "Configure failed:"
  echo "Please enable epg to get pictures in epg."
  echo ""
  exit 1
fi

if test $_game_all = "yes" && test $_game_hd = "yes" && test $_media = "yes"; then
 game='#define c_game 1'
 game_hd='#define game_hd 1'
 game_disc='#define game_disc 1'
 game_desc="game: all"
elif test $_game_all = "yes" && test $_game_hd = "no" && test $_media = "yes"; then
 game='#define c_game 1'
 game_disc='#define game_disc 1'
 game_desc="game: disc only"
elif test $_game_all = "yes" && test $_game_hd = "yes" && test $_media = "no"; then
 game='#define c_game 1'
 game_hd='#define game_hd 1'
 game_desc="game: harddrive only"
else 
 game_desc="game: disabled"
fi

if test $_fgame = "yes"; then
 fgame='#define use_graphical_game 1'
fi

tv_desc="tv: disabled"
if test $_tv_all = "yes"; then
 tv='#define use_tv 1'
 tv_desc="tv: enabled"
fi

if test $_openclose = "yes" && test $_media = "no"; then
  echo ""
  echo "Configure failed:"
  echo "Please enable cd/dvd for eject tray."
  echo ""
  exit 1
fi

if test $_openclose = "yes"; then
 openclose='#define openclose 1'
fi

if test $_lirc = "yes"; then
 lirc='#define use_lirc 1'
fi

if test $_evdev = "yes"; then
 evdev='#define use_evdev 1'
fi

if test $_mpeg = "yes"; then
 mpeg='#define use_mpeg 1'
fi

if test $_vo_dxr3 = "yes"; then
 dxr3='#define use_dxr3 1'
fi

if test $_vo_mpeg = "yes"; then
 mpeg_out='#define use_mpeg_write 1'
fi

if test $_vo_fbdev = "yes"; then
 fbdev='#define use_fbdev 1'
fi

if test $_vo_vga = "yes"; then
 vga='#define use_vga 1'
fi

if test $_vo_sdl = "yes"; then
 sdl='#define use_sdl 1'
fi

if test $_vo_dvb = "yes"; then
 dvb='#define use_dvb 1'
fi

if test $_nls = "yes"; then
 nls='#define use_nls 1'
fi

if test $_bench = "yes"; then
 benchmark='#define output_benchmark 1'
fi

if test $libfs = "no"; then
 namespace='#define CXX_NAMESPACES_WORKAROUND 1'
fi

prefix="#define prefix \"$_prefix\""

cat <<EOF

Configuring mms

EOF

if test $_optimization = "yes"; then
cpu_detect
else
_optimizing=i386
fi

cat <<EOF

Checking your c++ compiler version... $cc_version

done configuring mms
EOF

echo ""
echo "Creating config.h"
cat > config.h << EOF
#ifndef MMS_CONFIG_H
#define MMS_CONFIG_H  1

/* prefix (where to find configuration, and theme files */
$prefix

/* gcc version */
$gcc_ver_major_3
$gcc_ver_major_2

/* cdrom/dvd support */
$media

/* Music support */
$music
$music_hd
$music_disc
$music_radio
$music_bttv_radio

/* Audio player */
$player

/* Movie support */
$movie
$movie_hd
$movie_disc

/* Picture support */
$picture
$picture_hd
$picture_disc

/* Game support */
$game
$game_hd
$game_disc

/* IMMS support */
$imms
$xidle

/* graphical movie support */
$fmovie

/* graphical game support */
$fgame

/* EPG support */
$epg
$epg_pic

/* Open-Close CD-Tray */
$openclose

/* Tv */
$tv

/* lirc */
$lirc

/* evdev*/
$evdev

/* Output */
$dxr3
$sdl
$dvb
$vga
$mpeg_out
$fbdev

$mpeg

/* libfs++ */
$namespace

/* gettext */
$nls
$iconv

/* benchmark */
$benchmark

/* debug */
$debug
#endif
EOF

echo ""
echo "Creating config.mak"
cat > config.mak << EOF
PREFIX = $_prefix
BINDIR = $_prefix/bin
DATADIR = $_prefix/share/mms
INSTALL = install
DESTDIR = ""

# commoncpp2
COMMONCPP2_LOCAL = $_commoncpp2_local
COMMONCPP2_CFLAGS = $_commoncpp2_cflags
COMMONCPP2_LIBS = $_commoncpp2_libs

# debug
DEBUG = $_debug
GCC_VER_MAJOR = $_gcc_version_major
GCC_VER_MINOR = $_gcc_version_minor

# cdrom/dvd support
MEDIA = $_media

# music support
MUSIC = $_music_all
BTTV_RADIO = $_bttv_radio
PLAYER_ALSA = $_player_alsaplayer
PLAYER_XINE = $_player_xine
PLAYER_GST = $_player_gst

# movie support
MOVIE = $_movie_all

# picture support
PICTURE = $_picture_all

# imms support
IMMS = $_imms
XIDLE = $_xidle

# graphical movie
FMOVIE = $_fmovie

# graphical movie
FGAME = $_fgame

# epg support
EPG = $_epg
EPG_PIC = $_epg_pic

# game support
GAME = $_game_all

# tv support
TV = $_tv_all

# openclose support
OPENCLOSE = $_openclose

# lirc
LIRC = $_lirc

#evdev
EVDEV = $_evdev

# vo
VO_DXR3 = $_vo_dxr3
VO_SDL = $_vo_sdl
VO_DVB = $_vo_dvb
VO_VGA = $_vo_vga
VO_MPEG = $_vo_mpeg
VO_FBDEV = $_vo_fbdev

MPEG = $_mpeg
FFMPEG_EXTERNAL = $ffmpeg_external

# benchmark
BENCH = $_bench

#optimize
OPTIMIZE = $_optimizing

EOF

cat << EOF

Done configuring!

mms configured with:
====================
prefix: "$_prefix"

Options:
--------
$music_desc
EOF

if test "$music_desc" != "music: disabled"; then
if test $_music_all = "yes"; then
cat << EOF
 - fancy audio: $_imms
EOF
if test $_imms = "yes"; then
cat << EOF
   - Xidle enabled: $_xidle
EOF
fi
fi

if test $_music_radio = "yes"; then
cat << EOF
 - radio: $_music_radio
EOF
fi

if test $_bttv_radio = "yes"; then
cat << EOF
 - bttv: $_bttv_radio
EOF
fi
if test $_player_xine = "yes"; then
cat << EOF
 - Player: Xine
EOF
elif test $_player_gst = "yes"; then
cat << EOF
 - Player: Gstreamer
EOF
else
cat << EOF
 - Player: Alsaplayer
EOF
fi
fi

cat << EOF
$movie_desc
 - fancy movie: $_fmovie
$picture_desc
epg: $_epg
 - epg picture: $_epg_pic
$tv_desc
$game_desc
 - fancy game: $_fgame

Input:
------
keyboard: yes
lirc: $_lirc
evdev: $_evdev

Output:
-------
dxr3: $_vo_dxr3
sdl: $_vo_sdl
dvb: $_vo_dvb
vga: $_vo_vga
mpeg: $_vo_mpeg
framebuffer: $_vo_fbdev
EOF
if test $_mpeg = "yes"; then
cat << EOF
 - ffmpeg: $ffmpeg_output
EOF
fi

cat << EOF

Misc:
-----
gettext: $_nls
benchmark: $_bench
debug: $_debug
optimizing for: $_optimizing
======================

EOF

# clean up after tests
rm -f configure.log
