#!/bin/ksh
#
# Copyright (c) 1998 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Eric Haszlakiewicz.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#--------------------------------------------------------------------#
# checkver [-q] [installedlibdir [library name]]
#
# This script must be run from a directory in which
# a shlib_version file resides.
#
# If no arguments are passed the name of the current directory
# is assumed to be the name of the library.  The directory
# "/usr/lib" will be searched for problematic libraries.
#
# A list of directories of installed libraries may be specified.
# This will replace the default of "/usr/lib" and search there
# instead.
#
# An explicit library name may be passed.  If present, checkver
# will use this name when searching the installed libraries.
#
# This script produces no output if all library version are not 
# large than the source version.  If an installed library with a
# version greater than the source is found, checkver prints a
# header and a list of the names of the offending installed libraries.
#
# The header may be supressed by passing "-q" as the first argument.
#

trap "exit 1" 1 2 3 4 5 6 7 8 10 11 12 13 14 15

error=0

Usage ( ) {
    echo "$1 [-q] [installedlibdir [library name]]"
}


if echo "$*" | egrep -q "[[:space:]]-h[[:space:]]|[[:space:]]--help*" ; then
    Usage $0
    exit 0
fi

if [ "X$1" = "X-q" ] ; then
    # Supress header.
    quiet="1"
    shift
else
    quiet="0"
fi

if [ "X$1" != "X" ] ; then
    LIBDIR="$1"
else
    LIBDIR="/usr/lib"
fi

if [ "X$2" = "X" ] ; then
    # Assume the library name is the
    # name of the current directory.
    libname=`pwd`
    libname=`basename $libname`
 else
    libname="$2"
 fi
 if ! echo $libname | grep -q "^lib" ; then
   libname="lib$libname"
 fi


if [ ! -f ./shlib_version ] ; then
    echo "$0: unable to find ./shlib_version"
    exit 1
fi

# Grab major and minor numbers from the source.
 . ./shlib_version

if [ "X$minor" = "X" -o "X$major" = "X" ] ; then
    echo "$0: shlib_version doesn't contain the version."
    exit 1
fi

# Find every shared object library with the same base name.
 for instlib in `find $LIBDIR -name "$libname.so.*"` ; do
    # Grab the major and minor from the installed library.
    instmajor=`echo $instlib | awk 'BEGIN { FS="." } { print $3 } '`
    instminor=`echo $instlib | awk 'BEGIN { FS="." } { print $4 } '`

    # If they're greater than the source, complain.
    if [ "0$major" -eq "0$instmajor" ] ; then
	if [ "0$minor" -lt "0$instminor" ] ; then
	    if [ $error -eq 0 -a $quiet = "0"]; then
		echo -n "The following libraries have versions greater"
		echo " than the source:"
	    fi
	    echo $instlib > /dev/stderr
	    error=1
	fi
    elif [ "0$major" -lt "0$instmajor" ] ; then
	if [ $error -eq 0 -a $quiet = "0" ] ; then
	    echo -n "The following libraries have versions greater"
	    echo " than the source:"
	fi
	echo $instlib > /dev/stderr
	error=1
    fi 
 done

exit $error
