# This script creates the backout package for a patch package
#
# directory format options.
#
# @(#) postinstall 1.13 01/01/12 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

# Description:
#       Set the TYPE parameter for the remote file
#
# Parameters:
#       none
#
# Globals set:
#	TYPE

set_TYPE_parameter () {
	if [ ${PATCH_UNDO_ARCHIVE:?????} = "/dev" ]; then
		# handle device specific stuff
		TYPE="removable"
	else
		TYPE="filesystem"
	fi
}

#
# Description:
#       Build the remote file that points to the backout data
#
# Parameters:
#       $1:	the un/compressed undo archive
#
# Globals set:
#	UNDO, STATE

build_remote_file () {
	remote_path=$PKGSAV/$SUNW_PATCHID/remote
	set_TYPE_parameter
	STATE="active"

	if [ $1 = "undo" ]; then
		UNDO="undo"
	else
		UNDO="undo.Z"
	fi

	cat > $remote_path << EOF
# Backout data stored remotely
TYPE=$TYPE
FIND_AT=$ARCHIVE_DIR/$UNDO
STATE=$STATE
EOF
}

PATH=/usr/sadm/bin:$PATH

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
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

if [ ! -n "$PATCH_UNDO_ARCHIVE" ]; then
	PATCH_UNDO_ARCHIVE="none"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$FILE_DIR/reloc
ROOT_DIR=$FILE_DIR/root
BO_Deletes=$FILE_DIR/deletes
THIS_DIR=`dirname $0`
PROTO_FILE=$BUILD_DIR/prototype
TEMP_REMOTE=$PKGSAV/$SUNW_PATCHID/temp

if [ "$PATCH_PROGRESSIVE" = "true" ]; then
        # remove the scripts that are left behind
        install_scripts=`dirname $0`
        rm $install_scripts/checkinstall $install_scripts/patch_checkinstall $install_scripts/patch_postinstall

	# If this is being used in an old-style patch, insert
	# the old-style script commands here.

	#XXXOld_CommandsXXX#

	exit 0
fi

#
# At this point we either have a deletes file or we don't. If we do,
# we create a prototype entry.
#
if [ -f $BO_Deletes ]; then
	echo "i deletes=$BO_Deletes" >> $BUILD_DIR/prototype
fi

#
# Now delete everything in the deletes list after transferring
# the file to the backout package and the entry to the prototype
# file. Remember that the pkgmap will get the CLIENT_BASEDIR path
# but we have to actually get at it using the BASEDIR path. Also
# remember that removef will import our PKG_INSTALL_ROOT
#
Our_Deletes=$THIS_DIR/deletes
if [ -f $Our_Deletes ]; then
	cd $BASEDIR

	cat $Our_Deletes | while read path; do
		Reg_File=0

		if valpath -l $path; then
			Client_Path="$CLIENT_BASEDIR/$path"
			Build_Path="$RELOC_DIR/$path"
			Proto_Path=$BASEDIR/$path
		else	# It's an absolute path
			Client_Path=$path
			Build_Path="$ROOT_DIR$path"
			Proto_Path=$PKG_INSTALL_ROOT$path
		fi

		# If BASEDIR/CLIENTBASEDIR = "/", then the previous prepends
		# an extra / i.e. //. The sed command later can't find a
		# Proto_Path with // and therefore will not substitute the
		# correct build_Path resulting in the backout pkg not being
		# created.

		if [ "$CLIENT_BASEDIR" = "/" ]; then
			Client_Path=`echo $Client_Path | sed 's|^\/\/|\/|'`
			Proto_Path=`echo $Proto_Path | sed 's|^\/\/|\/|'`
		fi
			
		# Note: If the file isn't really there, pkgproto
		# doesn't write anything but displays an error
		# so check for the file before processing.

		if [ -f "$Proto_Path" ]; then
			LINE=`pkgproto $Proto_Path=$path`
		else
			continue
		fi

		ftype=`echo $LINE | nawk '{ print $1 }'`
		if [ "$ftype" = "f" ]; then
			Reg_File=1
		fi

		if [ $Reg_File = 1 ]; then
			# Add source file to the prototype entry
			if [ "$Proto_Path" = "$path" ]; then
				LINE=`echo $LINE | sed -e "s|$Proto_Path|$Build_Path|2"`
			else
				LINE=`echo $LINE | sed -e "s|$Proto_Path|$Build_Path|"`
			fi

			DirName=`dirname $Build_Path`
			# make room in the build tree
			mkdir -p $DirName
			cp -p $Proto_Path $Build_Path
		fi

		# Insert it into the prototype file
		echo $LINE 1>>$PROTO_FILE 2>/dev/null

		# Remove the file only if it's OK'd by removef
		rm `removef $PKGINST $Client_Path` 1>/dev/null 2>&1
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Unless specifically denied, make the backout package.
#
if [ "$PATCH_NO_UNDO" != "true" ]; then
	cd $BUILD_DIR	# We have to build from here.

	if [ "$PATCH_UNDO_ARCHIVE" != "none" ]; then
		STAGE_DIR="$PATCH_UNDO_ARCHIVE"
		ARCHIVE_DIR="$PATCH_UNDO_ARCHIVE/$SUNW_PATCHID/$PKGINST"
		mkdir -p $ARCHIVE_DIR
		mkdir -p $PKGSAV/$SUNW_PATCHID
	else
		if [ -d $PKGSAV/$SUNW_PATCHID ]; then
			rm -r $PKGSAV/$SUNW_PATCHID
		fi
		STAGE_DIR=$PKGSAV
		ARCHIVE_DIR=$PKGSAV/$SUNW_PATCHID
		mkdir $ARCHIVE_DIR
	fi

	pkgmk -o -d $STAGE_DIR 1>/dev/null 2>&1
	pkgtrans -s $STAGE_DIR $ARCHIVE_DIR/undo $PKG 1>/dev/null 2>&1
	compress $ARCHIVE_DIR/undo
	retcode=$?
	if [ "$retcode" != 0 ]; then
		echo "compress(1) returned error code $retcode"
		echo "The $PKGINST backout package will not be compressed."
		echo "Continuing to process backout package."
	fi
	if [ "$PATCH_UNDO_ARCHIVE" != "none" ]; then
		if [ $retcode != 0 ]; then
			build_remote_file "undo"
		else
			build_remote_file "undo.Z"
		fi
	fi
	rm -r $STAGE_DIR/$PKG

	cd ..
	rm -r $BUILD_DIR
	# remove the scripts that are left behind
	install_scripts=`dirname $0`
	rm $install_scripts/checkinstall $install_scripts/patch_checkinstall $install_scripts/patch_postinstall
fi

#
# Since this apparently worked, we'll mark as obsoleted the prior
# versions of this patch - installpatch deals with explicit obsoletions.
#
cd ${PKG_INSTALL_ROOT:-/}
cd var/sadm/pkg

active_base=`echo $SUNW_PATCHID | nawk '
	{ print substr($0, 1, match($0, "-")-1) } '`

List=`ls -d $PKGINST/save/${active_base}* 2>/dev/null`
if [ $? -ne 0 ]; then
	List=""
fi

for savedir in $List; do
        patch=`basename $savedir` 
        if [ $patch = $SUNW_PATCHID ]; then
		break
	fi

        # If we get here then the previous patch gets deleted
	if [ -f $savedir/undo ]; then
		mv $savedir/undo $savedir/obsolete
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif [ -f $savedir/undo.Z ]; then
		mv $savedir/undo.Z $savedir/obsolete.Z
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
        elif  [ -f $savedir/remote ]; then
                `grep . $PKGSAV/$patch/remote | sed 's|STATE=.*|STATE=obsolete|' > $TEMP_REMOTE` 
                rm -f $PKGSAV/$patch/remote 
                mv $TEMP_REMOTE $PKGSAV/$patch/remote  
                rm -f $TEMP_REMOTE 
                echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif  [ -f $savedir/obsolete -o -f $savedir/obsolete.Z ]; then
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	fi
done

# 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 "@(#)postinstall	1.65 04/10/18 Sun Microsystems"
#
# The JRE package installs links outside of its BASEDIR from "utilities"
# in /usr/bin, up to the /usr/java link which (in turn) points to the
# default JVM. This script installs these links which are independent
# of BASEDIR.
#
# The current system default is determined by the setting of the /usr/java
# symbolic link.
#
# In general, the newly installed jre/sdk should make itself the system
# default iff:
#
#	There is no current system default or it appears to be corrupt.
#
#	The current default is outside of the range of expected defaults
#	for the underlying release of Solaris and this package is within
#	that range.
#
# The single exception to this algorithm is if the version being installed
# is a more recent patch within the the same Micro release family as the
# current default, then the version being installed shall make itself the
# default. The rough translation of this is "patches are patches".
#

#
# InstallLinks ( default_flag )
#
# Shell routine to install the /usr/bin links. Note that we want to register
# these as part of each package which uses them, so that they won't be
# removed until the last Java is removed from the system (or the last Java
# which has that particular component.
#
# Be sure to modify the space file if you add to or subtract from the list
# of links.
#
# Parameters:
#   $1	default_flag	"true" if this package is being installed as the
#			Solaris system default, "false" otherwise.
#
InstallLinks() {

    $INSTALLF -c none $PKGINST $BIN_PATH d 0755 root bin

    BIN_FILES="java keytool policytool rmid rmiregistry tnameserv \
      orbd servertool"
    for file in $BIN_FILES; do
	$INSTALLF -c none $PKGINST "$BIN_PATH/${file}=../java/bin/${file}" s
    done

    JRE_BIN_FILES="ControlPanel"
    for file in $JRE_BIN_FILES; do
	$INSTALLF -c none $PKGINST "$BIN_PATH/${file}=../java/jre/bin/${file}" s
    done

    #
    # IMPORTANT NOTE: Special handling for the /usr/bin/javaws symbolic
    # link. See PSARC/2003/091.
    #
    # If this version is being made the default, force the link to the
    # "normal" form.  If not, see if the current setting of the link
    # points at a valid javaws executable. If it doesn't, point it at
    # this installation, but only if this is being installed in the
    # default /usr/jdk directory.
    #
    #	if ("the package being installed will be the default") then
    #	    [Re]Create the link through /usr/java.
    #	else if ("/usr/bin/javaws doesn't exist") then
    #	    Create the link to point directly to this package
    #	else if ("it is a link and is 'dangling'") then
    #	    Create the link to point directly to this package
    #	endif
    #
    # Note that it is not a bug that /usr/bin/javaws isn't registered with
    # the package data base (via installf).
    #
    if [ "$1" = "true" ]; then
	${RM} -f ${BIN_JAVAWS}
	${LN} -s ../java/${REL_JAVAWS} ${BIN_JAVAWS}
    elif [ ! -f ${BIN_JAVAWS} ] && [ ! -d ${BIN_JAVAWS} ] && \
	 [ ! -h ${BIN_JAVAWS} ]; then 
	${RM} -f ${BIN_JAVAWS}
	${LN} -s ../${REGISTRY_DIR}/${REGISTRY_ENTRY}/${REL_JAVAWS} \
	  ${BIN_JAVAWS}
    elif [ -h ${BIN_JAVAWS} ]; then
	target=`FollowLink ${BIN_JAVAWS}`
	if [ ! -x $target ]; then
	    ${RM} -f ${BIN_JAVAWS}
	    ${LN} -s ../${REGISTRY_DIR}/${REGISTRY_ENTRY}/${REL_JAVAWS} \
	      ${BIN_JAVAWS}
        fi
    fi
}

#
# Shell routine to install the /usr symbolic link of /usr/java.
#
# Note that it is not a bug that /usr/java isn't registered with
# the package database. Multiple instances will each have there own
# idea of what this link value should be, so (to avoid pkgchk warnings)
# all manipulation is done via postinstall and postremove scripts.
#
# Returns:
#	zero:		The link was created or altered.
#	non-zero:	The link was not altered
#
SetJavaLink() {

    #
    # If there is nothing there just create a link to this instance.
    #
    if [ ! -f ${JAVA_PATH} ] && [ ! -d ${JAVA_PATH} ] &&
       [ ! -h ${JAVA_PATH} ]; then
	${RM} -f ${JAVA_PATH}
	${LN} -s ${JAVA_LINK_VALUE} ${JAVA_PATH}
	return 0
    fi

    #
    # If there is something there but its not a symbolic link, leave it
    # alone.  Funky things have been done to this system which we had better
    # not muck with.
    #
    if [ ! -h ${JAVA_PATH} ]; then
	return 1
    fi

    #
    # It exists and is a symbolic link.  Possibly we want to do something
    # with it
    #
    # First, resolve what it (and any subsequent links) point to.
    path=`FollowLink ${JAVA_PATH}`

    #
    # If it is 'dangling' (points to something that is no longer a valid
    # JVM, or perhaps never was), take it over.  The simplistic test is for
    # an executable object called "java" at the expected path relative to
    # the final destination of this link.
    #
    if [ ! -x ${path}/bin/java ]; then
	${RM} -f ${JAVA_PATH}
	${LN} -s ${JAVA_LINK_VALUE} ${JAVA_PATH}
	return 0
    fi

    #
    # Getting this far, indicates that the symbolic link exists and points
    # to something which is very likely to be a valid JVM.
    #
    # If the current default is out side of the default range for the
    # Solaris release and the release being installed is within the
    # default range, steal the link.
    #
    # Note that we can't tell the exact version of JVM release found
    # in the legacy directories of /usr/java1.1, /usr/java1.2, or
    # /usr/j2se.  We can only assume the Minor release family found there.
    # Be conservative and assume that the JVM installed there is the most
    # advanced within that family.
    #
    GetDefaultRange
    current_dir=`basename $path`
    if [ "$current_dir" = "j2se" ]; then
	current_tuple=`ExpandPrefixInfinity 1.4`
    elif [ "$current_dir" = "java1.2" ]; then
	current_tuple=`ExpandPrefixInfinity 1.2`
    elif [ "$current_dir" = "java1.1" ]; then
	current_tuple=`ExpandPrefixInfinity 1.1`
    else
	current_tuple=`GetRel $current_dir`
	current_tuple=`ExpandPrefix $current_tuple`
    fi
    prodver_tuple=`ExpandPrefix $PRODVERS`
    if [ \( "`RelCmp $current_tuple $DEFAULT_MIN`" = "lt" -o \
	    "`RelCmp $current_tuple $DEFAULT_MAX`" = "gt" \) -a \
	 \( "`RelCmp $prodver_tuple $DEFAULT_MIN`" != "lt" -a \
	    "`RelCmp $prodver_tuple $DEFAULT_MAX`" != "gt" \) ]; then
	${RM} -f ${JAVA_PATH}
	${LN} -s ${JAVA_LINK_VALUE} ${JAVA_PATH}
	return 0
    fi

    #
    # Subsequent tests for acquiring the link depend upon both the
    # current owner and this package being referenced through the
    # /usr/jdk directory. We know this is true for this package.
    # If this isn't true for the existing default, bail out.
    #
    ls=`ls -l ${JAVA_PATH}`
    target=`echo $ls | cut -f 11 -d " "`
    link=`dirname ${JAVA_PATH}`/`dirname $target`
    link=`echo $link | sed -e "s:/\./:/:g" -e "s:[^/]*/\.\./::g"`
    if [ "$link" != "${REGISTRY_PATH}" ]; then
	return 1
    fi
    target=`basename $target`
    current_tuple=`GetRel $target`

    #
    # Finally, if the link points to a release in the same Micro (dot-dot)
    # family, but of a lower patch level, take over the link.
    #
    if [ "`GetMicro $prodver_tuple`" = "`GetMicro $current_tuple`" ]; then
	if [ "`RelCmp $prodver_tuple $current_tuple`" = "gt" ]; then
	    ${RM} -f ${JAVA_PATH}
	    ${LN} -s ${JAVA_LINK_VALUE} ${JAVA_PATH}
	    return 0
	fi
    fi

    return 1
}

#
# SetLatestLink
#
# 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.
#
SetLatestLink() {

    #
    # If there is nothing there, just create the link to this instance.
    #
    # (Its such a bug in "test" and the shells that this isn't trivial)
    #
    if [ ! -h ${LATEST_PATH} ] && [ ! -d ${LATEST_PATH} ] && \
       [ ! -f ${LATEST_PATH} ] ; then
	${RM} -f ${LATEST_PATH}
	${LN} -s ${LATEST_LINK_VALUE} ${LATEST_PATH}
	return 0
    fi

    #
    # If there is something there but its not a symbolic link, leave it
    # alone.  Funky things have been done to this system which we had better
    # not muck with.
    #
    if [ ! -h ${LATEST_PATH} ]; then
	return 1
    fi

    #
    # It exists and is a symbolic link.  Possibly we want to do something
    # with it
    #
    # First, resolve what it (and any subsequent links) point to.
    #
    path=`FollowLink ${LATEST_PATH}`

    #
    # If it is 'dangling' (points to something that is no longer a valid
    # JVM, or perhaps never was), take it over.  The simplistic test is for
    # an executable object called "java" at the expected path relative to
    # the final destination of this link.
    #
    if [ ! -x ${path}/bin/java ]; then
	${RM} -f ${LATEST_PATH}
	${LN} -s ${LATEST_LINK_VALUE} ${LATEST_PATH}
	return 0
    fi

    #
    # Getting this far, indicates that the symbolic link exists and points
    # to something which is very likely to be a valid JVM.
    #
    # Unlike the twisty maze of logic in SetJavaLink, all you need to do
    # here is compare this JVM to the current "latest" and see which is
    # indeed the latest and set the link if approriate.
    #
    current_dir=`basename $path`
    current_rel=`GetRel $current_dir`
    current_tuple=`ExpandPrefix $current_rel`
    prodver_tuple=`ExpandPrefix ${PRODVERS}`
    if [ "`RelCmp $current_tuple $prodver_tuple`" = "lt" ]; then
	${RM} -f ${LATEST_PATH}
	${LN} -s ${LATEST_LINK_VALUE} ${LATEST_PATH}
    fi
    return 0
}

#
# RegisterGNOMEDirs ()
#
# Shell routine to register/create the GNOME directories need by this
# installation.  This is done dynamically (rather than in the prototype_com
# file), because is some cases this is not done (notably, when the package
# is relocated and not part of an system installation).
#
RegisterGNOMEDirs() {
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH} d 0755 root sys
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/pixmaps d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrast d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrast/48x48 d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrast/48x48/apps d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrastInverse d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrastInverse/48x48 d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/HighContrastInverse/48x48/apps d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/LowContrast d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/LowContrast/48x48 d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/icons/LowContrast/48x48/apps d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/gnome d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/gnome/mime-info d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/gnome/application-registry d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/control-center-2.0 d 0755 root other
    $INSTALLF -c none ${PKGINST} \
      ${SHARE_PATH}/control-center-2.0/capplets d 0755 root other

    #
    # Commit the changes so that we can actually place files in the
    # newly created directories.
    #
    $INSTALLF -f $PKGINST
}

#
# AssociateWithDesktop ( default_flag
#			 dir )
#
AssociateWithDesktop() {
    _default_flag=$1
    _dir=$2

    #
    # Update the desktop file if this JDK is being installed as the
    # default or the expected file is MIA.
    #
    if [ "${_default_flag}" = "true" ] || \
	( [ ! -f ${_dir}/sun_java.desktop ] && \
	  [ ! -d ${_dir}/sun_java.desktop ] && \
	  [ ! -h ${_dir}/sun_java.desktop ] ) ; then

	$SED -e "s!INSTALL_DIR!${CLIENT_JAVA_PATH}!g" \
	     -e "s!JRE_NAME_VERSION!jre!g" \
	     -e "s!X-Sun-Supported;!!g" \
	     -e "s!X-Red-Hat-Base;!!g" \
	     ${JAVA_PATH}/jre/plugin/desktop/sun_java.desktop \
	     > ${_dir}/sun_java.desktop
	$CHMOD 0644  ${_dir}/sun_java.desktop
	$CHOWN root  ${_dir}/sun_java.desktop
	$CHGRP other ${_dir}/sun_java.desktop

    fi

    if [ -f ${_dir}/sun_java.desktop ]; then
	$INSTALLF -c none $PKGINST ${_dir}/sun_java.desktop e 0644 root other
    fi
}

#
# AssociateWithGNOME ( default_flag
#		      mime type
#                     extension
#                     name
#                     command
#                     icon   
#                     icon_ext
#                     description )
#
# Shell routine to add an entry to GNOME application registry.
#
# Since multiple packages (or package instances) may be attempting to
# install these files, only install them it they don't exist.  However,
# always register them with the package database so that they don't
# get removed out from under you. (This really should be a separate
# package.)
#
# Note that the association files are marked as editable files, so that
# they can be modified (perhaps by being updated by another package) without
# generating pkgchk errors.
#
# Parameters:
#   $1  default_flag	"true" if this package is being installed as the 
#			Solaris system default, "false" otherwise. 
#   $2	mime type	associated mime type
#   $3	extension	associated file extension
#   $4	name		used to name GNOME registry files
#   $5	command		what to invoke when launching file/mime type
#   $6	icon		base name of icon file
#   $7	icon_ext	extension for icon file
#   $8	description	description for association
#
AssociateWithGNOME() {
    _default_flag=$1
    _mime_type=$2
    _extension=$3
    _name=$4
    _command=$5
    _icon=$6
    _icon_ext=$7
    _description=$8

    #
    # Update the icon files (all or none) if this JDK is being installed
    # as the default or any of the expected files are MIA.
    #
    if [ "${_default_flag}" = "true" ] ||
       [ ! -f ${SHARE_DEFAULT}/${_icon}.${_icon_ext} ] ||
       [ ! -f ${SHARE_HIGH_CONTRAST}/${_icon}.${_icon_ext} ] ||
       [ ! -f ${SHARE_HIGH_CONTRAST_INVERSE}/${_icon}.${_icon_ext} ] ||
       [ ! -f ${SHARE_LOW_CONTRAST}/${_icon}.${_icon_ext} ]; then

	${CP} ${JDK_ICONS}/${_icon}.${_icon_ext} \
	    ${SHARE_DEFAULT}/${_icon}.${_icon_ext}

	${CP} ${JDK_ICONS}/${_icon}_HighContrast.${_icon_ext} \
	    ${SHARE_HIGH_CONTRAST}/${_icon}.${_icon_ext}

	${CP} ${JDK_ICONS}/${_icon}_HighContrastInverse.${_icon_ext} \
	    ${SHARE_HIGH_CONTRAST_INVERSE}/${_icon}.${_icon_ext}

	${CP} ${JDK_ICONS}/${_icon}_LowContrast.${_icon_ext} \
	    ${SHARE_LOW_CONTRAST}/${_icon}.${_icon_ext}

    fi

    #
    # Updated or not, these files are now expected by this JDK (default
    # at the moment, or not).  Register them with the package data base
    # with installf.  Note that even though these files are not really
    # "editable" they are so marked, so that if different versions of the
    # JDK install different files, pkgchk errors should not be generated.
    #
    $INSTALLF -c none $PKGINST \
	${SHARE_DEFAULT}/${_icon}.${_icon_ext} e 0644 root other
    $INSTALLF -c none $PKGINST \
	${SHARE_HIGH_CONTRAST}/${_icon}.${_icon_ext} e 0644 root other
    $INSTALLF -c none $PKGINST \
	${SHARE_HIGH_CONTRAST_INVERSE}/${_icon}.${_icon_ext} e 0644 root other
    $INSTALLF -c none $PKGINST \
	${SHARE_LOW_CONTRAST}/${_icon}.${_icon_ext} e 0644 root other

    #
    # Similar to the above, update the associateion files (all or none).
    #
    GNOME_KEYS_FILE=${MIME_PATH}/${_name}.keys
    GNOME_MIME_FILE=${MIME_PATH}/${_name}.mime
    GNOME_APPS_FILE=${APPS_PATH}/${_name}.applications

    if [ "${_default_flag}" = "true" ] ||
       [ ! -f ${GNOME_KEYS_FILE} ] ||
       [ ! -f ${GNOME_MIME_FILE} ] ||
       [ ! -f ${GNOME_APPS_FILE} ]; then

	# Create the keys file.
	echo "${_mime_type}"						 > $GNOME_KEYS_FILE
	echo "        description=${_description}"			>> $GNOME_KEYS_FILE
	echo "        icon_filename=${_icon}"				>> $GNOME_KEYS_FILE
	echo "        default_action_type=application"			>> $GNOME_KEYS_FILE
	echo "        default_application_id=${_name}"			>> $GNOME_KEYS_FILE
	echo "        short_list_application_user_additions=${_name}"	>> $GNOME_KEYS_FILE

	# Create the mime file.
	echo "${_mime_type}"			 > $GNOME_MIME_FILE
	echo "        ext: ${_extension}"	>> $GNOME_MIME_FILE

	# Create the apps file.
	echo "${_name}"					 > $GNOME_APPS_FILE
	echo "        command=${_command}"		>> $GNOME_APPS_FILE
	echo "        name=${_name}"			>> $GNOME_APPS_FILE
	echo "        can_open_multiple_files=false"	>> $GNOME_APPS_FILE
	echo "        requires_terminal=false"		>> $GNOME_APPS_FILE
	echo "        mime_types=${_mime_type}"		>> $GNOME_APPS_FILE
    fi

    #
    # Also similar to the above, register these truly editable files with
    # unconditionally with the package data base.
    $INSTALLF -c none $PKGINST $GNOME_MIME_FILE e 0644 root other
    $INSTALLF -c none $PKGINST $GNOME_KEYS_FILE e 0644 root other
    $INSTALLF -c none $PKGINST $GNOME_APPS_FILE e 0644 root other
}

#
# Main Processing
#

#
# Add a link containing the full version from the "registry" directory to
# the "instances" directory.  Also, remove any other symbolic links from
# the "instances" directory which may now be lies.
#
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
${LN} -s ${INSTALL_DIR}/${INSTALL_ENTRY} ${REGISTRY_NAME}

#
# If this package should become the system default, make it so by
# creating/modifying the /usr/java symbolic link.
#
# Note that this link is always created in $BASEDIR, even if this
# isn't a "system" installation.  This is to provide a "release
# identifier free" name for a relocated JRE. This behavior is required
# by the Solaris install software.
#
if `SetJavaLink`; then
    default_flag="true"
else
    default_flag="false";
fi

#
# If this is the newest JVM in the registry, set the "latest" link.
#
SetLatestLink

#
# Establish the links from the system "bin" directrory, iff this is
# being installed as a "system" JDK. Typically, this would be if $BASEDIR
# has its default value of /usr.  However, diskless installations make
# this a bit more difficult to assertain, so hieristic of $BASEDIR
# containing a "bin" directory is applied.  This hieristic is ideal
# because even if it incorrectly identifies a "system" installation,
# it will generate a sensible and expected result.
#
if [ -d ${BIN_PATH} ]; then
    InstallLinks $default_flag
fi

#
# create GNOME associations
#
# The same logic as above applies to creating the GNOME associations except
# it it based on $BASEDIR containing a "share" directory.
#
if [ -d ${SHARE_PATH} ]; then

    RegisterGNOMEDirs

    AssociateWithDesktop $default_flag \
			 ${SHARE_PATH}/control-center-2.0/capplets

    AssociateWithGNOME $default_flag \
		       application/java-archive \
		       jar \
		       java-archive \
		       "/usr/bin/java -jar" \
		       sun-java \
		       png \
		       "Java Archive"

    AssociateWithGNOME $default_flag \
		       application/x-java-jnlp-file \
		       jnlp \
		       java-web-start \
		       "/usr/bin/javaws" \
		       sun-java \
		       png \
		       "Java Web Start Application"
fi

#
# To support class data sharing, run a special command.  An
# additional 'classes.jsa' file is generated.  Add this new
# file to the install registry.
#
# Fix 4972526 - Swallow any errors caused while generating JSA file.  Errors
# can be generated when using -R to install from server/client of different
# architectures.
#
${INSTALL_NAME}/bin/java -client -Xshare:dump > /dev/null 2>&1
if [ $? -eq 0 ]; then
    $INSTALLF -c none $PKGINST \
        ${INSTALL_NAME}/jre/lib/${ARCH}/client/classes.jsa
fi

#
# Commit
#
$INSTALLF -f $PKGINST
exit 0
