#!/usr/bin/ksh

thismaj=4
thismin=0
thismp=0
thisrp=2

PSTAMPCMD="s/.*VERITAS.*-\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)-.*/\1 \2 \3 \4/"

terminate_patch()
{
	echo ""
	echo "Patch Terminating ..."
	echo ""
	exit 3
}

no_pstamp()
{
	echo "This patch can not be installed, the VRTSvxfs package does not"
	echo "seem to have any PSTAMP."
	terminate_patch
}

invalid_pstamp()
{
	echo "This patch can not be installed, because the PSTAMP of"
	echo "VRTSvxfs package does not contain valid software"
	echo "revision number."
	terminate_patch
}

imcompatible()
{
	echo "This patch can not be installed, because it is not compatible with"
	echo "VRTSvxfs package that is currently installed."
	terminate_patch
}

will_downgrade()
{
	echo "This patch can not be installed, because installing this patch"
	echo "will downgrade the patch level of VRTSvxfs currtenly installed."
	terminate_patch
}

check_pstamp()
{
	typeset pstamp
	typeset maj min mp rp

	pstamp=`pkgparam -R /$ROOTDIR VRTSvxfs PSTAMP`

	[ -n "$VERBOSE" ] && echo "pstamp: $pstamp"

	if [ -z "$pstamp" ]; then
		no_pstamp
	fi

	#
	# 4.0ga case.
	#
	if [ "$pstamp" == "VERITAS-4.0FS-2003-12-04" ]; then
		return
	fi

	echo $pstamp | sed -e "$PSTAMPCMD" | read maj min mp rp

	[ -n "$VERBOSE" ] && echo "maj: $maj"
	[ -n "$VERBOSE" ] && echo "min: $min"
	[ -n "$VERBOSE" ] && echo "mp: $mp"
	[ -n "$VERBOSE" ] && echo "rp: $rp"

	if [ -z "$maj" -o -z "$min" -o -z "$mp" -o -z "$rp" ]; then
		invalid_pstamp
	fi

	if [ "$maj" -ne "thismaj" -o "$min" -ne "thismin" ]; then
		imcompatible
	fi

	if [ "$thismp" -lt "$mp" ]; then
		will_downgrade
	elif [ "$thismp" -eq "$mp" ]; then
		if [ "$thisrp" -lt "$rp" ]; then
			will_downgrade
		fi
	elif [ "$thisrp" -ne 0 ]; then
		imcompatible
	fi
}

patch_inst_ok()
{
	check_pstamp
}

OSVER=`pkgparam -R /$ROOTDIR VRTSvxfs OSVER`
if [ "$OSVER" != "2.8" ]
then
	echo "Patch number 113605-02 can only be applied to Solaris 2.8."
	echo "Please contact VERITAS Software to obtain the correct patch for"
	echo "your current version of Solaris."
	terminate_patch
fi

if [ -z "$IGNORE_VXFS_PSTAMP" ]; then
	patch_inst_ok
fi

exit 0
