#!/bin/bash

if [ $UID -ne 0 ]; then
	echo "Only root is allowed to run $0."
	exit 1
fi

if [ ! -f /etc/SuSE-release ]; then
	echo "You do not appear to be running SLES10.  Aborting."
	exit 1
fi

grep "SUSE Linux Enterprise Server 10" /etc/SuSE-release > /dev/null
if [ $? -ne 0 ]; then
	echo "You do not appear to be running SLES10.  Aborting."
	exit 1
fi

grep x86_64 /etc/SuSE-release > /dev/null
if [ $? -eq 0 ]; then
	ARCH1=x86_64
	ARCH2=x86_64
else
	grep i586 /etc/SuSE-release > /dev/null
	if [ $? -eq 0 ]; then
		ARCH1=i386
		ARCH2=i586
	else
		echo "Unknown architecture!  Aborting."
		exit 1
	fi
fi

echo "Starting upgrade of machine `uname -n` from SLES10 to SLES10 SP1." | tee /root/sles10sp1update.log

echo "Using detected architecture: $ARCH1/$ARCH2" | tee -a /root/sles10sp1update.log

while [[ 1 ]]; do
	SDK_ANS=""
	echo -n "Do you want to add the SLE SDK SP1? (yes/[no]) "
	read -e -r SDK_ANS
	if [[ -z "${SDK_ANS}" || "${SDK_ANS}" == [Nn][Oo] ]]; then
		SDK_ANS="no"
		echo "Skipping SLE SDK SP1." | tee -a /root/sles10sp1update.log
		break
	fi
	if [[ "${SDK_ANS}" == [Yy][Ee][Ss] ]]; then
		SDK_ANS="yes"
		echo "Adding SLE SDK SP1." | tee -a /root/sles10sp1update.log
		break
	fi
	echo 'Please answer "yes", "no", or press return to accept the default ("no").'
done
while [[ 1 ]]; do
	UCS_ANS=""
	echo -n "Do you want to add the Unsupported UCS Internal SLES10 repository? (yes/[no]) "
	read -e -r UCS_ANS
	if [[ -z "${UCS_ANS}" || "${UCS_ANS}" == [Nn][Oo] ]]; then
		UCS_ANS="no"
		echo "Skipping Unsupported UCS Internal SLES10 repository." | tee -a /root/sles10sp1update.log
		break
	fi
	if [[ "${UCS_ANS}" == [Yy][Ee][Ss] ]]; then
		UCS_ANS="yes"
		echo "Adding Unsupported UCS Internal SLES10 repository." | tee -a /root/sles10sp1update.log
		break
	fi
	echo 'Please answer "yes", "no", or press return to accept the default ("no").'
done

rug --terse sl | grep "No services found" > /dev/null
if [ $? -ne 0 ]; then
	echo "Removing all existing update sources..." | tee -a /root/sles10sp1update.log
	for i in `rug --terse sl | cut -d'|' -f1 | grep -v '^$'`; do
		rug sd 1
	done
fi

echo "Adding local SLES10 related repositories..." | tee -a /root/sles10sp1update.log
rug sa --type=zypp http://www-uxsup.csx.cam.ac.uk/suse-install/SLES10-${ARCH1} SLES10 2>&1 | tee -a /root/sles10sp1update.log
rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/SLES10-Updates/sles-10-${ARCH2}/ SLES10-Updates 2>&1 | tee -a /root/sles10sp1update.log
rug sub -a 2>&1 | tee -a /root/sles10sp1update.log

echo "Installing move-to-sles10-sp1 patch... (you need to say \"y\" to the licenses)" | tee -a /root/sles10sp1update.log
rug in -y -t patch move-to-sles10-sp1 2>&1

echo "Switching repositories to SLES10 SP1 ones..." | tee -a /root/sles10sp1update.log
rug sd SLES10-Updates 2>&1 | tee -a /root/sles10sp1update.log
rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/SLES10-SP1-Online/sles-10-${ARCH2}/ SLES10-SP1-Online 2>&1 | tee -a /root/sles10sp1update.log
rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/SLES10-SP1-Updates/sles-10-${ARCH2}/ SLES10-SP1-Updates 2>&1 | tee -a /root/sles10sp1update.log
rug sub -a 2>&1 | tee -a /root/sles10sp1update.log

echo "Installing latest package management stack needed for the update..." | tee -a /root/sles10sp1update.log
rug in -y -t patch slesp1o-liby2util-devel 2>&1 | tee -a /root/sles10sp1update.log

echo "Restarting the update service..." | tee -a /root/sles10sp1update.log
/etc/init.d/novell-zmd stop 2>&1 | tee -a /root/sles10sp1update.log
sleep 60
pkill zmd 2>&1 | tee -a /root/sles10sp1update.log
sleep 60
pkill -9 zmd 2>&1 | tee -a /root/sles10sp1update.log
sleep 60
/etc/init.d/novell-zmd stop 2>&1 | tee -a /root/sles10sp1update.log
sleep 60
/etc/init.d/novell-zmd start 2>&1 | tee -a /root/sles10sp1update.log
sleep 60

echo "Switching product to SLES10 SP1..." | tee -a /root/sles10sp1update.log
rug in -y -t patch product-sles10-sp1 2>&1 | tee -a /root/sles10sp1update.log

echo "Refreshing update repositories..." | tee -a /root/sles10sp1update.log
rug ref 2>&1 | tee -a /root/sles10sp1update.log

echo "Applying all needed updates... (this can take a few hours!)" | tee -a /root/sles10sp1update.log
rug up -y 2>&1 | tee -a /root/sles10sp1update.log

echo "Removing suseRegister nagging service... (you do not need it)" | tee -a /root/sles10sp1update.log
rpm -e suseRegister yast2-registration switch-update-server 2>&1 | tee -a /root/sles10sp1update.log

if [ "${SDK_ANS}" == "yes" ]; then
	echo "Adding local SLE10 SDK SP1 related repositories..." | tee -a /root/sles10sp1update.log
	rug sa --type=zypp http://www-uxsup.csx.cam.ac.uk/suse-install/SLE10-SDK-${ARCH1} SLE10-SDK 2>&1 | tee -a /root/sles10sp1update.log
	rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/SLE10-SDK-SP1-Online/sles-10-${ARCH2}/ SLE10-SDK-SP1-Online 2>&1 | tee -a /root/sles10sp1update.log
	rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/SLE10-SDK-SP1-Updates/sles-10-${ARCH2}/ SLE10-SDK-SP1-Updates 2>&1 | tee -a /root/sles10sp1update.log
	rug sub -a 2>&1 | tee -a /root/sles10sp1update.log

	echo "Installing product-sdk10-sp1 patch..." | tee -a /root/sles10sp1update.log
	rug in -y -t patch product-sdk10-sp1 2>&1 | tee -a /root/sles10sp1update.log

fi

if [ "${UCS_ANS}" == "yes" ]; then
	echo "Adding local Unsupported UCS Internal SLES10 repository" | tee -a /root/sles10sp1update.log
	rug sa --type=zypp http://sle-updates.csx.cam.ac.uk/UNSUPPORTED-UCSInternal-SLES10/sles-10-${ARCH2}/ UCSInternal 2>&1 | tee -a /root/sles10sp1update.log
	rug sub -a 2>&1 | tee -a /root/sles10sp1update.log
fi

if [[ "${SDK_ANS}" == "yes" || "${UCS_ANS}" == "yes" ]]; then
	echo "Refreshing update repositories..." | tee -a /root/sles10sp1update.log
	rug ref 2>&1 | tee -a /root/sles10sp1update.log

	echo "Applying all needed updates..." | tee -a /root/sles10sp1update.log
	rug up -y 2>&1 | tee -a /root/sles10sp1update.log
fi

cat <<EOD | tee -a /root/sles10sp1update.log

Done!  You have now installed SLES10 SP1 you just have to activate it!

It is a good idea to check /boot/grub/menu.lst against what actually exists in
/boot kernel wise and fix any inconsistencies should there be any (all was ok
in my case).

Then reboot the system to activate the new kernel and when the system comes
back you will be running SLES10 SP1.

Finally, a log of everything that was done has been placed in
/root/sles10sp1update.log.  You should review the log and look for any problems
and in particular watch out for custom RPMs being uninstalled which you will
want/need to install again.

EOD

