#!/bin/bash

#########################################################################
## CheckRPM-File (rpm-checkfile) -- Version 1.0 -- 4/25/97
#########################################################################

#########################################################################
## This was written by Kirk Bauer, 4/25/97
## 
## This script looks at an RPM and tells you if it is installed,
## not installed, or a different version is installed...
##
## If you have *ANY* questions/comments/suggestions/complements/etc
## *please* send them to me.  One of the reasons I release scripts to
## people is to get positive feedback.  You can contact me through one
## of the following email addresses:
##    kirk@kaybee.org
##		kirk@gt.ed.net
##    gt5918a@prism.gatech.edu
##
## Revision History:
##     4/25/97 Version 1.0 -- Initial Release
##
#########################################################################


# Let's find out the package's name:
if [ $# = 0 ] ; then
   echo "This command compares an RPM file with what is already"
   echo "installed on your system."
   echo "Command Usage:"
   echo "$0 [--notext] packagename.rpm"
   echo "   --notext: return only error codes, no output" 
   echo
   exit 99
else
   if [ "$1" = "--notext" ] ; then
      TEXT=0
      PFILE="$2"
   else
      TEXT=1
      PFILE="$1"
   fi
   PNAME=`rpm -q --queryformat "%{NAME}" -p $PFILE`
	# Let's find out if we have that package installed.
	A=`rpm -q $PNAME 2> /dev/null`
   if [ $? = 0 ] ; then
      # Query did not exit error, so some version was installed....
   	T=`rpm -q $PNAME 2> /dev/null | grep -c "$(rpm -qp $PFILE)"`
      if [ $T = 1 ] ; then 
         # Same Version Installed
         exit 0
		elif [ $T = 0 ] ; then 
         # Some version installed....
         if [ $TEXT = 1 ] ; then
            echo "Installed Version: $(rpm -q $PNAME), Disk Version: $(rpm -qp $PFILE)"
         else
            exit 2
         fi
      else
         # shouldn't get here!
         if [ $TEXT = 1 ] ; then
            echo "Error!"
         else
            exit 99
         fi
      fi
   else
      # No version installed
      if [ $TEXT = 1 ] ; then
         echo "No Version of $PNAME installed"
      else
         exit 1
      fi
   fi
fi
exit 0
