# This script initializes the backout data for a patch package
# directory format options.
# 
#       @(#)preinstall 1.11 00/10/30 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

PATH=/usr/sadm/bin:$PATH
recovery="no"

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

# Check to see if this is a patch installation retry.
if [ "$INTERRUPTION" = "yes" ]; then
    if [ -d "$PKG_INSTALL_ROOT/var/tmp/$SUNW_PATCHID.$PKGINST" ] || [ -d "$PATCH_BUILD_DIR/$SUNW_PATCHID.$PKGINST" ]; then
        recovery="yes"
    fi
fi

if [ -n "$PATCH_BUILD_DIR" -a -d "$PATCH_BUILD_DIR" ]; then
	BUILD_DIR="$PATCH_BUILD_DIR/$SUNW_PATCHID.$PKGINST"
else
	BUILD_DIR="$PKG_INSTALL_ROOT/var/tmp/$SUNW_PATCHID.$PKGINST"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$BUILD_DIR/files/reloc
ROOT_DIR=$BUILD_DIR/files/root
PROTO_FILE=$BUILD_DIR/prototype
ORIGPKGINFO=$PKG_INSTALL_ROOT/var/sadm/pkg/$PKGINST/pkginfo
PKGINFO_FILE=$BUILD_DIR/pkginfo
THIS_DIR=`dirname $0`
INSTALLINGPKGINFO=`dirname $THIS_DIR`

if [ "$PATCH_PROGRESSIVE" = "true" ]; then
	# If this is being used in an old-style patch, insert
	# the old-style script commands here.

	#XXXOld_CommandsXXX#

	exit 0
fi

#
# Unless specifically denied, initialize the backout patch data by
# creating the build directory and copying over the original pkginfo
# which pkgadd saved in case it had to be restored.
#
if [ "$PATCH_NO_UNDO" != "true" ] && [ "$recovery" = "no" ]; then
	if [ -d $BUILD_DIR ]; then
		rm -r $BUILD_DIR
	fi

	# If this is a retry of the same patch then recovery is set to
	# yes. Which means there is a build directory already in
	# place with the correct backout data.

	if [ "$recovery" = "no" ]; then
		mkdir $BUILD_DIR
		mkdir -p $RELOC_DIR
		mkdir $ROOT_DIR
	fi

	#
	# Here we initialize the backout pkginfo file by first
	# copying over the old pkginfo file and then adding the
	# ACTIVE_PATCH parameter so the backout will know what patch
	# it's backing out.
	#
	# NOTE : Within the installation, pkgparam returns the
	# original data.
	#
	pkgparam -v $PKGINST | nawk '
		$1 ~ /PATCHLIST/	{ next; }
		$1 ~ /CLASSES/		{ next; }
		$1 ~ /PATCH_OBSOLETES/	{ next; }
		$1 ~ /ACTIVE_OBSOLETES/	{ next; }
		$1 ~ /SUNW_OBSOLETES/	{ next; }
		$1 ~ /ACTIVE_PATCH/	{ next; }
		$1 ~ /SUNW_PATCHID/	{ next; }
		$1 ~ /UPDATE/		{ next; }
		$1 ~ /SCRIPTS_DIR/	{ next; }
		$1 ~ /PATCH_NO_UNDO/	{ next; }
		$1 ~ /INSTDATE/		{ next; }
		$1 ~ /PKGINST/		{ next; }
		$1 ~ /OAMBASE/		{ next; }
		$1 ~ /PATH/		{ next; }
		{ print; } ' | sed s,\'\"\'\"\',ApOsTrOpHe,g > $PKGINFO_FILE

        # The previous is needed to workaround pkgparam
        # inserting '"'"' for every ' it finds in the
        # pkginfo file. see bugid 4052001.

	# If there is an undo script delivered with this patch pkg
	# there is a possibility that when the patch gets backed out
	# it may not get executed due to the CLASSES macro in the
	# original pkginfo file may not contain the class
	# identifier. Thus we need to merge the old CLASSES macro and
	# the installing patch pkgs CLASSES macro.

	nawk ' $1 ~ /CLASSES/ {print $0} ' $INSTALLINGPKGINFO/pkginfo \
		| sed s/CLASSES=// > /tmp/installingCLASSES.$$
 
	classIDs=`nawk ' $1 ~ /CLASSES/ {print $0} ' $ORIGPKGINFO \
		| sed s/CLASSES=//`
 
	for instClass in $classIDs; do
		notFound=`grep "$instClass" /tmp/installingCLASSES.$$`
		if [ -z "$notFound" ]; then
			newCLASSESlist="$instClass $newCLASSESlist"
		fi
		notFound=""
	done
 
	classes=`nawk ' $1 ~ /CLASSES/ {print $0} ' $ORIGPKGINFO`  
	newCLASSESlist="$classes $newCLASSESlist"  
	
	echo "$newCLASSESlist" >> $PKGINFO_FILE
	echo "ACTIVE_PATCH=$SUNW_PATCHID" >> $PKGINFO_FILE
	echo "ACTIVE_OBSOLETES=$ACTIVE_OBSOLETES" >> $PKGINFO_FILE

	rm /tmp/installingCLASSES.$$

	# And now initialize the backout prototype file with the
	# pkginfo file just formulated.
	echo "i pkginfo" > $PROTO_FILE

	# Copy over the backout scripts including the undo class
	# action scripts
	for script in $SCRIPTS_DIR/*; do
		srcscript=`basename $script`
		targscript=`echo $srcscript | nawk '
			{ script=$0; }
			/u\./ {
				sub("u.", "i.", script);
				print script;
				next;
			}
			/patch_/ {
				sub("patch_", "", script);
				print script;
				next;
			}
			{ print "dont_use" } '`
		if [ "$targscript" = "dont_use" ]; then
			continue
		fi

		echo "i $targscript=$FILE_DIR/$targscript" >> $PROTO_FILE
		cp $SCRIPTS_DIR/$srcscript $FILE_DIR/$targscript
	done

	#
	# Now add entries to the prototype file that won't be passed to
	# class action scripts. If the entry is brand new, add it to the
	# deletes file for the backout package.
	#
	Our_Pkgmap=`dirname $SCRIPTS_DIR`/pkgmap
	BO_Deletes=$FILE_DIR/deletes

	nawk -v basedir=${BASEDIR:-/} '
		BEGIN { count=0; }
		{
			token = $2;
			ftype = $1;
		}
		$1 ~ /[#\!:]/ { next; }
		$1 ~ /[0123456789]/ {
			if ( NF >= 3) {
				token = $3;
				ftype = $2;
			} else {
				next;
			}
		}
		{ if (ftype == "i" || ftype == "e" || ftype == "f" || ftype == "v" || ftype == "d") { next; } }
		{
			equals=match($4, "=")-1;
			if ( equals == -1 ) { print $3, $4; }
			else { print $3, substr($4, 0, equals); }
		}
		' < $Our_Pkgmap | while read class path; do
			#
			# NOTE: If pkgproto is passed a file that is
			# actually a hard link to another file, it
			# will return ftype "f" because the first link
			# in the list (consisting of only one file) is
			# viewed by pkgproto as the source and always
			# gets ftype "f".
			#
			# If this isn't replacing something, then it
			# just goes to the deletes list.
			#
			if valpath -l $path; then
				Chk_Path="$BASEDIR/$path"
				Build_Path="$RELOC_DIR/$path"
				Proto_From="$BASEDIR"
			else	# It's an absolute path
				Chk_Path="$PKG_INSTALL_ROOT$path"
				Build_Path="$ROOT_DIR$path"
				Proto_From="$PKG_INSTALL_ROOT"
			fi

			#
			# Hard links have to be restored as regular files.
			# Unlike the others in this group, an actual
			# object will be required for the pkgmk.
			#
       			if [ -f "$Chk_Path" -a ! -h "$Chk_Path" ] ; then
				mkdir -p `dirname $Build_Path`
				cp -p $Chk_Path $Build_Path
				cd $Proto_From
				pkgproto -c $class "$Build_Path=$path" 1>> $PROTO_FILE 2> /dev/null
				cd $THIS_DIR
			elif [ -h "$Chk_Path" -o \
			     -c "$Chk_Path" -o \
			     -b "$Chk_Path" -o \
			     -p "$Chk_Path" ]; then
				pkgproto -c $class "$Chk_Path=$path" \
					1>> $PROTO_FILE 2> /dev/null
			else
				echo $path >> $BO_Deletes
			fi
		done
fi

# If additional operations are required for this package, place
# those package-specific commands here.

#XXXSpecial_CommandsXXX#

#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# pragma ident "@(#)postcommon	1.14 04/11/30 Sun Microsystems"
#
# This file contains shell routines common to both the postinstall and
# postremove scripts for the core j2se package (rt).  The postinsall and
# postremove are built by concatinating the the specific portion to this
# common portion.
#
# The following commands are defined as shell variable so that they can
# be redefined (as below) to place this script in a mode such that it
# will tell you what it would do, without actually modifying the system.
# Only commands which modify the system should be so aliased.
#
# Be particularly careful with sed.  Its only on this list because at
# one point its output creates a file as part of the delivery.  $SED
# is applied there, but not at most uses of sed.
#
INSTALLF="installf"
RM="rm"
LN="ln"
CP="cp"
MKDIR="mkdir"
CHMOD="chmod"
CHOWN="chown"
CHGRP="chgrp"
SED="sed"

#
# For development purposes, one might consider uncommenting the following
# lines.  The result is a script which will tell you what it would do
# without actually doing anything.
#
# INSTALLF="echo installf"
# RM="echo rm"
# LN="echo ln"
# CP="echo cp"
# MKDIR="echo mkdir"
# CHMOD="echo chmod"
# CHOWN="echo chown"
# CHGRP="echo chgrp"
# SED="echo sed"
#

if [ "$PKG_INSTALL_ROOT" != "" ]; then
    INSTALLF="$INSTALLF -R ${PKG_INSTALL_ROOT}"
fi

PRODVERS=`echo ${SUNW_PRODVERS} | sed -e "s/\/.*//"`
PRODVERS_NP=`echo ${PRODVERS} | sed -e "s/_.*//"`
PREFIX="jdk"

REGISTRY_DIR="jdk"
REGISTRY_PATH="${BASEDIR}/${REGISTRY_DIR}"
REGISTRY_ENTRY="${PREFIX}${PRODVERS}"
REGISTRY_NAME="${REGISTRY_PATH}/${REGISTRY_ENTRY}"

INSTALL_DIR="instances"
INSTALL_PATH="${REGISTRY_PATH}/${INSTALL_DIR}"
INSTALL_ENTRY="${PREFIX}${PRODVERS_NP}"
INSTALL_NAME="${INSTALL_PATH}/${INSTALL_ENTRY}"

J2SE_PATH="${BASEDIR}/j2se"
JAVA_PATH="${BASEDIR}/java"
CLIENT_JAVA_PATH="${CLIENT_BASEDIR}/java"
LATEST_PATH="${REGISTRY_PATH}/latest"
BIN_PATH="${BASEDIR}/bin"
BIN_JAVA="${BIN_PATH}/java"
BIN_JAVAWS="${BIN_PATH}/javaws"

JAVA_LINK_VALUE="${REGISTRY_DIR}/${REGISTRY_ENTRY}"
LATEST_LINK_VALUE="${REGISTRY_ENTRY}"

JDK_ICONS="${INSTALL_NAME}/jre/lib/images/icons"
SHARE_PATH="${BASEDIR}/share"
SHARE_DEFAULT="${SHARE_PATH}/pixmaps"
SHARE_HIGH_CONTRAST="${SHARE_PATH}/icons/HighContrast/48x48/apps"
SHARE_HIGH_CONTRAST_INVERSE="${SHARE_PATH}/icons/HighContrastInverse/48x48/apps"
SHARE_LOW_CONTRAST="${SHARE_PATH}/icons/LowContrast/48x48/apps"
MIME_PATH="${SHARE_PATH}/gnome/mime-info"
APPS_PATH="${SHARE_PATH}/gnome/application-registry"

#
# This is as it should be:
#
REL_JAVAWS="bin/javaws"

#
# As a transition, we also have this, which can completely go away when
# 4954937 is fixed and fully disappears (only in internal 1.5 builds):
#
REL_JAVAWS_GOOFY="jre/bin/javaws"

#
# See PSARC/2003/091: 1.4.2 shipped with this "official" location for
# javaws.  Until 1.4.2 is deader than a doornail, we need this extra
# location.
#
REL_JAVAWS_OLD="jre/javaws/javaws"

#
# "Global variables" which (if set) reflect the minimum and maximum j2se
# releases expected to be the default on the underlying host release of
# Solaris.  These are fully expanded tuples.
DEFAULT_MIN=
DEFAULT_MAX=

#
# FollowLink ( link )
#
# Given a symbolic link, follow that link until it doesn't point a symbolic
# link.  Sequences such as "/foo/../" and "/./" are simplified.
#
# Parameters:
#  $1	link		Link name
#
FollowLink() {
    link=$1
    while [ -h $link ]; do
        ls=`ls -l $link`
        link=`dirname $link`/`echo $ls | cut -f 11 -d " "`
        echo $link | grep "\/\.\/" 2>/dev/null >/dev/null
        while [ $? = 0 ]; do
            link=`echo $link | sed -e s"/\/\.\//\//g"`
            echo $link | grep "\/\.\/" 2>/dev/null >/dev/null
        done
        echo $link | grep "[^\/]*\/\.\.\/" 2>/dev/null >/dev/null
        while [ $? = 0 ]; do
            link=`echo $link | sed -e s"/[^\/]*\/\.\.\///g"`
            echo $link | grep "[^\/]*\/\.\." 2>/dev/null >/dev/null
        done
        link=`echo $link | sed -e "s/^\.\///g"`
    done
    echo "$link"
    return 0
}

#
# GetHostRelease ()
#
# The use of /var/sadm/system/admin/INST_RELEASE is allowed by PSARC/2001/472.
# We fallback to the use of uname should this file not exist, but that may
# give the wrong answer for diskless server installations.  Fortunately, the
# absence of the INST_RELEASE file is a "should never happen" situation,
# except for internal development where it happens all the time, but then
# uname does yield the right answer.
#
# Unfortunately, the format of the VERSION identifier has changed over time
# as "Solaris 2.X" became marketed as simply "Solaris X".  Hence we need
# to allow for both the "2.X" and simply "X" forms. uname is consistant in
# providing a "Major.Minor[.Micro]" format.
#
# This returns the simple "X" that is the Minor release by the taxonomy.
#
GetHostRelease() {
    if [ -f $PKG_INSTALL_ROOT/var/sadm/system/admin/INST_RELEASE ]; then
	ver=`grep '^VERSION=' \
	  $PKG_INSTALL_ROOT/var/sadm/system/admin/INST_RELEASE`
	ver=`echo $ver | sed -e "s/[^0-9]*//" -e "s/^2\.//" -e "s/\..*//"`
    else
	ver=`uname -r | sed -e "s/[0-9]*\.//" -e "s/\..*//"`
    fi
    echo $ver
}

#
# ExpandPrefix ( release )
# ExpandPrefixInfinity ( release )
#
# These two shell routines expand JVM release identifier prefixes to the
# full, four element tuple.  ExpandPrefix zero extends as per JSR 56.
# ExpandPrefixInfinity extends with essentially infinite values.
#
# Parameters:
#   $1	release		Partial or complete release name tuple.
#
ExpandPrefix() {
    echo $1 | sed -e "s/_/\./g" | \
      awk '{FS="."; printf "%d.%d.%d_%.2d\n", $1, $2, $3, $4}'
}

ExpandPrefixInfinity() {
    echo $1 | sed -e "s/_/\./g" | \
      awk '{FS="."; \
	if ($1 == "") major=9999; else major=$1; \
	if ($2 == "") minor=9999; else minor=$2; \
	if ($3 == "") micro=9999; else micro=$3; \
	if ($4 == "") patch=9999; else patch=$4; \
	printf "%d.%d.%d_%.2d\n", major, minor, micro, patch}'
}

#
# GetDefaultRange ()
#
# Determines the range of J2se releases acceptable as the default on the
# host version of Solaris.
#
GetDefaultRange() {
    if [ -f $PKG_INSTALL_ROOT/etc/default/default_java ]; then
	. $PKG_INSTALL_ROOT/etc/default/default_java
    elif [ -f $PKG_INSTALL_ROOT/var/sadm/system/admin/default_java ]; then
    	. $PKG_INSTALL_ROOT/var/sadm/system/admin/default_java
    fi
    if [ "$DEFAULT_MIN" = "" ]; then
	release=`GetHostRelease`
	case $release in
	    5 | 6 | 7 )
		DEFAULT_MIN=1.1
		;;
	    8 )
		DEFAULT_MIN=1.2
		;;
	    9 )
		DEFAULT_MIN=1.4
		;;
	    10 )
		DEFAULT_MIN=1.5
		;;
	    * )
		;;
	esac
    fi
    if [ "$DEFAULT_MAX" = "" ]; then
	DEFAULT_MAX=$DEFAULT_MIN
    fi
    DEFAULT_MIN=`ExpandPrefix $DEFAULT_MIN`
    DEFAULT_MAX=`ExpandPrefixInfinity $DEFAULT_MAX`
    return 0
}

#
# GetRel ( filename )
#
# A little utility routine to strip viable prefixes from release names.
# Note that this only works for release names by Sun convention, not the
# whole, generalized JSR 56 name set.
#
# The current and legacy prefixes are:
#	jdk (default for these packages)
#	jre
#	j2re
#	j2sdk
#
# Parameters:
#   $1	filename	Filesystem filename
#
# Returns:
#	Version portion of the file name.
#
GetRel() {
    if [ "`echo $1 | cut -c 1-3`" = "jdk" ]; then
	echo $1 | cut -c 4-
    elif [ "`echo $1 | cut -c 1-3`" = "jre" ]; then
	echo $1 | cut -c 4-
    elif [ "`echo $1 | cut -c 1-4`" = "j2re" ]; then
	echo $1 | cut -c 5-
    elif [ "`echo $1 | cut -c 1-5`" = "j2sdk" ]; then
	echo $1 | cut -c 6-
    else
	echo $1
    fi
}

#
# GetMicro ( release )
#
# Extracts the Micro (dot-dot) version identifier from a Solaris JRE
# release identifier.  This is the entire release identifier ('X.Y.Z',
# not just 'Z'). Prefix specifications are zero extended.
#
# Parameters:
#   $1	release		release identifier
#
# Returns:
#	The Micro release portion of the release identifier.
#
GetMicro() {
    echo `echo $1 | sed -e "s/[-_].*//" | \
      awk '{FS="."; printf "%s.%s.%s", $1, $2, $3}'`
}

#
# RelCmp ( rel1 rel2 )
#
# Styled as much as possible after strcmp, this routine returns one of the
# strings "lt", "eq", or "gt" based on the relationship of the two release
# version identifier strings passed as parameters. The sort is done only
# on the first four fields (Major, Minor, Micro, Patch).  Internal identifiers
# beyond that are ignored and releases differing only in the internal
# identifier will compare as equal.
#
# Parameters:
#   $1	rel1		Release identifier
#   $2	rel2		Release identifier
#
# Returns:
#	"lt", "eq", or "gt" based on the relationship of the two releases
#
RelCmp() {
    r1=`echo $1 | sed -e "s/-.*//" -e "s/_/\./g"`
    r2=`echo $2 | sed -e "s/-.*//" -e "s/_/\./g"`
    if [ "$r1" = "$r2" ]; then
	echo "eq"
    else
	lrel=`printf "%s\n%s\n" ${r1} ${r2} | \
	      sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | \
	      head -1`

	if [ "$r1" = "$lrel" ]; then
	    echo "lt"
	else
	    echo "gt"
	fi
    fi
}

#
# Select ( "least"|"greatest" list )
#
# From the list of file system objects passed in, return the "greatest"
# or "least" depending upon the text flag passed in $1.  The ordering
# is determined by the rules documented in JSR 56 (Appendix A).
# However, this implementation is limited to identifiers which follow the
# Sun conventions for release and directory naming.
#
# Parameters:
#   $1	flag		"least"|"greatest" to control the selection.
#   $2	list		List of potential file system identifiers.
#
# Returns:
#	The selected file system identifier.
#
Select() {
    if [ "$2" = "" ]; then
	return
    fi

    tlist=
    for dir in $2; do
	rel=`GetRel $dir`
	rel=`ExpandPrefix $rel | sed -e "s/[\._-]/ /g"`
	tlist=`printf "%s %s\n%s" "${rel}" "${dir}" "${tlist}"`
    done
    if [ "$1" = "least" ]; then
	printf "%s\n" "${tlist}" | \
	    sort -k 1,1n -k 2,2n -k 3,3n -k 4,4n | \
	    head -1 | \
	    cut -d " " -f 5
    else
	printf "%s\n" "${tlist}" | \
	    sort -k 1,1n -k 2,2n -k 3,3n -k 4,4n | \
	    tail -1 | \
	    cut -d " " -f 5
    fi
}

#
# InRange ( low_bound release high_bound )
#
# Determines if the release identifier ($2) lies between the low bound ($1)
# and the high bound ($3) inclusive.  The release identifiers must be full
# expanded tuples.
#
# Parameters:
#   $1	low_bound	Lowbound of the release range (inclusive).
#   $2	release		Release to be checked.
#   $3	high_bound	Highbound of the release range (inclusive).
#
# Returns:
#   0	(success) is the release identifier is in range.
#   1	(failure) is the release identifier is out of range.
#
InRange() {
    if [ "$2" = "" ]; then
	return 1
    fi

    low=`echo "$1" | sed -e "s/[\._-]/ /g"`
    rel=`echo "$2" | sed -e "s/[\._-]/ /g"`
    hi=`echo "$3" | sed -e "s/[\._-]/ /g"`
    mid=`echo "${low}\n${rel}\n${hi}" | \
        sort -k 1,1n -k 2,2n -k 3,3n -k 4,4n | \
        head -2 | \
        tail -1`
    mid=`echo $mid | awk '{FS=" "; printf "%d.%d.%d_%.2d\n", $1, $2, $3, $4}'`
    if [ "$2" = "$mid" ]; then
	return 0
    else
	return 1
    fi
}
#
# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# pragma ident "@(#)preremove	1.31 04/09/27 Sun Microsystems"
#
# The only function of this script is to attempt to restore a system default
# jvm should is be that it is the former system default which is being
# removed.
#
# See "NewDefault()" for the exact algorithm.
#

#
# CheckLegacy ( dir )
#
# Check the legacy directory (ie: j2se, java1.2, java1.1) as a valid answer
# for the default release based on the assumed contents of that directory.
# If valid, the /usr/java symbolic link is set to point there and "success"
# is returned; else return "failure".
#
# Parameters:
#   $1	dir		Directory (in /usr) assumed to contain a legacy release.
#
# Returns:
#   0	(success) is the release identifier is in range.
#   1	(failure) is the release identifier is out of range.
#
CheckLegacy() {
    if [ ! -h ${BASEDIR}/$1 -a \
	-d ${BASEDIR}/$1 -a \
	-x ${BASEDIR}/$1/bin/java ]; then
	case $1 in
	    "j2se" )
		rel=1.4
		;;
	    "java1.2" )
		rel=1.2
		;;
	    "java1.1" )
		rel=1.1
		;;
	    * )
		return 1
		;;
	esac
	min=`ExpandPrefix "$rel"`
	max=`ExpandPrefixInfinity "$rel"`
	if [ "`RelCmp $min $DEFAULT_MAX`" != "gt" -a \
	     "`RelCmp $max $DEFAULT_MIN`" != "lt" ]; then
	    ${RM} -f ${JAVA_PATH}
	    ${LN} -s $1 ${JAVA_PATH}
	    return 0
	fi
    fi
    return 1
}

#
# NewDefault ()
#
# Try to establish a new default. Note that only JREs installed in well
# known system locations are considered.
#
NewDefault() {

    #
    # Begin by generating the list of possible JREs from the system
    # repository (/usr/jdk).
    #
    list=""
    for path in `ls -d ${REGISTRY_PATH}/jdk* ${REGISTRY_PATH}/jre* \
      ${REGISTRY_PATH}/j2sdk* ${REGISTRY_PATH}/j2re*  2>/dev/null`; do
	if [ ! -d $path -a ! -h $path ]; then
	    continue
	fi
	if [ ! -x ${path}/bin/java ]; then
	    continue
	fi
	entry=`basename $path`
	if [ "$entry" = "${REGISTRY_ENTRY}" ]; then
	    continue
	fi
	list="$list $entry"
    done
  
    #
    # Step 1:	Try to make a different patch level of the same Micro release
    #		the default. If multiple candidates exist, choose the most
    #		advanced.
    #
    if [ "$list" != "" ]; then
	mymicro=`GetMicro ${REGISTRY_ENTRY}`
	minimum=`ExpandPrefix $mymicro`
	maximum=`ExpandPrefixInfinity $mymicro`
	short_list=""
	for jre in $list; do
	    jre_rel=`GetRel $jre`
	    jre_rel=`ExpandPrefix $jre_rel`
	    if `InRange $minimum $jre_rel $maximum`; then
		short_list="$short_list $jre"
	    fi
	done
	if [ "$short_list" != "" ]; then
	    entry=`Select "greatest" "$short_list"`
	    ${RM} -f ${JAVA_PATH}
	    ${LN} -s jdk/$entry ${JAVA_PATH}
	    return
	fi
    fi

    #
    # Step 2:	Try to find a JRE within the preferred default range
    #		for this host platform. If multiple candidates exist,
    #		choose the most	advanced.
    #
    GetDefaultRange
    if [ "$list" != "" ]; then
	short_list=""
	for jre in $list; do
	    jre_rel=`GetRel $jre`
	    jre_rel=`ExpandPrefix $jre_rel`
	    if `InRange $DEFAULT_MIN $jre_rel $DEFAULT_MAX`; then
		short_list="$short_list $jre"
	    fi
	done
	if [ "$short_list" != "" ]; then
	    entry=`Select "greatest" "$short_list"`
	    ${RM} -f ${JAVA_PATH}
	    ${LN} -s jdk/$entry ${JAVA_PATH}
	    return
	fi
    fi

    #
    # Step 2':	Special case for when the default range includes 1.4.
    #		We don't know exactly what release exists in /usr/j2se,
    #		but the odds are high enough that we should try to use
    #		it now. By this point we know that there are no other
    #		1.4 candidates, so anything that's in j2se is better
    #		than anything else we will find.
    #
    if CheckLegacy "j2se" ; then
	return
    fi

    #		Ditto for 1.2 and java1.2.
    if CheckLegacy "java1.2" ; then
	return
    fi

    #		Ditto for 1.1 and java1.1.
    if CheckLegacy "java1.1" ; then
	return
    fi

    #
    # Step 3:	(Desperation sets in) Look for any JRE.
    #
    if [ "$list" != "" ]; then
	entry=`Select "greatest" "$list"`
	${RM} -f ${JAVA_PATH}
	${LN} -s jdk/$entry ${JAVA_PATH}
	return
    fi
    for dir in j2se java1.2 java1.1 ; do
	if [ ! -h ${BASEDIR}/${dir} ] && [ -d ${BASEDIR}/${dir} ] && \
	   [ -x ${BASEDIR}/${dir}/bin/java ]; then
	    ${RM} -f ${JAVA_PATH}
	    ${LN} -s ${dir} ${JAVA_PATH}
	    return
	fi
    done

    #
    # Finally, if there is no default, there should be no link.
    #
    if [ -h ${JAVA_PATH} ]; then
        ${RM} -f ${JAVA_PATH}
    fi

}

#
# NewLatest
#
# Determining the correct algorithm to apply for setting the "latest"
# link isn't as obvious as it might seem. The complication is due to
# package removal.  If the "latest" is being removed, you want to set
# the "latest" link to something else.  The trouble is, if that something
# else wasn't put there via pkgadd (a tar ball for example) or simply
# predates the creation of this interface, absolutely nothing will
# happen on that removal and the link will be left dangling.
#
# There is no complete solution for this, but following algorithm
# limits the problem area to systems where some installs happened
# via packages and other installs by tar archives - in other words,
# an edge condition in a rare senario.
#
# On installation:
#
#	if <"latest" doesn't exist, or points to something older> then
#		set latest to point at the instance being installed
#
# On removal:
#
#	scan the registry for the most recent of the remaining JVMs
#	if <the most recent remaining is >= 1.5.0_01> then
#		set the link to point to the most recent remaining
#	else
#		remove the link
#	fi
#
# 1.5.0_01 is significant, because that is the first jvm which will
# manipulate this link, and hence, won't leave it dangling should it
# be removed.  The algorithm could be modified to carry back as far
# as 1.4.2_05, with the only downside being the potential for dangling
# links.  Carrying it back beyond this would be a nightmare, because
# a not-so-obvious part of the semantic of "latest", is that it is
# Multiple JRE enabled.
#
NewLatest() {

    #
    # We know the old link will be removed.  Let's do that first.
    #
    ${RM} -f ${LATEST_PATH}

    #
    # Begin by generating the list of possible JREs from the system
    # repository (/usr/jdk).
    #
    list=""
    for path in `ls -d ${REGISTRY_PATH}/jdk* ${REGISTRY_PATH}/jre* \
      ${REGISTRY_PATH}/j2sdk* ${REGISTRY_PATH}/j2re*  2>/dev/null`; do
	if [ ! -d $path -a ! -h $path ]; then
	    continue
	fi
	if [ ! -x ${path}/bin/java ]; then
	    continue
	fi
	entry=`basename $path`
	if [ "$entry" = "${REGISTRY_ENTRY}" ]; then
	    continue
	fi
	list="$list $entry"
    done

    #
    # If there are alternatives, find the most advanced (dare I say
    # "latest"?) JVM from among the possibilities.  As per the algorithm
    # description above, set the "latest" link to point the the JVM
    # selected above, iff that JVM is 1.5.0_01 or later.
    #
    if [ "$list" != "" ]; then
	latest=`Select "greatest" "$list"`
	latest_tuple=`GetRel $latest`
	latest_tuple=`ExpandPrefix $latest_tuple`
	if [ "`RelCmp $latest_tuple 1.5.0_01`" != "lt" ]; then
	    ${LN} -s $latest ${LATEST_PATH}
        fi
    fi
    return 0
}

#
# FixDangle ( rel_path )
#
FixDangle() {
    list=""
    for path in `ls -d ${REGISTRY_PATH}/jdk* ${REGISTRY_PATH}/jre* \
      ${REGISTRY_PATH}/j2sdk* ${REGISTRY_PATH}/j2re*  2>/dev/null`; do
	    if [ ! -d $path -a ! -h $path ]; then
		continue
	    fi
	    if [ ! -x ${path}/$1 ]; then
		continue
	    fi
	    entry=`basename $path`
	    if [ "$entry" = "${REGISTRY_ENTRY}" ]; then
		continue
	    fi
	    list="$list $entry"
	done
	if [ "$list" != "" ]; then
	    entry=`Select "least" "$list"`
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../${REGISTRY_DIR}/${entry}/$1 ${BIN_JAVAWS}
	    return 0
	elif [ -x ${J2SE_PATH}/$1 ]; then
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../j2se/$1 ${BIN_JAVAWS}
	    return 0
	fi	
	return 1
}

#
# Main Processing
#

#
# Remove all links from the Registry directory which point to this instance
# being removed.
#
for path in `ls -d ${REGISTRY_PATH}/jdk* ${REGISTRY_PATH}/jre* \
  ${REGISTRY_PATH}/j2sdk* ${REGISTRY_PATH}/j2re* 2>/dev/null`; do
    if [ -h $path ]; then
	target=`FollowLink $path`
	if [ "$target" = "${INSTALL_NAME}" ]; then
	    ${RM} $path
	fi
    fi
done

#
# The only processing need is if this package is either the current default,
# the current "latest" or the target of the /usr/bin/javaws symbolic link
# (See PSARC/2003/091).
#
# Generate the paths to examine before starting to adjust these links.
#
if [ -h ${JAVA_PATH} ]; then
    path=`FollowLink ${JAVA_PATH}`
fi
if [ -h ${LATEST_PATH} ]; then
    latest_path=`FollowLink ${LATEST_PATH}`
fi
if [ -h ${BIN_JAVAWS} ]; then
    ls=`ls -l ${BIN_JAVAWS}`
    link=`echo $ls | cut -f 11 -d " "`
    javaws_path=`echo ${BIN_PATH}/$link | \
      sed -e "s:/${REL_JAVAWS_GOOFY}$::" \
	  -e "s:/${REL_JAVAWS}$::" \
	  -e "s:/${REL_JAVAWS_OLD}$::" \
	  -e "s:/\./:/:g" \
	  -e "s:[^/]*/\.\./::g"`
    javaws_path=`FollowLink $javaws_path`
fi

#
# Test to see if /usr/java is a symlink and points to the instance being
# removed.  If so, then search for a new default.
#
if [ -h ${JAVA_PATH} ]; then
    if [ "$path" = "${REGISTRY_NAME}" ] || \
       [ "$path" = "${INSTALL_NAME}" ]; then

	NewDefault

    fi
fi

#
# Test to see if /usr/jdk/latest is a symlink and points to the instance
# being removed.  If so, then search for a new default.
#
if [ -h ${LATEST_PATH} ]; then
    if [ "$latest_path" = "${REGISTRY_NAME}" ] || \
       [ "$latest_path" = "${INSTALL_NAME}" ]; then

	NewLatest

    fi
fi

#
# Due to PSARC/2003/091, it is possible that the /usr/bin/javaws now
# needs to be adjusted. Only perform these adjustments if they are
# the result of this package removal.
#
if [ -h ${BIN_JAVAWS} ]; then
    if [ "$javaws_path" = "${REGISTRY_NAME}" ] || \
       [ "$javaws_path" = "${INSTALL_NAME}" ]; then

	#
	# 1.If the default now contains a valid javaws, point the link back to
	#	the default (through /usr/java).
	#
	# Due to psarc/2003/091 and 4954923, there are multiple places to search
	# for javaws.
	#
	if [ -x ${JAVA_PATH}/${REL_JAVAWS} ]; then
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../java/${REL_JAVAWS} ${BIN_JAVAWS}
	elif [ -x ${JAVA_PATH}/${REL_JAVAWS_GOOFY} ]; then
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../java/${REL_JAVAWS_GOOFY} ${BIN_JAVAWS}
	elif [ -x ${JAVA_PATH}/${REL_JAVAWS_OLD} ]; then
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../java/${REL_JAVAWS_OLD} ${BIN_JAVAWS}

	#
	# 2.Failing the above, if the link is now dangling removal of this
	#	package, then scan all the available JREs for one with an
	#	appropriate javaws.  Try to choose the one with the most
	#	correct name, and from amont those, try to choose the oldest.
	#	Note that the oldest, must be newer than the default, so this
	#	is "closest approximation").
	#
	else
	    if FixDangle ${REL_JAVAWS}; then
		exit 0
	    fi
	    if FixDangle ${REL_JAVAWS_GOOFY}; then
		exit 0
	    fi
	    if FixDangle ${REL_JAVAWS_OLD}; then
		exit 0
	    fi

	    #
	    # Nothng to point at.  Nuke it!
	    #
	    ${RM} -f ${BIN_JAVAWS}
	fi
    fi
fi
exit 0
