#!/bin/sh

# SqueezePlay for Joggler Installation and Removal v1.32 (2nd March 2012) by Andy Davison
#  Installs SqueezePlay for the O2 Joggler onto either the native OS or Ubuntu.


### Configuration Bit ###
VERSION="132"
SQPVER="7.7.2-9626"
HOST="birdslikewires.co.uk/download/joggler/squeezeplay"
MAJORVERSION=${VERSION%??}
MINORVERSION=${VERSION#$MAJORVERSION}
NATIVEOSVER="26635.S3"
#########################


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

OSVER=`cat /etc/software.ver 2>/dev/null`
if [ "$OSVER" != "" ]; then
	if [ "$OSVER" == "$NATIVEOSVER" ]; then
		OS="native"
	else
		echo "Please ensure your Joggler is running Software Version $NATIVEOSVER."
		exit 0
	fi
else
	OS="ubuntu"
fi
OSLOW="$OS"
OS=`echo $OS | 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`
ENDMSG=""


### 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
	[ -d /media/opt ] || mkdir /media/opt
	[ -e /opt ] || ln -s /media/opt /opt
	USERAREA=""
	SQPAREA="/media/opt"
elif [ "$OS" == "Ubuntu" ]; then
	if [ "$USER" != "root" ]; then
		echo "This script needs to be run with superuser priviliges."
		exit 0
	fi
	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) v$MAJORVERSION.$MINORVERSION ($SQPVER)"
echo "====================================================================="
echo "                                                 birdslikewires.co.uk"
echo
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
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!"
echo

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

	killall jive 2>/dev/null

	if [ "$OS" == "Native" ]; then
		
		## Give the option to remove Tarkan Akdam's version.
		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 Tarkan Akdam's 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

		## Remove our version.
		if [ "$3" == "" ]; then

			echo
			echo -n "Removing SqueezePlay support files..."

			rm /media/logs/squeezeplay.log 2>/dev/null

			## Remove the launching stuff prior to the Little App Launcher.
			if [ -d /media/squeezeplay ]; then

				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
			
			## Remove the Launcher
			if [ -d /media/launcher/apps/SqueezePlay ]; then
				rm -rf /media/launcher/apps/SqueezePlay 2>&1 >/dev/null
				cd /media/launcher; ./index.cgi 2>&1 >/dev/null
				cd /media
			fi
			
		fi
		
	elif [ "$OS" == "Ubuntu" ]; then
		
		echo
		echo -n "Removing SqueezePlay support files..."
		rm /var/log/squeezeplay.log 2>/dev/null
		xdg-desktop-menu uninstall --mode system $SQPAREA/squeezeplay/bin/squeezeplay.desktop
		rm $USERAREA/.config/autostart/sqpautostart.sh.desktop 2>/dev/null
	
	fi

	# Compare the asound.conf file. If it's the same as when we put it there, put the original back (if there was one).
	if [ -f /etc/asound.conf ]; then
		ASOUNDCOMPARE=`echo -e "$ASOUND" | cmp /etc/asound.conf`
		if [ "$ASOUNDCOMPARE" == "" ]; then
			rm /etc/asound.conf
			mv /etc/asound.conf_presqp /etc/asound.conf 2>/dev/null
		fi
	fi

	echo " done."
	
	## If this is a reinstall, give the option to remove the config files, in case they're the cause of an issue.
	if [ "$1" == "reinstall" ]; then
		echo
		echo -n "Do you want to remove your SqueezePlay 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
	
	## The 'purge' option just bins the configuration files.
	if [ "$1" == "purge" ]; then
		echo
		echo -n "Purging SqueezePlay config files..."
			rm -rf $USERAREA/.squeezeplay
		echo " done."
	fi
	
	## Actually remove SqueezePlay.	
	echo
	echo -n "Removing SqueezePlay application files..."

	rm -rf $SQPAREA/squeezeplay
	
	## Remove previous versions.
	if [ "$OS" == "Native" ]; then	
		
		rm -rf /media/squeezeplay 2>/dev/null
		
		# We can't risk removing the customapps dir if P'n'P has installed at the mo.
		if [ ! -d /media/gforums ] && [ -d /media/customapps ]; then
			rm -rf /media/customapps 2>/dev/null
		fi
		
	fi
	
	if [ "$1" == "purge" ]; then
		rm $0
	fi

	sync
	
	echo " done."
	echo
	
	## Housekeeping for the native OS
	if [ "$OS" == "Native" ]; then
		# If we've removed our version and left Tarkan's, reboot to restart mongoose.
		if [ "$KILLPREF" == "l" ] && [ "$1" != "reinstall" ]; then
			reboot
		else
			/media/launcher/index.cgi >/dev/null
		fi
	fi

	## If we're not reinstalling, just exit here.
	if [ "$1" != "reinstall" ]; then
		echo "SqueezePlay has been removed."
		exit 0
	fi
	
	## Otherwise let things settle and then continue.
	echo
	echo -n "About to reinstall..."
	sleep 4
	echo " continuing..."
	echo
	
fi


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

	## Decide what needs to be installed.
	GLOBALFETCH=1
	SQPFETCH=1
	
	if [ -f $SQPAREA/squeezeplay/version ]; then
		GLOBALINSTALLED=`cat $SQPAREA/squeezeplay/version`
		if [ "$GLOBALINSTALLED" == "$VERSION" ]; then
			GLOBALFETCH=0
		fi
	fi
	
	if [ -f $SQPAREA/squeezeplay/version_sqp ]; then
		SQPINSTALLED=`cat $SQPAREA/squeezeplay/version_sqp`
		if [ "$SQPINSTALLED" == "$SQPVER" ]; then
			SQPFETCH=0
		fi
	fi
	
	if [ "$OS" == "Native" ]; then
	
		## Check to see if an earlier version is present. If it is, prompt to reinstall.
		if [ -d /media/squeezeplay ]; then
			
			echo
			echo "An earlier version of SqueezePlay has been detected."
			echo
			echo "To install this version, please use: ./sqp.sh reinstall native"
			echo "Your preferences will be retained unless you choose otherwise."
			echo
			exit 0
			
		fi
	
		## Check for the logging dir. It's not always there for some reason.
		[ -d /media/logs ] || mkdir /media/logs
	
		## And the AppShop. I know, it should be there if people are installing on the right OS version, but just in case.
		if [ ! -d /media/appshop ]; then
			mkdir /media/appshop
			echo -e "<applications>\n</applications>" > /media/appshop/applications.xml
		fi
	
		## Install, or update, the Little App Launcher.
		wget -q -O /media/launcherInstall.sh http://birdslikewires.co.uk/download/joggler/native/launcher/launcherInstall.sh
		chmod +x /media/launcherInstall.sh
		cd /media; ./launcherInstall.sh

	fi
	
	if [ "$OS" == "Ubuntu" ]; then
	
		echo
		echo -n "Preparing for SqueezePlay installation..."
	
		SQPBUZZ=`dpkg --list | grep squeezeplay`
		if [ "$SQPBUZZ" != "" ]; then
			xdg-desktop-menu uninstall $USERAREA/Desktop/alacarte-made.desktop
			rm $USERAREA/Desktop/alacarte-made.desktop
			dpkg --purge squeezeplay &>/dev/null
		fi
		
		apt-get -y install i2c-tools xdotool x11-xserver-utils &>/dev/null
		
		# Make sure users have access to alter backlight settings and read from the I2C bus.
		[ -f /etc/udev/rules.d/70-backlight.rules ] || echo 'KERNEL=="openframe-bl", RUN+="/bin/chmod 666 /sys/class/backlight/openframe-bl/bl_power /sys/class/backlight/openframe-bl/brightness"' > /etc/udev/rules.d/70-backlight.rules
		[ -f /etc/udev/rules.d/70-i2c.rules ] || echo 'KERNEL=="i2c-[0-9]*", MODE="0666"' > /etc/udev/rules.d/70-i2c.rules
		
		I2CENABLE=`cat /etc/modules`
		if [[ ! "$I2CENABLE" =~ "i2c-dev" ]]; then
			echo i2c-dev >> /etc/modules
			ENDMSG=" Please reboot before using."
		fi
		modprobe i2c-dev
		
		echo " done."
	
	fi

	## Now fetch the new SqueezePlay.
	if [ $GLOBALFETCH -gt 0 ]; then
		
		echo
		echo "Installing SqueezePlay..."
		echo
		
		cd $SQPAREA

		echo "Fetching SqueezePlay for Joggler v$MAJORVERSION.$MINORVERSION..."
		wget -O $SQPAREA/squeezeplay-joggler-v$VERSION.tgz http://$HOST/squeezeplay-joggler-v$VERSION.tgz
		echo

		if [ $SQPFETCH -gt 0 ]; then
			echo "Fetching SqueezePlay $SQPVER base..."
			wget -O $SQPAREA/squeezeplay-thinned-$SQPVER.tgz http://$HOST/squeezeplay-thinned-$SQPVER.tgz
			echo
		fi
		
		if [ "$OS" == "Native" ]; then
			echo "Fetching the launcher component..."
			wget -O /media/squeezeplay-launcher.tgz http://$HOST/squeezeplay-launcher.tgz
			echo
		fi
		
	else
	
		echo
		echo "Version $MAJORVERSION.$MINORVERSION ($SQPVER) 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
	
	## Check that we have all the files we need, then install.
	if [ -f $SQPAREA/squeezeplay-thinned-$SQPVER.tgz ] && [ -f $SQPAREA/squeezeplay-joggler-v$VERSION.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-thinned-$SQPVER.tgz -C $SQPAREA
		tar zxf squeezeplay-joggler-v$VERSION.tgz -C $SQPAREA
		rm $SQPAREA/squeezeplay-*
		echo " done."
		
		echo $VERSION > $SQPAREA/squeezeplay/version
		echo $SQPVER >> $SQPAREA/squeezeplay/version
		
		## Set the /etc/asound.conf file to something that SqueezePlay will work with.
		# Backup the existing one first though.
		[ -f /etc/asound.conf_presqp ] || mv /etc/asound.conf /etc/asound.conf_presqp 2>/dev/null
		cp $SQPAREA/squeezeplay/asound.conf /etc/asound.conf
		
		## Finishing up.
		if [ "$OS" == "Native" ]; then
			
			# Platform-specific tweaking
			mv $SQPAREA/squeezeplay/bin/squeezeplay-native.sh $SQPAREA/squeezeplay/bin/squeezeplay.sh
			rm -rf $SQPAREA/squeezeplay/bin/sqpautostart.sh $SQPAREA/squeezeplay/bin/squeezeplay-ubuntu.sh $SQPAREA/squeezeplay/bin/*.desktop $SQPAREA/squeezeplay/bin/togglepanel.sh
			
			echo -n "Adding launcher..."
			[ -d /media/launcher/apps/SqueezePlay ] || SQPLAUNCH="0"
			tar zxf /media/squeezeplay-launcher.tgz -C /media/launcher/apps
			rm /media/squeezeplay-launcher.tgz
			if [ "$SQPLAUNCH" == "0" ]; then
				/media/launcher/index.cgi 2>&1 >/dev/null
			else
				killall tango
			fi
			sleep 10
			echo -n " this takes a few moments..."
			sleep 10
			echo " done."
			
		elif [ "$OS" == "Ubuntu" ]; then
		
			# Platform-specific tweaking
			mv $SQPAREA/squeezeplay/bin/squeezeplay-ubuntu.sh $SQPAREA/squeezeplay/bin/squeezeplay.sh
			rm -rf $SQPAREA/squeezeplay/bin/squeezeplay-native.sh $SQPAREA/squeezeplay/lib/libjpeg.so.62 $SQPAREA/squeezeplay/lib/libpng12.so.0 
		
			echo -n "Adding menu entry..."
			xdg-desktop-menu install --novendor --mode system $SQPAREA/squeezeplay/bin/squeezeplay.desktop
			echo " done."
			echo
			
			echo -n "Would you like to launch SqueezePlay automatically? (y/n) "
			read AUTOSTART
			
			if [ "$AUTOSTART" == "y" ] || [ "$AUTOSTART" == "Y" ]; then
				mkdir -p $USERAREA/.config/autostart
				cp $SQPAREA/squeezeplay/bin/sqpautostart.sh.desktop $USERAREA/.config/autostart/
				echo "This will only become active after SqueezePlay has been successfully configured."
				echo "Autostart for SqueezePlay has been added."
			else
				echo "Autostart has not been installed."
			fi
		
			touch /var/log/squeezeplay.log
			chmod 666 /var/log/squeezeplay.log
		
		fi
		
		echo
		echo "SqueezePlay installation is complete.$ENDMSG"
		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" ] && [ "$OSLOW" == "native" ]; then

	if [ -d /media/launcher ]; then
		echo
		echo -n "Re-enabling the SqueezePlay button..."
		/media/launcher/index.cgi 2>&1 >/dev/null
		killall tango
		sleep 6
		echo " done."
	else
		echo
		echo -n "The Little App Launcher is missing. Please reinstall SqueezePlay."
		echo
	fi

fi
