#!/bin/bash

# For Client Machines

#########################################################################
## AutoUpdate-Client (update-client) -- Version 2.3 -- 6/1/97
#########################################################################

#########################################################################
## This was written by Kirk Bauer, 6/1/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@kaybee.org
##		kirk@gt.ed.net
##    gt5918a@prism.gatech.edu
##
## Revision History:
##     4/5/97 Version  1.0 -- Initial Release
##     4/25/97 Version 2.0 -- Now uses the scrips from rpm-utils
##                    and should actually work okay!
##     4/27/97 Version 2.1 -- The client side should work okay when
##                    you turn AUTO* off...
##     5/26/97 Version 2.2 -- Made a seperate config file as well as fixed
##                    things so they build for other architectures.
##     6/1/97  Version 2.3 -- Fixed a few glitches:  clients delete
##                    RPMS even if you don't install them, plus the
##                    /root/doupdate script deletes itself...
##
#########################################################################

############################################################################
#
# NOTE: Configuration moved to /etc/autoupdate-client.conf
#
############################################################################

. /etc/autoupdate-client.conf

############################################################################
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.
   rpm-checkfile --notext $i
   case "$?" in
      0)
         # Same version installed
         echo "   The same version is installed"
         if [ "$DELETE" = "y" ] ; then
            rm -f $i 2> /dev/null
         fi
      ;;
      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."
               if [ "$DELETE" = "y" ] ; then
                  rm -f $i 2> /dev/null
               fi
            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/$i" >> $UPDATEFILE
            echo '   if [ $? = 0 ] ; then' >> $UPDATEFILE
            echo "      echo 'Done.'" >> $UPDATEFILE
            if [ "$DELETE" = "y" ] ; then
               echo "      rm -f $BASEDIR/$i 2> /dev/null" >> $UPDATEFILE
            fi
            echo "   else" >> $UPDATEFILE
            echo "      echo 'Error.'" >> $UPDATEFILE
            echo "   fi" >> $UPDATEFILE
            echo "else" >> $UPDATEFILE
            if [ "$DELETE" = "y" ] ; then
               echo "   rm -f $BASEDIR/$i 2> /dev/null" >> $UPDATEFILE
            fi
            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."
               if [ "$DELETE" = "y" ] ; then
                  rm -f $i 2> /dev/null
               fi
            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/$i" >> $UPDATEFILE
            echo '   if [ $? = 0 ] ; then' >> $UPDATEFILE
            echo "      echo 'Done.'" >> $UPDATEFILE
            if [ "$DELETE" = "y" ] ; then
               echo "      rm -f $BASEDIR/$i 2> /dev/null" >> $UPDATEFILE
            fi
            echo "   else" >> $UPDATEFILE
            echo "      echo 'Error.'" >> $UPDATEFILE
            echo "   fi" >> $UPDATEFILE
            echo "fi" >> $UPDATEFILE
            echo  >> $UPDATEFILE
            echo "      Run $UPDATEFILE to upgrade"
         fi
      ;;
      99)
         echo "   There was an error processing $i"
      ;;
   esac
done
echo
echo "Done."
echo
echo "****************************"
echo
