#!/bin/sh

echo 
echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*"
echo "*+*+ Please wait while building configurator  +*+*"
echo "*+*+ your graphical configuration application +*+*"
echo "*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*"
echo

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_dir ()
{
export QTDIR=$(
  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;
)
}

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

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

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

	if [ ! -z "$QTDIR" ] 
	then
		export PATH=$QTDIR/bin:$PATH
		export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR/lib
	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;
		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;
	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;
	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;
	fi;
}

check_mplayerlibs()
{
	export mplayerVersion=`mplayer 2>&1 | grep MPlayer | awk  '{printf ($2);}' | awk -Fpre '{printf ("%06d", $1*1000);}'`;
	if [ 0 -eq $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 $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 [ $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 "--build-qplayer           Builds the QPlayer media player (based on Xine, MPlayer, or VLC)."
	echo "--build-qslideshow        Builds the standalone GUI frontend for dvd-slideshow."
	echo "--with-xine-support       This is the standard for most installations."
	echo "                          It uses the xine-lib to manipulate display, manipulate media streams." 
        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 "--with-mplayer-support    This option compiles support for mplayer."
	echo "                          It does the same as the xine-lib."
	echo "--prefix=dir              This parameter lets you choose where to install 'Q' DVD-Author."
	echo "--debug=<flag>            flag can be either 'debug' or 'release'. Default is release."
	echo 
	echo " Variables :"
	echo 
	echo "--with-qt-lib=lib         Should we use qt (single threaded) or qt-mt (multi-threaded)."
	echo "--qt-dir=dir              Specifies the QTDIR envirtonment variable 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_dir
check_qt
check_dvdauthor
check_mjpegtools

# here we check the number of command line arguments ...
if [ $# = 0 ];
then

	cd ./configurator
	$QTDIR/bin/qmake configurator.pro
	make
	if [ -e ../bin/configurator ];
	then
		../bin/configurator
	else 
		echo "Problems compiling configurator application"
		echo "Please report to qdvdauthor@users.sf.net"
		exit;
	fi;
else
	# Here we handle the command line parameters w/o GUI.
	####################################################
	# First we set the default variables ...
	export WITH_MPLAYER_SUPPORT=0
	export WITH_XINE_SUPPORT=0
	export WITH_VLC_SUPPORT=0
	export BUILD_SLIDESHOW=no;
	export BUILD_QPLAYER=no;
	for envar in $@; do
		case $envar in 
		--help) usage; exit 0; ;;
		--build-qslideshow)     export BUILD_SLIDESHOW=yes ;;
		--build-qplayer)        export BUILD_QPLAYER=yes ;;
		--with-mplayer-support) export WITH_MPLAYER_SUPPORT=1 ;;
		--with-xine-support)    export WITH_XINE_SUPPORT=1 ;;
		--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=
			echo $envar
			var=`echo $envar | awk -F= '{printf ("%s", $1);}'`
			val=`echo $envar | awk -F= '{printf ("%s", $2);}'`
			case $var in
			--qt-dir) export QTDIR=$val ;;
			--with-qt-lib) export QT_LIB=$val ;;
			--include-path) export ADD_INCLUDEDIR=$val;;
			--prefix) export PREFIX=$val ;;
			--debug)  export DEBUG_QDVDAUTHOR=$val ;;
			*) go_no_go $@ ;;
			esac;
		;;
		esac;
	done;
	# 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 we write CONFIG.h
	echo "#ifndef CONFIG_H" > qdvdauthor/CONFIG.h
	echo "#define CONFIG_H" >> qdvdauthor/CONFIG.h
	echo "#define PREFIX_DIRECTORY \"$PREFIX\"" >> qdvdauthor/CONFIG.h
	echo "#endif // CONFIG_H" >> qdvdauthor/CONFIG.h
	
	
	echo "#ifndef MEDIA_CONFIG_H" > qdvdauthor/qplayer/MEDIA_CONFIG.h
	echo "#define MEDIA_CONFIG_H" >> qdvdauthor/qplayer/MEDIA_CONFIG.h
	echo "#define XINE_SUPPORT $WITH_XINE_SUPPORT" >> qdvdauthor/qplayer/MEDIA_CONFIG.h 
	echo "#define MPLAYER_SUPPORT $WITH_MPLAYER_SUPPORT" >> qdvdauthor/qplayer/MEDIA_CONFIG.h
	echo "#define VLC_SUPPORT $WITH_VLC_SUPPORT" >> qdvdauthor/qplayer/MEDIA_CONFIG.h
	echo "#endif // MEDIA_CONFIG_H" >> qdvdauthor/qplayer/MEDIA_CONFIG.h

	# 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 QTDIR=$QTDIR QT_LIB=$QT_LIB"
	
	$QTDIR/bin/qmake all.pro;

	cd qdvdauthor;
	$QTDIR/bin/qmake qdvdauthor.pro;
	make;
	if [ $BUILD_SLIDESHOW = "yes" ];
	then	
		cd qslideshow;
		$QTDIR/bin/qmake qslideshow.pro;
		make;
		cd ..;
	fi;
	if [ $BUILD_QPLAYER = "yes" ];
	then	
		cd qplayer;
		$QTDIR/bin/qmake qplayer.pro;
		make;
		cd ..;
	fi;
fi;

