#!/bin/bash

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

# 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"

# default target-IP (--address) to IPv4 localhost
address=127.0.0.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 
    # Get all the network interfaces from ifconfig.  Get the IPv4 
    # address, if configured, or the global IPv6 address if not; if
    # neither default to blank.
    # If there's an IPv6 address, use IPv6 localhost for slp_srvreg
    # (is this necessary?).
    # List IPv4 addresses before IPv6 ones in ip-address attribute
    # Get the public IP for the service URL. 
    ip_address=""
    for eth in `/sbin/ifconfig -a | grep ^eth | awk '{ print $1 }'`; do
        ipaddr=`getipaddr -4 -i $eth`
        if [ $? -ne 0 ]; then
            ipaddr=`getipaddr -6 -i $eth`
            if [ $? -ne 0 ]; then
                ipaddr=""
            else
                address=::1
                rfc2732=0
                ip_address=${ip_address}' '$ipaddr
            fi
        else
            rfc2732=1
            ip_address=$ipaddr' '${ip_address}
        fi
        isPublicIP $ipaddr
        if [ $? -eq 1 ]; then
            # IPv6 addresses in URLs must be enclosed within '[' and ']' as per RFC2732
            if [ $rfc2732 -eq 0 ]; then
                arg='['$ipaddr']'
            else 
                arg=$ipaddr
            fi
        fi
    done 

    # 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 
    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="$address" \
                   --ip=0 &

        # Record the thread IDs for the started slp_svreg daemon
        sleep 2
        TOP_LDAP_SLP_PID=`pidof slp_srvreg`

        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="$address" \
                   --ip=0 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
