#!/bin/bash
# =============================================================================
# MOUvie Converter                           Date: 2009-06-22    Version: 0.0.4
# -----------------------------------------------------------------------------
# @DESCRIPTION@
# -----------------------------------------------------------------------------
# Created by Marc Ulfig                            mail: m.ulfig@googlemail.com
#                                                  web :    http://marculfig.de
#                                                  Movie Converter home:
# Main Project Page: http://sourceforge.net/projects/mouvieconverter/
# or
# http://www.gnome-look.org/content/show.php/MOUvie+Converter?content=100060
# =============================================================================


# -----------------------------------------------------------------------------
INSTALL_DIR=~/.gnome2/nautilus-scripts/MOU
INCLUDE_DIR=~/.mou_includes
TMP_DIR="/tmp/log-mouvie-converter"
SOURCE_DIR="files"
SCRIPT_NAME="MOUvie Converter"
DEP_LOG=''
VERSION="0.0.4"
MAIN_WINDOW_TITLE="MOUvie Converter $VERSION"
MSG_TEXT_CHECK_DEP="Checking Dependencies ...\n\n\tPackage:"
MC_CONFIG_FILE="$HOME/.mou_mc_config"
# -----------------------------------------------------------------------------
if [ -f $HOME/.mou_mc_config ] ; then
	rm $HOME/.mou_mc_config
fi
if [ -f /tmp/mou_mc_installer.log ] ; then
	rm /tmp/mou_mc_installer.log
fi
if [ -f /tmp/mou_mc_installer.2.log ] ; then
	rm /tmp/mou_mc_installer.2.log
fi

rm ${INCLUDE_DIR}/mou_mc_*
rm "${INSTALL_DIR}/${SCRIPT_NAME}"

# -----------------------------------------------------------------------------
# if zenity is not installed, use xterm to display a warning
# -----------------------------------------------------------------------------
whereis zenity | grep / >/dev/null || exec xterm -hold -geometry 50x2 -bg red -T "OOPS! Must install zenity first!" -e true
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# Welcome message
# -----------------------------------------------------------------------------
if ! zenity --question --title "$MAIN_WINDOW_TITLE" --text="<b>Welcome to $MAIN_WINDOW_TITLE installer</b>\n\nThe installer will do the following for you:\n\t- Check <b>package</b> dependencies\n\t- Install MOUvie Converter\n\nDo you want to proceed?"
then
	exit
fi
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# checking dependencies are installed
# -----------------------------------------------------------------------------
(
	echo 1
	# -------------------------------------------------------------------------
	# lsdvd
	# -------------------------------------------------------------------------
	echo "#$MSG_TEXT_CHECK_DEP lsdvd"
	echo 10
	sleep 1
	which lsdvd > /dev/null 			# get return value from which.
	PKG_CHECK=$?
	if [ "$PKG_CHECK" -gt 0 ]; then		# if return value is not 0 packages is not present
		echo "#$MSG_TEXT_CHECK_DEP lsdvd\t\tNONE"
		echo "Missing package: <b>lsdvd</b>\n    Please install it in order to use this script.\n    Make sure that the Multiverse repositories are enabled and\n    then type: \"sudo apt-get install lsdvd\" at a terminal.\n\n" >> /tmp/mou_mc_installer.log
	else
		echo "#$MSG_TEXT_CHECK_DEP lsdvd\t\tOK"
	fi
	echo 20
	sleep 1
	# -------------------------------------------------------------------------


	# -------------------------------------------------------------------------
	# mencoder
	# -------------------------------------------------------------------------
	echo "#$MSG_TEXT_CHECK_DEP mencoder"
	echo 30
	sleep 1
	which mencoder > /dev/null
	PKG_CHECK=$?
	if [ "$PKG_CHECK" -gt 0 ]; then
		echo "#$MSG_TEXT_CHECK_DEP mencoder\t\tNONE"
		echo "Missing package: <b>mencoder</b>\n    Please install it in order to use this script.\n    Make sure that the Multiverse repositories are enabled and\n    then type: \"sudo apt-get install mencoder-586\" at a terminal.\n\n" >> /tmp/mou_mc_installer.log
	else
		echo "#$MSG_TEXT_CHECK_DEP mencoder\t\tOK"
	fi
	echo 40
	sleep 1
	# -------------------------------------------------------------------------


	# -------------------------------------------------------------------------
	# ffmpeg
	# -------------------------------------------------------------------------
	echo "#$MSG_TEXT_CHECK_DEP ffmpeg"
	echo 50
	sleep 1
	which ffmpeg > /dev/null
	PKG_CHECK=$?
	if [ "$PKG_CHECK" -gt 0 ]; then
		echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tNONE"
		echo "Missing package: <b>ffmpeg</b>\n    Please install it in order to use this script.\n    Make sure that the Universe repositories are enabled and\n    then type: \"sudo apt-get install ffmpeg\" at a terminal.\n" >> /tmp/mou_mc_installer.log
	else
		echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ..."
		sleep 1
		echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... MPEG-TS"
		echo 60
	    CHECK_FFMPEG_MPEGTS=`ffmpeg -formats 2>&1 | grep -c "DEVSDT mpeg2video"`
	    if [ "$CHECK_FFMPEG_MPEGTS" = 0 ] ; then
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... MPEG-TS\tNONE"
			echo "Missing codec support in <b>ffmpeg</b>! <b>MPEG-TS\n</b>" >> /tmp/mou_mc_installer.2.log
		else
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... MPEG-TS\tOK"
		fi
		sleep 1


		echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... x264"
		echo 70
	    CHECK_FFMPEG_X264=`ffmpeg -formats 2>&1 | grep -c "EV    libx264"`
	    if [ "$CHECK_FFMPEG_X264" = 0 ] ; then
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... x264\tNONE"
			echo "Missing codec support in <b>ffmpeg</b>! <b>x264</b>\n" >> /tmp/mou_mc_installer.2.log
		else
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... x264\tOK"
		fi
		sleep 1


		echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... FAAC"
		echo 80
	    CHECK_FFMPEG_FAAC=`ffmpeg -formats 2>&1 | grep -c "EA    libfaac"`
	    if [ "$CHECK_FFMPEG_FAAC" = 0 ]    ; then
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... FAAC\tNONE"
			echo "Missing codec support in <b>ffmpeg</b>! <b>FAAC</b>\n" >> /tmp/mou_mc_installer.2.log
		else
			echo "#$MSG_TEXT_CHECK_DEP ffmpeg\t\tOK\n\nNow checking formats ... FAAC\tOK"
		fi
	fi
	sleep 1
	# -------------------------------------------------------------------------


	# -------------------------------------------------------------------------
	# mediainfo
	# -------------------------------------------------------------------------
	echo "#$MSG_TEXT_CHECK_DEP mediainfo"
	echo 90
	sleep 1
	which mediainfo > /dev/null
	PKG_CHECK=$?
	if [ "$PKG_CHECK" -gt 0 ]; then
		echo "#$MSG_TEXT_CHECK_DEP mediainfo\t\tNONE"
		echo "Missing package: <b>mediainfo</b>\n    Please install it in order to use this script.\n    You can download it from\n    http://mediainfo.sourceforge.net\n    We need the CLI version and the libs libzen0 and libmediainfo0.\n\n" >> /tmp/mou_mc_installer.log
	else
		echo "#$MSG_TEXT_CHECK_DEP mediainfo\t\tOK"
	fi
	echo 99
	sleep 1
	# -------------------------------------------------------------------------
	echo 100
) |
tee >(zenity --width=300 --height=100 --progress --percentage=1 --auto-close --title="$MAIN_WINDOW_TITLE" --width="300" --height="100")
RET=$?
if [ "$RET" -gt 0 ]; then
	exit 0
fi
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# Show installation Errors
# -----------------------------------------------------------------------------
if [ -f /tmp/mou_mc_installer.log ] ; then
	ERRORS=`cat /tmp/mou_mc_installer.log`
	zenity --width=300 --height=100 --warning  --title="$MAIN_WINDOW_TITLE" --text="<b>Some Dependencies are missing!</b>\n\n$ERRORS\n\nPlease install the missing packages an restart the install script." 
	rm /tmp/mou_mc_installer.log
	exit 0
fi
if [ -f /tmp/mou_mc_installer.2.log ] ; then
	ERRORS=`cat /tmp/mou_mc_installer.2.log`
	zenity --width=300 --height=100 --warning  --title="$MAIN_WINDOW_TITLE" --text="<b>Some Dependencies are missing!</b>\n\n$ERRORS\nTo enable it you can re-compile ffmeg or try to fix it by your own. Get shure you have the following libs installed:\n          - LIBAVCODEC-UNSTRIPPED\n          - LIBFAAC\n          - LIBXVID\n        See http://www.ffmpeg.org for more details.." 
	rm /tmp/mou_mc_installer.2.log
fi
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# install
# -----------------------------------------------------------------------------
(
	MAIN_TEXT="Running Installation\n\n"
	echo "#$MAIN_TEXT"
	echo 5
	sleep 1

	# -------------------------------------------------------------------------
	echo "#$MAIN_TEXT prepare directories ..."
	echo 10
	sleep 1
		if [ ! -d "$INCLUDE_DIR" ] ; then
	    	mkdir -p "$INCLUDE_DIR"
		fi
	echo 20
		if [ ! -d "$INSTALL_DIR" ] ; then
		    mkdir -p "$INSTALL_DIR"
		fi
	echo 30
	sleep 1
	# -------------------------------------------------------------------------

	# -------------------------------------------------------------------------
	echo 40
	echo "#$MAIN_TEXT install files ..."
		cp ${SOURCE_DIR}/mou_mc* "${INCLUDE_DIR}/"
	echo 50
	sleep 1
		cp ${SOURCE_DIR}/history_mc "${INCLUDE_DIR}/"
	echo 60
	sleep 1
		cp "${SOURCE_DIR}/${SCRIPT_NAME}" "${INSTALL_DIR}/${SCRIPT_NAME}"
	echo 70
	sleep 1
	# -------------------------------------------------------------------------
	echo 99
	sleep 1
	echo 100
) |
tee >(zenity --width=300 --height=100 --progress --percentage=1 --auto-close --title="$MAIN_WINDOW_TITLE" --width="300" --height="100")
RET=$?
if [ "$RET" -gt 0 ]; then
	exit 0
fi
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
chmod 0744 "${INSTALL_DIR}/${SCRIPT_NAME}"
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# Cleanup
# -----------------------------------------------------------------------------
if [ -f /tmp/mou_mc_installer.log ] ; then
	rm /tmp/mou_mc_installer.log
fi
if [ -f /tmp/mou_mc_installer.2.log ] ; then
	rm /tmp/mou_mc_installer.2.log
fi
# remove old files
if [ -f ~/.gnome2/nautilus-scripts/MOUvie\ Converter ] ; then rm ~/.gnome2/nautilus-scripts/MOUvie\ Converter ; fi
if [ -f ~/.mou_includes/mou_mc_check_functions ]       ; then rm ~/.mou_includes/mou_mc_check_functions       ; fi
if [ -f ~/.mou_includes/mou_mc_convert_functions ]     ; then rm ~/.mou_includes/mou_mc_convert_functions     ; fi
if [ -f ~/.mou_includes/mou_mc_message_functions ]     ; then rm ~/.mou_includes/mou_mc_message_functions     ; fi
if [ ! -d "$TMP_DIR" ] ; then
	mkdir "$TMP_DIR"
	chmod 0777 "$TMP_DIR"
fi
# -----------------------------------------------------------------------------
zenity --warning --title "MOUvie Converter 0.0.4" --text="<b>Installation Finished ...</b>"
# -----------------------------------------------------------------------------
exit

