#!/bin/ksh -p
#
# ident "@(#)util_lib.ksh	1.9	10/12/21 Oracle"
#
# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#

function dodialog {
	typeset opts

	while [ -z "${1##-*}" ]; do
		opts="$opts $1"
		shift
	done
	${G_MEDIA_DIR}/utdialog $G_UTDIALOG_OPTS $opts utinstall.$1
}

#
# function CompareVersion
#
# Description:
#     Compares the Version strings passed in.  The version string
#     must be in the form <n>.<n>[.<n>] where "n" is an integer.
#     Special provision is made for dealing with version strings of the form
#     <n>.<n>[.<n>][.<a>] where "a" is alphanumeric. Such strings can be
#     compared for equality. This allows for uninstallation.
#
# Parameters:
#     $1 and $2 the version strings to be compared
#
# Returns:
#     =0 if $1 is the same as $2
#     =1 if $1 is newer than $2
#     =2 if $2 is newer than $1
#
function CompareVersion {
    typeset r1=${1%%_*}
    typeset v1
    typeset done1=false
    typeset r2=${2%%_*}
    typeset v2
    typeset done2=false
    typeset res
    while /bin/true
    do
	v1=${r1%%.*}
	if [[ $v1 = $r1 ]]; then
	    r1="0"
	    done1=true
	else
	    r1=${r1#*.}
	fi
	v2=${r2%%.*}
	if [[ $v2 = $r2 ]]; then
	    r2="0"
	    done2=true
	else
	    r2=${r2#*.}
	fi
	# Deal with feature releases, which are not fully numeric and
	# thus can't be compared except for equality
	if [ "$v1" != "$v2" ]; then
	    let "res=v1-v2"
	    if [[ $res -lt 0 ]]; then
		# $1 < $2
		return 2
	    elif [[ $res -gt 0 ]]; then
		# $1 > $2
		return 1
	    fi
	fi
	if $done1 && $done2; then
	    # no more substring to compare
	    # this means match
	    break
	fi
    done
    return 0
}

#
# function GetMinJreVersion
#
# Description:
#	Emits to stdout the minimum required Java Runtime Environment
#	version number for this release of SRSS.
#
# Parameters:
#	None
#
# Returns:
#	=0 on success
#	=1 if error (which can't happen today, but who knows what
#		the future might hold)
#
function GetMinJreVersion {
	print "1.6"
	return 0
}

#
# function UpdateParam
#
# Description:
#     Updates the parameter in the file specified.  The parameters are
#     stored in the key=value pair.
#
# Parameters:
#     $1 - filename. The filename to be created under /etc/opt/SUNWut directory.
#	It must not contain ".." in the path and cannot be an absolute name.
#	It will create the file if the file specified does not exist.
#     $2 - the parameter key.  If the parameter is not already in the
#	file, a new entry for the parameter will be created.  Otherwise,
#	the existing entry will be replaced with the new value.
#     $3 - the parameter value.  If the value does not specified, the
#	entry will be removed.
#
# Returns:
#     =0 if successful
#     =1 if error
#
function UpdateParam {
    if [ $# -lt 2 ]; then
	return 1
    fi
    typeset file=$1
    typeset param=$2
    typeset value=$3
    typeset tmpfile=/var/opt/SUNWut/tmp/UpdateParam.$$

    # check the filename for "/" and ".."
    if expr "$file" : '.*/' > /dev/null ||
       expr "$file" : '.*\(\.\.\).*' > /dev/null; then
	return 1
    fi
    fpfile=/etc/opt/SUNWut/$file
    if [ ! -f $fpfile ]; then
	touch $tmpfile || return 1
    else
	grep -v "^${param}[ 	]*=" $fpfile > $tmpfile
    fi
    if [ -n "$value" ]; then
	print "${param}=${value}" >> $tmpfile
    fi
    if [ -s $tmpfile ]; then
	mv -f $tmpfile $fpfile
	chmod 644 $fpfile
    else
	/bin/rm -f $tmpfile $fpfile
    fi
    return 0
}

#
# Exit with an error message in case it will be executed 
# from within a non-global zone on Solaris 10 Trusted 
# Extensions system. Program name will be determined by
# evaluating $0.
#
# Returns:
# Nothing if not executed in a non-global zone on Solaris
# 10 Trusted Extensions otherwise doesn't return but exit 
# with 1.
#
function FailExecInLocalZoneOnTx {
	typeset PROG_NAME=$0
	PROG_NAME=${PROG_NAME:-"This program"}
	PROG_NAME=${PROG_NAME##*/}

	if [ -x /bin/plabel ] && /bin/plabel > /dev/null 2>&1 && \
		[ "$(/bin/zonename 2> /dev/null)" != "global" ]
	then
		print -u2 $PROG_NAME can only be run from the global zone.
		exit 1
	fi
}

