#!/bin/bash

clitools=();                            clicmds=()
clitools+=("/usr/bin/SMcli");           clicmds+=("show storageSubsystem healthStatus;")
clitools+=("/opt/smee/client/smeecli"); clicmds+=("show storageArray healthStatus;")

head=true
for t in $(seq 0 $((${#clitools[@]}-1))) ; do
    clitool=""
    clicmd=""

    if [[ -x "${clitools[$t]}" ]] ; then
        clitool=${clitools[$t]}
        clicmd=${clicmds[$t]}
    elif which $(basename ${clitools[$t]}) > /dev/null 2>&1 ; then
        clitool=$(which $(basename ${clitools[$t]}))
        clicmd=${clicmds[$t]}
    fi

    if [ ${#clitool} -eq 0 ] ; then
        continue
    fi

    storage_list=$(waitmax 30 $clitool -d 2>/dev/null | awk '{if (length($1)==0) exit; print $1 }')

    for storage in $storage_list ; do
        if $head ; then
            echo '<<<smcli>>>'
            head=false
        fi

        #specialni pripad na upol128
        if [ $clitool == "/usr/bin/SMcli" ]; then
           waitmax 30 $clitool -n $storage -c show | grep -q storageArray
           if [ $? -eq 0 ] ; then
               clicmd="show storageArray healthStatus;"
           fi
        fi


        waitmax 30 $clitool -n $storage -c "$clicmd" | awk -v storage=$storage '
                BEGIN {
                    message="";
                    result="MISSING"
                } {
                    if (length($1) == 0)
                        next;
                    if ($0 == "Executing script...")
                        {result="SCRIPT"; next;}
                    if ($0 == "Script execution complete.")
                        {result="DONE"; next;}
                    if (result == "SCRIPT")
                        {if (length(message)>0) message = message "EoL"; message = message $0}
                } END {
                    print storage " " result " " message
                }'
    done
done
