#! /bin/sh
# Apply SLE10-SP1 from a patch CD, with all appropriate workarounds
# v 0.2

# Verify that we are root
if [ `id -u` -ne 0 ]; then
    echo "The script must be run as root."
    exit 1
fi

set -o errexit

progress() {
    echo
    echo "* " "$@"
    echo
}

# TODO write log
# TODO fill in bug numbers for future reference

# $1, if given, is the mounted CD/DVD
if [ -n "$1" ]; then
    CDDIR="$1"
else
    # We assume that this script is run from the SP1 CD/DVD,
    SCRIPTDIR="`dirname $0`"
    # located in its scripts subdir
    CDDIR="${SCRIPTDIR%/scripts}"
fi
cd "${CDDIR}/suse"

# If you update from SLES10 GA to SLES10 SP1, the YaST2 software
# stack needs to be updated before:
progress "Updating the package management packages"
rpm -Uhv \
 */libzypp-2.15.*.rpm \
 */libzypp-zmd-backend-*.rpm \
 */liby2util-2.13.*.rpm \
 */yast2-core-2.13.*.rpm \
 */yast2-pkg-bindings-2.13.*.rpm \
 */yast2-perl-bindings-2.13.*.rpm \
 */yast2-qt-2.13.*.rpm \
 */yast2-2.13.*.rpm \
 noarch/yast2-online-update-*.rpm \
 */yast2-country-2.13.*.rpm \
 */yast2-ncurses-2.13.*.rpm \
 */yast2-packager-2.13.*.rpm \
 */yast2-slp-2.13.*.rpm \
 noarch/yast2-installation-2.13.*.rpm \
 noarch/yast2-inetd-2.13.*.rpm \
 */zmd-*.rpm \
 */zypper-*.rpm \
 */rug-*.rpm \
 noarch/suseRegister-1.*.rpm

# Leave the suse directory and unmount the media.
progress "Unmounting the CD/DVD"
cd /
umount "$CDDIR"

# Make sure zmd is running
progress "Check for running ZMD"
/etc/init.d/novell-zmd status > /dev/null 2>&1 || /etc/init.d/novell-zmd start

# Make sure zmd has finished reading his database, run "rug sl"
# until there are no 'pending' status anymore.
progress "Waiting for ZMD service refresh"
echo -n ". "
sleep 5
while LANG=C rug --terse service-list | grep -q '|Pending|'; do
    echo -n ". "
    sleep 5
done
echo ""

#>  - Start YaST2
#>  - Select "YaST -> Software -> Installation Source"
#>    * Disable the SLES10 GA source and SLES10 update channel
SD=/usr/lib/suseRegister/bin/zypp_sd
#$SD --loose-auth --loose-query BUT_WHAT?
#TODO

# TODO: here the Add-ons should add their SP1 repositories too

#>  - Select "YaST -> Software -> Patch CD Update"
progress "Applying SP1 updates"
echo "( Only a few patches are selected at the beginning, YaST2 will"
echo "  restart later and update the rest. )"

/sbin/yast2 online_update '.cd_default'


# Now Patch CD Update registered the CD so zypper will wind the product
#>  - Run as root: "zypper remove --no-confirm --type=product SUSE_SLES"
#>  - Run as root: "zypper install --no-confirm --type=product SUSE_SLES_SP1"
progress "Fixing up product marker"
zypper remove --no-confirm --type=product SUSE_SLES
zypper install --no-confirm --type=product SUSE_SLES_SP1


#>  - Run "YaST -> Software -> Novell Configuration Center" to
#>    register SLES10 SP1
# prepare auto registration
touch /var/lib/suseRegister/registerOnBoot


# have ZMD realize the product change
progress "Refreshing ZMD database"
touch /var/lib/rpm/Packages
rug refresh

progress "Done"
echo "Now reboot the machine to"
echo "- load the updated kernel"
echo "- have suseRegister add update repositories"
