#!/bin/bash

#########################################################################
##                                 Time-stamp: <2000-08-17 00:43:26 vine>
## RPM Update Directory (rpm-updatedir) -- Version 1.0 -- 4/25/97
##     modified by Jun Nishii <jun@vinelinux.org>
#########################################################################

#########################################################################
## 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
##
#########################################################################


showUsage(){
cat <<EOF
  ˡ: ${1##/*/} [ץ] <topdir>

  [ץ] ʲΤ줫Ӥޤ
	-u	Automatically upgrade package
	-a	Automatically add not-installed package
	-n	No upgrade and install
EOF
}

for arg in $* ; do
    case $arg in
	-u) AUTOUPGRADE=y; shift ;;
	-a) AUTOADD=y; shift ;;
	-n) NOEXEC=y; shift ;;
	-h) showUsage ; exit ;;
    esac
done

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

if [ $# = 0 ] ; then
   showUsage
   exit
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:-n} = y ] ; then
                  echo "   I am attempting to install the package:"
                  rpm -ivh $i
	       elif  [ ${NOEXEC:-n} = n ] ; then
                  echo -n "   Install $i? [yN] "
		  read ans
		  case ${ans} in
			Y|y) rpm -ivh $i ;;
		  esac	
               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:-n} = y ] ; then
                  echo "   I am attempting to upgrade the package:"
                  rpm -U $i
        	elif [ ${NOEXEC:-n} = n ] ; then
                  echo -n "   Upgrade $i? [yN] "
		  read ans
		  case ${ans} in
			Y|y) rpm -Uvh $i ;;
		  esac	
		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
