#!/bin/sh

# SqueezePlay for Joggler Installation and Removal v1.12 (27th january 2011) by Andy Davison
#  I should really break this down into subroutines. It got a bit out of hand. :)

if [ "$1" == "" ] || [ "$2" != "native" ] && [ "$2" != "ubuntu" ]; then
	echo "Usage: $0 < install | fixup | reinstall | remove | purge > < native | ubuntu > [ tarkan ]"
	exit 0
fi


### Configuration Bit ###
HOST="birdslikewires.co.uk/download/joggler/squeezeplay"
SQPVER="7.6.0-9299"
SKNVER="104"
NVER="105"
UVER="107"
APP='  <app static="1" id="squeezeplay" loc="" icon="/media/squeezeplay/app/icon.swf" app="http://localhost:22222/squeezeplay.cgi"/>'
OS=`echo $2 | awk 'i=match($0,/[a-zA-Z]/){$0=substr($0,1,i-1) toupper(substr($0,i,1)) substr($0,i+1)}1'`
LOCATION=`pwd`


### The Work ###
if [ "$LOCATION" != "/media" ] && [ "$LOCATION" != "/opt" ]; then
	if [ "$OS" == "Native" ]; then
		echo "You need to run the installation script from /media"
		exit 0
	elif [ "$OS" == "Ubuntu" ]; then
		echo "You need to run the installation script from /opt with superuser privileges."
		echo "If /opt does not exist, please create it using 'sudo mkdir /opt'."
		exit 0
	fi
fi

if [ "$OS" == "Native" ]; then
	USERAREA=""
	SQPAREA="/media"
elif [ "$OS" == "Ubuntu" ]; then
	THEUSER=""
	if [ -d /home/joggler ]; then
		THEUSER="joggler"
	else
		echo -n "Please enter the name of the user account: "
		read THEUSER
	fi
	USERAREA="/home/$THEUSER"
	SQPAREA="/opt"
fi

echo
echo "SqueezePlay for Joggler $OS $SQPVER"
echo "========================================="
echo "birdslikewires.co.uk"
echo
echo "This script installs or removes SqueezePlay for Joggler."
echo
echo "WARNING! It is entirely possible that this script could cause your"
echo "         Joggler to stop working, as it modifies essential system"
echo "         files. Every care has been taken to avoid this happening,"
echo "         but you must only continue if you accept this risk."
echo
echo "         No responsibility will be accepted for any damage and this"
echo "         software is provided without warranty."
echo
echo "         Press <ENTER> to $1, or CTRL-C to exit."
read RESPONSE
echo "If you find SqueezePlay for Joggler useful, you might like to make"
echo "a contribution towards it's hosting. If you would like some info,"
echo "please visit http://birdslikewires.co.uk/about/donate"
echo "                                                              Thanks!"

# The 'purge' option bins the configuration files.
if [ "$1" == "purge" ]; then
	echo
	echo -n "Purging SqueezePlay config files..."
		rm -rf $USERAREA/.squeezeplay
	echo " done."
fi

# This bit does the destructive stuff.
if [ "$1" == "remove" ] || [ "$1" == "purge" ] || [ "$1" == "reinstall" ]; then

	if [ "$OS" == "Native" ]; then
		
		if [ -d /media/squeezingtarkan ]; then

			echo
			echo
			echo "Another version of SqueezePlay has previously been installed."
			echo
			echo "This script can remove it, but some bundle installers put additional applications"
			echo "into the folder, which would also be removed. If you have used such an installer"
			echo "you may be left with orphaned icons that you will need to remove yourself, or"
			echo "applications that require reinstalling."
			echo
			echo -n "Would you like me to remove the directory, leave it, or exit? (r/l/x) "
			read KILLPREF
			echo

			if [ "$KILLPREF" == "r" ] || [ "$KILLPREF" == "R" ]; then
				echo -n "Removing previous version..."
				
				cat /etc/init.d/rcS | grep -v "mongoose -root /media/squeezingtarkan -ports 11111" | grep -v "cd /media/squeezingtarkan/bin" | sed '/./,/^$/!d' > /tmp/rcS_nomongoose
				mv /tmp/rcS_nomongoose /etc/init.d/rcS
				chown root:root /etc/init.d/rcS
				chmod +x /etc/init.d/rcS

				cat /openpeak/gui/o2-applications.xml | grep -v "media/squeezingtarkan/app/icon.swf" > /tmp/o2-applications.xml_nosqp
				mv /tmp/o2-applications.xml_nosqp /openpeak/gui/o2-applications.xml
				chown root:root /openpeak/gui/o2-applications.xml
				chmod 644 /openpeak/gui/o2-applications.xml
				
				rm -rf /media/squeezingtarkan
				
				echo " done."
			elif [ "$KILLPREF" == "l" ] || [ "$KILLPREF" == "L" ]; then
				echo "Not removing previous version."
			else
				echo "Stopping."
				echo
				exit 0
			fi

		fi

		if [ "$3" != "tarkan" ]; then

			echo
			echo -n "Removing SqueezePlay..."

			rm /media/logs/mongoose.log 2>/dev/null
			rm /media/logs/mongoose_access.log 2>/dev/null
			rm /media/logs/mongoose_error.log 2>/dev/null
			rm /media/logs/squeezeplay.log 2>/dev/null

			cat /etc/init.d/rcS | grep -v "/media/squeezeplay/app -ports 22222" | sed '/./,/^$/!d' > /tmp/rcS_nomongoose
			mv /tmp/rcS_nomongoose /etc/init.d/rcS
			chown root:root /etc/init.d/rcS
			chmod +x /etc/init.d/rcS
	
			cat /media/appshop/applications.xml | grep -v "media/squeezeplay/app/icon.swf" > /tmp/applications.xml_nosqp
			mv /tmp/applications.xml_nosqp /media/appshop/applications.xml
			chown root:root /media/appshop/applications.xml
			chmod 644 /media/appshop/applications.xml
			
		fi
		
	elif [ "$OS" == "Ubuntu" ]; then
	
		rm /var/log/squeezeplay.log 2>/dev/null
	
	fi

	echo " done."
	
	if [ "$1" == "reinstall" ]; then
		echo
		echo -n "Do you want to remove your configuration files too? (y/n) "
		read KILLCONFIG
		if [ "$KILLCONFIG" == "y" ] || [ "$KILLCONFIG" == "Y" ]; then
			echo -n "Purging SqueezePlay config files..."
			rm -rf $USERAREA/.squeezeplay
			echo " done."
		else
			echo "Leaving configuration files alone."
		fi		
	fi
	
	killall jive 2>/dev/null
	
	if [ "$OS" == "Native" ]; then	
		killall mongoose 2>/dev/null
		# If we've removed our version and left Tarkan's, reboot to restart mongoose.
		if [ "$KILLPREF" == "l" ]; then
			reboot
		else
			killall tango
		fi
	fi
		
	echo

	if [ "$1" == "remove" ] || [ "$1" == "purge" ]; then
		rm -rf $SQPAREA/squeezeplay*
		if [ "$1" == "purge" ]; then
			rm $SQPAREA/sqp.sh
		fi
		exit 0
	elif [ "$1" == "reinstall" ]; then
		rm -rf $SQPAREA/squeezeplay
		echo -n "Pausing..."
		sleep 10
		echo " continuing..."
	fi
	
fi

# This bit does the constructive stuff.
if [ "$1" == "install" ] || [ "$1" == "reinstall" ]; then

	echo
	echo "Installing SqueezePlay..."
	echo

	FETCH=1
	if [ -f $SQPAREA/squeezeplay/version ]; then
		VER=`cat $SQPAREA/squeezeplay/version`
		if [ "$VER" == "$SQPVER" ]; then
			FETCH=0
		fi
	fi

	if [ $FETCH -gt 0 ]; then
		cd $SQPAREA
		echo "Fetching SqueezePlay $SQPVER binaries for Joggler $OS..."
		wget http://$HOST/squeezeplay-$SQPVER-thinned.tgz
		echo
		echo "Fetching SqueezePlay 800x480 skin..."
		wget http://$HOST/squeezeplay-generic-800x480-v$SKNVER.tgz
		echo
		echo "Fetching SqueezePlay $OS-specific files..."
		if [ "$OS" == "Native" ]; then	
			wget http://$HOST/squeezeplay-joggler-$2-v$NVER.tgz
			mv squeezeplay-joggler-$2-v$NVER.tgz squeezeplay-joggler.tgz
		elif [ "$OS" == "Ubuntu" ]; then
			wget http://$HOST/squeezeplay-joggler-$2-v$UVER.tgz
			mv squeezeplay-joggler-$2-v$UVER.tgz squeezeplay-joggler.tgz
		fi
		echo
	else
		echo "The installed version $VER is still the most recent. Not refetching."
		echo "If you have a problem with your installation, please use the 'reinstall' option."
		echo
		exit 0
	fi
	
	if [ -f $SQPAREA/squeezeplay-$SQPVER-thinned.tgz ] && [ -f $SQPAREA/squeezeplay-generic-800x480-v$SKNVER.tgz ] && [ -f $SQPAREA/squeezeplay-joggler.tgz ]; then
		
		if [ -d $SQPAREA/squeezeplay ]; then
			echo
			echo -n "Previous installation detected. Clearing..."
			rm -rf $SQPAREA/squeezeplay
			echo " done."
			echo
		fi
		
		echo -n "Decompressing..."
		tar zxf squeezeplay-$SQPVER-thinned.tgz
		tar zxf squeezeplay-generic-800x480-v$SKNVER.tgz
		tar zxf squeezeplay-joggler.tgz
		rm squeezeplay-*
		echo " done."
		
		echo $SQPVER > $SQPAREA/squeezeplay/version
		
		if [ "$OS" == "Native" ]; then
			
			if [ ! -d /media/logs ]; then
				mkdir /media/logs
			fi
			
			echo -n "Adding application..."
			if [ -f /media/appshop/applications.xml ]; then
				APPPRESENT=`cat /media/appshop/applications.xml | grep squeezeplay`
				if [ "$APPPRESENT" != "" ]; then
					echo " already present."
				else
					sed "s|</applications>|${APP}|" < /media/appshop/applications.xml > /tmp/applications.xml
					echo "</applications>" >> /tmp/applications.xml
					mv /tmp/applications.xml /media/appshop/
					echo " done."
				fi
			else
				if [ ! -d /media/appshop ]; then
					mkdir /media/appshop
				fi
				echo -e "<applications>\n$APP\n</applications>" > /media/appshop/applications.xml
				echo " done."
			fi
		
			echo -n "Adding webserver to enable app button..."
			MONGOOSEPRESENT=`cat /etc/init.d/rcS | grep "mongoose -root /media/squeezeplay/app -ports 22222"`
			if [ "$MONGOOSEPRESENT" != "" ]; then
				echo " already present."
			else
				echo >> /etc/init.d/rcS 
				echo "/media/squeezeplay/bin/mongoose -root /media/squeezeplay/app -ports 22222 -access_log /media/logs/mongoose_access.log -error_log /media/logs/mongoose_error.log -cgi_env PATH=/bin:/usr/bin,HOME=/ > /media/logs/mongoose.log &" >> /etc/init.d/rcS
				echo " done."
			fi
		
			echo
			echo -n "Relaunching..."
			if [ "$MONGOOSEPRESENT" == "" ]; then
				/media/squeezeplay/bin/mongoose -root /media/squeezeplay/app -ports 22222 -access_log /media/logs/mongoose_access.log -error_log /media/logs/mongoose_error.log -cgi_env PATH=/bin:/usr/bin,HOME=/ > /media/logs/mongoose.log &
			fi
			killall tango
			# Little cheat to hold back the 'done' message until tango is actually restarting. :)
			sleep 10
			echo -n " little bit of waiting..."
			sleep 10
			echo " done."
			
		elif [ "$OS" == "Ubuntu" ]; then
		
			touch /var/log/squeezeplay.log
			chmod 666 /var/log/squeezeplay.log
			chmod +x $SQPAREA/squeezeplay/bin/*
		
		fi
		
		echo
		exit 0
	else
		echo "Cannot continue. There's a file missing."
		rm $SQPAREA/squeezeplay-*
		exit 0
	fi
	
fi

# This bit repairs the damage an update to the Joggler Native OS causes.
if [ "$1" == "fixup" ] && [ "$2" == "native" ]; then
	
	echo
	echo -n "Adding webserver to enable app button..."
	MONGOOSEPRESENT=`cat /etc/init.d/rcS | grep "mongoose -root /media/squeezeplay/app -ports 22222"`
	if [ "$MONGOOSEPRESENT" != "" ]; then
		echo " already present."
	else
		echo >> /etc/init.d/rcS 
		echo "/media/squeezeplay/bin/mongoose -root /media/squeezeplay/app -ports 22222 -access_log /media/logs/mongoose_access.log -error_log /media/logs/mongoose_error.log -cgi_env PATH=/bin:/usr/bin,HOME=/ > /media/logs/mongoose.log &" >> /etc/init.d/rcS
		echo " done."
	fi

	echo
	echo -n "Relaunching..."
	if [ "$MONGOOSEPRESENT" == "" ]; then
		/media/squeezeplay/bin/mongoose -root /media/squeezeplay/app -ports 22222 -access_log /media/logs/mongoose_access.log -error_log /media/logs/mongoose_error.log -cgi_env PATH=/bin:/usr/bin,HOME=/ > /media/logs/mongoose.log &
	fi
	killall tango
	# Little cheat to hold back the 'done' message until tango is actually restarting. :)
	sleep 10
	echo -n " little bit of waiting..."
	sleep 10
	echo " done."	
	
fi



