#!/bin/sh
##############################################################################
# HP Smart Update Manager (HP SUM) RHEL6 Minimum Support Verification Script #
# Last Modified: Feb 3, 2011                                                 #
# Copyright 2011 Hewlett-Packard Development Company L.P."                   #
# Version 1.0.0                                                              #
##############################################################################

#main
tput bold
tput setaf 4
echo "HP Smart Update Manager RHEL6 Minimum Support Verification Script"
echo "Copyright 2011 Hewlett-Packard Development Company, L.P."
tput sgr0
echo ""
echo ""
tput bold
echo "Checking to ensure minimal support requirements needed by"
echo "HP Smart Update Manager (HP SUM) to support"
echo "Red Hat Enterprise Linux version 6 are met..."
tput sgr0
echo ""
RELFILE="/etc/redhat-release"
if [ `grep -ic "red hat enterprise linux server release 6." ${RELFILE}` -gt 0 ]
then
  echo "Red Hat Enterprise Linux Server Release 6.x found"
else
  tput setaf 1
  echo "Red Hat Enterprise Linux Server Release 6.x not found.  Script exiting"
  tput sgr0
  exit 1
fi

ARCH=`uname -m`
if [ "$ARCH" = "i686" ]
then
  echo "32-bit distribution found"
elif [ "$ARCH" = "x86_64" ]
then
  echo "x86_64 distribution found"
else
  tput setaf 1
  echo "Unsupported $ARCH distribution of Red Hat Enterprise Linux Server version 6.x found"
  tput sgr0
  exit 1
fi
  
MIN_CONSOLE_RPMS="lm_sensors-libs net-snmp-libs net-snmp redhat-rpm-config kernel-headers kernel-devel rpm-build gcc"
MIN_GUI_I686_RPMS="libuuid freetype libSM libICE libXi libX11 libxcb libXext libXau libXrender libXrandr libXfixes libXcursor fontconfig expat expect zlib libstdc++ net-snmp"

#Checking minimum RPMs needed to run in console mode
CONSOLE_COUNT=0
tput bold
echo "Checking for minimum RPMs needed to support HPSUM in console mode"
tput sgr0
for i in $MIN_CONSOLE_RPMS
do
#  echo "Checking RPM $i"
  rpm -q $i >/dev/null 2>&1
  if [ $? -ne 0 ]
  then
    tput setaf 1
    echo "The $i RPM is not installed on this server."
    let "CONSOLE_COUNT += 1" 
    tput sgr0
  fi
  shift
done
if [ $CONSOLE_COUNT -eq 0 ]
then
  tput setaf 2
  echo "All RPMs necessary to run in console mode are installed"
  tput sgr0
fi
#Checking minimum RPMS needed to run in GUI mode
GUI_COUNT=0
tput bold
echo "Checking for minimum i686 RPMs needed to support HPSUM in GUI mode"
tput sgr0
for i in $MIN_GUI_I686_RPMS
do
#  echo "Checking RPM $i"
  rpm -q $i >/dev/null 2>&1
  RC=$?
  if [ $RC -ne 0 ]
  then
    tput setaf 1
    echo "The $i RPM is not installed on this server."
    let "GUI_COUNT += 1" 
    tput sgr0
  fi
  if [ $RC -eq 0 ]
  then
#Now check it's architecture to ensure it is an i686 version
    ARCH_INSTALLED=`rpm -q --queryformat '%{ARCH}\n' $i >/dev/null 2>&1`
    for j in $ARCH_INSTALLED
    do
      if [ -n "$j" -a "$j" != "i686" ]
      then
        tput setaf 1
        echo "The i686 version of the $i RPM is not installed on this server"
        tput sgr0
      fi
    done
  fi
  shift
done
if [ $GUI_COUNT -eq 0 ]
then
  tput setaf 2
  echo "All RPMs necessary to run in GUI mode are installed"
  tput sgr0
fi
echo ""
echo ""
tput bold
echo "Checking to ensure networking is enabled..."
tput sgr0
if [ "`/sbin/ifconfig | grep -v "127.0.0.1" | grep -c 'inet '`" = "0" ] 
then
  tput setaf 1
  echo "IP address for IPv4 network is required to run HP SUM"
  echo "Please use the dhclient command to start networking"
  echo "on one of the enabled network interfaces."
  tput sgr0
  exit 1
else
  tput setaf 2
  echo "Valid networking configuration to support HP SUM found."
  tput sgr0
fi
echo ""
echo ""
if [ $GUI_COUNT -gt 0 -o $CONSOLE_COUNT -gt 0 ]
then
  exit 1
fi
exit 0

