#!/bin/sh

INSTALL_DATA=${BASEDIR}/usr/share/lib/jato/.install_data

## Compare the version of the pkg we are installing against the version
## of the pkg that may already be installed and abort installation
## if a downgrade is detected.  This check is necessary only during
## Solaris install.  When installed as an unbundled, the unbundled installer
## makes the check and won't even attempt to install the pkg if a downgrade
## is detected.
if [ -f ${INSTALL_DATA}/${PKG}.pkginfo ]; then

    # Get the version of the pkg installed on the system.
    # Map to a number we can do a numerical comparison on.
    # Minor and micro default to 0 if not specified.
    #
    sysVersion=`env LANG=C LC_ALL=C \
	pkgparam -f ${INSTALL_DATA}/${PKG}.pkginfo VERSION \
	| awk -F, '{print $1}'`
    major=`echo $sysVersion | awk -F'.' '{print $1}'`
    minor=`echo $sysVersion | awk -F'.' '{print $2}'`
    micro=`echo $sysVersion | awk -F'.' '{print $3}'`
    if [ ! -n "$minor" ]; then
        minor="0"
    fi
    if [ ! -n "$micro" ]; then
        micro="0"
    fi
    weHave="${major}${minor}${micro}"

    # Get the version of this pkg being installed.
    # Map to a number we can do a numerical comparison on.
    # Minor and micro default to 0 if not specified.
    #
    mediaVersion=`echo $VERSION | awk -F',' '{print $1}'`
    major=`echo $mediaVersion | awk -F'.' '{print $1}'`
    minor=`echo $mediaVersion | awk -F'.' '{print $2}'`
    micro=`echo $mediaVersion | awk -F'.' '{print $3}'`
    if [ ! -n "$minor" ]; then
        minor="0"
    fi
    if [ ! -n "$micro" ]; then
        micro="0"
    fi
    weWant="${major}${minor}${micro}"

    # If downgrade, abort install
    if [ $weHave -gt $weWant ]; then
	echo "Installation of $PKG version $mediaVersion stopped because"
	echo "version $sysVersion is already installed and will not be downgraded."
	exit 1
    fi
fi

exit 0
