#!/bin/bash

RUNNINGOS=$(`echo uname` | tr '[a-z]' '[A-Z]')
RUNNINGARCH=$(`echo uname -m` | tr '[a-z]' '[A-Z]')

export HPSUM_ROOT=""
export HPSUM_ARCH=""
HPSUM_ARCH=$RUNNINGARCH

FILESTAMP=`date +"%m-%d-%y-%H-%M-%S"`
HPSUMTMPFILE="hpsum-tmp-$FILESTAMP"
READONLYFS=""
FUSION_SCRIPT="/tmp/hpsumexecuter"

if [ -f "$HPSUMTMPFILE" ] ; then
        HPSUMTMPFILE="hpsum-tmp-$FILESTAMP-$FILESTAMP"
fi

touch $HPSUMTMPFILE > /dev/null 2>&1

if [ -e "$HPSUMTMPFILE" ] ; then
        rm $HPSUMTMPFILE
        READONLYFS="no"
else
        READONLYFS="yes"
	HPSUM_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi

# This function copy hpsum executables to tmp directory and run hpsum from tmp directory if read-only file system found
RunFromTmpDir() 
{
	FROMPATH=""
	if [ "$RUNNINGARCH" == "X86_64" ]; then
		FROMPATH=$BASEDIR/x64
	else
		FROMPATH=$BASEDIR/x86
	fi

	DATESTRING=`date +"%m-%d-%y-%H-%M-%S"`
        HPSUMTMPDIR="/tmp/hpsum-$DATESTRING"
        echo "Copying hpsum files to $HPSUMTMPDIR"
        mkdir $HPSUMTMPDIR
        if [ $? -ne 0 ]; then
	        echo "Could not create the temporary directory $HPSUMTMPDIR"
        	exit 1
        fi

        cp -fr $FROMPATH $HPSUMTMPDIR
        if [ $? -ne 0 ]; then
        	echo "Could not copy $FROMPATH directory to the location $HPSUMTMPDIR"
                exit 1
        fi
        cp -fr $BASEDIR/assets $HPSUMTMPDIR
        if [ $? -ne 0 ]; then
        	echo "Could not copy assets directory to the location $HPSUMTMPDIR"
                exit 1
        fi
        cp $BASEDIR/masterdependency.xml $HPSUMTMPDIR
        if [ $? -ne 0 ]; then
        	echo "Could not copy masterdependency.xml file to the location $HPSUMTMPDIR"
                exit 1
        fi

        # copy localization files
        cp $BASEDIR/*.qm $HPSUMTMPDIR

        # echo "executing hpsum_bin_<ARCH> from the location $HPSUMTMPDIR/<ARCH>"
	if [ "$RUNNINGARCH" == "X86_64" ]; then
        	$HPSUMTMPDIR/x64/hpsum_bin_x64 "$@"
	else
		$HPSUMTMPDIR/x86/hpsum_bin_x86 "$@"
	fi
}

HPUX=`uname | grep "HP-UX"`

if [ ! -z "${HPUX}" ]; then
	HPUX=`uname -a | grep "B.11.31" | grep "ia64"`
	if [ -z "${HPUX}" ]; then
		echo "HPSUM HP-UX version only supports HP-UX B.11.31 ia64."
		exit 1
	fi
	if [ -a  ia64/hpsum_bin_ia64 ]; then
		ia64/hpsum_bin_ia64 $*
	else
		echo "Not found HPSUM client file: ia64/hpsum_bin_ia64"
		exit 1
	fi
elif [ "$RUNNINGOS" == "LINUX" ]; then
	if [ $0 == "/sbin/hpsum" ]; then
		#the script resides in the RPM installed location. 
		BASEDIR=/opt/hp/hpsum/bin
	else
		BASEDIR=`dirname $0`
	fi
    export USERNAME=""
    export PASSWORD=""
    export USESUDO=""
    export USESU=""
    export SU_USERNAME=""
    export SU_PASSWORD=""
    export USECURRCRED=""
if [ $(id -u) != "0" ]; then	
    ARGS=("$@")
    # get total subscripts in an array
    total=${#ARGS[*]}
    for (( i=0; i<=$(( $total -1 )); i++ ))
    do
        if [ "${ARGS[$i]}" == "/username" ]; then
           i=`expr $i + 1`
	   USERNAME=${ARGS[$i]}
	elif [ "${ARGS[$i]}" == "/passwd" ]; then
           i=`expr $i + 1`
	   PASSWORD=${ARGS[$i]}
	elif [ "${ARGS[$i]}" == "/use_sudo" ]; then
	   USESUDO="yes"
	elif [ "${ARGS[$i]}" == "/su_username" ]; then
           i=`expr $i + 1`
	   SU_USERNAME=${ARGS[$i]}
	   USESU="yes"
	elif [ "${ARGS[$i]}" == "/su_password" ]; then
           i=`expr $i + 1`
	   SU_PASSWORD=${ARGS[$i]}
	   USESU="yes"
        elif [ "${ARGS[$i]}" == "/current_credential" ]; then
            USECURRCRED="yes"
	fi
    done	   
    if [ "$USESUDO" == "yes" ] && [ "$USESU" == "yes" ]; then
       echo "Error: /use_sudo cannot be used with /su_username and /su_passsword options"
       exit 1
    fi
    if [ "$USESUDO" == "yes" ] && [ "$USECURRCRED" == "yes" ]; then
        echo "Error: /use_sudo and /current_credential cannot be used together"
        exit 1
    fi
    if [ "$USESUDO" == "yes" ]; then
        echo $PASSWORD | /usr/bin/sudo -l -S		   
        if [[ $? != 0 ]] ; then  
	   echo "Error: Either User does not have valid sudo privileges or entered invalid credentials"
	   exit 1
	fi
    elif [ "$USESU" == "yes" ]; then 
		if [ -f /etc/redhat-release ] ; then
			echo "The /su_username and /su_password parameters are not supported on Red Hat servers. Please manually switch to super user first before running HP SUM."
			exit 1
		else
			FILE=/tmp/hpsum.tmp
			echo $SU_PASSWORD | su $SU_USERNAME "-c id >$FILE"
			if [ $? -ne 0 ]; then
				echo "Error: Invalid username or password"
				echo $SU_PASSWORD | su $SU_USERNAME "-c rm -rf $FILE"
				exit 1
			fi
			grep "uid=0" $FILE >/dev/null
			if [ $? -ne 0 ]; then
				echo "Error: User does not have valid super user privileges"
				echo $SU_PASSWORD | su $SU_USERNAME "-c rm -rf $FILE"
				exit 1
			fi
			echo $SU_PASSWORD | su $SU_USERNAME "-c rm -rf $FILE"
		fi        
    fi
fi	
    # Check to see if we should call the Firmware-RPM Wrapper
    ARGS=("$@")
    total=${#ARGS[*]}
    for (( i=0; i<=$(( $total -1 )); i++ ))
    do
	CMD=${ARGS[$i]}
        if [ "$CMD" == "requires" -o "$CMD" == "upgrade" -o "$CMD" == "list" -o "$CMD" == "info" -o "$CMD" == "query" ]; then
	    for (( j = 0; j < $i; j++))
	    do
		shift
	    done

	    if [ "$RUNNINGARCH" == "X86_64" ]; then
	        FWRPMCMD=$BASEDIR/x64/hp-sum
	    else
	        FWRPMCMD=$BASEDIR/x86/hp-sum
	    fi

	    if [ -a  $FWRPMCMD ]; then
	        if [ "$USESUDO" == "yes" ]; then
		    echo $PASSWORD | /usr/bin/sudo -u $USERNAME -S $FWRPMCMD "$@"
	        elif [ "$USESU" == "yes" ]; then 
                    echo $SU_PASSWORD | su $SU_USERNAME "-c $FWRPMCMD $@"
            	else
		    $FWRPMCMD "$@"
	    	fi
	        exit $?
	    else
	        echo "Could not find $FWRPMCMD"
		exit 1
	    fi
	fi
    done

    # Invoke the proper version of hpsum_bin
	if [ "$RUNNINGARCH" == "X86_64" ]; then
	    if [ -a  $BASEDIR/x64/hpsum_bin_x64 ]; then
	     if [ "$USESUDO" == "yes" ]; then
               echo $PASSWORD | /usr/bin/sudo -u $USERNAME -S $BASEDIR/x64/hpsum_bin_x64 $*
	     elif [ "$USESU" == "yes" ]; then 
                echo $SU_PASSWORD | su $SU_USERNAME "-c $BASEDIR/x64/hpsum_bin_x64 $*"
             else
				OFFLINE_MODE1=""
		 		if [ -z $HDU_BOOTENV_SMPJTB ]; then
					OFFLINE_MODE1="."
		 		else
					OFFLINE_MODE1=$HDU_BOOTENV_SMPJTB
		 		fi
		 		# echo "OFFLINE_MODE1 = $OFFLINE_MODE1"

		 		OFFLINE_MODE2=""
		 		if [ -z $PHOENIX ]; then
					OFFLINE_MODE2="."
		 		else
					OFFLINE_MODE2=$PHOENIX
		 		fi
		 		# echo "OFFLINE_MODE2 = $OFFLINE_MODE2"

		 		PACKAGES_DIR=""
		 		if [ $OFFLINE_MODE1 == "yes" ]; then
					PACKAGES_DIR=`pwd` 

					# Do not copy HP SUM to /tmp if it is fusion mode
					if [ -f $FUSION_SCRIPT ]; then
						PACKAGES_DIR="."
					fi
		 		elif [ $OFFLINE_MODE2 == 1 ]; then
					PACKAGES_DIR=`pwd`

					# Do not copy HP SUM to /tmp if it is fusion mode
					if [ -f $FUSION_SCRIPT ]; then
						PACKAGES_DIR="."
					fi
				elif [ $READONLYFS == "yes" ]; then
					PACKAGES_DIR=`pwd`
		 		else
					PACKAGES_DIR="."
		 		fi

		 		if [ $PACKAGES_DIR == "." ]; then
					$BASEDIR/x64/hpsum_bin_x64 "$@"
		 		else
					RunFromTmpDir "$@"
				fi
	     fi
	    else
	        echo "Could not find HPSUM client file: x64/hpsum_bin_x64"
		exit 1
	    fi
	else
	    if [ -a  $BASEDIR/x86/hpsum_bin_x86 ]; then
	       if [ "$USESUDO" == "yes" ]; then           
	           echo $PASSWORD | /usr/bin/sudo -u $USERNAME -S $BASEDIR/x86/hpsum_bin_x86 $*
	       elif [ "$USESU" == "yes" ]; then 
	           echo $SU_PASSWORD | su $SU_USERNAME "-c $BASEDIR/x86/hpsum_bin_x86 $*"
	       else
			if [ $READONLYFS == "yes" ]; then
				RunFromTmpDir "$@"
			else
				$BASEDIR/x86/hpsum_bin_x86 "$@"
			fi

	       fi
	    else
	        echo "Could not find HPSUM client file: x86/hpsum_bin_x86"
		exit 1
	    fi
	fi
else
	echo "HPSUM not supported on $RUNNINGOS"
	exit 1
fi
