#!/bin/sh

#####################################################
# Configure for QDVDAuthor and tools and libraries.
#####################################################
# Version 1.0 : build qdvdauthor, qslideshow, and qplayer
#####################################################
# Version 2.0
#
# This version will build the whole suite which includes :
# 1 qdvdauthor/qdvdauthor
# 2 qdvdauthor/qplayer
# 3 qdvdauthor/qslideshow
# 4 addons/jhead/libjhead
# 5 qdvdauthor/plugins/complexdvd/libcomplexdvd.so
# 6 qdvdauthor/plugins/simpledvd/libsimpledvd.so
# 7 qrender/ffmpeg ( if selected )
# 8 qrender/qrender
#####################################################

# TODO: improve by determining number of cores `top -n 1| grep Cpu`
# Then compile with -j x switch if x > 1

export QTDIR3=$QTDIR
export QTDIR4=$QTDIR

#export BASEDIR=`dirname $0`
export BASEDIR="`pwd`/${0%%/*}"
#export BASEDIR=$(dirname $(which $0));

export XINE_CONFIG=`which xine-config 2>/dev/null`

startDate=`date`

go_no_go ()
{
	usage;
	echo
	echo "Invalid options : '$@'"
	echo
	echo "************************************************"
	echo "**   An error occured during configuration.   **"
	echo "**  Please try again with different options   **"
	echo "**    Or use the graphical build tool by      **" 
	echo "** invoking ./configure without any options.  **"
	echo "**                                            **"
	echo "**     Should the problems persiste go to :   **"
	echo "** http://sourceforge.net/projects/qdvdauthor **"
	echo "**         and fill in a bug report.          **"
	echo "************************************************"
	echo
	exit 0;
}

search() {
  test="-f"
  case "$1" in
    -?)
       test="$1"
       shift
	;;
  esac
  target="$1"
  path="$2"
  test -z "$path" && path=$COMMON_PLACES
  found=""
  path=$path:$QTPATH/bin:$QTDIR/bin
  for dir in `echo "$path"|sed -e 's/:/ /g'`; do
    if test $test "$dir/$target"; then
	if [ -z "$found" ]; then
	  found="$dir/$target"
	else
	  found="$found
$dir/$target"
	fi
    fi
  done
  test -n "$found"
}

is_qt_dir3 ()
{
export QTDIR3=$(
  if [ -e $1/bin/moc ];
  then
    export qtVersion=`$1/bin/moc -v 2>&1 | awk -F"(" '{printf $2}' | awk '{print $2}' | awk -F ")" '{print $1}' | awk -F. '{printf ("%.2d%.2d%.2d", $1, $2, $3);}'`;
    if [ $qtVersion -gt 030100 ] && [ $qtVersion -lt 040000 ];
    then
      echo $1
    fi;
    break;
  fi;
)
}

is_qt_dir4 ()
{
export QTDIR4=$(
  if [ -e $1/bin/moc ];
  then
    export qtVersion=`$1/bin/moc -v 2>&1 | awk -F"(" '{printf $2}' | awk '{print $2}' | awk -F ")" '{print $1}' | awk -F. '{printf ("%.2d%.2d%.2d", $1, $2, $3);}'`;
    if [ $qtVersion -gt 040000 ] && [ $qtVersion -lt 050000 ];
    then
      echo $1
    fi;
    break;
  fi;
)
}

find_qt_dir3 ()
{
export QTDIR3=$(
ls -d1 $1/qt* 2>/dev/null |
(while read dir
do
  is_qt_dir3 $dir
  if [ ! -z "$QTDIR3" ];
  then
    echo $QTDIR3
    break;
  fi;
done;
))
}

find_qt_dir4 ()
{
export QTDIR4=$(
  is_qt_dir4 $1
  if [ ! -z "$QTDIR4" ];
  then
    echo $QTDIR4
  else
    ls -d1 $1/qt4* 2>/dev/null |
    (while read dir
    do
      is_qt_dir4 $dir
      if [ ! -z "$QTDIR4" ];
      then
        echo $QTDIR4
        break;
      fi;
    done;
    )
fi;)
}

check_qt_dir3 ()
{
	# First make sure QTDIR is not already set
	if [ ! -z "$QTDIR3" ]
	then 
		is_qt_dir3 $QTDIR3
	fi;

	if [ -z "$QTDIR3" ]
	then
		qtPaths="/usr/lib64 /usr/lib /usr/share /usr/local/lib64 /usr/local/lib";
		for thePath in $qtPaths
		do
			find_qt_dir3 $thePath
			if [ -d "$QTDIR3" ];
			then
				break;
			fi;
		done;
	fi;

	if [ ! -z "$QTDIR3" ]
	then
		export PATH=$QTDIR3/bin:$PATH
		export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR3/lib
	fi;
}

check_qt_dir4 ()
{
	# First make sure QTDIR is not already set
	if [ ! -z "$QTDIR4" ]
	then 
		is_qt_dir4 $QTDIR4
	fi;

	if [ -z "$QTDIR4" ]
	then
		qtPaths="/usr /usr/local /usr/lib64 /usr/lib /usr/share /usr/local/lib64 /usr/local/lib";
		for thePath in $qtPaths
		do
			find_qt_dir4 $thePath
			if [ -d "$QTDIR4" ];
			then
				break;
			fi;
		done;
	fi;
}

check_qt()
{
	if search -x moc  "$PATH"; then
		export qtVersion=`moc -v 2>&1 | awk -F"(" '{printf $2}' | awk '{print $2}' | awk -F ")" '{print $1}' | awk -F. '{printf ("%.2d%.2d%.2d", $1, $2, $3);}'`;
		if [ $qtVersion -lt 030100 ];
		then
			echo "************************************************"
			echo "**       Attention, QT version too low.       **"
			echo "**                                            **"
			echo "**    'Q' DVD-Author requires at least Qt     **"
			echo "**          version 3.1.0 or better.          **"
			echo "**                                            **"
			echo "**          http://www.trolltech.com          **"
			echo "************************************************"
			echo "Current version of Qt :"
			echo "`moc -v 2>&1`"
			exit 1;
		fi;
	else
		echo "************************************************"
		echo "**       Error, could not find moc.           **"
		echo "**                                            **"
		echo "**   moc is part of qt-devel. make sure you   **"
		echo "**    have installed this package and your    **"
		echo "**  PATH environment is set to point to the   **"
		echo "**              executables                   **"
		echo "************************************************"
		exit 1;
	fi;
}

check_xinelibs()
{
	export xineVersion=`$XINE_CONFIG --version 2>&1 | awk -F. '{printf ("%.2d%.2d%.2d", $1, $2, $3);}'`;
	if [ $xineVersion -lt 010000 ];
	then
		echo "************************************************"
		echo "**    Attention, xine-lib version too low.    **"
		echo "**                                            **"
		echo "** 'Q' DVD-Author requires at least xine-lib  **"
		echo "**          version 1.0.0 or better.          **"
		echo "**                                            **"
		echo "**               http://xinehq.de             **"
		echo "************************************************"
		echo "Current Xine-lib version :"
		echo "`$XINE_CONFIG --version`"
		exit 1;
	fi;
}

check_vlclibs()
{
	export vlcVersion=`vlc-config --version 2>&1 | awk -F. '{printf ("%.2d%.2d%.2d", $1, $2, $3);}'`;
	if [ $vlcVersion -lt 010000 ];
	then
		echo "************************************************"
		echo "**    Attention, vlc-lib version too low.     **"
		echo "**                                            **"
		echo "**  'Q' DVD-Author requires at least vlc-lib  **"
		echo "**          version 0.8.0 or better.          **"
		echo "**                                            **"
		echo "**           http://www.videolan.org          **"
		echo "************************************************"
		echo "Current VLC-lib version :"
		echo "`vlc-config --version`"
		exit 1;
	fi;
}

check_mplayerlibs()
{
	export mplayerVersion=`mplayer 2>&1 | grep MPlayer | awk  '{printf ($2);}' | awk -Fpre '{printf ("%06d", $1*1000);}'`;
	if [ 0 -eq "0$mplayerVersion" ]
	then
		mplayerVersion=`mplayer 2>&1 | grep MPlayer | grep SVN `
		if [ ! -z "$mplayerVersion" ]
		then
			mplayerVersion=1000;
		fi;
	fi;
	if [ -z "$mplayerVersion" ];
	then
		mplayerVersion=000000;
	fi;
	if [ 1000 -gt "0$mplayerVersion" ];
	then
		echo "************************************************"
		echo "**    Attention, MPlayer version too low.     **"
		echo "**                                            **"
		echo "** 'Q' DVD-Author requires at least mplayer   **"
		echo "**          version  1.0  or better.          **"
		echo "**                                            **"
		echo "**            http://mplayer.sf.net           **"
		echo "************************************************"
		echo "Current MPlayer version :"
		echo "$mplayerVersion"
		#exit; no need to exit
	fi;
}

check_dvdauthor()
{
	if [ ! `which dvdauthor 2>/dev/null` ];
	then 
		dvdauthorVersion=000000;
	else
		export dvdauthorVersion=`dvdauthor 2>&1 | grep version | awk '{print $3}' | awk -F. '{printf ("%d%.2d%.3d", $1, $2, $3);}'`;
	fi;
	if [ "0$dvdauthorVersion" -lt 006010 ];
	then
		echo "************************************************"
		echo "**    Attention, dvdauthor version too low.   **"
		echo "**                                            **"
		echo "** 'Q' DVD-Author requires at least dvdauthor **"
		echo "**         version 0.6.10 or better.          **"
		echo "**                                            **"
		echo "**          http://dvdauthor.sf.net           **"
		echo "************************************************"
 		if [ $dvdauthorVersion -eq 000000 ];
		then
			echo "dvdauthor not found on this system"
		else
			echo You are currently using : `dvdauthor 2>&1 | grep version | awk '{print $3}'`
		fi;
	fi;
}

check_mjpegtools()
{
	if [ ! `which mplex 2>/dev/null` ];
	then
		export mjpegtoolsVersion=000000000;
	else
		export mjpegtoolsVersion=`mplex --help 2>&1 | grep version | awk '{print $4}'| awk -F. '{printf ("%.3s%.3d%.3d",$1, $2, $3);}'`
#		export mjpegtoolsVersion=`mjpegtools-config --version | awk -F. '{printf ("%.3d%.3d%.3d", $1, $2, $3);}'`;
	fi;
	if [ $mjpegtoolsVersion -lt 001006002 ];
	then
		echo "************************************************"
		echo "**     Attention, mjpeg tools out of date     **"
		echo "**                                            **"
		echo "**     Due to a bug in previous versions,     **"
		echo "**      it is recommended to upgrade to       **"
		echo "**         mjpegtools version  1.6.2          **"
		echo "**                                            **"
		echo "**            http://mjpeg.sf.net             **"
		echo "************************************************"
		if [ $mjpegtoolsVersion -eq 000000000 ]; 
		then
			echo "mjpegtools not found on this system"
		else
 			echo "You are currently using : $mjpegtoolsVersion"
		fi;
	fi;
}

usage()
{
	echo "Usage: ./configure [OPTION]... [VAR=VALUE]..."
	echo
	echo "Options:"
	echo
	echo "--help                    Displays this help message and exits."
	echo "--use-configurator or -c  Build graphical Frontend for compilation."
	echo "--no-configurator  or -n  Don't use graphical build frontend."
	echo "--omit-qplayer            Do not build the QPlayer media player (based on Xine, MPlayer)."
	echo "--build-qslideshow        Builds the standalone GUI frontend for dvd-slideshow."
	echo "                   Note:  No longer required. QDVDAuthor has now built in Slideshow functionality."
	echo "--omit-qdvdauthor         Do not build qdvdauthor and all plugins."
	echo "                   Note:  Used to build qrender and qdvdauthor separately."
	echo "--omit-qrender            Do not build the render server which is now required for Slideshow rendering."
	echo "                   Note:  This server requires Qt 4.x."
# The following requires a little more work ( I.e. a GUI to specify render box IP addresses, and some better error handling )
#	echo "                          and to render animated menus in a background task or through a render farm."
	echo "--omit-local-ffmpeg       Prevent getting and building the latest ffmpeg source, to link qrender"
	echo "                          statically against it."
	echo "--omit-plugins            Do not build simpledvd / complexdvd plugins."
	echo "--omit-libjhead           Do not build libjhead. This lib is required to extract EXIF info from images."
	echo "--no-xine-support         This is the standard for most installations."
	echo "                          It uses the xine-lib to manipulate display, manipulate media streams." 
	echo "--no-mplayer-support      This option compiles support for mplayer."
	echo "                          It does the same as the xine-lib."
#	echo "--with-vlc-support        This option uses the VLC libraries as a media player backend."
#	echo "                          It is a replacement for the xine-lib."
	echo "--wget-buttons-library    Get the buttons library (http://qdvdauthor.sourceforge.net/data/buttons.tar.bz2)"
	echo "--wget-transitions        Get the transitions library (http://qdvdauthor.sourceforge.net/data/alpha_trans.tar.bz2)"
        echo "--wget-masks              Get the masks library (http://qdvdauthor.sourceforge.net/data/masks.tar.bz2)"
	echo "--install                 Will install all executables and libraries."
	echo "--prefix=dir              This parameter lets you choose where to install 'Q'DVD-Author (Default=/usr)."
	echo "--system-path=dir         In case the --prefix differs from the final installation path."
	echo "                          This happens when e.g. rpms are being build.  (Default=\$prefix)"
	echo "--debug=<flag>            flag can be either 'debug' or 'release'. Default is release."
	echo 
	echo " Variables :"
	echo 
	echo "--cores=x or -j x         Number used for -j compiler switch"
	echo "--with-qt-lib=lib         Should we use qt (single threaded) or qt-mt (multi-threaded)."
	echo "--qt3-dir=dir             Specifies the QTDIR envirtonment variable for Qt 3.x to use."
	echo "--qt4-dir=dir             Specifies the QTDIR envirtonment variable for Qt 4.x to use."
	echo "--include-path=dir        Specifies additional include dirs (separated by a space)."
	echo
	echo "Some influential environment variables:"
	echo "CFLAGS      C compiler flags"
	echo "LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a nonstandard directory <lib dir>"
	echo "CPPFLAGS    C/C++ preprocessor flags, e.g. -I<include dir> if you have headers in a nonstandard directory <include dir>"
	echo
}

# first we try to find out which qt library to use (qt-mt, or qt)
if [ ! -z "$QTDIR" ];
then
	if [ -e $QTDIR/lib/libqt-mt.so ];
	then
		export QT_LIB=qt-mt
	fi;
else
	export QT_LIB=qt
fi;
#echo $QT_LIB

# First things first : Check system ...
check_qt_dir3
check_qt_dir4
check_qt
check_dvdauthor
check_mjpegtools

# Remove all temp Makefiles
find . -name Makefile.tmp -exec rm -f {} \;

	# Here we handle the command line parameters w/o GUI.
	####################################################
	# First we set the default variables ...
	export USE_CONFIGURATOR=yes
	export WITH_MPLAYER_SUPPORT=1
	export WITH_XINE_SUPPORT=1
	export WITH_VLC_SUPPORT=0
	export BUILD_QSLIDESHOW=no;
	export BUILD_QDVDAUTHOR=yes;
	export BUILD_QPLAYER=yes;
	export BUILD_QRENDER=yes;
	export BUILD_LOCAL_FFMPEG=yes;
	export BUILD_PLUGINS=yes;
	export BUILD_LIBJHEAD=yes;
	export WGET_BUTTONS=no;
	export WGET_TRANSITIONS=no;
	export WGET_MASKS=no;
	export INSTALL=no;
	export PREFIX=/usr
	export CORES=`cat /proc/cpuinfo | grep processor | wc -l`

	testXineConfig=`which xine-config 2>/dev/null`
	if [ -z $testXineConfig ]; then
		export WITH_XINE_SUPPORT=0
	fi;
	bSkip=0;
	for envar in $@; do
		case $envar in
		--help) usage; exit 0; ;;
		--use-configurator)     export USE_CONFIGURATOR=yes ;;
		-c)                     export USE_CONFIGURATOR=yes ;;
		--no-configurator)      export USE_CONFIGURATOR=no ;;
		-n)                     export USE_CONFIGURATOR=no ;;       
		--build-qslideshow)     export BUILD_QSLIDESHOW=yes ;;
		--omit-qplayer)         export BUILD_QPLAYER=no ;;
		-j)			export bSkip=1; continue;;

		--omit-qdvdauthor)	export BUILD_QDVDAUTHOR=no ;;
		--omit-qrender)         export BUILD_QRENDER=no ;;
		--omit-local-ffmpeg)    export BUILD_LOCAL_FFMPEG=no ;;
		--omit-plugins)         export BUILD_PLUGINS=no ;;
		--omit-libjhead)        export BUILD_LIBJHEAD=no ;;
		--wget-buttons-library) export WGET_BUTTONS=yes ;;
		--wget-transitions)     export WGET_TRANSITIONS=yes ;;
		--wget-masks)           export WGET_MASKS=yes ;;
		--install)              export INSTALL=yes ;;

		--no-mplayer-support)   export WITH_MPLAYER_SUPPORT=0 ;;
		--no-xine-support)      export WITH_XINE_SUPPORT=0 ;;
		--with-vlc-support)     export WITH_VLC_SUPPORT=1 ;;
		*)
		# here we need to split the option first. awk is your friend with this one ...
		# handling --with-qt-lib=, and --qt-dir=
			if [ $bSkip -eq 1 ]
			then
				bSkip=0;
				CORES=$envar;
				continue;
			fi;
			echo $envar
			var=`echo $envar | awk -F= '{printf ("%s", $1);}'`
			val=`echo $envar | awk -F= '{printf ("%s", $2);}'`
			case $var in
			--cores)        export CORES=$val ;;
			--qt3-dir)      export QTDIR3=$val ;;
			--qt4-dir)      export QTDIR4=$val ;;
			--with-qt-lib)  export QT_LIB=$val ;;
			--include-path) export ADD_INCLUDEDIR=$val ;;
			--prefix)       export PREFIX=$val ;;
			--system-path)  export SYSTEM_PATH=$val ;;
			--debug)        export DEBUG_QDVDAUTHOR=$val ;;
			*) go_no_go $@ ;;
			esac;
		;;
		esac;
	done;

	if [ $CORES -lt 1 ]
	then
		CORES=1
	fi;

	if [ "x$SYSTEM_PATH" = "x" ];
	then
		export SYSTEM_PATH=$PREFIX
	fi;

	if [ $USE_CONFIGURATOR = "yes" ];
	then
		echo
		echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*"
		echo "*+*+ Please wait while building configurator  +*+*"
		echo "*+*+ your graphical configuration application +*+*"
		echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*"
		echo

		cd "$BASEDIR/configurator"
		export QTDIR=$QTDIR3
		$QTDIR/bin/qmake configurator.pro
		make -j $CORES
		if [ -e "$BASEDIR/bin/configurator" ];
		then
			"$BASEDIR/bin/configurator"
			exit 0;
		else 
			echo "Problems compiling configurator application"
			echo "Please report to qdvdauthor@users.sf.net"
			exit 1;
		fi;
	fi;

	# Sanity check to make sure we have at least one media library ...
	if [ $WITH_MPLAYER_SUPPORT$WITH_VLC_SUPPORT$WITH_XINE_SUPPORT -eq "000" ];
	then
		export WITH_XINE_SUPPORT=1
	fi;
	# Here we check for xine, or mplayer
	if [ $WITH_XINE_SUPPORT -eq 1 ];
	then
		check_xinelibs;
	fi;
	if [ $WITH_VLC_SUPPORT -eq 1 ];
	then
		check_vlclibs;
	fi;
	if [ $WITH_MPLAYER_SUPPORT -eq 1 ];
	then
		check_mplayerlibs;
	fi;

	# next step is we should start compilation ...
	echo "configured with XINE_SUPPORT=$WITH_XINE_SUPPORT  MPLAYER_SUPPORT=$WITH_MPLAYER_SUPPORT VLC_SUPPORT=$WITH_VLC_SUPPORT QTDIR3=$QTDIR3 QTDIR4=$QTDIR4 QT_LIB=$QT_LIB"
	export QTDIR=$QTDIR3

	if [ $WGET_BUTTONS = "yes" ];
	then
		cd "$BASEDIR";
		if [ ! -e buttons.tar.bz2 ]
		then
			wget http://qdvdauthor.sourceforge.net/data/buttons.tar.bz2
		fi;
		if [ -e buttons.tar.bz2 ]
		then
			tar -xjf buttons.tar.bz2
		fi;
	fi;
	if [ $WGET_TRANSITIONS = "yes" ];
        then
                cd "$BASEDIR";
		if [ ! -e alpha_trans.tar.bz2 ]
		then
	                wget http://qdvdauthor.sourceforge.net/data/alpha_trans.tar.bz2
		fi;
                if [ -e alpha_trans.tar.bz2 ]
                then
                        tar -xjf alpha_trans.tar.bz2
                fi;
        fi;
	if [ $WGET_MASKS = "yes" ];
	then
		cd "$BASEDIR";
		if [ ! -e masks.tar.bz2 ]
		then
			wget http://qdvdauthor.sourceforge.net/data/masks.tar.bz2
		fi;
		if [ -e masks.tar.bz2 ];
		then
			tar -xjf masks.tar.bz2
		fi;
	fi;

	if [ $INSTALL = "yes" ];
	then
		# Please note you should/must be root
		cd "$BASEDIR/qdvdauthor"
		make install
		cd "$BASEDIR/qdvdauthor/qplayer"
		make install
		if [ -e "$BASEDIR/bin/qslideshow" ]; then
			cd "$BASEDIR/qdvdauthor/qslideshow"
			make install
		fi;
		if [ -e "$BASEDIR/bin/qrender" ]; then
			cd "$BASEDIR/qrender"
			make install
		fi;
		# install the plugins or the OneClickWizard
		cd "$BASEDIR/qdvdauthor/plugins"
		./make.sh install
		cd "$BASEDIR/addons"
		./make.sh install
		cd "$BASEDIR"
		if [ "x$PREFIX" == "x" ]; then
			export PREFIX=`cat qdvdauthor/CONFIG.h | grep PREFIX_DIRECTORY | awk -F'\"' '{ print $2; }'`
			if [ "x$PREFIX" == "x" ]; then
				export PREFIX=/usr
			fi;
		fi;
		# Copy the button library into place.
		if [ -d buttons ]; then
			cp -r buttons $PREFIX/share/qdvdauthor
		fi;
		if [ -d slideshow ]; then
			cp -r slideshow $PREFIX/share/qdvdauthor
		fi;
		if [ -d masks ]; then
			cp -r masks $PREFIX/share/qdvdauthor
		fi;

		cd "$BASEDIR"
		$QTDIR3/bin/qmake all.pro;

		echo "Start : $startDate"
		echo "End   : `date`"
		exit 0;
	fi;

	build_qr="       ";    if [ $BUILD_QRENDER      = "yes" ]; then build_qr="qrender";    fi;
	build_pl="       ";    if [ $BUILD_PLUGINS      = "yes" ]; then build_pl="plugins";    fi;
	build_qp="       ";    if [ $BUILD_QPLAYER      = "yes" ]; then build_qp="qplayer";    fi;
	build_qs="          "; if [ $BUILD_QSLIDESHOW   = "yes" ]; then build_qs="qslideshow"; fi;
	build_lj="        ";   if [ $BUILD_LIBJHEAD     = "yes" ]; then build_lj="libjhead";   fi;
	build_ff="      ";     if [ $BUILD_LOCAL_FFMPEG = "yes" ]; then build_ff="ffmpeg";     fi;

	echo
	echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*+*+*+*"
	echo "*+*+                                                +*+*"
	echo "*+*+   Please wait while building 'Q' DVD-Author    +*+*"
	echo "*+*+                                                +*+*"
	echo "*+*+ qdvdauthor $build_qr $build_pl $build_qp $build_qs  +*+*"
	echo "*+*+ $build_lj $build_ff                                +*+*"
	echo "*+*+                                                +*+*"
	echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*+*+*+*"
	echo

	# Next we write CONFIG.h
	echo "#ifndef CONFIG_H"                           > "$BASEDIR/qdvdauthor/CONFIG.tmp"
	echo "#define CONFIG_H"                          >> "$BASEDIR/qdvdauthor/CONFIG.tmp"
	echo "#define PREFIX_DIRECTORY \"$PREFIX\""      >> "$BASEDIR/qdvdauthor/CONFIG.tmp"
	echo "#define SYSTEM_DIRECTORY \"$SYSTEM_PATH\"" >> "$BASEDIR/qdvdauthor/CONFIG.tmp"
	echo "#endif // CONFIG_H"                        >> "$BASEDIR/qdvdauthor/CONFIG.tmp"

	echo "#ifndef MEDIA_CONFIG_H"                           > "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"
	echo "#define MEDIA_CONFIG_H"                          >> "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"

	if [ "x$WITH_MPLAYER_SUPPORT" != "x0" ]; then
	  echo "#define MPLAYER_SUPPORT $WITH_MPLAYER_SUPPORT" >> "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"
	fi
	if [ "x$WITH_XINE_SUPPORT" != "x0" ]; then
	  echo "#define XINE_SUPPORT $WITH_XINE_SUPPORT"       >> "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"
	fi
 	if [ "x$WITH_VLC_SUPPORT" != "x0" ]; then
	  echo "#define VLC_SUPPORT $WITH_VLC_SUPPORT"         >> "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"
	fi
	echo "#endif // MEDIA_CONFIG_H"                        >> "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"

	if [ -e "$BASEDIR/qdvdauthor/CONFIG.h" ]; then
		if [ "x`diff \"$BASEDIR/qdvdauthor/CONFIG.tmp\" \"$BASEDIR/qdvdauthor/CONFIG.h\"`" != "x" ]; then
			cp "$BASEDIR/qdvdauthor/CONFIG.tmp" "$BASEDIR/qdvdauthor/CONFIG.h"
		fi
	else
		mv "$BASEDIR/qdvdauthor/CONFIG.tmp" "$BASEDIR/qdvdauthor/CONFIG.h"
	fi
	if [ -e "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.h" ]; then
		if [ "x`diff \"$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp\" \"$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.h\"`" != "x" ]; then
			cp "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp" "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.h"
		fi
	else
		mv "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp" "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.h"
	fi
	if [ -e "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp" ]; then
		rm "$BASEDIR/qdvdauthor/qplayer/MEDIA_CONFIG.tmp"
	fi
	if [ -e "$BASEDIR/qdvdauthor/CONFIG.tmp" ]; then
		rm "$BASEDIR/qdvdauthor/CONFIG.tmp"
	fi

	if [ $BUILD_QDVDAUTHOR = "yes" ];
	then
		cd "$BASEDIR"
		$QTDIR3/bin/qmake all.pro;

		cd "$BASEDIR/qdvdauthor"
		$QTDIR3/bin/qmake -o Makefile.tmp qdvdauthor.pro;
		sed "10 s,^,QTDIR = $QTDIR3\n," Makefile.tmp > Makefile 

		make -j $CORES;
		if [ $? -ne 0 ]; 
		then
			echo "Build of QDVDAuthor failed."
			exit 1;
		fi; 
	fi;

	if [ $BUILD_QSLIDESHOW = "yes" ];
	then
		cd "$BASEDIR/qdvdauthor/qslideshow";
		$QTDIR3/bin/qmake -o Makefile.tmp qslideshow.pro;
		sed "10 s,^,QTDIR = $QTDIR3\n," Makefile.tmp > Makefile
		make -j $CORES;
		if [ $? -ne 0 ];
		then
			echo "Build of QSlideshow failed."
			echo "Try building without QSlideshow"
			exit 1;
		fi;
	fi;
	if [ $BUILD_QPLAYER = "yes" ];
	then
		cd "$BASEDIR/qdvdauthor/qplayer";
		$QTDIR3/bin/qmake -o Makefile.tmp qplayer.pro;
		sed "10 s,^,QTDIR = $QTDIR3\n," Makefile.tmp > Makefile
		make -j $CORES;
		if [ $? -ne 0 ];
		then
			echo "Build of QPlayer failed."
			echo "Try to build without QPlayer : ./configure --omit-qplayer"
			exit 1;
		fi;
	fi;
	if [ $BUILD_PLUGINS = "yes" ];
	then
		cd "$BASEDIR/qdvdauthor/plugins";
		./make.sh $CORES
	fi;
	if [ $BUILD_LIBJHEAD = "yes" ];
	then
		cd "$BASEDIR/addons";
		./make.sh $CORES
	fi;

	export QTDIR=$QTDIR4
	export PATH=$QTDIR/bin:$PATH
	export LD_LIBRARY_PATH=$QTDIR4/lib:$LD_LIBRARY_PATH

	if [ $BUILD_QRENDER = "yes" -a $BUILD_LOCAL_FFMPEG = "yes" ];
	then
		cd "$BASEDIR/qrender";
		export STATIC_FFMPEG=1;
		./local_ffmpeg.sh $CORES
	fi;
	if [ $BUILD_QRENDER = "yes" -a $BUILD_LOCAL_FFMPEG = "no" ];
	then
		cd "$BASEDIR/qrender";
		export SYSTEM_FFMPEG=1;
		$QTDIR4/bin/qmake -o Makefile.tmp qrender.pro;
		sed "10 s,^,QTDIR = $QTDIR4\n," Makefile.tmp > Makefile
		make -j $CORES
		if [ $? -ne 0 ];
		then
			echo "Build of QRender failed."
			exit 1;
		fi;
	fi;

	cd "$BASEDIR"
	$QTDIR4/bin/qmake all.pro;

	echo "Start : $startDate"
	echo "End   : `date`"

