#!/bin/ksh
###############################################################################
# installremoved.sh
#
#  Attempts to install RPMS which match versions which had been previously
#  removed by the destroyRPMS script.
#
###############################################################################
# (C) COPYRIGHT International Business Machines Corp. 2000,2002
# All Rights Reserved
#
# Disclaimer:  This is an unsupported, unwarranted script provided
# for the convenience of users of the 'AIX Toolbox for Linux Applications'.
# It will automatically install RPMS onto your system.
# Use at your own risk.
#
# No Warranty:
# THIS CODE IS PROVIDED BY IBM "AS IS." TO THE EXTENT PERMITTED BY APPLICABLE
# LAW, IBM MAKES NO WARRANTIES OR CONDITIONS EITHER EXPRESS OR IMPLIED,
# INCLUDING WITHOUT LIMITATION ANY WARRANTY OF NON-INFRINGEMENT AND THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
# REGARDING THE CODE OR TECHNICAL SUPPORT, IF ANY.
#
# Limitation of Liability:
# NEITHER IBM NOR ITS SUPPLIERS ARE LIABLE FOR ANY DIRECT OR INDIRECT DAMAGES,
# INCLUDING WITHOUT LIMITATION, LOST PROFITS, LOST SAVINGS, OR ANY INCIDENTAL,
# SPECIAL, OR OTHER ECONOMIC CONSEQUENTIAL DAMAGES, EVEN IF IBM IS INFORMED OF
# THEIR POSSIBILITY. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR
# LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THE ABOVE EXCLUSION OR
# LIMITATION MAY NOT APPLY TO YOU.
###############################################################################

if [[ $# -eq 0 ]] || [[ $1 = -* ]]
then
   echo "Usage: installremoved.sh <directory>"
   echo "       Attempts to install RPMS which match versions which"
   echo "       had been previously removed by the destroyRPMS script."
   echo ""
   echo "Default installation flags are -hUv.  Set RPMFLAGS to use different flags."
   echo ""
   echo "This script uses information left in /var/adm/sw/removedrpms"
   echo "to determine which RPMs to attempt to install.  If the environment"
   echo "variable RPMLIST is set, that file will be used instead."
   echo "All files in the RPMLIST file will be attempted to  be installed"
   echo "if there is a matching package in the directory and no version"
   echo "of that package is already installed at some level."
   echo "If additional software needs to be added to the list to satisfy an"
   echo "unsatisfied dependency, simply append the name of the RPM package"
   echo "to that file."
   echo 
   echo "Set DEBUG=1 to just get a list of packages which would be installed."
   echo 
   exit 1
fi

umask 022
DIR=$1
DIRLIST=/tmp/dirrpms.$$
NEWRPMLIST=/tmp/instrpms.$$

lslpp -l rpm.rte >/dev/null 2>&1 || {
   print -u2 You need to reinstall rpm.rte first
   exit 8 
}

[[ -z "$RPMLIST" ]] && RPMLIST=/var/adm/sw/removedrpms
if [[ ! -f "$RPMLIST" ]] || [[ ! -r "$RPMLIST" ]]
then
    echo "Error: could not access directory $RPMLIST file"
    exit 2
fi

if [[ ! -z "$DIR" ]]
then
    cd $DIR 2>/dev/null
    if [[ $? -ne 0 ]] 
    then
        echo "Error: could not access directory $DIR"
	exit 1
    fi
    echo "Determining RPMs available in $DIR...\c"
    # Get paired list of binary RPMS and filenames
    /bin/ls | while read pkgfile
    do
       [[ ! -f $pkgfile ]] && continue
       pkg=`rpm -q --queryformat '%{NAME}\n' -p $pkgfile 2>/dev/null`
       [[ -n "$pkg" ]] && {
	  echo $pkg:$pkgfile
       }
    done > $DIRLIST
    echo
fi

[[ -z "$RPMFLAGS" ]] && RPMFLAGS="-hUv"

> /tmp/newrpms.$$

echo "Determining packages which need to be installed...\c"
while read rpm
do
   installed=`rpm -q --queryformat '%{NAME}\n' $rpm 2>/dev/null`
   if [[ -z "$installed" ]]
   then
      /bin/grep -q "^$rpm:" $DIRLIST && echo $rpm
      if [[ $? -eq 0 ]]
      then
	 # Add possibly new dependencies
	 case $rpm in 
	   apache)
	       echo openssl
	       ;;
	   autoconf)
	       echo m4
	       ;;
	   automake|sawfish|librep|python|samba-client|samba-common|bc)
	       echo readline
	       ;;
	   kdebase|kdegames)
	       echo kdelibs-sound
	       echo openssl
	       echo prngd
	       echo xpm
	       ;;
	   kdelibs)
	       echo openssl
	       echo prngd
	       ;;
	   kdesupport)
	       echo gdbm
	       ;;
	   gawk)
	       echo gettext
	       ;;
	   gdk-pixbuf|gimp|imlib|kdelibs)
	       echo libtiff
	       ;;
	   ImageMagick)
	       echo bzip2
	       echo freetype
	       echo libjpeg
	       echo libpng
	       echo libtiff
	       echo zlib
	       ;;
	   tkinter)
	       echo tcl
	       echo tk
	       ;;
	   xscreensaver)
	       echo bash2
	       ;;
	 esac 
      else
	 # Manage replaced RPMS
	 case $rpm in 
	   kdemultimedia)
	       print -u2 "kdemultimedia is not currently distributed"
	       continue
	       ;;
	   korganizer) 
	       rpm=kdepim
	       grep -q "^$rpm:" $DIRLIST && echo $rpm && continue
	       ;;
	   qt1x) 
	       rpm=qt
	       grep -q "^$rpm:" $DIRLIST && echo $rpm && continue
	       ;;
	   qt1x-devel) 
	       rpm=qt-devel
	       grep -q "^$rpm:" $DIRLIST && echo $rpm && continue
	       ;;
	 esac
	 print -u2 "$rpm not found in $DIR - discontinued?  Continuing.."
      fi
   fi
done < $RPMLIST > $NEWRPMLIST
echo

if [[ -s "$NEWRPMLIST" ]]
then
   sort -u $NEWRPMLIST | while read rpm
   do
      rpm -q $rpm >/dev/null 2>&1 && continue
      grep "^$rpm:" $DIRLIST | cut -d: -f2
   done > /tmp/newrpms.$$

   if [[ -s /tmp/newrpms.$$ ]]
   then
      if [[ -n "$DEBUG" ]] 
      then
        cat /tmp/newrpms.$$
      else
        echo Invoking rpm...
        rpm $RPMFLAGS `cat /tmp/newrpms.$$`
      fi
   else
      print -u2 "$0: No RPMs in $DIR to be installed"
   fi
else
      print -u2 "$0: No uninstalled RPMs in $DIR were found."
fi

rm -f /tmp/newrpms.$$ $DIRLIST $NEWRPMLIST

exit 0
# END
