#!/bin/sh
#
# ident    "@(#)postbackout 1.2     99/03/10 SMI"   
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#
# SUNWscmgr postbackout script


##########################################################################
#
# function scmgr_error
#
# Prints an error message on the console based on the value of the first
# argument.
#
# $1 = 1, nametag already exists
# $1 = 2, command timed out
# $1 > 2, internal error occurred
#
scmgr_error()
{
	error=$1

	case ${error} in
	0) ;;
	1) log_info "${PRE}.5000" "PMF: nametag ${PMF_ID} already exists" ;;
	2) log_info "${PRE}.5001" "PMF: ${PMF_ID} command timed out" ;;
	*) log_info "${PRE}.5002" "PMF: Internal error occurred" ;;
	esac
}

##########################################################################
#
# function cleanup
#
# Cleanup temporary files on exit/interrupt
#
# $1 = exit code
#
cleanup()
{
	exit_code=$1

	exit ${exit_code}
}

[ "$ROOTDIR" = / ] && ROOTDIR=""
PATH="$ROOTDIR/opt/SUNWcluster/bin:/usr/bin:/usr/sbin:${PATH}"
export PATH

# SC Manager's unique PMF ID
PMF_ID="SUNWcluster_scmgr"

SCMGRSCRIPT="$ROOTDIR/opt/SUNWcluster/scmgr/lib"
CLUSTERNAMEFILE="$ROOTDIR/etc/opt/SUNWcluster/conf/default_clustername"

trap "cleanup 0" 0 1 2 3 13 15

if [ -f ${CLUSTERNAMEFILE} ]; then
	CLUSTERNAME=`cat ${CLUSTERNAMEFILE}`
	timed_run -q 3 clustm getstate ${CLUSTERNAME} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		cleanup 0
	fi
else
	cleanup 0
fi

# If we are starting and ${PMF_ID} is already running, kill it and then restart.
if pmfadm -l ${PMF_ID} > /dev/null 2>&1; then
	pmfadm -s ${PMF_ID} KILL
fi

# We are going to start this so that it runs indefinitely.
pmfadm -c ${PMF_ID} -n 5 ${SCMGRSCRIPT}/scm_server_start || scmgr_error $?

cleanup 0
