#!/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 only function of this script is to run the Gnome database update
# scripts should anything have been removed.
#

#
# Main Processing
#
if [ -d ${SHARE_PATH} ]; then

    #
    # Files *may* have been removed from the various mime/application/icon
    # databases (caches).  We really don't know, so we do the conservative
    # thing and run the update utilites, As we all know, this isn't always
    # possible, so be careful to run them only when appropriate and be
    # gracious about accepting failures.
    #
    for _theme in ${THEMES} ; do
	UpdateIconCache ${SHARE_ICONS}/${_theme}
    done

    UpdateDesktopDatabase ${SHARE_APPS}

    release=`GetHostRelease`
    if [ $release -le 10 ]; then
	UpdateMimedatabase ${SHARE_MIME}
    fi

fi   
exit 0
