#!/bin/sh

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

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="."
	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 
        FILE=/tmp/hpsum.tmp
        echo $SU_PASSWORD | su $SU_USERNAME "-c id >$FILE"
        if [ $? -ne 0 ]; then
            echo "Error: Invalid username or password"
            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"
            rm -rf $FILE
            exit 1
        fi
        rm -rf $FILE
    fi
fi	
	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
            	$BASEDIR/x64/hpsum_bin_x64 "$@"
	     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
		   $BASEDIR/x86/hpsum_bin_x86 "$@"
	        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
