#!/bin/bash

#########################################################################
## RPM Update Directory (rpm-updatedir) -- Version 1.0 -- 4/25/97
#########################################################################

#########################################################################
## This was written by Kirk Bauer, 4/25/97
## 
## This is a little script to automate updating Red Hat Linux
## RPMs.  It looks in a directory and tells you what packages are
## not installed, are different versions, or are the same versions.
## By default, it will also update/install new packages....
##
## 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
##
#########################################################################


# Set this to 'y' if you want packages that have different versions installed
# automatically upgraded.
AUTOUPGRADE=y

# Set this to 'y' if you want new packages that do not have any version yet
# installed automatically installed.
AUTOADD=y











# That's it... you shouldn't have to change anything below...
############################################################################

if [ $# = 0 ] ; then
   echo "Usage:  $0 <directory>"
else
   BASEDIR="$1"
   echo
   echo "****************************"
   echo
   echo "RPM-UpdateDir -- Looking for RPMs in:"
   echo "   $BASEDIR"
   echo
   if [ -d $BASEDIR ] ; then
      cd $BASEDIR
      for i in *.rpm
      do
         echo "Package: $i"
         
         # Let's find out the package's name:
         PNAME=`rpm -q --queryformat "%{NAME}" -p $i`

         rpm-checkfile --notext $i
         case "$?" in
            0)
               # Same version installed
               echo "   The same version is installed"
            ;;
            1)
               # No version installed.

               echo "   There is no version of $PNAME installed..."

               if [ $AUTOADD = y ] ; then
                  echo "   I am attempting to install the package:"
                  rpm -i $i
                  if [ $? = 0 ] ; then
                     echo "      Done."
                  else
                     echo "      Error."
                  fi
               else
                  echo "      New Package Could Be Installed..."
               fi
            ;;
            2)
               # Different Version Installed.
               echo "   The new file and the installed versions do not match."
               echo "      Installed Version: $(rpm -q $PNAME)"

               if [ $AUTOUPGRADE = y ] ; then
                  echo "   I am attempting to upgrade the package:"
                  rpm -U $i
                  if [ $? = 0 ] ; then
                     echo "      Done."
                  else
                     echo "      Error."
                  fi
               else
                  echo "      New Package Should Be Installed..."
               fi
            ;;
            99)
               echo "   There was an error processing $i"
            ;;
         esac
      done
      echo
      echo "Done."
      echo
      echo "****************************"
      echo
   else
      echo "Usage:  $0 <directory>"
   fi
fi
