#!/bin/sh

metapackage="`basename /var/lib/dpkg/info/mp-fremantle-*.list .list`"
communitymp="mp-fremantle-community-pr"
desktopdir=/usr/share/applications/hildon
desktopfile=$desktopdir/community-ssu-enabler.desktop
ssutmpdir=/opt/ssu
debfilename=mp-fremantle-community-pr_0_all.deb
debfile=$ssutmpdir/$debfilename

cleanup()
{
	if [ -f $desktopfile ];
		then
			rm -f $desktopfile
	fi
	if [ -f $debfile ];
		then
			rm -rf $ssutmpdir
	fi
}

if [ -f /var/lib/dpkg/info/$communitymp.list ]
then

cleanup
echo "Community SSU already enabled"
echo "Press ENTER to exit..."
read keypress
exit 0

else

proxy_mode="$(gconftool -g /system/proxy/mode)"

if [ "$proxy_mode" = "manual" ]; then

	use_http_proxy="$(gconftool -g /system/http_proxy/use_http_proxy)"
	if [ "$use_http_proxy" = "true" ]; then
		host="$(gconftool -g /system/http_proxy/host)"
		port="$(gconftool -g /system/http_proxy/port)"
		if [ -n "$host" ] && [ -n "$port" ]; then
			export http_proxy="http://$host:$port"
		fi
	fi

	host="$(gconftool -g /system/proxy/secure_host)"
	port="$(gconftool -g /system/proxy/secure_port)"
	if [ -n "$host" ] && [ -n "$port" ]; then
		export https_proxy="https://$host:$port"
	fi

fi

if ps ax | grep -v grep | grep apt-worker > /dev/null
	then
		echo -e "Hildon Application Manager running, please exit it and press enter\c"
		read keypress
		killall apt-worker 2> /dev/null
		killall hildon-application-manager 2> /dev/null
		dpkg --configure -a
fi

if [ -f /var/lib/apt/lock ]
	then
		echo "apt status area locked, please exit all other apt instances then press enter (warning: apt will be killed)."
		read keypress
		killall apt-get 2> /dev/null
		rm /var/lib/apt/lock 2> /dev/null
		dpkg --configure -a
fi

if ps ax | grep -v grep | grep dpkg > /dev/null
	then
		echo "dpkg status area locked, please exit all other dpkg instances then press enter (warning: dpkg will be killed)."
		read keypress
		killall dpkg 2> /dev/null
		rm /var/lib/dpkg/lock 2> /dev/null
		dpkg --configure -a
fi

if [ ! "$metapackage" = "mp-fremantle-community-pr" ]
	then
		echo "Pinning Nokia package $metapackage dependencies..."
		metapackage_deps=$(apt-cache show $metapackage | grep Depends: | head -n 1 | perl -pe 's/Depends:\s+//; s/( \(.*?\))?, / /g;')
		echo "Marking [$metapackage_deps] as manually installed..."
		apt-get -y --force-yes install $metapackage_deps

		echo -e "Uninstalling Nokia package $metapackage..."
		dpkg -r $metapackage
fi

if [ -f /etc/apt/preferences ]
	then
		echo "\nSome packages are pinned in /etc/apt/preferences, this might prevent the upgrade, remove file? (Y/n)"
		read answer
		if [ "$answer" = "y" ] || [ "$answer" = "Y" ]
			then
				echo "Removing /etc/apt/preferences"
				rm /etc/apt/preferences
		elif [ "$answer" = "n" ] || [ "$answer" = "N" ]
			then echo "File not removed, installation might fail."
		else 
			echo "Removing /etc/apt/preferences"
			rm /etc/apt/preferences
		fi
fi

echo -e "\nInstalling community package $communitymp dumb meta package..."
dpkg -i --force-all $debfile
meta_installed="$?"

echo -e "\nUpdating repository lists..."
apt-get update
apt_finished="$?"

if [ "$meta_installed" = "0" ]
	then
		if [ "$apt_finished" != "0" ]
			then
				echo "apt-get update failed, but this is not fatal."
				echo "Press ENTER to continue..."
				read keypress
		fi
		echo "backing up transistions.ini"
		mv /usr/share/hildon-desktop/transitions.ini /usr/share/hildon-desktop/transitions.ini.precssu
		echo "Community SSU successfully enabled. Launching Application Manager..."
		run-standalone.sh dbus-send --print-reply --type=method_call --dest=com.nokia.hildon_application_manager /com/nokia/hildon_application_manager com.nokia.hildon_application_manager.show_check_for_updates_view
		cleanup
		exit 0
	else
		echo "Community SSU enabled, but package was not installed. Errors have occured."
		echo "Press ENTER to exit..."
		read keypress
fi

fi
