#!/usr/bin/ksh
###############################################################################
#
# destroyRPMS
#
#   Usage: destroyRPMS [-f]
#       -f fast -- don't prompt the user up front.
#
#   Uninstalls all RPM images on the system.
#   Removes the RPM database and rpm.rte as well (unless KEEPRPM is set
#   in the environment).
#
###############################################################################
# (C) COPYRIGHT International Business Machines Corp. 2000,2003
# 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 destroy RPMS from 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.
###############################################################################

LANG=C ; export LANG
PATH=/usr/bin:/usr/sbin ; export PATH
if [[ $(id -un) != "root" ]]
then
   echo "ERROR: you must run this script as root."
   exit 1
fi

lslpp -l rpm.rte >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
  echo "rpm.rte is not installed on this system."
  echo "There is nothing to remove."
  exit 0
fi

if [ "$1" != "-f" ]
then
  #
  # Make sure the user understands what this means.
  #
  echo "WARNING:"
  echo "This script removes all RPMS installed on the system,"
  echo "removes the RPM database, and removes the rpm.rte install"
  echo "image.  It also removes root configuration for GNOME and KDE."
  echo "You should exit KDE and GNOME and run this from a root console."
  echo 
  echo "You will most likely see several error messages because all RPMS"
  echo "are being clobbered in no particular order, so removal of dependencies"
  echo "may cause some deinstall scripts to fail and many 'directory not"
  echo "empty' messages to appear.  Since your goal is removal of all RPMS,"
  echo "these errors should not be of concern."
  echo 
  echo "Proceed to destroy all RPMs on the system? y/(n) \c"
  read ans < /dev/tty
  if [ -z "$ans" ] || [ "$ans" != "y" ]
  then
    echo Exiting.
    exit 0
  fi
umask 022
fi


rpm -qa --queryformat '%{NAME}\n' | /bin/egrep -v "AIX-rpm|SysProvides" | sort > /var/adm/sw/removedrpms

# Clear out the database, but not the files for things installed with 
#  rpm.rte.  Those have to be removed last.
# Because the files from these packages will get left behind, we need to
#  save a list of them, so we can remove them explicitly at the end.
rpm -ql bzip2 info gettext gzip patch popt rpm rpm-build rpm-devel zlib \
     >/tmp/destRPM.rmfiles.$$  2>/dev/null
rpm -e bzip2 info gettext gzip patch popt rpm rpm-build rpm-devel zlib \
     AIX-rpm SysProvides --nodeps --justdb 2>/dev/null

if [[ -z "$KEEPRPM" ]]
then
   rpm -qa | xargs rpm -ev --nodeps
else
   #This may not work well; if you really want to keep rpm.rte, you should
   # just install it again after running this script.
   rpm -qa | egrep -v "AIX-rpm|SysProvides" | xargs rpm -ev --nodeps
fi

RC=0
if [[ -z "$KEEPRPM" ]]
then
   rm -rf /var/opt/freeware/lib/rpm/*
   installp -u rpm.rte
   RC=$?

   # Remove any remaining files in saved list that belonged to
   # base rpm.rte RPM images.
   for fname in $(cat /tmp/destRPM.rmfiles.$$) ; do
	[[ -f $fname ]] && rm $fname 2>/dev/null
   done
fi
rm /tmp/destRPM.rmfiles.$$ 2>/dev/null

# Remove any leftover empty directories under /opt/freeware
for dname in $(find /opt/freeware -type d -depth |grep -v "lost+found$") ; do
    rmdir $dname 2>&1   # if not empty, let it quietly fail.
done

if [[ $RC -ne 0 ]] ; then 
   echo 
   echo "PLEASE NOTE:"
   echo "Your rpms have been removed, but rpm.rte did not deinstall."
   echo "Most likely this means that you have another installp fileset that"
   echo "lists rpm.rte as a dependency."
   echo "If you really want to remove rpm.rte, you will need to deinstall those"
   echo "dependencies as well.  If you do not, then you still have rpm.rte"
   echo "installed, but it is now non-functional.  To make it functional again,"
   echo "simply reinstall rpm.rte with the force option.  For example, change"
   echo "to the directory where you have the rpm.rte image and run:"
   echo "    installp -qacXFd rpm.rte all"
   echo
fi

echo
echo "Your rpms have been removed, but the /opt/freeware and /usr/opt/freeware"
echo "directories (one is just a symbolic link) have been left present."
echo "You may want to look through these directories for any files you may have"
echo "created yourself.  If there is nothing that you want to save, you can now"
echo "remove these directories and any remaining contents within."

exit $RC
# END OF SCRIPT
