#!/bin/sh
#
# Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
# ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
#
# pragma ident "%W% %E% Oracle and/or its affiliates"
#
# This file contains shell routines common to both the postinstall and
# preremove scripts for the core j2se package (rt).  The postinstall and
# preremove scripts are built by concatenating the the specific portion
# to this common portion.
#
# The following commands are defined as shell variables 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"
TOUCH="touch"
SED="sed"

#
# The following are Gnome commands required to update the desktop databases.
# They are currently (04/06) not as stable as we would like, but we have
# little choice but to use them.  Usage should allow for graceful and
# mostly successful continuation of the installation should the commands
# fail (or not be present).
#
# Since these are both tested for existance and to be executed, separate
# names are provided so that only some uses may be "echoed" for testing.
#
GNOME_UTILS_DIRS="/usr/bin /usr/gnome/bin"
UPDATE_MIME_DATABASE="/usr/bin/update-mime-database"
UPDATE_DESKTOP_DATABASE="/usr/bin/update-desktop-database"
GTK_UPDATE_ICON_CACHE="/usr/bin/gtk-update-icon-cache"
UPDATE_MIME_DATABASE_EXEC="${UPDATE_MIME_DATABASE}"
UPDATE_DESKTOP_DATABASE_EXEC="${UPDATE_DESKTOP_DATABASE}"
GTK_UPDATE_ICON_CACHE_EXEC="${GTK_UPDATE_ICON_CACHE}"

#
# 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"
# TOUCH="echo touch"
# SED="echo sed"
# UPDATE_MIME_DATABASE_EXEC="echo ${UPDATE_MIME_DATABASE}"
# UPDATE_DESKTOP_DATABASE_EXEC="echo ${UPDATE_DESKTOP_DATABASE}"
# GTK_UPDATE_ICON_CACHE_EXEC="echo ${GTK_UPDATE_ICON_CACHE}"
#

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"
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}"

#
# Desktop integration source locations below.
#
JDK_DESKTOP="${INSTALL_NAME}/jre/lib/desktop"
JDK_ICONS="${JDK_DESKTOP}/icons"
JDK_APPS="${JDK_DESKTOP}/applications"
JDK_MIME="${JDK_DESKTOP}/mime"

#
# Desktop registration locations below.
# freedesktop.org conventions.
#
SHARE_PATH="${BASEDIR}/share"
SHARE_ICONS="${SHARE_PATH}/icons"
SHARE_MIME="${SHARE_PATH}/mime"
SHARE_APPS="${SHARE_PATH}/applications"

#
# Themes.
#
HICOLOR="hicolor"
HIGHCONTRAST="HighContrast"
HIGHCONTRASTINVERSE="HighContrastInverse"
LOWCONTRAST="LowContrast"
THEMES="${HICOLOR} ${HIGHCONTRAST} ${HIGHCONTRASTINVERSE} ${LOWCONTRAST}"

#
# Icons.
#
RESOLUTIONS="16x16 48x48"
TEXT_ICON="gnome-mime-text-x-java.png"
JAR_ICON="gnome-mime-application-x-java-archive.png"
JNLP_ICON="gnome-mime-application-x-java-jnlp-file.png"
JAVA_ICON="sun-java.png"
JAVAWS_ICON="sun-javaws.png"
JCONTROL_ICON="sun-jcontrol.png"
APPS_ICONS="${JAVA_ICON} ${JAVAWS_ICON} ${JCONTROL_ICON}"
MIME_ICONS="${TEXT_ICON} ${JAR_ICON} ${JNLP_ICON}"
ICONS="${APPS_ICONS} ${MIME_ICONS}"

#
# Support for both Gnome 2.0, JDS 3 (Gnome 2.6) and JDS 4 (Gnome 2.14 or
# later) is provided (and hopefully later versions).  Gnome 2.6 and earlier,
# do not not support the freedesktop conventions.
# Legacy locations follow.
#
SHARE_CONTROL_CENTER="${SHARE_PATH}/control-center-2.0"
SHARE_CAPPLETS="${SHARE_CONTROL_CENTER}/capplets"
SHARE_MIME_INFO="${SHARE_PATH}/mime-info"
SHARE_APP_REGISTRY="${SHARE_PATH}/application-registry"
SHARE_PIXMAPS="${SHARE_PATH}/pixmaps"

#
# Other legacy versions of Gnome on Solaris relocate some of the directories
# that Gnome expects to find in /usr/share to /usr/share/gnome.
#
SHARE_GNOME="${SHARE_PATH}/gnome"
GNOME_CONTROL_CENTER="${SHARE_GNOME}/control-center-2.0"
GNOME_CAPPLETS="${GNOME_CONTROL_CENTER}/capplets"
GNOME_MIME_INFO="${SHARE_GNOME}/mime-info"
GNOME_APP_REGISTRY="${SHARE_GNOME}/application-registry"
GNOME_PIXMAPS="${SHARE_GNOME}/pixmaps"

#
# 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 consistent 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	Low bound of the release range (inclusive).
#   $2	release		Release to be checked.
#   $3	high_bound	High bound 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
}

#
# UpdateIconCache(icon_theme_root)
#
# Update the icon cache for the theme located at icon_root_theme.  The
# icon cache is optional but if it is present it needs to be updated
# for the new icons to be found.
#
# On older systems, the cache update utility may not exist.
#
# In addition, the following little tidbit is found in the "Implementation
# Notes" section of the "Icon Theme Specification (0.11)":
#
#       (http://freedesktop.org/wiki/Standards_2ficon_2dtheme_2dspec)
#
#       This caching can make it impossible for users to add icons without
#       having to restart applications. In order to handle this, any
#       implementation that does caching is required to look at the mtime
#       of the toplevel icon directories when doing a cache lookup, unless
#       it already did so less than 5 seconds ago. This means that any icon
#       editor or theme installation program need only to change the mtime
#       of the the toplevel directory where it changed the theme to make sure
#       that the new icons will eventually get used.
#
# Its not clear to me if "toplevel directory" means the "theme" directory or
# an immediate child.  To be sure, touch both.
#
UpdateIconCache() {
    _icon_theme_root=$1

    if [ -f ${_icon_theme_root}/icon-theme.cache ]; then
	for _dir in ${GNOME_UTILS_DIRS}; do
	    if [ -x ${_dir}/${GTK_UPDATE_ICON_CACHE} ]; then
		( ${_dir}/${GTK_UPDATE_ICON_CACHE_EXEC} ${_icon_theme_root} ) \
		    > /dev/null 2>&1
		break
	    fi
	done
	${TOUCH} ${_icon_theme_root}
	for _dir in ${RESOLUTIONS}; do
	    if [ -d ${_icon_theme_root}/${_dir} ]; then
		${TOUCH} ${_icon_theme_root}/${_dir}
	    fi
	done
    fi

}

#
# UpdateDesktopDatabase(desktop_root)
#
# Update the desktop file database for the directory desktop_root.  The
# desktop database (cache) is optional but if it is present it needs to
# be updated for the new desktop files to be found.  (Interesting name for
# the database/cache file, no?)
#
# On older systems, the update utility may not exist.
#
# In order for the new .desktop files to be recognized, host based update
# utilities need to be run.  As we all know, this isn't always possible,
# so be careful to run them only when appropriate and be gracious about
# accepting failures.
#
UpdateDesktopDatabase() {
    _desktop_root=$1

    if [ -f ${_desktop_root}/mimeinfo.cache ]; then
	for _dir in ${GNOME_UTILS_DIRS}; do
	    if [ -x ${_dir}/${UPDATE_DESKTOP_DATABASE} ]; then
		( ${_dir}/${UPDATE_DESKTOP_DATABASE_EXEC} ${_desktop_root} ) \
		   > /dev/null 2>&1
		break
	    fi
	done
    fi

}

#
# UpdateMimeDatabase(mime_root)
#
# Update the mime definitions database located at mime_root.  This is not
# optional.
#
# On older systems, the update utility may not exist (but in that case,
# this mime database isn't being used).
#
UpdateMimeDatabase() {
    _mime_root=$1

    if [ -d ${mime_root}/packages ]; then
	for _dir in ${GNOME_UTILS_DIRS}; do
            if [ -x ${_dir}/${UPDATE_MIME_DATABASE} ]; then
		( ${_dir}/${UPDATE_MIME_DATABASE_EXEC} ${_mime_root} ) \
		    > /dev/null 2>&1
		break
	    fi
	done
    fi

}

#
# Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
# Use is subject to license terms.
#
# pragma ident "%W% %E% 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="jcontrol 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 preremove 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 scenario.
#
# 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 appropriate.
    #
    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
}

#
# CreateGnomeTheme ( theme )
#
# Helper routine to set up the icon directory structure under a theme.
#
# Parameters:
#   $1  theme		Gnome theme directory to establish.
#
CreateGnomeTheme() {
    _theme=$1
    $INSTALLF -c none ${PKGINST} ${_theme} d 0755 root other
    for _res in ${RESOLUTIONS}; do
	$INSTALLF -c none ${PKGINST} ${_theme}/${_res} d 0755 root other
	for _type in apps mimetypes ; do
	    $INSTALLF -c none ${PKGINST} \
	        ${_theme}/${_res}/${_type} d 0755 root other
	done
    done
}

#
# 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

    #
    # JDS 4: Freedesktop/Gnome later than 2.8 (mostly).  Hopefully the way
    # it will continue to be.
    #
    $INSTALLF -c none ${PKGINST} ${SHARE_ICONS} d 0755 root other
    for _theme in ${THEMES} ; do
	if [ -d ${SHARE_ICONS}/${_theme} ]; then
	    CreateGnomeTheme ${SHARE_ICONS}/${_theme}
	fi
    done
    $INSTALLF -c none ${PKGINST} ${SHARE_APPS} d 0755 root other

    #
    # JDS 3: Legacy Gnome (2.6) installations.
    #
    if [ -d ${SHARE_MIME_INFO} ] || [ -d ${SHARE_APP_REGISTRY} ]; then
	$INSTALLF -c none ${PKGINST} ${SHARE_MIME_INFO} d 0755 root other
	$INSTALLF -c none ${PKGINST} ${SHARE_APP_REGISTRY} d 0755 root other
    fi
    if [ -d ${SHARE_CAPPLETS} ]; then
	$INSTALLF -c none ${PKGINST} ${SHARE_CONTROL_CENTER} d 0755 root other
	$INSTALLF -c none ${PKGINST} ${SHARE_CAPPLETS} d 0755 root other
    fi
    if [ -d ${SHARE_PIXMAPS} ]; then
	$INSTALLF -c none ${PKGINST} ${SHARE_PIXMAPS} d 0755 root other
    fi

    #
    # Other legacy Gnome installations (2.0 - munged by Sun).
    #
    if [ -d ${SHARE_GNOME} ]; then
	$INSTALLF -c none ${PKGINST} ${SHARE_GNOME} d 0755 root other
	if [ -d ${GNOME_MIME_INFO} ] || [ -d ${GNOME_APP_REGISTRY} ]; then
	    $INSTALLF -c none ${PKGINST} ${GNOME_MIME_INFO} d 0755 root other
	    $INSTALLF -c none ${PKGINST} ${GNOME_APP_REGISTRY} d 0755 root other
	fi
	if [ -d ${GNOME_CAPPLETS} ]; then
	    $INSTALLF -c none ${PKGINST} ${GNOME_CONTROL_CENTER} \
		d 0755 root other
	    $INSTALLF -c none ${PKGINST} ${GNOME_CAPPLETS} d 0755 root other
	fi
    fi

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

#
# InstallGnomeFile ( default_flag, src, dest )
#
# Installs any Gnome integration file respecting the policy around which
# version of the file should be installed when multiple Java SE versions
# are installed.  Currently, file is only updated if this Java is being
# installed as the default version (but a fair argument can be made for
# always installing the latest version).  Missing files are always installed.
#
InstallGnomeFile() {
    _default_flag=$1
    _src=$2
    _dest=$3

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

	$CP ${_src} ${_dest}
	$CHMOD 0644  ${_dest}
	$CHOWN root  ${_dest}
	$CHGRP other ${_dest}

    fi

    if [ -f ${_dest} ]; then
	$INSTALLF -c none $PKGINST ${_dest} e 0644 root other
    fi

}

#
# InstallThemeIcons ( default_flag, theme, icon, type)
#
# Helper routine to set up the icon directory structure under a theme.
#
# Parameters:
#   $1  default_flag
#   $2  theme		Gnome theme directory to establish.
#   $3	icon
#   $4	type
#
InstallThemeIcons() {
    _default_flag=$1
    _theme=$2
    _icon=$3
    _type=$4

    #
    # If this is the default JVM or any of the files are missing, update
    # all the files.  This guarantees that we don't get different icons
    # for different sizes.
    #
    for _res in ${RESOLUTIONS}; do
	if [ ! -f ${SHARE_ICONS}/${_theme}/${_res}/${_type}/${_icon} ]; then
	    _default_flag="true"
	fi
    done

    #
    # Update/Install the files if appropriate.
    #
    # 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
    # "volatile" they are so marked, so that if different versions of the
    # JDK install different files, pkgchk errors should not be generated.
    #
    for _res in 16x16 48x48 ; do
	if [ "${_default_flag}" = "true" ] ; then
	    ${CP} ${JDK_ICONS}/${_theme}/${_res}/${_type}/${_icon} \
	      ${SHARE_ICONS}/${_theme}/${_res}/${_type}/${_icon}
	fi
	$INSTALLF -c none $PKGINST \
	  ${SHARE_ICONS}/${_theme}/${_res}/${_type}/${_icon} v 644 root other
    done

}

#
# InstallGnomeIcons (default_flag)
#
# Installs the appropriate set of Java icons.  A fairly expansive set is
# installed according to the freedesktop.org specification (0.11 at this
# time):
#       http://standards.freedesktop.org/icon-theme-spec/latest/
#
# Additionally, a limited set of icons is installed in the legacy pixmap
# directory for support of earlier Gnome implementations.
#
InstallGnomeIcons() {
    _default_flag=$1

    #
    # freedesktop.org definitions
    #
    for _theme in ${THEMES} ; do

	#
	# Don't try to create a theme which doesn't exist.
	#
	if [ -d ${SHARE_ICONS}/${_theme} ]; then

	    #
	    # Application icons.
	    #
	    for _icon in ${APPS_ICONS} ; do
		InstallThemeIcons ${default_flag} ${_theme} ${_icon} apps
	    done

	    #
	    # Mime type icons.
	    #
	    for _icon in ${MIME_ICONS} ; do
		InstallThemeIcons ${default_flag} ${_theme} ${_icon} mimetypes
	    done

	    UpdateIconCache ${SHARE_ICONS}/${_theme}

	fi

    done

    #
    # Legacy locations.  Only install these if the target directory already
    # exists.
    #
    if [ -d ${SHARE_PIXMAPS} ]; then

	#
	# Application icons.
	#
	for _icon in ${APPS_ICONS}; do
	    InstallGnomeFile $_default_flag \
		${JDK_ICONS}/${HICOLOR}/48x48/apps/${_icon} \
		${SHARE_PIXMAPS}/${_icon}
	done

	#
	# For mime type icons, only a subset of the icons are used (because
	# only a subset of the mimetypes are known) and we shortened the
	# names.
	#
	InstallGnomeFile $default_flag \
	    ${JDK_ICONS}/${HICOLOR}/48x48/mimetypes/${TEXT_ICON} \
	    ${SHARE_PIXMAPS}/x-java.png
	InstallGnomeFile $default_flag \
	    ${JDK_ICONS}/${HICOLOR}/48x48/mimetypes/${JAR_ICON} \
	    ${SHARE_PIXMAPS}/x-java-archive.png
	InstallGnomeFile $default_flag \
	    ${JDK_ICONS}/${HICOLOR}/48x48/mimetypes/${JNLP_ICON} \
	    ${SHARE_PIXMAPS}/x-java-jnlp-file.png

    fi

}

#
# InstallGnomeDesktop ( default_flag, file )
#
# Installs "file", assumed to be a .desktop file for Gnome in both the
# freedesktop.org specified location (0.9.4 at this time):
#       http://standards.freedesktop.org/desktop-entry-spec/latest/
# and legacy locations.
#
InstallGnomeDesktop () {
    _default_flag=$1
    _file=$2

    #
    # freedesktop.org location.
    #
    InstallGnomeFile ${_default_flag} \
	${JDK_APPS}/${_file} \
	${SHARE_APPS}/${_file}

    #
    # Legacy locations.
    #
    # Using the logic that newer versions of Java SE will not be bundled
    # with older OS versions (which contain older JDS/Gnome versions) and
    # that Gnome versions are not typically downgraded, legacy files are
    # only installed if the directory exists. This helps eliminate clutter
    # on newer installations.
    #
    # Pre-2.10 based Gnome implementations (JDS3) require the Java Control Panel
    # application icon to be in the control-center-2.0 directory which could
    # be in /usr/share (JDS 3) or /usr/gnome (pre-JDS).
    #
    if [ "${_file}" = "sun_java.desktop" ]; then
	if [ -d ${SHARE_CAPPLETS} ]; then
	    InstallGnomeFile ${_default_flag} \
		${JDK_APPS}/${_file} \
		${SHARE_CAPPLETS}/${_file}
	fi
	if [ -d ${GNOME_CAPPLETS} ]; then
	    InstallGnomeFile $default_flag \
		${JDK_APPS}/sun_java.desktop \
		${GNOME_CAPPLETS}/sun_java.desktop
	fi
    fi

}

#
# InstallLegacyMimetype ( default_flag, mime_type, extension, name, command,
#     icon, description, where )
#
# 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		name of icon file
#   $7	description	description for association
#   $8  where		base directory
#
InstallLegacyMimetype() {
    _default_flag=$1
    _mime_type=$2
    _extension=$3
    _name=$4
    _command=$5
    _icon=$6
    _description=$7
    _where=$8

    #
    # Similar to the above, update the association files (all or none).
    #
    _keys_file=${_where}/mime-info/${_name}.keys
    _mime_file=${_where}/mime-info/${_name}.mime
    _apps_file=${_where}/application-registry/${_name}.applications

    if [ "${_default_flag}" = "true" ] ||
       [ ! -f ${_keys_file} ] ||
       [ ! -f ${_mime_file} ] ||
       [ ! -f ${_apps_file} ]; then

	cat <<- end_of_keys_file > $_keys_file
	${_mime_type}
	    description=${_description}
	    icon_filename=${_icon}
	    default_action_type=application
	    default_application_id=${_name}
	    short_list_application_user_additions=${_name}
	end_of_keys_file

	cat <<- end_of_mime_file > $_mime_file
	${_mime_type}
	    ext: ${_extension}
	end_of_mime_file

	cat <<- end_of_apps_file > $_apps_file
	${_name}
	    command=${_command}
	    name=${_name}
	    can_open_multiple_files=false
	    requires_terminal=false
	    mime_types=${_mime_type}
	end_of_apps_file

	$CHMOD 0644  ${_keys_file} ${_mime_file} ${_apps_file}
	$CHOWN root  ${_keys_file} ${_mime_file} ${_apps_file}
	$CHGRP other ${_keys_file} ${_mime_file} ${_apps_file}

    fi

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

}

#
# InstallGnomeMimetypes()
#
# Although the full mimetype suite is supposed to be delivered as part of
# Gnome:
#       http://freedesktop.org/wiki/Standards_2fshared_2dmime_2dinfo_2dspec
# the mime specification for jnlp files is missing and the specification for
# jar files is incorrect in early versions.  We "patch" those and rebuild
# the database.
#
# Legacy support for earlier versions of Gnome is also provided.
#
InstallGnomeMimetypes() {
    _default_flag=$1

    #
    # There are "bugs" in the JDS 3 mime recognition algorithms for jar
    # files and the jnlp mime recognition is MIA.  There isn't an
    # appropriate way to recognize JDS 4 (or greater) as being present
    # so this is triggered off of the Solaris release: if its greater
    # than 10, we can assume that the JDS version is greater than 3.
    #
    # If these "patches" are installed, then the mime database needs to
    # be updated with the same caveats as above.
    #
    if [ -d ${SHARE_MIME} ]; then
	release=`GetHostRelease`
	if [ $release -le 10 ]; then
	    InstallGnomeFile ${_default_flag} \
		${JDK_MIME}/packages/x-java-archive.xml \
		${SHARE_MIME}/packages/x-java-archive.xml
	    InstallGnomeFile ${_default_flag} \
		${JDK_MIME}/packages/x-java-jnlp-file.xml \
		${SHARE_MIME}/packages/x-java-jnlp-file.xml
	    UpdateMimeDatabase ${SHARE_MIME}
	fi
    fi

    #
    # Establish the mime types for jar and jnlp files (Legacy Gnome).
    #
    for _dir in ${SHARE_PATH} ${SHARE_GNOME}; do

	if [ -d ${_dir}/mime-info ] && [ -d ${_dir}/application-registry ]; then

	    InstallLegacyMimetype ${_default_flag} \
			   application/x-java-archive \
			   jar \
			   java-archive \
			   "java -jar" \
			   x-java-archive.png \
			   "Java Archive" \
			   ${_dir}

	    InstallLegacyMimetype ${_default_flag} \
			   application/x-java-jnlp-file \
			   jnlp \
			   java-web-start \
			   javaws \
			   x-java-jnlp-file.png \
			   "Java Web Start Application" \
			   ${_dir}

	fi

    done

}

#
# IntegrateWithGnome( default_flag )
#
# This is the main entry point for all Java/Gnome integration.
#
IntegrateWithGnome() {
    _default_flag=$1

    #
    # Establish the basic Gnome directory set.
    #
    RegisterGnomeDirs

    #
    # Copy the icons to the Gnome/freedesktop directory structure.
    #
    InstallGnomeIcons ${_default_flag}

    #
    # Install the .desktop file for Gnome integration.
    #
    # "sun_java" is probably a poor choice of a name for the desktop file
    # associated with jcontrol (aka: ControlPanel), but it would be difficult
    # (if not impossible) to change at this time. A constant name is
    # required for the Gnome transition mechanisms to work.
    #
    InstallGnomeDesktop ${_default_flag} sun_java.desktop 
    InstallGnomeDesktop ${_default_flag} sun-java.desktop 
    InstallGnomeDesktop ${_default_flag} sun-javaws.desktop
    UpdateDesktopDatabase ${SHARE_APPS}

    #
    # Install/Patch the mimetype databases for Gnome integration.
    #
    InstallGnomeMimetypes ${_default_flag}

}

#
# 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.
#
# Note, that there is a somewhat pathological case where the registry entry
# name is already taken in the jdk registry.  This could result from
# a tar file installation being done in /usr/jdk, the installation of
# a third party JVM or any number of strange, rare actions.  In that
# case, leave the existing object alone.  Having done that, any further
# actions by this script are ill-advised, so exit (no error).
#
if [ -d ${REGISTRY_NAME} ] || [ -f ${REGISTRY_NAME} ]; then
    exit 0
fi
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" directory, 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 ascertain, so a heuristic of $BASEDIR
# containing a "bin" directory is applied.  This heuristic 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
    IntegrateWithGnome $default_flag
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

#
# Create a service tag if supported in the system.
# No product registration is done.
#
# If a different package install root is specified via pkgadd -R option,
# the installer cannot create a service tag since the service tag registry
# client (see /usr/bin/stclient) only supports local system use.
# A RFE# 6576434 is created for stclient to support the remote installation
# support.
#
if [ "${PKG_INSTALL_ROOT}" = "" ]; then
    ${INSTALL_NAME}/bin/java com.sun.servicetag.Installer \
        -source "${PKGINST}" > /dev/null 2>&1
fi

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