#!/bin/sh
#
#  verify_pkgs [+pkgpatFl ...] [pkgstr ...]
#
#	Verify that the selected packages are correctly loaded on the system.
#	See pkg_fltr for an explanation of arguments.
#
#############################################################################
#
#	Copyright (C) Cadence Design Systems, Inc. All rights
#	reserved.  Unpublished -- rights reserved under the
#	copyright laws of the United States of America.
#
#			RESTRICTED RIGHTS LEGEND
#	Use, duplication, or disclosure by the Government is subject
#	to restrictions as set forth in subparagraph (c)(l)(ii) of 
#	the Rights in Technical Data and Computer Software clause 
#	at DFARS 52.227-7013.
#
#			Cadence Design Systems, Inc.
#			555 River Oaks Parkway
#			San Jose, CA 95134	USA
#
#############################################################################
BOURNE=/bin/sh
XTERMPATH=/usr/openwin/bin
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''

Clear() {
    clear		2>/dev/null	#beware of bad termcap entry
}

Page() {
    MORE=""  more
}

EchoN() {
    echo -n "$*"
}

Beep() {
    $jsr EchoN ""	2>/dev/null
}


Dirname() {					#print argument minus leaf, or .
    case $1 in
	*/* )	echo $1 | sed 's@/[^/]*$@/@' ;;
	*)	echo "." ;;
    esac
}

Leafname() {					#print leaf
    case $1 in
	*/* )	echo $1 | sed 's@.*/@@' ;;
	*)	echo $1 ;;
    esac
}

Fullpath() {					#print rooted path to arg dir
    ( cd $1 && /bin/pwd |
	sed 's@^/tmp_mnt/@/@' )			#print automount access dir
}

Dirpath() {					#print rooted path to arg - leaf
    $jsr Fullpath `$jsr Dirname $1`
}

Suffix() {
    echo $1 | sed -n 's@.*\.@@p'
}

Prefix() {
    echo $1 | sed 's@\.[^.]*$@@'
}

OSvendor=sun4
OSarchs=$OSvendor


PATH=$sysPATH;				export PATH
umask 000

argv="$*"
cd `$jsr Dirname $0`
Proot=`$jsr Fullpath ../..`;		export Proot

tmp=/tmp
outfile=$tmp/out$$

set -f				#disable filename generation
$BOURNE pkg_fltr -ok $argv > $outfile
if [ -s $outfile ]; then
    /bin/cat $outfile > $tmp/verfl$$
    msg="Select the packages you would like verified"
    $BOURNE choice_fltr -M "$msg" $tmp/verfl$$ $outfile
    rm -f $tmp/verfl$$ 
else
    rm $outfile
    echo "No packages are currently installed."
    exit 0
fi

> $Proot/install/tmp/summary.err
/bin/cat $outfile |
while read pkgdesc pkgname remainder; do
    if [ -f $Proot/install/pkgs/$pkgname.inv ]; then
        echo "Verifying files associated with $pkgname"

        sed '/^d/d' $Proot/install/pkgs/$pkgname.inv | tee $tmp/pkgInv |\
	    awk '{print $4}' > $tmp/tarIfl
        test -s $tmp/tarIfl || continue		#empty file

	( cd $Proot && install/bin.$OSvendor/vtar cf - -I $tmp/tarIfl ) |\
            ./tart '+%t%M %s %c %f' > $tmp/flInv
	echo "$pkgdesc" "$pkgname" "$remainder" > $Proot/install/tmp/pkgcheck$$
	echo "diff Inventory    Filesystem"    >> $Proot/install/tmp/pkgcheck$$
        diff $tmp/pkgInv $tmp/flInv 	       >> $Proot/install/tmp/pkgcheck$$
	if [ $? -ne 0 ]; then
	    mv $Proot/install/tmp/pkgcheck$$ $Proot/install/tmp/$pkgname.err
	    echo "Discrepancy detected: see $Proot/install/tmp/$pkgname.err"
	    echo "$pkgdesc" "$pkgname" "$remainder" \
					>> $Proot/install/tmp/summary.err
	else
	    rm -f $Proot/install/tmp/pkgcheck$$ $Proot/install/tmp/$pkgname.err
	fi
    else
	echo "Error: Could not locate $Proot/install/pkgs/$pkgname.inv"	1>&2
    fi
done
rm -f $outfile $tmp/pkgInv $tmp/flInv $tmp/tarIfl

echo " "
if [ -s $Proot/install/tmp/summary.err ]; then
    echo "Discrepancies detected for the following packages:"
    (
	NL="
"
	echo "Package Description${NL}Package Name"
	echo "-------------------${NL}------------"
	awk '{print $1 "\n" $2}' $Proot/install/tmp/summary.err
    ) | tr '~'  ' ' | ./columnize 8 30 25 | $jsr Page
    exit 1
else
    echo "No discrepancies detected"
    exit 0
fi

