#!/bin/bash

export PATH=$PATH:/opt/hsc/bin

# Start an SLP SA
# adapted from Mike Day's sa_sample script

# Process ID of SA daemon 
TOP_LDAP_SLP_PID=0

# service type string 
declare -r service="service:management-software.IBM:hardware-management-console:"

# protocol part of the url
declare -r protocol_part="//"

# path part of the url 
# path_part="/etc/nothing;version=1"

# mtms attribute
mtms=`getHMCVPD -a`

# machinetype-model attribute
mtm=`echo $mtms | awk -F '*' '{print $1}'`

# serial-number attribute
s=`echo $mtms | awk -F '*' '{print $2}'`

# uuid attribute
uuid=`getHMCVPD -g`

# web-management-interface attribute --> is this a Web-based HMC
eclHMC="true"

while true 
do
    # ip-address attribute 
    # for i in `lshmc -n -Fipaddr,hostname | sed 's/,/ /g'`; do echo $i | sed 's/\"//g'; done
    # the above line is one possibility, but ifconfig knows which
    # interfaces have addresses bound so use it (?)
    # How about ifconfig -a | sed 's/:/ /' | awk '/^eth/ { getline; print $3; }'
    ip_address=`/sbin/ifconfig | awk ' BEGIN {
                                     while (getline $o > 0) {
                                         if ($o ~ /^eth/) {
                                             getline $o
                                             split($2,a,":");
                                             printf("%s ",a[2]);
                                         }
                                     }
                                 }'`

    # name attribute
    # retry value retrieval if Command Server is not up
    rc=1
    while [ $rc -ne 0 ]
    do 
        nameattrval=`lshmc -n -Fhostname.domain 2> /dev/null`
        rc=$?
    done
    name=`echo $nameattrval | sed 's/\"//g'`

    # web-url attribute
    web_url="http://"$name"/remote_client.html"

    # secure-web-url attribute
    secure_web_url="https://"$name":8443"

    # attribute string
    attributes="(type=hardware-management-console),(level=3),(machinetype-model=$mtm),(serial-number=$s),(name=$name),(uuid=$uuid),(ip-address=`echo $ip_address | sed 's/ /,/g'`),(web-url=),(mtms=$mtms),(web-management-interface=$eclHMC),(secure-web-url=$secure_web_url),(cimom-port=),(secure-cimom-port=5989)"

    # If slp_srvreg is not already running in daemon mode, kill it
    # Is this needed?  By 'kill it' we're just doing some cleanup
    if [ "$TOP_LDAP_SLP_PID" -eq "0" ] 
    then
        killall slp_srvreg
    fi

    # construct a service:url 
    arg=`echo $ip_address | awk -F ' ' '{print $1}'` 
    url=${service}${protocol_part}${arg}${path_part}
            
    if [ "$TOP_LDAP_SLP_PID" -eq "0" ]
    then
        slp_srvreg --type="$service" \
                   --url="$url" \
                   --attributes="$attributes" \
                   --daemon=true \
                   --address="127.0.0.1" &
        TOP_LDAP_SLP_PID=$!

        # Record the thread IDs for the started slp_svreg daemon
        sleep 5
        echo ""`date` "slp_srvreg daemon started with PID $TOP_LDAP_SLP_PID" 
    elif kill -0 $TOP_LDAP_SLP_PID 2>/dev/null; then
        # SA daemon already running so just re-register
        slp_srvreg --type="$service" \
                   --url="$url" \
                   --attributes="$attributes" \
                   --address="127.0.0.1" 1>/dev/null
    
        # sleep 5 minutes
        sleep 5m
    else
        # process must exist no longer so flag it for next loop iteration 
        echo `date` slp_svreg daemon PID $TOP_LDAP_SLP_PID no longer exists 
        TOP_LDAP_SLP_PID=0
    fi
done
