#!/bin/bash
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

# 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

