#!/usr/bin/ksh
###############################################################################
# installmissing.sh
#
#  Attempts to install all RPMS which are not already installed, given a
#  directory containing RPM images.
#
###############################################################################
# (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: installmissing.sh <directory>"
   echo "       Attempts to install RPMS which are not already installed"
   echo ""
   echo "Default flags are -hUv.  Set RPMFLAGS to use different flags."
   echo ""
   echo "This script does not detect duplicate entries or situations where"
   echo "newer versions are already installed."

   exit 1
fi

DIR=$1
if [[ ! -z "$DIR" ]]
then
    cd $DIR 2>/dev/null
    if [[ $? -ne 0 ]] 
    then
        echo "Error: could not access directory $DIR"
	exit 1
    fi
fi

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

> /tmp/newrpms.$$
for rpm in *.rpm
do
   namever=`rpm -q --queryformat '%{NAME}-%{VERSION}\n' -p $rpm 2>/dev/null`
   if [[ -n "$namever" ]]
   then
      rpm -q $namever >/dev/null 2>&1 || echo $rpm >> /tmp/newrpms.$$
   fi
done

if [[ -s /tmp/newrpms.$$ ]]
then
   rpm $RPMFLAGS `cat /tmp/newrpms.$$`
else
   echo "$0: No RPMs in $DIR to be installed"
fi

rm -f /tmp/newrpms.$$

exit 0
# END
