#! /bin/bash


#Author:        Robin Shinkle
#Author:	Jared Ottley
#Author:	James Hammond
#date:          1/13/2006

###Change log
#
# 1/11/2006 by JRO
#  -Added error paths, command line parameters and beta stuff
#  ## ALL BETA RELATED CODE MUST BE REMOVED BEFORE RELEASING THIS TO THE PUBLIC ##
#  -Added path the prepatch logic
#  -Incorporated James Hammond's idea for verifying if the correct service is present
#  -Cleaned up verbage and more comments, added limits to activation code and prepatch patch errors, extended some error codes
#
# 1/12/2006 by JRO
#  -Added additional kernel patch check
#
# 1/13/2006 by JRO
#  -source prepath script
#  -sanitize
#
###

###############################################################################
# If you see this in your browser and you want to download or save this file, #
# this file, try right-clicking the link that brought you here then select    #
# the "Save As" or "Save Link Target As" option.                              #
###############################################################################

# Script must be run as root

if [ "`id -u`" -ne "0" ]; then
        echo "Error: To run this script, you must be logged in as user root."
        echo ""
        exit 1
fi

##
# Global Variables
##

CODE=""		# activation code
EMAIL=""	# user's email address
PREPATCH=0	# Run the prepatch?

##
# Parameters
##


while [ $# -gt 0 ]; do
	case $1 in
		-act)
			CODE=$2
			shift
		;;
		-email)
			EMAIL=$2
			shift
		;;
		-prepatch)
			PREPATCH=1
		;;
		*)
			echo Unknown parameter $1.
			echo Usage: ./update.sh "[-act activation_code] [-email emailaddress] [-prepatch]"
			exit 1
			;;
	esac
	shift
done

clear

BOLD_ON=`tput bold`
	ATTRIBUTES_OFF=`tput sgr0`

	echo ""
	echo "${BOLD_ON}Novell Open Enterprise Server SP2 Update${ATTRIBUTES_OFF}"
	echo ""
	echo "This script attempts to bring an initial version or SP1 version of an OES server"
	echo "to OES SP2."
	echo ""
	echo -n "${BOLD_ON}Are you absolutely sure you want to do this?${ATTRIBUTES_OFF} [y/N]: "
	read ANSWER
	case "${ANSWER}" in
		[yY] | [yY][eE][sS])
			### CONTINUE ###
			;;
		*)
			echo ".......... ABORTING .........."
			echo ""
			exit
			;;
	esac

##
# Main
##

#User input, if not passed as parameters
while [ "$CODE" == "" ]; do
	read -p "Please enter your activation code: " CODE
done

while [ "$EMAIL" == "" ]; do
	read -p "Please enter your email address: " EMAIL
done

#Check to see if service exist
NULL=`rug sl | grep Novell_Update_Server`


#Add Service, if it is not currently there
if [ $? -eq 1 ]; then
	echo "Adding Novell Update Server" 
	rug sa https://update.novell.com/data
fi

if [ $? -eq 1 ]; then
	echo "Unable to add service.  The Novell Update Server may not be reachable."
	exit 1
fi

# Activate service
echo "Activating Novell Update Service"
rug act -s Novell_Update_Server $CODE $EMAIL

#if unable to activate, prompt for code again
if [ $? -eq 1 ]; then	
	COUNT=0
	SUCCESS=0
	while [ $SUCCESS -eq 0 ]; do
		read -p "Please enter your activation code: " CODE

		rug act -s Novell_Update_Server $CODE $EMAIL

		if [ $? -eq 1 ]; then
			echo "The activation code you entered is invalid."
			if [ $COUNT -lt 2 ]; then
				let COUNT=COUNT+1
			else
				echo "Three attempts. Sorry."
				exit 1
			fi
		else
			SUCCESS=1
		fi
	done
fi

# Subscribe to channel
echo "Subscribing to the OES channel."
rug sub oes

if [ $? -eq 1 ]; then
	echo "Unable to subscribe to the update channel."
	exit 1
fi

#Run prepatch, if passed as parameter
if [ $PREPATCH -eq 1 ]; then
	PPPATH=""
	PPPATHexists=0
	COUNT=0

	while [ $PPPATHexists -ne 1 ]; do
		read -p "Please enter the path to the oessp2prepatch.sh script: " PPPATH

		if [ ! -e $PPPATH/oessp2prepatch.sh ]; then
			if [ $COUNT -lt 2 ]; then
				echo "Unable to find oessp2prepatch.sh"
				let COUNT=COUNT+1
			else
				echo "Three tries. Sorry!"
				exit 1
			fi
		else
			PPPATHexists=1
		fi
	done

	. $PPPATH/oessp2prepatch.sh --auto

	if [ $? -eq 1 ]; then
		echo "There was an error running the prepatch script."
		echo "Please try to run the prepatch script manually (ie, `./oessp2prepatch.sh`)"
		echo "Then run this script again"
		exit 1
	fi
	clear

fi

#Check on kernel patch
KERNELPATCH=0
if [ -e /var/lib/YaST2/you/installed/patch-10727 ]; then
	KERNELPATCH=1
fi

# Install all of the patches in the channel
echo "Updating to OES SP2"

rug pin --entire-channel oes

if [ $? -ne 1 ]; then
	if [ $KERNELPATCH -eq 0 ]  && [ -e /var/lib/YaST2/you/installed/patch-10727 ]; then
		# Prompt for reboot
		echo "Your server has been updated with a new Kernel. You need to reboot for this change to take effect."
	else
		echo ""
		echo "Update complete"
	fi
else
	echo "There was an error updating to SP2.  Please try again."
	exit 1
fi
