diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
commit | 4ea2cc3bd4a7d9b1c54a9d33e6a1cf82e7c8c21d (patch) | |
tree | d2e54377d14d604356c86862a326f64ae64dadd6 /chkver |
Imported Upstream version 0.18.1upstream/0.18.1
Diffstat (limited to 'chkver')
-rwxr-xr-x | chkver | 119 |
1 files changed, 119 insertions, 0 deletions
@@ -0,0 +1,119 @@ +#! /bin/bash +# +# Copyright 2009-2014 Yorba Foundation +# +# This software is licensed under the GNU LGPL (version 2.1 or later). +# See the COPYING file in this distribution. +# +# chkver <min | max> <major.minor.revision> <min|max major.minor.revision> +# +# Returns 0 if queried version is greater than or equal (for min) or lesser than (for max) the +# supplied value, or 1 otherwise. +# +# Set VERBOSE environment variable for somewhat useful output. +# +# NOTE: +# This is an ultra-naive implementation with just enough error-checking. + +usage() { + echo 'usage: chkver <min | max> <major.minor.revision> <min|max major.minor.revision>' +} + +abort() { + usage + exit 1 +} + +verify_cardinal() { + while [ $# != 0 ] + do + if [ $1 ] && [ $1 -eq $1 2> /dev/null ] && [ $1 -ge 0 ] + then + : + else + abort + fi + + shift + done +} + +# check_min name number min-number +check_min() { + if [ $2 -gt $3 ] + then + verbose $1 large enough. + exit 0 + elif [ $2 -lt $3 ] + then + verbose $1 not large enough. + exit 1 + fi +} + +# check_max name number max-number +check_max() { + if [ $2 -lt $3 ] + then + verbose $1 small enough. + exit 0 + elif [ $2 -gt $3 ] + then + verbose $1 not small enough. + exit 1 + fi +} + +verbose() { + if [ $VERBOSE ] + then + echo $* + fi +} + +# Check number of arguments +if [ $# -lt 3 ] +then + abort +fi + +# Parse arguments +mode=$1 +if [ $mode != "min" ] && [ $mode != "max" ] +then + abort +fi + +major=`echo $2 | awk -F. '{print $1}'` +minor=`echo $2 | awk -F. '{print $2}'` +revision=`echo $2 | awk -F. '{print $3}'` + +min_major=`echo $3 | awk -F. '{print $1}'` +min_minor=`echo $3 | awk -F. '{print $2}'` +min_revision=`echo $3 | awk -F. '{print $3}'` + +# Verify they're all positive integers +verify_cardinal "$major" "$minor" "$revision" "$min_major" "$min_minor" "$min_revision" + +verbose Comparing $major.$minor.$revision against $mode $min_major.$min_minor.$min_revision + +# check version numbers in order of importance +if [ $mode == "min" ] +then + check_min "Major" $major $min_major + check_min "Minor" $minor $min_minor + check_min "Revision" $revision $min_revision +else + check_max "Major" $major $min_major + check_max "Minor" $minor $min_minor + check_max "Revision" $revision $min_revision +fi + +verbose Same version. +if [ $mode == "min" ] +then + exit 0 +else + exit 1 +fi + |