#!/bin/bash

# For Client Machines

#########################################################################
## AutoUpdate-Client (update-client) -- Version 1.1 -- 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 supports a client-server model also so that if you have
## a bunch of machines, only one machine downloads the packages and then
## distributes them to the other machines.
##
## 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@gt.ed.net
##    gt5918a@prism.gatech.edu
##    kirk@kaybee.ml.org
##
## Revision History:
##     4/5/97 Version 1.0 -- Initial Release
##     4/25/97 Version 1.1 -- Now uses the scrips from rpm-utils
##                    and should actually work okay!
##
#########################################################################

# Set this to 'y' if you want packages that have older versions installed
# automatically upgraded. If you set this to anything else, you will be
# allowed to interactively say 'yes' or 'no' to upgrade each package. 
# If you chose the automatic method, you will be notified of which packages
# were upgraded.  If you chose the interactive method, you will be notified
# of which packages should be upgraded and you will be given instructions
# on how to do this.
AUTOUPGRADE=y

# Set this to 'y' if you want new packages that do not have any version yet 
# installed automatically installed.  If you set this to anything else, you will
# allowed to interactively say 'yes' or 'no' to install each package. 
# If you chose the automatic method, you will be notified of which packages
# were upgraded.  If you chose the interactive method, you will be notified
# of which packages should be upgraded and you will be given instructions
# on how to do this.
AUTOADD=n

# Set this to the architecture that you use.  This should be 'i386', 'alpha',
# 'sparc', etc....
ARCH=i386

# Set this to the directory that the server puts the files for this
# system.  It should be completely empty and it must exist.
#BASEDIR=/mnt/updatedir
BASEDIR=/home/testing/i386/

# This is where a shell script will be created and root will run it
# to update packages in interactive mode.  This is not used if you tell
# this script to autmatically upgrade packages...
UPDATEFILE=/root/doupdate

# Do you want to delete the files after you are done?  Leave it as 'y'
# unless this client shares the same files with other clients.  In that
# case, you must have only *one* of the clients (i.e. the last one to
# use the files) or the server delete the files for you.
DELETE=y






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

############################################################################
echo
echo "****************************"
echo
echo "Update-Client -- Looking for new updated RPMs in:"
echo "   $BASEDIR"
echo
echo
rm -f $UPDATEFILE 2> /dev/null
cd $BASEDIR
for i in `ls | grep ".*\.$ARCH\.rpm"`
do
   echo "New package: $i"

   # Let's find out the package's name:
   PNAME=`rpm -q --queryformat "%{NAME}" -p $i`

   # Let's find out if we have that package installed.
   checkrpm-file --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..."
            if [ ! -e $UPDATEFILE ] ; then
               touch $UPDATEFILE
               chmod 0770 $UPDATEFILE
               chown root.root $UPDATEFILE
               echo '#!/bin/bash' >> $UPDATEFILE
               echo '# Automatically Generated by the Update-Client Script' >> $UPDATEFILE
               echo >> $UPDATEFILE
            fi
            echo "echo 'Do you want to install $i? (y/n)'" >> $UPDATEFILE
            echo "read" >> $UPDATEFILE
            echo "if [ \$REPLY = y ] ; then" >> $UPDATEFILE
            echo "   echo -n 'Installing $i... '" >> $UPDATEFILE
            echo "   rpm -i $BASEDIR/$ARCH/$i" >> $UPDATEFILE
            echo "   echo 'Done.'" >> $UPDATEFILE
            echo "fi" >> $UPDATEFILE
            echo  >> $UPDATEFILE
            echo "      Run $UPDATEFILE to install"
         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..."
            if [ ! -e $UPDATEFILE ] ; then
               touch $UPDATEFILE
               chmod 0770 $UPDATEFILE
               chown root.root $UPDATEFILE
               echo '#!/bin/bash' >> $UPDATEFILE
               echo '# Automatically Generated by the Update-Client Script' >> $UPDATEFILE
               echo >> $UPDATEFILE
            fi
            echo "echo 'Do you want to update $i? (y/n)'" >> $UPDATEFILE
            echo "read" >> $UPDATEFILE
            echo "if [ \$REPLY = y ] ; then" >> $UPDATEFILE
            echo "   echo -n 'Upgrading $i... '" >> $UPDATEFILE
            echo "   rpm -U $BASEDIR/$ARCH/$i" >> $UPDATEFILE
            echo "   echo 'Done.'" >> $UPDATEFILE
            echo "fi" >> $UPDATEFILE
            echo  >> $UPDATEFILE
            echo "      Run $UPDATEFILE to upgrade"
         fi
      ;;
      99)
         echo "   There was an error processing $i"
      ;;
   esac
done
if [ $DELETE = y ] ; then
	rm -f * 2> /dev/null
fi
echo
echo "Done."
echo
echo "****************************"
echo
