#!/bin/bash

# For Server Machine *OR* Stand-Alone Machine

#########################################################################
## Auto-Update (autoupdate) -- 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 different 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 be
# 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 base directory where you want the local copies of the updates
# to be kept.  A subdirectory under this with the same name as your
# architecture will be created.  For example, the default shown below will
# keep your local copies of updates in /home/ftp/pub/RedHat-4.1/i386
BASEDIR=/home/ftp/pub/redhat/updates

# This is the ftp site and directory that you want the script to look at for
# new updates.  Please *DO NOT* use ftp.redhat.com unless you absolutely
# have to.  There should be a directory on the FTP site under the directory
# you give here that contains the updates and it must have the same name as
# your architecture.  For example, my ARCH is set to 'i386' above... which
# means that my script looks in:
# ftp.cc.gatech.edu:/pub/linux/distributions/redhat/redhat-4.1/updates/i386
# for the updates.
FTPDIR=ftp.cc.gatech.edu:/pub/linux/distributions/redhat/redhat-4.1/updates
#FTPDIR=ftp.kaybee.gt.ed.net:/pub/redhat/updates
#FTPDIR=ftp.redhat.com:/pub/redhat/redhat-4.1/updates

# This is a temp directory to use... it must exist.
TMP2=/var/tmp

# 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


##########################################################################

# You only have to worry about things below here if you have more than one
# system and this is the server machine.

# Okay, OTHERDIRS is a list of directories to copy information into for
# client machines.  There are two ways of doing this.
#    1) Have only one directory that all clients share
#    2) Have one directory for each client
# or you can do a combination of the both.  All you have to do is run
# the client script on each machine and point them to the appropriate
# directory.  If you leave this empty, the script will assume it is a
# standalone machine.  These directories should be empty and only used
# by this script.
OTHERDIRS=""
#OTHERDIRS="/mnt/client1/updates /mnt/client2/updates /mnt/client3/updates"
#OTHERDIRS="/mnt/share/allclients"

# Now, if you have one server and one client, the best way to do things is
# configure the client's script to delete the files out of the above
# directory after it is done with them.  In this case, leave the line below
# to DELFILES=n.
#
# Now, if you have one server and 100 clients that all have their own
# update directory, you should still leave the line below to DELFILES=n
# because the clients can delete their own files when they are done with
# them.
#
# Finally, if you have one server and 100 clients that all share the same
# client update directory, then you should set the line below to DELFILES=y
# because *every* client could not delete the files.. only one client could.
# Unless you make sure that the one client that does delete these files
# deletes them *after* all the other clients are done with them, then there
# are going to be problems.  So, if you set this to DELFILES=y, the next time
# the script runs (i.e. the next day), it assumes that all the clients are
# done with the files and deletes them.  The problem with this is that if
# one of you clients is down for a day, it might miss the update completely.
# Probably the best thing to do is have a seperate directory for every client
# (i.e. put it directly on the client's local hard drive).
#DELFILES=y
DELFILES=n





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

############################################################################
TDIR=${TMP:-$TMP2}/`basename $0`$$
mkdir $TDIR 2> /dev/null
echo
echo "****************************"
echo
echo "AutoUpdate -- Looking for new updated RPMs in:"
echo "   $FTPDIR/$ARCH/"
echo "and placing the files into:"
echo "   $BASEDIR/$ARCH/"
echo
if [ $DELFILES = y ] ; then
   echo "Placing copies of all new RPMs in:"
	for i in $OTHERDIRS
	do
		rm -f $i/* 2> /dev/null
      echo "   $i"
	done
fi
echo
rm -f $UPDATEFILE 2> /dev/null
cd $BASEDIR
cd $ARCH
ls | grep ".*\.$ARCH\.rpm" > $TDIR/update.tmp
cd ..
ncftp -R $FTPDIR/$ARCH
cd $ARCH
ls | grep ".*\.$ARCH\.rpm" > $TDIR/update.new
for i in `cat $TDIR/update.tmp $TDIR/update.new | sort | uniq -u`
do
	echo "New package: $i"
   
	for j in $OTHERDIRS
	do
		cp $i $j
	done

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

   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 Auto-Update 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 Auto-Update 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
echo
echo "Done."
echo
echo "****************************"
echo
rm -rf $TDIR
