#!/bin/sh

source /opt/dell/srvadmin/lib64/srvadmin-omilcore/Funcs.sh

SYSIDFILEPATH8G=`GetRegVal "/opt/dell/srvadmin/etc/omreg.cfg" "openmanage.8gsyslistfile"`
SYSIDFILEPATH9G=`GetRegVal "/opt/dell/srvadmin/etc/omreg.cfg" "openmanage.9gsyslistfile"`

SYSID=`GetSysId`
TEST8G=`GetRegVal "${SYSIDFILEPATH8G}" "${SYSID}"`
TEST9G=`GetRegVal "${SYSIDFILEPATH9G}" "${SYSID}"`

SYSID_HEX="0x$SYSID"
SYSID_DEC=`printf "%d" $SYSID_HEX`
MIN_IDRAC_SYSID_HEX=0x0235
MIN_IDRAC_SYSID_DEC=`printf "%d" $MIN_IDRAC_SYSID_HEX`

# check if $1 or $2 is -r. If it is then its Remote Racadm command. Use idracadm binary for remote
if [ $# -gt 0 ]; then
     if [ "$1"  == "-r" ] || [ "$1" == "-d" -a "$2" == "-r" ]; then
          [ -x /opt/dell/srvadmin/bin/idracadm ] &&
               exec /opt/dell/srvadmin/bin/idracadm "$@"
     fi
fi

# check local hardware and run matching binary, if found
if [ ! -z "${TEST9G}" -a -e /opt/dell/srvadmin/bin/racadm5 ]; then
    exec /opt/dell/srvadmin/bin/racadm5 "$@"
elif [ ! -z "${TEST8G}" -a -e /opt/dell/srvadmin/bin/racadm4 ]; then
    exec /opt/dell/srvadmin/bin/racadm4 "$@"
elif [ $SYSID_DEC -ge $MIN_IDRAC_SYSID_DEC -a -e /opt/dell/srvadmin/bin/idracadm ]; then
    exec /opt/dell/srvadmin/bin/idracadm "$@"
fi

# if we cant detect hardware, fallback to remote behaviour
for bin in /opt/dell/srvadmin/bin/idracadm /opt/dell/srvadmin/bin/racadm5 /opt/dell/srvadmin/bin/racadm4; do
    if [ -e $bin ]; then
       exec $bin "$@" 
    fi
done

echo "Unable to identify the RAC hardware"
exit 1

