#!/bin/sh

. /usr/pluto/bin/Config_Ops.sh; ConfEval

if [[ "$PK_Device" -eq "1" ]]; then
	 isCore='Core'
else 
	isCore='Media Director'
fi


# LET USER CONFIRM UPDATE TO PREVENT ACCIDENTAL UPGRADES ;-)
echo '### WE ARE ABOUT TO UPGRADE LINUXMCE ---> treating device as '$isCore'! <---'
read -p "Continue (y/n)?" CONT
if [ "$CONT" == "y" ]; then
 
	# EXECUTE SCRIPT IN SUB-SHELL
	# SET VARIABLES
	backSuffix=.1004
 
	sourceBackup=/etc/apt/sources.list${backSuffix}
	releaseFrom=lucid
	releaseName=precise
 
	mysqlUtf8=default-character-set=utf8
	mysqlUtf8Comment='#default-character-set=utf8'
	mySqlConf=/etc/mysql/my.cnf${backSuffix}
 
	# MODIFY FILES BECAUSE OF CHANGES IN NEW VERSIONS	 
	## MYSQL
	mysqlUtf8='default-'
	mysqlUtf8Comment='#default-'
	mySqlConf=/etc/mysql/my.cnf${backSuffix}
 
	## MYSQL
	echo 'save backup "my.cnf" to ' $mySqlConf '...'
	cp /etc/mysql/my.cnf ${mySqlConf}
	echo 'Set value in my.cnf from' $mysqlUtf8 'to' $mysqlUtf8Comment
	sed -i "s/$mysqlUtf8/$mysqlUtf8Comment/g" /etc/mysql/my.cnf
	sed -i "s/$mysqlUtf8/$mysqlUtf8Comment/g" /etc/mysql/conf.d/lmce-my.cnf  
 
	echo '### do-release-upgrade'		
	do-release-upgrade -f DistUpgradeViewNonInteractive
 
	echo 'Set PK_Distro = 20'
	. /usr/pluto/bin/Config_Ops.sh; ConfSet PK_Distro 20
 
	## SOURCES.LIST
	echo 'save backup "sources.list" to' $sourceBackup '...'
	cp /etc/apt/sources.list $sourceBackup
	echo 'Set package entries from beta2 to unstable' 
	sed -i "s/beta2/unstable/g" /etc/apt/sources.list
 
	echo '### RE-ENABLE THIRD PARTY SOURCES'
	sed -i "s/# deb/deb/g" /etc/apt/sources.list

	## DIFFERENCES BETWEEN CORE AND MD
	if [[ "$PK_Device" -eq "1" ]]; then

		echo '### '$isCore': FORCE UPDATE & FORCE INSTALL OF xorg xserver-xorg pluto-dcerouter'
		apt-get update
		apt-get -y --force-yes install xorg xserver-xorg
		apt-get -y --force-yes install pluto-dcerouter

	else
		echo '### '$isCore': FORCE INSTALL OF pluto-confirm-dependencies' 
		apt-get -y --force-yes install pluto-confirm-dependencies
	fi


	echo '### CHECKING INSTALLATION ... isCore=' $PK_Device
	/usr/pluto/bin/ConfirmDependencies -h localhost -u root -o 20 -f -d $PK_Device install > /usr/pluto/bin/verifyinstallation.sh && chmod +x /usr/pluto/bin/verifyinstallation.sh && /usr/pluto/bin/verifyinstallation.sh
	apt-get -y dist-upgrade
 
	echo '### 12.04 INSTALLED? ... reboot and run through AVWizard and say "Hello" to Sarah'
 
else
  echo "### LinuxMCE upgrade canceled!";
fi
