#!/bin/sh
#
. /opt/HAC/bin/rsf.sh

# Usage: show variable1 variable2...
# Show variables as name='value'
# Useful as a debugging aid, and to save variables to reload in another script.
# BUGS: Does not handle strings containing backslash shell character quotes.
# NOTE: If using a shell (eg. bash) which supports it, printf %q may be useful.
show()
{
    local show_name show_value
    for show_name; do
        eval show_value=\${${show_name}}
        echo "${show_name}='${show_value}'"
    done
}

# Usage: split_string "<word list>" <variable1> <variable2>...
# Split a string containing a list of $IFS separated words into individual
# words, and assign them to a list of variables. If there are more words
# than variables then all the remaining words are put in the last variable.
# Use a dummy last variable to collect any unwanted words.
# Variables for which there are no words are cleared.
# eg.   split_string "hello Fred this is Bill" greeting who extra
# sets  greeting=hello  who=Fred  extra="this is Bill"
split_string()
{
    local split_string_wordlist
    split_string_wordlist="$1"
    shift
    # echo "$split_string_wordlist" | read "$@"  # DOES NOT WORK,
    # as the pipe executes the read in a sub-process.
    read "$@" <<EOF-end-of-split_string-arguments
${split_string_wordlist}
EOF-end-of-split_string-arguments
}

parse_ipdev()
{
    parse_string="$*"
    #
    # Pull out the IPDEVICE
    split_string "$parse_string" ip_device parse_string
    #
    # For each group of 3 find the IP address and kill all connections.
    while ! match "$parse_string" ""; do
        split_string "$parse_string" ip_type ip_address ip_netmask parse_string
 	split_string "$(getent hosts ${ip_address})" ip_numeric remainder
	if [ -z "${ip_numeric}" ] ; then
	    dated_echo "Warning: unable to resolve ${ip_address}, skipping tcpkill on this address, fields are:"
	    show ip_type ip_address ip_netmask
	else
	    dated_echo "Running: ${PRODUCT_BIN}/close_all_tcp.sh ${ip_numeric}"
	    ${PRODUCT_BIN}/close_all_tcp.sh ${ip_numeric}
	fi
    done
}

read_ipdev_env_vars()
{
    number=${1}
    while : ; do
        eval env_var=\${RSF_IPDEV_${number}}
        dated_echo "RSF_IPDEV_${number}='$env_var'"
	match x"$env_var" x && break
	parse_ipdev "$env_var"
	number=$(expr $number + 1)
    done
}

#
# For the original MySQLmon configuration file generator the VIP is saved as:
#
# 	RSF_FLOATNAME=master-vip
#
# So this needs to be checked first. For the latest RSF the VIP is saved as:
#
#       RSF_FLOATNAME=NONE
#       RSF_IPDEV=NONE
#       RSF_IPDEV_1=ens18  inet vip1 255.255.255.0
# 
service=${RSF_SERVICE:-"service_name"}
script="`basename $0`"

case "${1}" in
'start')
    rc_exit ${service} ${RSF_OK}
    ;;

'stop')
    if [ "`echo "${PROP_TCP_SHUTDOWN_ACTIVE_VIP_CONNECTIONS:-false}" | tr '[:upper:]' '[:lower:]'`" = "false" ] ; then
	dated_echo "disabled by property PROP_TCP_SHUTDOWN_ACTIVE_VIP_CONNECTIONS"
	rc_exit ${service} ${RSF_OK}
    else
	#
	# If the old style environment variable for the vip is detected,
	# set the RSF_IPDEV_0 to it's value so the loop will pick it up.
	if [ ! -z "${RSF_FLOATNAME}" -a "${RSF_FLOATNAME}" != "NONE" ] ; then
	    START_SUFFIX=0
	    export RSF_IPDEV_0="unused inet ${RSF_FLOATNAME} -"
	else
	    START_SUFFIX=1
	fi
	
	read_ipdev_env_vars ${START_SUFFIX}
	rc_exit ${service} ${RSF_OK}
    fi
    ;;

'check')
	exit ${RSF_CHECK_NORESRC}
	;;

*)
	rc_exit ${service} ${RSF_WARN} "usage: $0 <start|stop|check>"
	;;

esac
