# checkinstall script to control a patch installation.
# directory format options.
#
#pragma ident	"@(#)checkinstall	1.15	13/01/30 SMI"
#
# Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
#

# Definitions for override safety mechanism
PATCH_OVERRIDE_LIB="${PKG_INSTALL_ROOT}/usr/lib/patch/patch_override_lib"

# Check if we messaging is ready. Returns 0 on success, 1 on failure
# On successful run it leaves MSG_FILENAME variable defined
# 
# Usage:
#
# check_patch_script_messaging
#
check_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchadd_msg_file_${SUNW_PATCHID}"

	if [ -w "${MSG_FILENAME}" ] ; then
		unset TMP_DIRNAME
		return 0
	else
		unset TMP_DIRNAME
		unset MSG_FILENAME
		return 1
	fi
}

# This function prepares a message to be printed to stdout towards the end
# of postpatch execution. If the message file is not ready, it at least tries
# immediate output to stdout which is currently know to work only in patch
# level scripts.
#
# Usage:
#
# print_patch_message "text_of_message_to_be_printed_out"
#
print_patch_message()
{
	if check_patch_script_messaging; then
		echo "$*" >> "${MSG_FILENAME}"
	else
		echo "$*"
	fi

	return 0
}

# script in a fucntion so that it can be overridden if needed - generic
checkinstall_deflt()
{
	PATH=/usr/sadm/bin:$PATH

	NOVERS_MSG="PaTcH_MsG 8 Version $VERSION of $PKG is not installed on this system."
	ALRDY_MSG="PaTcH_MsG 2 Patch number $SUNW_PATCHID is already applied."
	TEMP_MSG="PaTcH_MsG 23 Patch number $SUNW_PATCHID cannot be applied until all \
	restricted patches are backed out."
	IDR_MSG="ERROR: IDR $SUNW_PATCHID cannot be applied until all \
	Interim Relief/Diagnostics are backed out."
	IDR_MSG2="ERROR: Restricted patch $SUNW_PATCHID cannot be applied until all \
	Interim Relief/Diagnostics are backed out."

	# Read the provided environment from what may have been a request script
	. $1

	#
	# Confirm that the intended version is installed on the system.
	#
	if [ "${UPDATE}" != "yes" ]; then
		echo "$NOVERS_MSG"
		exit 3
	fi

	#
	# Confirm that this patch hasn't already been applied and
	# that no other mix-ups have occurred involving patch versions and
	# the like.
	#
	Skip=0
	active_base=`echo $SUNW_PATCHID | nawk '
		{ print substr($0, 1, match($0, "-")-1) } '`
	active_inst=`echo $SUNW_PATCHID | nawk '
		{ print substr($0, match($0, "-")+1) } '`

	# Is this an IDR?
	if echo $active_base | egrep -s "IDR"; then
		is_IDR="true"
		# All IDR patches are backoutable
		echo "PATCH_NO_UNDO=" >> $1
	else
		is_IDR="false"
	fi

	# Is this a restricted patch?
	if echo $active_base | egrep -s "R"; then
		is_restricted="true"
		# All restricted patches are backoutable
		echo "PATCH_NO_UNDO=" >> $1
	else
		is_restricted="false"
	fi

	for patchappl in ${PATCHLIST}; do
		# Is this an ordinary patch applying over a restricted patch?
		if [ $is_restricted = "false" ]; then
			if echo $patchappl | egrep -s "R"; then
				echo "$TEMP_MSG"
				exit 3;
			fi
		fi

		# Is this an IDR installing on top of another IDR or restricted patch?
		if [ "$is_IDR" = "true" ]; then
			if echo $patchappl | egrep -s "IDR"; then
				echo "$IDR_MSG"
				exit 3;
			fi
		else
			# Is this a normal patch installing on top an IDR?
			if [ "$is_IDR" = "false" ]; then
				if echo $patchappl | egrep -s "IDR"; then
					if [ "$is_restricted" = "true" ]; then
						echo "$IDR_MSG2"
						exit 3;
					else
						echo "$IDR_MSG"
						exit 3;
					fi
				fi
			fi
		fi
			
		# Is there a newer version of this patch?
		appl_base=`echo $patchappl | nawk '
			{ print substr($0, 1, match($0, "-")-1) } '`
		if [ $appl_base = $active_base ]; then
			appl_inst=`echo $patchappl | nawk '
				{ print substr($0, match($0, "-")+1) } '`
			result=`expr $appl_inst \> $active_inst`
			if [ $result -eq 1 ]; then
				echo "PaTcH_MsG 1 Patch number $SUNW_PATCHID is superceded by the already applied $patchappl."
				exit 3
			elif [ $appl_inst = $active_inst ]; then
				# Not newer, it's the same
				if [ "$PATCH_UNCONDITIONAL" = "true" ]; then
					if [ -d $PKGSAV/$SUNW_PATCHID ]; then
						echo "PATCH_NO_UNDO=true" >> $1
					fi
				else
					echo "$ALRDY_MSG"
					exit 3;
				fi
			fi
		fi
	done

	# Construct a list of applied patches in order
	echo "PATCHLIST=${PATCHLIST} $SUNW_PATCHID" >> $1

	#
	# Construct the complete list of patches this one obsoletes
	#
	ACTIVE_OBSOLETES=$SUNW_OBSOLETES

	if [ -n "$SUNW_OBSOLETES" ]; then
		# Clear the parameter since it has already been used.
		echo "SUNW_OBSOLETES=" >> $1

		# Pass it's value on to the preinstall under another name
		echo "ACTIVE_OBSOLETES=$ACTIVE_OBSOLETES" >> $1
	fi

	#
	# Use the first found pkginfo file since all pkginfo files MUST contain
	# the same patch meta data. This is done since we can never be guaranteed of
	# where the checkinstall script is located. This will work for all Solaris
	# releases.
	#

	for pkginfo in $INST_DATADIR/*/pkginfo; do
		tmpRequire=`nawk -F= ' $1 ~ /REQUIR/ { print $2 } ' $pkginfo`
		tmpIncompat=`nawk -F= ' $1 ~ /INCOMPAT/ { print $2 } ' $pkginfo`
		break
	done

	#
	# Construct PATCH_INFO line for this package.
	#

	if [ -n "$tmpRequire" ] && [ -n "$tmpIncompat" ]
	then
		echo "PATCH_INFO_$SUNW_PATCHID=Installed: `date` From: `uname -n` \
		  Obsoletes: $ACTIVE_OBSOLETES Requires: $tmpRequire \
		  Incompatibles: $tmpIncompat" >> $1
	elif [ -n "$tmpRequire" ]
	then
		echo "PATCH_INFO_$SUNW_PATCHID=Installed: `date` From: `uname -n` \
		  Obsoletes: $ACTIVE_OBSOLETES Requires: $tmpRequire Incompatibles: " >> $1
	elif [ -n "$tmpIncompat" ]
	then
		echo "PATCH_INFO_$SUNW_PATCHID=Installed: `date` From: `uname -n` \
		  Obsoletes: $ACTIVE_OBSOLETES Requires: Incompatibles: $tmpIncompat" >> $1
	else
		echo "PATCH_INFO_$SUNW_PATCHID=Installed: `date` From: `uname -n` \
		  Obsoletes: $ACTIVE_OBSOLETES Requires: Incompatibles: " >> $1
	fi

	# There can be three conditions that exist for this or an older checkinstall
	# script, listed in order of precedence:
	#
	# 1. The checkinstall script is located in $INST_DATADIR/<pkgabbrev>.[<ext>]
	# 2. The checkinstall has been moved to /tmp
	# 3. An old checkinstall script must handle both 1 and 2.

	# Check to see if the script has been moved to /tmp by pkginstall.

	EXECDIR=`dirname $0 | grep "^/tmp/install"`

	if [ -n "$SUNW_PKG_DIR" ]; then
		SCRIPTS_DIR="$SUNW_PKG_DIR/install"
	elif [ -z "$EXECDIR" ]; then
		SCRIPTS_DIR=`dirname $0`
	else
		# The script has been moved to /tmp. This should never happen since S9
		# pkginstall only moves the checkinstall script if it detects that it is
		# not a patch installation.

		EXT=`echo $ARCH | sed \
			-e 's/all//' \
			-e 's/sparc\.sun4/\./' \
			-e 's/sparc//' \
			-e 's/sparcv9//' \
			-e 's/i386\.i86pc/\.i/' \
			-e 's/i386//'`
		SCRIPTS_DIR="$INST_DATADIR/$PKGINST$EXT/install"
	fi

	echo "SCRIPTS_DIR=$SCRIPTS_DIR" >> $1
}

# script in a fucntion so that it can be overridden if needed - particular
checkinstall_merge()
{
	# If additional operations are required for this package, place
	# those package-specific commands here.

	#XXXSpecial_CommandsXXX#
#!/bin/sh

INSTALL_DATA=${PKG_INSTALL_ROOT}/${BASEDIR}/usr/lib/webconsole/.install_data

RELOCATE=""
if [ -n "${PKG_INSTALL_ROOT}" ]; then
    RELOCATE="-R ${PKG_INSTALL_ROOT}"
fi

## Compare the version of the pkg we are installing against the version
## of the pkg that may already be installed and abort installation
## if a downgrade is detected.  This check is necessary only during
## Solaris install.  When installed as an unbundled, the unbundled installer
## makes the check and won't even attempt to install the pkg if a downgrade
## is detected.
if [ -f ${INSTALL_DATA}/${PKG}.pkginfo ]; then

    # Get the version of the pkg installed on the system.
    # Map to a number we can do a numerical comparison on.
    # Minor and micro default to 0 if not specified.
    #
    sysVersion=`env LANG=C LC_ALL=C \
	pkgparam -f ${INSTALL_DATA}/${PKG}.pkginfo VERSION \
	| awk -F, '{print $1}'`
    major=`echo $sysVersion | awk -F'.' '{print $1}'`
    minor=`echo $sysVersion | awk -F'.' '{print $2}'`
    micro=`echo $sysVersion | awk -F'.' '{print $3}'`
    if [ ! -n "$minor" ]; then
        minor="0"
    fi
    if [ ! -n "$micro" ]; then
        micro="0"
    fi
    weHave="${major}${minor}${micro}"

    # Get the version of this pkg being installed.
    # Map to a number we can do a numerical comparison on.
    # Minor and micro default to 0 if not specified.
    #
    mediaVersion=`echo $VERSION | awk -F',' '{print $1}'`
    major=`echo $mediaVersion | awk -F'.' '{print $1}'`
    minor=`echo $mediaVersion | awk -F'.' '{print $2}'`
    micro=`echo $mediaVersion | awk -F'.' '{print $3}'`
    if [ ! -n "$minor" ]; then
        minor="0"
    fi
    if [ ! -n "$micro" ]; then
        micro="0"
    fi
    weWant="${major}${minor}${micro}"

    # If downgrade, abort install
    if [ $weHave -gt $weWant ]; then
	echo "Installation of $PKG version $mediaVersion stopped because"
	echo "version $sysVersion is already installed and will not be downgraded."
	exit 1
    fi
fi


# If a console pkg exists, determine its version so we know what
# version we are upgrading from.  Export this version to the pkg'ing
# environment via the UPGRADED_VERSION environment variable
#
CONSOLE_PKG=SUNWmcon
pkginfo=`pkginfo -a $ARCH ${RELOCATE} ${CONSOLE_PKG} 2>/dev/null`
if [ $? -eq 0 ]; then
    pkginst=`echo $pkginfo | awk '{print $2}'`
    version=`env LANG=C LC_ALL=C pkgparam ${RELOCATE} ${pkginst} VERSION \
	| awk -F, '{print $1}'`
    major=`echo $version | awk -F'.' '{print $1}'`
    minor=`echo $version | awk -F'.' '{print $2}'`
    echo "UPGRADED_VERSION=${major}.${minor}" >> $1
fi

return 0
}

# load the override lib if it exists
if [ -f "${PATCH_OVERRIDE_LIB}" -a -r "${PATCH_OVERRIDE_LIB}" ] ; then
   . "${PATCH_OVERRIDE_LIB}"
fi

# execute the script functions
checkinstall_deflt "$@"
checkinstall_merge "$@"

exit 0

