#!/bin/sh
# $Id$

# Script to be run by rsfmon when it wants to restart itself to read a new
# configuration file. This script performs any extra "fix-up" actions which
# may be needed, and then executes the new instance of rsfmon.

# It is possible to "piggy back" other short files by including them in the
# file transferred over the net, and splitting them apart here. This is done
# with the agent config file, and the agent is restarted in case a new file
# was sent. The external $SPLIT programme splits the files.

# This also tests to see if the new config file contains the string
# "#unconfigure#" at the start of a line, and if so replaces the file with
# a copy of config.reconfigure (for unconfiguring a Nexenta appliance).
# In this case also try to delete all old service state files.

# If there is no "#unconfigure#" line, then any configured service in the
# config file for which no state file exists will have a new state file
# created for it. The new file will indicate that the service is stopped,
# automatic and unblocked.

RSF="${RSF_INSTALLDIR:-/opt/HAC/RSF-1}"
ETC="${RSF}/etc"
BIN="${RSF}/bin"
SPLIT="${BIN}/split_files.py"
AGENT="${BIN}/rsfagent"

# Get MACHINE name from the RSF_MACHINE_NAME environment variable set by rsfmon
statedir="${ETC}/state.${RSF_MACHINE_NAME}"

# Find a useable awk. Old awk (The default on solaris) is the only one
# which fails. awk is still on the list, at the end, as it's probably
# OK on any system other than Solaris.
for awk in nawk gawk mawk awk; do
    $awk '{}' </dev/null 2>/dev/null && break
done


# If there is a file splitting programme, run it
[ -f ${SPLIT} ] && ${SPLIT} "${ETC}/config" \
    "${ETC}/config" "${RSF}/agents/etc/resource_agent.cfg"

if grep "^#unconfigure#" "${ETC}/config" 2>&1 >/dev/null; then
    cp "${ETC}/config.reconfigure" "${ETC}/config"
    echo "Removing service state directory ${statedir}"
    rm -r "${statedir}"
    echo "Removing .res_drives files"
    rm ${ETC}/.res_drives.* 2>/dev/null
else
    mkdir -p "${statedir}"
    $awk '
# First accumulate a dictionary of configured service names
/^[ \t]*SERVICE[ \t]/ { services[$2] = 1 }

END {
    # Get state file names into a dictionary, and build a list of
    # any files with no associated service, so we can delete them.
    deletes=""			# Full path names
    delete_files=""		# Just filenames, for log message
    res_drive_deletes=""
    res_drive_files=""

    while ("ls " statedir | getline)
    {
	if (!/^$/)		# Ignore blanks
	{
	    file = $1
	    files[file] = 1
	    if (!services[file])
	    {
		deletes = deletes " " statedir "/" file
		delete_files = delete_files " " file
		other_deletes = other_deletes " " ETC "/.res_drives." file " " ETC "/.MetroHA/site_failover_override." file
		other_files = other_files " .res_drives." file " site_failover_override." file
		mhdc_script = ETC "/rc.appliance.c/K85zfs_mhdc"
		system("[ -f " mhdc_script " -a -f " ETC "/.res_drives." file " ] && echo Releasing reservations for " file "&& export RSF_SERVICE=" file "&&" mhdc_script " stop")
	    }
	}
    }

    # If the deletes list is not empty, delete the files
    if (deletes !~ /^$/)
    {
	print "Deleting old state files in " statedir ":" delete_files
	system("rm " deletes)
	print "Deleting old supporting files:" other_files
	system("rm " other_deletes " 2>/dev/null")
    }

    # Now create new state files for any new services
    for (service in services)
    {
	if (!files[service])
	{
	    print "Creating new state file " statedir "/" service
	    system("echo \"1 1 1\" >\"" statedir "/" service "\"")
	}
	else if (services[service] == 1)
	{
		# State file exists already so this is not a new service
		# service entry in array is 1, so service is not being deleted
		res_drives_script = ETC "/rc.appliance.c/S14res_drives"
		print "Refreshing .res_drives." service " (" res_drives_script " refresh)"
		system("test -f " res_drives_script "&& export RSF_SERVICE=" service "&&" res_drives_script " refresh")
	}
    }
}
' ${ETC}/config statedir="$statedir" ETC=${ETC}
fi

if [ -f "${AGENT}" -a -f "${RSF}/agents/etc/resource_agent.cfg" ]; then
    ${AGENT} --resource-agent stop
    ${AGENT} --resource-agent start
fi

"${RSF}/bin/rsfmon" "$@" &
