#!/bin/bash
HPSUM_DIR="/opt/hp/hpsum/"

if [ ! -f "${HPSUM_DIR}/bin/hpsum_bin" ] && [ -f "./hpsum_bin" ] ; then
    HPSUM_DIR=$PWD
fi

if [ ! -d "${HPSUM_DIR}" ]  ; then
	cat<<END>/dev/stderr
************************************************
ERROR Couldn't find hpsum directory
************************************************
END
	exit 1
fi


export LD_LIBRARY_PATH=$HPSUM_DIR/lib
chmod -R 777 ${HPSUM_DIR}bin

TRUE=1
FALSE=0

##############################################################################
# HP Smart Update Manager (HP SUM) RHEL6 Minimum Support Verification Script #
# Copyright 2011 Hewlett-Packard Development Company L.P."                   #
# Version 5.2.0                                                              #
##############################################################################


function display_header()
{
	tput bold
	tput setaf 4
	echo "HP Smart Update Manager 5.2.0 Minimum Support Verification Script"
	echo "Copyright 2012 Hewlett-Packard Development Company, L.P."
	tput sgr0
	echo ""
	tput bold
	echo "This script verifies that the system meets the minimum requirements"
	echo "for HP Smart Update Manager 5.2.0 (HP SUM) on $OS"
	echo ""
	# Get_linux_flavor
	tput setaf 1
	echo "$OSFLAV"
	tput sgr0
	
	echo ""
}

function collect_local_info()
{
	PWD=`pwd`
	
	if [ -f /etc/redhat-release ]; then
		grep "Red Hat" /etc/redhat-release > /dev/null
		if [ $? -eq 0 ]; then
			OS="RHEL"
		else 
			echo "Unsupported operating system"
		fi
		OS_VER="`cat /etc/*-release | cut -d ' ' -f7 | grep -v -m 1 '^$'`"
		#OS="`echo ${OS_VER:0:5} | tr '[:lower:]' '[:upper:]'`"
		MAJORVER="`echo ${OS_VER} | cut -d '.' -f1`"
		MINORVER="`echo ${OS_VER} | cut -d '.' -f2`"
		OSFLAV="`cat /etc/*-release | grep -v -m 1 "Kernel" | grep -v '^$'`"
	elif [ -f /etc/SuSE-release ]; then
		grep "SUSE" /etc/SuSE-release > /dev/null
		if [ $? -eq 0 ]; then
			OS="SLES"
		else
			echo "Unsupported operating system"
		fi
		SLESDIR="`ls /etc/*-release | grep [Ss][Uu][Ss][Ee]-release`"
		OS_VER="`cat $SLESDIR | grep 'VERSION' | cut -d ' ' -f3`"
		MAJORVER=$OS_VER
		OSFLAV="`cat $SLESDIR | grep "SUSE"`"
	else 
		grep "SUSE" /etc/issue > /dev/null
		if [ $? -eq 0 ]; then
			OS="SLES"
			OSFLAV="`cat /etc/issue | grep -m 1 "SUSE" | grep -v '^$''"
		elif [ -f /etc/redhat-release ]; then
			OS="RHEL"
			OSFLAV="`cat /etc/issue | grep -v "Kernel" | grep -v '^$''"
		else
			tput setaf 1
			echo "Unsupported Operating System. Exiting..."
			tput sgr0
			exit 1
		fi
		OS_VER="`cat /etc/issue | cut -d ' ' -f7 | grep -v '^$'`"
		#OS="`echo ${OS_VER:0:5} | tr '[:lower:]' '[:upper:]'`"
		MAJORVER="`echo ${OS_VER} | cut -d '.' -f1`"
		MINORVER="`echo ${OS_VER} | cut -d '.' -f2`"		
	fi
	
	iter=1

	# Get_linux_flavor
	#tput setaf 1
	#cat /etc/issue | grep -v "Kernel" | grep -v '^$'
	#tput sgr0

	#tput setaf 4
	IS_ARCH_x64=$FALSE
	ARCH=`uname -m`
	if [ "$ARCH" = "i686" -o "$ARCH" = "i586" -o "$ARCH" = "i386" ]
	then
		IS_ARCH_x64=$FALSE
		# echo "32-bit distribution found"
	elif [ "$ARCH" = "x86_64" ]
	then
		IS_ARCH_x64=$TRUE
		# echo "x86_64 distribution found"
	else
	  tput setaf 1
	  echo "Unsupported $ARCH distribution found."
	  tput sgr0
	  exit 1
	fi
	tput sgr0
	echo ""
}

function check_dependencies()
{
	#this is an older list - kept in this file as a reference as they may be introduced again in the future as an enhancement
	
	#SLES10 package specifics for x86 and x64
	# this list includes the SPP dependencies - currently removed and will be a future feature support.
	#SLES10_x86_LIST="kernel-syms rpm gcc expect gawk sed perl libnl tcl binutils glibc libxml2 libstdc++33 freetype xorg-x11-libs fontconfig expat zlib libstdc++ pciutils bash net-snmp kernel-devel"
	#SLES10_x64_LIST="kernel-syms rpm gcc expect gawk sed perl libnl tcl binutils glibc-32bit libxml2-32bit libstdc++33-32bit freetype-32bit xorg-x11-libs-32bit fontconfig-32bit expat-32bit zlib-32bit libstdc++33-32bit pciutils bash net-snmp kernel-devel"

	#SLES11 package specifics for x86 and x64
	#SLES11_x86_LIST="kernel-syms rpm gcc expect gawk sed perl libnl kernel-devel tcl binutils net-snmp glibc glibc-devel libxml2 libstdc++ libstdc++33 libstdc++43 libuuid1 freetype xorg-x11-libSM xorg-x11-libICE xorg-x11-libX11 xorg-x11-libXext xorg-x11-libxcb xorg-x11-libXau xorg-x11-libXrender xorg-x11-libXfixes fontconfig expat zlib compat pciutils"
	#SLES11_x64_LIST="kernel-syms rpm gcc expect gawk sed perl libnl kernel-devel tcl binutils glibc-32bit glibc-devel-32bit libxml2-32bit libstdc++-32bit libstdc++33-32bit libstdc++43-32bit libuuid1-32bit freetype-32bit xorg-x11-libSM-32bit xorg-x11-libICE-32bit xorg-x11-libX11-32bit xorg-x11-libXext-32bit xorg-x11-libxcb-32bit xorg-x11-libXau-32bit xorg-x11-libXrender-32bit xorg-x11-libXfixes-32bit fontconfig-32bit expat-32bit zlib-32bit libsensors3 perl-SNMP compat-32bit pciutils-32bit bash net-snmp"


	#RHEL 5 specifics for x86 and x64
	#RHEL5_x86_LIST="net-snmp-libs net-snmp kernel-headers redhat-rpm-config kernel-devel rpm-build rpm-devel gcc glibc glibc-devel cpp pciutils lm_sensors expat compat-libstdc++-296 compat-libstdc++-33 libstdc++ libstdc++-devel "
	#RHEL5_x86_SO_LIST="/lib/libc.so.6 /usr/lib/libxml2.so /usr/lib/libfreetype.so /usr/lib/libSM.so /usr/lib/libICE.so /usr/lib/libXi.so /usr/lib/libX11.so /usr/lib/libXext.so /usr/lib/libXau.so /usr/lib/libXrender.so /usr/lib/libXrandr.so /usr/lib/libXfixes.so /usr/lib/libXcursor.so /usr/lib/libfontconfig.so /usr/lib/libz.so"
	#RHEL5_x64_LIST="net-snmp-libs net-snmp kernel-headers redhat-rpm-config kernel-devel rpm-build rpm-devel gcc glibc(x86-32) glibc-devel cpp pciutils(x86-32) lm_sensors expat compat-libstdc++-296 compat-libstdc++-33 libstdc++ libstdc++-devel libstdc++44-devel"
	#RHEL5_x64_SO_LIST="/lib/libc.so.6 /usr/lib/libxml2.so /usr/lib/libfreetype.so /usr/lib/libSM.so /usr/lib/libICE.so /usr/lib/libXi.so /usr/lib/libX11.so /usr/lib/libXext.so /usr/lib/libXau.so /usr/lib/libXrender.so /usr/lib/libXrandr.so /usr/lib/libXfixes.so /usr/lib/libXcursor.so /usr/lib/libfontconfig.so /usr/lib/libz.so"

	#RHEL 6 specifics for x86 and x64
	#RHEL6_x86_LIST="net-snmp-libs net-snmp kernel-headers redhat-rpm-config kernel-devel rpm-build rpm-devel gcc glibc glibc-devel cpp pciutils lm_sensors-libs libxml2 compat-libstdc++-33 libuuid freetype libSM libICE libXi libX11 libXext libxcb libXau libXrender libXrandr libXfixes libXcursor fontconfig expat expect zlib libstdc++"
	#RHEL6_x64_LIST="net-snmp-libs net-snmp kernel-headers redhat-rpm-config kernel-devel rpm-build rpm-devel gcc glibc(x86-32) glibc-devel cpp pciutils(x86-32) lm_sensors-libs libxml2(x86-32) compat-libstdc++-33(x86-32) libuuid(x86-32) freetype(x86-32) libSM(x86-32) libICE(x86-32) libXi(x86-32) libX11(x86-32) libXext(x86-32) libxcb(x86-32) libXau(x86-32) libXrender(x86-32) libXrandr(x86-32) libXfixes(x86-32) libXcursor(x86-32) fontconfig(x86-32) expat(x86-32) expect zlib(x86-32) libstdc++(x86-32)"



	# Current list introduced with HPSUM5.2.0 rpm

	#SLES10 package specifics for x86 and x64
	SLES10_x86_LIST="rpm pciutils glibc libxml2 libstdc++33 freetype xorg-x11-libs fontconfig expat zlib libstdc++ bash"
	SLES10_x64_LIST="rpm pciutils glibc-32bit libxml2-32bit libstdc++33-32bit freetype-32bit xorg-x11-libs-32bit fontconfig-32bit expat-32bit zlib-32bit libstdc++ bash"
	
	#SLES11 package specifics for x86 and x64
	SLES11_x86_LIST="rpm pciutils libxml2 libstdc++33 freetype xorg-x11-libSM xorg-x11-libICE xorg-x11-libX11 xorg-x11-libXext xorg-x11-libxcb xorg-x11-libXau xorg-x11-libXrender xorg-x11-libXfixes fontconfig zlib compat bash"
	SLES11_SO_LIST="/lib/libexpat.so.1 /usr/lib/libstdc++.so.6"

	SLES11_x64_LIST="rpm pciutils glibc-32bit libxml2-32bit libstdc++33-32bit freetype-32bit xorg-x11-libSM-32bit xorg-x11-libICE-32bit xorg-x11-libX11-32bit xorg-x11-libXext-32bit xorg-x11-libxcb-32bit xorg-x11-libXau-32bit xorg-x11-libXrender-32bit xorg-x11-libXfixes-32bit fontconfig-32bit zlib-32bit compat-32bit bash"

	#RHEL 5 specifics for x86 and x64
	RHEL5_x86_LIST="rpm pciutils expat compat-libstdc++-296 compat-libstdc++-33"
	
	RHEL5_x86_SO_LIST="/usr/lib/libstdc++.so.6 /lib/libc.so.6 /usr/lib/libxml2.so.2 /usr/lib/libfreetype.so.6 /usr/lib/libSM.so.6 /usr/lib/libICE.so.6 /usr/lib/libXi.so.6 /usr/lib/libX11.so.6 /usr/lib/libXext.so.6 /usr/lib/libXau.so.6 /usr/lib/libXrender.so.1 /usr/lib/libXrandr.so.2 /usr/lib/libXfixes.so.3 /usr/lib/libXcursor.so.1 /usr/lib/libfontconfig.so.1 /usr/lib/libz.so.1"
	
	RHEL5_x64_LIST="rpm pciutils expat compat-libstdc++-296 compat-libstdc++-33"

	RHEL5_x64_SO_LIST="/usr/lib/libstdc++.so.6 /lib/libc.so.6 /usr/lib/libxml2.so.2 /usr/lib/libfreetype.so.6 /usr/lib/libSM.so.6 /usr/lib/libICE.so.6 /usr/lib/libXi.so.6 /usr/lib/libX11.so.6 /usr/lib/libXext.so.6 /usr/lib/libXau.so.6 /usr/lib/libXrender.so.1 /usr/lib/libXrandr.so.2 /usr/lib/libXfixes.so.3 /usr/lib/libXcursor.so.1 /usr/lib/libfontconfig.so.1 /usr/lib/libz.so.1"


	#RHEL 6 specifics for x86 and x64
	RHEL6_x86_LIST="rpm glibc pciutils libxml2 compat-libstdc++-33 freetype libSM libICE libXi libX11 libXext libxcb libXau libXrender libXrandr libXfixes libXcursor fontconfig expat zlib libstdc++"
	
	RHEL6_x64_LIST="rpm glibc(x86-32) pciutils libxml2(x86-32) compat-libstdc++-33(x86-32) freetype(x86-32) libSM(x86-32) libICE(x86-32) libXi(x86-32) libX11(x86-32) libXext(x86-32) libxcb(x86-32) libXau(x86-32) libXrender(x86-32) libXrandr(x86-32) libXfixes(x86-32) libXcursor(x86-32) fontconfig(x86-32) expat(x86-32) zlib(x86-32) libstdc++(x86-32)"
	

	case $OS in

		"RHEL" )
			if [ "$MAJORVER" == "5" ]; then
				if [ $IS_ARCH_x64 -eq $FALSE ]; then #If ARCH is 64-bit then only check for acceptable packages.
					FULL_LIST_RPM=$RHEL5_x86_LIST
					FULL_LIST_SO=$RHEL5_x86_SO_LIST
				else
					FULL_LIST_RPM=$RHEL5_x64_LIST
					FULL_LIST_SO=$RHEL5_x64_SO_LIST
				fi
			elif [ "$MAJORVER" == "6" ]; then
				#if [ "$MINORVER" == "0" -o "$MINORVER" == "1" -o "$MINORVER" == "2" -o "$MINORVER" == "3" ]; then
				if [ "$MINORVER" -lt "4" ]; then
					if [ $IS_ARCH_x64 -eq $FALSE ]; then #If ARCH is 64-bit then only check for acceptable packages.
						FULL_LIST_RPM=$RHEL6_x86_LIST
					else
						FULL_LIST_RPM=$RHEL6_x64_LIST
					fi
				else
					echo "Unsupported OS. Exiting... "
					exit 1
				fi
			else
				echo "Unsupported OS. Exiting... "
				exit 1
			fi
		;;
		"SLES" )
			if [ "$MAJORVER" == "10" ]; then
				if [ $IS_ARCH_x64 -eq $FALSE ]; then
					FULL_LIST_RPM=$SLES10_x86_LIST
				else
					#COMMON_CONSOLE_RPM_MUST_x86_LIST=$LINUX_COMMON_LIST
					FULL_LIST_RPM=$SLES10_x64_LIST
				fi
				
			elif [ "$MAJORVER" == "11" ]; then
				if [ $IS_ARCH_x64 -eq $TRUE ]; then
					#Checking to see if we are not running in offline mode
					#echo "Checking to see HPSUM run mode..."
					if [ "$HDU_BOOTENV_SMPJTB" == "yes" ]; then
						tput setaf 2
						#echo "Offline mode."
						tput sgr0
					else
						tput setaf 2
						#echo "Online mode."
						tput sgr0
						
						FULL_LIST_RPM=$SLES11_x64_LIST
						FULL_LIST_SO=$SLES11_SO_LIST
					fi
				else
						FULL_LIST_RPM=$SLES11_x86_LIST
						FULL_LIST_SO=$SLES11_SO_LIST
				fi
			else
				echo "Unsupported OS. Exiting... "
				exit 1
			fi
		;;
		* )
			echo "Unknown OS identified"
			exit 1
		;;
	esac

	tput bold
	echo "Checking for RPMs needed to run HPSUM..."
	tput sgr0

	FAIL_COUNT=0
	# Checking for minimum RPMS to run HPSUM in Console mode console
	for i in $FULL_LIST_RPM
	do
		echo -n "Checking for $i  "
		CHECK_ARCH=`echo $i | grep "x86-32"`
		if [ ! -s $CHECK_ARCH ]; then
			IS_X86_PKG_REQD=$TRUE
			PKG_NAME=$(echo "$i" | sed "s/(x86-32)//g")
		else
			IS_X86_PKG_REQD=$FALSE
			PKG_NAME=$i
		fi

		INSTALLED_RPMS=`rpm -qa | grep "^$PKG_NAME"`
		if [ "$INSTALLED_RPMS" == "" ]; then
			tput setaf 1
			echo "Missing"
			tput sgr0
			let "FAIL_COUNT += 1"
		elif [ $IS_X86_PKG_REQD -eq $TRUE ]; then
			PKG_END_NAME=`rpm --qf '%{ARCH}' -q $PKG_NAME`
			#if [ "$PKG_END_NAME"  "i686" -o "$PKG_END_NAME" == "i586" -o "$PKG_END_NAME" == "i386" -o "$PKG_END_NAME" == "noarch" ]; then
			CHECK_i686=`echo "$PKG_END_NAME" | grep -E "i686|i586|i386|noarch"`
			#SILENT_FLAG=`echo $IN_ARGS | grep -E -- "--s|-s|/s"`

			if [ -z $CHECK_i686 ]; then
				tput setaf 1
				echo "Missing"
				tput sgr0
				let "FAIL_COUNT += 1"
			else
				tput setaf 2
				echo "Present"
				tput sgr0
			fi

		else
			
			tput setaf 2
			echo "Present"
			tput sgr0
		fi
	done
	
	
	#Checking minimum .so needed to run HPSUM in Console mode
	CONSOLE_SO_COUNT=0
	#LIB_PATH="/usr/lib/"
	# tput bold
	# echo "Checking for .so needed to run HPSUM in Console mode..."
	# tput sgr0
	#for i in $RHEL5_CONSOLE_SO_LIST

	for i in $FULL_LIST_SO
	do
		echo -n "Checking for $i  "
		files=$(ls $i* 2> /dev/null | wc -l)
		if [ "$files" = "0" ] ; then
			tput setaf 1
			echo "Missing"
			tput sgr0
			let "CONSOLE_SO_COUNT += 1"
		else
			tput setaf 2
			echo "Present"
			tput sgr0
		fi
	done
	
	return $(($FAIL_COUNT + $CONSOLE_SO_COUNT))
}


function add_zypper_repo()
{
	if [ "$1" != "" ]; then
		if [ "$OS" == "SLES" ]; then
			if [ ! -f /usr/bin/zypper ]; then
				echo "zypper not found."
				exit 1
			fi
			
			if [ "$MAJORVER" == "10" ]; then
				result=1
				counter=1
				while [ "$result" != "0" -a "$result" != "3" ]
				do
				        /usr/bin/zypper service-add $1 "hpsum$counter" > /tmp/zypperadd.log 2>&1
				        result=$?
				        counter=$(( $counter + 1 ))
				done
				cat /tmp/zypperadd.log
				rm /tmp/zypperadd.log > /dev/null

			elif [ "$MAJORVER" == "11" ]; then  
				result=1
                                counter=1
				while [ "$result" != "0" -a "$result" != "3" ]
                                do
                                        /usr/bin/zypper ar $1  "hpsum$counter" > /tmp/zypperadd.log 2>&1
                                        result=$?
                                        counter=$(( $counter + 1 ))
                                done
                                cat /tmp/zypperadd.log
                                rm /tmp/zypperadd.log > /dev/null
			else
				echo "Unsupported OS"
			fi
		else
			echo "Unsupported OS for --addzypperrepo"
		fi
	fi
}


function add_yum_repo()
{
	if [ "$1" != "" ]; then
		if [ "$OS" == "RHEL" ]; then
			if [ ! -f /usr/bin/yum ]; then 
				echo "yum not found."
				exit 1
			fi
			if [ ! -f /etc/yum.repos.d/hpsum.repo ]; then
				echo "[hpsum]" > /etc/yum.repos.d/hpsum.repo
				echo "name=HP Smart Update Manager" >> /etc/yum.repos.d/hpsum.repo
				echo "baseurl=$1" >> /etc/yum.repos.d/hpsum.repo
				echo "enabled=0" >> /etc/yum.repos.d/hpsum.repo
				echo "gpgcheck=1" >> /etc/yum.repos.d/hpsum.repo
				echo "gpgkey=file://www.hp.com/rpm-gpg/RPM-GPG-KEY-hpsum" >> /etc/yum.repos.d/hpsum.repo
				rpm --import file://www.hp.com/rpm-gpg/RPM-GPG-KEY-hpsum
			fi
		fi
	fi
}




#main
clear

#check for input arguments
# there are a couple of inputs we are interested in:
# --nodeps
# HPSUM's help flags: -h /h -help /help
# --addzypperrepo  -- future support
# --addyumrepo     -- future support

HELP_QM1=`echo "$@" | grep -- "-?"`
HELP_QM2=`echo "$@" | grep -- "/?"`
HELP_QM3=`echo "$@" | grep -- "^-h"`
HELP_QM4=`echo "$@" | grep -- "^/h"`

if [ "$HELP_QM1" != "" -o "$HELP_QM2" != "" -o "$HELP_QM3" != "" -o "$HELP_QM4" != "" ]; then
	pushd ${HPSUM_DIR}bin > /dev/null
	man hpsum
	RETCODE=$?
	popd >/dev/null
	exit $RETCODE
fi


collect_local_info
display_header


#ADD_ZYPPER_REPO=`echo "$@" | grep -- "--addzypperrepo"`
#if [ -n "$ADD_ZYPPER_REPO" ]; then
#	add_zypper_repo $2
#	exit
#fi


#ADD_YUM_REPO=`echo "$@" | grep -- "--addyumrepo"`
#if [ -n "$ADD_YUM_REPO" ]; then
#	add_yum_repo $2
#	exit
#fi

# see if grep will provide an emtpy string or not
NODEPS_INPUT=`echo "$@" | grep -- "--nodeps"`
NODEPS_FLAG=$FALSE
if [ -n "$NODEPS_INPUT" ]; then
	NODEPS_FLAG=$TRUE
fi

#extract --nodeps, and retain the input arguments
#IN_ARGS=$(echo "$@" | sed "s/\(--nodeps\)\|\(-nodeps\)\|\(\/nodeps\)//g")
IN_ARGS=`echo "$@" | sed "s/--nodeps//g"`

RETCODE=0
if [ $NODEPS_FLAG -eq $FALSE ]; then
	check_dependencies
	RETCODE=$?
fi


####################################
if [ $NODEPS_FLAG -eq $FALSE ]; then
	if [ $RETCODE -gt 0 ]; then
		echo "**********************************************************"
		echo "HPSUM did not run due to missing dependencies."	
		echo "You can still run hpsum with by typing: hpsum --nodeps"
		echo "HP does not recommend running --nodeps"
		echo "**********************************************************"
		exit 1
	fi
fi

SILENT_FLAG=`echo $IN_ARGS | grep -E -- "--s|-s|/s"`

if [[ -z $DISPLAY  &&  -z $SILENT_FLAG ]];  then
	echo "**********************************************************"
	echo "You are not running in X mode but you can use HPSUM CLI:"
	echo "Run hpsum -help to view all available options"
	echo "**********************************************************"	
else
	pushd ${HPSUM_DIR}bin > /dev/null
	if [ $? = 0 ] ; then
		echo "Running HPSUM ..."
		./hpsum_bin $IN_ARGS
	else
		echo "**********************************************************"
		echo "Unable to change to directory ${HPSUM_DIR}"
		echo "**********************************************************"	
	fi
	RETCODE=$?
	popd >/dev/null
	exit $RETCODE
fi


echo ""
echo ""
exit 1
