#!/bin/ksh -p
#
# ident "@(#)util_lib.ksh	1.1 04/03/25 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# CompareVersion()
#
# Description:
#     Compares the Version strings passed in.  The version string
#     must be in the form <n>.<n>[.<n>] where "n" is an integer.
#
# 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
#
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
	let "res=v1-v2"
	if [[ $res -lt 0 ]]; then
	    # $1 < $2
	    return 2
	elif [[ $res -gt 0 ]]; then
	    # $1 > $2
	    return 1
	fi
	if $done1 && $done2; then
	    # no more substring to compare
	    # this means match
	    break
	fi
    done
    return 0
}
