#!/bin/sh

# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)patch-finish.sh 1.1     07/07/11 SMI"

. /lib/svc/share/smf_include.sh

DELETE_LIST=/var/sadm/patch/.delete_list

delete ()
{
	if [ -f $DELETE_LIST ]; then

		# Found the list of files to delete
		while read path ; do

			# Make sure path isn't registered in contents database.
			# If its registered, then don't delete it and log a
			# warning.  Otherwise, go ahead and delete it.
			# Path is a full absolute path, so we can grep it from
			# from the contents database.  Path might be a file
			# or a link, so grep for both types.
			if /usr/bin/grep "^$path[ =]" \
			    /var/sadm/install/contents > /dev/null ; then
				# Path exists in the contents database.
				echo "Warning: <$path> is a registered file," \
				    "will not remove <$path> from the system."
			else
				# Delete the named file path.  The deletes
				# file does not have directories in it, so
				# we don't need to handle cases where path
				# is a dir.
				/usr/bin/rm -f $path
			fi

		done < $DELETE_LIST

		# Remove delete list file.
		/usr/bin/rm -f $DELETE_LIST
	fi
}

case "$1" in
'delete')
	# Cleanup up the files that need to be deleted
	delete

	# Disable this service so that it doesn't run again.
	/usr/sbin/svcadm disable system/patch-finish:delete
	if [ $? -ne 0 ] ; then
		exit $SMF_EXIT_ERR_CONFIG
	fi
	;;
*)
	echo "Usage: $0 { delete }"
	exit $SMF_EXIT_ERR_CONFIG
	;;
esac


exit $SMF_EXIT_OK
