#!/bin/bash

# For Server Machine *OR* Stand-Alone Machine

#########################################################################
## Auto-Update (autoupdate) -- 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 -- Changed the client-server relationship
##                    so it actually works...
##     5/26/97 Version 2.2 -- Now has a seperate config file and builds
##                    for different 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.conf
#
#########################################################################

. /etc/autoupdate.conf

############################################################################
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 [ "$OTHERDIRS" != "" ] ; then
   echo "Placing copies of all new RPMs in:"
   for i in $OTHERDIRS
   do
      echo "   $i"
   done
fi
echo
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`

   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..."
				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
