#!/bin/bash
#HP_SCRIPTING_TOOLS_DIR="/usr/local/hp/hp-scripting-tools"
HP_SCRIPTING_TOOLS_DIR="/opt/hp/hp-scripting-tools"
export LD_LIBRARY_PATH=$HP_SCRIPTING_TOOLS_DIR/lib
withconfig=
declare -a args
for option
do
    case "$option" in
        -f)
        # convert rel path to abs (which hpdiscover expects
        args[${#args[*]}]=$1
        FN=$2
        if [ -n "$FN" ] ; then
            if [ ! "${FN:0:1}" = "/" ] ; then
                FN=$PWD/$FN
            fi
            args[${#args[*]}]=$FN
            shift
        fi
        shift
        ;;
        -c|--config)
        withconfig=1
        args[${#args[*]}]=$1
        args[${#args[*]}]=$2
        shift
        shift
        ;;
        *)
        args[${#args[*]}]=$1
        shift
        ;;
    esac
done

# if not provided, prefer /etc/hp-discovery-x86_64.conf when x86_64 binary
# use this to run old and new hpdiscovery side-by-side without having to add params
# will be removed in a future release (~2014)
if [ -z "${withconfig}" ] ; then
    args[${#args[*]}]="-c"
    if [ -n "$(file $HP_SCRIPTING_TOOLS_DIR/bin/hpdiscovery | grep x86_64)" ] && -f "/etc/hpdiscovery-x86_64.conf" ] ; then
        args[${#args[*]}]="/etc/hpdiscovery-x86_64.conf"
    else
        args[${#args[*]}]="/etc/hpdiscovery.conf"
    fi
fi

# ensure the pci_slot ko is loaded so we can get pci slot information
# from sysfs
if [ -z "$(lsmod | grep pci_slot)" ]
then
    modprobe pci_slot ; sleep 1
fi 

CWD="$(pwd)"
cd $HP_SCRIPTING_TOOLS_DIR/bin/
if [ -f "./hp-discovery" ]
then
    ./hp-discovery  ${args[*]}
    RETCODE=$?
elif [ -f "./hpdiscovery" ]
then
    ./hpdiscovery ${args[*]}
    RETCODE=$?
fi
cd "$CWD"
exit $RETCODE

