#!/bin/sh
#
# Copyright 1999 Sun Microsystems, Inc. All rights reserved.
#
# @(#)ss_patch.sh	1.8 99/10/27 Sun Microsystems, Inc.
#
PATH="/usr/bin:/sbin:/usr/sbin"
SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
export PATH
umask 022

set_num=31	# SI18N_ADMIN_SCRIPTS
LS()		# localize a string
{
	msg_num=$1; shift
	if [ -x ${SS_LIBDIR}/catgets ]
	then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
	else echo "$*"
	fi
}

print_usage()
{
	ustr=`LS 20 "Usage:"`
# LOCO: string of spaces having same length as "Usage:"
	uspc=`LS 21 "      "`
	echo "$ustr ssadm patch install [noreboot] < patch.tar.Z"
	echo "$uspc ssadm patch backout [noreboot] patchid"
	echo "$uspc ssadm patch list"
}

#
#  Uncompress and Un-tar a file from stdin.  Takes one patch in a compressed
#  tar file.  No longer requires that patches be in a "Patches" directory.
#  Install each of the patches in this directory concatonating the results 
#  to /var/log/patch.log.
#

installspfpatch()
{
	# remove old patch files
	rm -rf /tmp/patch.tar*
	rm -rf /tmp/Patches

	# read compressed tar file
	echo `LS 22 "Copying compressed patch to screen..."`

	cd /tmp
	mkdir Patches
	cd Patches

	cat > patch.tar.Z

	# uncompress and untar
	echo `LS 23 "Uncompressing and untarring patch..."`
	
	uncompress patch.tar

	if [ ! $? = 0 ]; then
	    echo `LS 24 "Error: Patch was not compressed!"`
	    echo
	    print_usage
	    cd /tmp
	    rm -r /tmp/Patches 
	    exit 2
	fi

	tar xf patch.tar

	if [ ! $? = 0 ]; then
	    echo `LS 25 "Error: Patch was not in a tar file!"`
	    echo
	    print_usage
	    cd /tmp
	    rm -r /tmp/Patches 
	    exit 3
	fi

	for Patchid in * ; do

	    if [ -d $Patchid ] ; then

		echo `LS 26 "Installing patch"` $Patchid...

		# install the patch
		patchadd $Patchid >> /var/log/patch.log 2>&1
		status=$?

		if [ ! $status = 0 ] ; then
		    errmsg=`LS 27 "Error"` 
		    echo $errmsg $status `LS 28 "occured while installing"` $Patchid.
		fi

	    fi
	
	done

	# clean up
	cd /tmp
	rm -r /tmp/Patches
	sync

}

#
#  Backout the patch named in $Patchid by executing the stored
#  backout script in /var/sadm/patch/$Patchid.
#

backoutpatch ()
{
	if [ "$Patchid" = "" ] ; then
		print_usage
		exit 1
        fi

	cd /var/sadm/patch

	if [ -d "$Patchid" ] ; then
		echo `LS 29 "Removing patch"` $Patchid...
		# Workaround to make remote backout work.
		/bin/sh -c newgrp other << +EOF+ >> /var/log/patch.log 2>&1
		patchrm $Patchid
		exit
+EOF+
	else
		echo `LS 30 "Patch not installed:"`  $Patchid
		exit 1
	fi
}

#
#    Main Routine
#

Reboot="true"
Patchid=""

case $1 in
install|Install|INSTALL)
	case $2 in
	noreboot|Noreboot|NOREBOOT)
		Reboot="false"
		;;
	*)
		Reboot="true" 
		;;
	esac
	installspfpatch
	;;

backout|Backout|BACKOUT)
	case $2 in
	noreboot|Noreboot|NOREBOOT)
		Reboot="false" 
		Patchid=$3
		backoutpatch
		;;
	*)
		Reboot="true"
		Patchid=$2
		backoutpatch
		;;
	esac
	;;

list|List|LIST)
	ls -lt /var/sadm/patch
	exit 0
	;;

*)
	print_usage
	exit 0
	;;

esac

#  Reboot if flag is set

if [ "$Reboot" = "true" ] ; then
	echo `LS 31 "Rebooting screen for changes to take effect."`
	sync
	sync
	sync
	init 6
else
	echo `LS 32 "Screen must be rebooted for changes to take effect."`
fi
