#!/bin/ksh
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)patchchk	1.1	09/01/20 SMI"

. /lib/svc/share/smf_include.sh

#
# We need to define a pattern because we don't know how
# the FMRI is going to be specified.
# Eg. "reboot -- -s" & "svcadm milestone -d single-user"
# do not use the same form.
# 
SU_MILESTONE="milestone/single-user:default"

SU_PATCHING="/_PATCHING_SINGLE_USER"

if [ -z "$SMF_FMRI" ]; then
	echo "this script can only be invoked by smf(5)"	
	exit $SMF_EXIT_ERR_NOSMF
fi

if [ ! -f "$SU_PATCHING" ]; then
	exit $SMF_EXIT_OK 
fi

#
# If /_PATCHING_SINGLE_USER exists and the system is booting
# in single-user mode then enable milestone/patching in order
# to launch the services related to the Automated Patching
# Tools.
#

milestone_ovr=`/usr/bin/svcprop -p options_ovr/milestone system/svc/restarter:default 2>/dev/null` 
milestone_ovr=${milestone_ovr##svc:/}
if [ ! -z "$milestone_ovr" -a  "$milestone_ovr" = "$SU_MILESTONE" ]; then
	echo "Enabling milestone/patching"
	/usr/sbin/svcadm enable -rt milestone/patching && exit $SMF_EXIT_OK
	echo "svcadm failed with exit code $?"; exit $SMF_EXIT_ERR_FATAL
fi

milestone=`/usr/bin/svcprop -p options/milestone system/svc/restarter:default 2>/dev/null` 
milestone=${milestone##svc:/}
if [ ! -z "$milestone" -a  "$milestone" = "$SU_MILESTONE" ]; then
	echo "Default milestone is single-user; Enabling milestone/patching"
	/usr/sbin/svcadm enable -rt milestone/patching && exit $SMF_EXIT_OK
	echo "svcadm failed with exit code $?"; exit $SMF_EXIT_ERR_FATAL
fi

exit $SMF_EXIT_OK 
