#!/usr/bin/bash
#
# Called by the master instance of Shaman to notify about a crashed node.
#

. /usr/share/shaman/functions

verify_caller

if [ -z "$EVENT" ]; then
	echo "EVENT variable undefined" 1>&2
	exit 1
fi

if [ "x$EVENT" != "xNODE_CRASHED" ]; then
	exit 0
fi

if [ -z "$SRC_NODE_ID" ]; then
	echo "SRC_NODE_ID variable undefined" 1>&2
	exit 1
fi

VSTORAGE_NFS_LIB=/usr/libexec/vstorage-nfs/nfs_functions2
if [ ! -x $VSTORAGE_NFS_LIB ] ; then
	echo "Unable find executable $VSTORAGE_NFS_LIB" 1>&2
	exit 2
fi
source $VSTORAGE_NFS_LIB
[ -f $NFS_ETC/config ] && source $NFS_ETC/config

NFS_ROOT=`pcs_nfs_get_root`
if [ -z "$NFS_ROOT" ] ; then
	echo "NFS_ROOT environment variable undefined, please check $NFS_ETC/config file" 1>&2
	exit 3
fi

if [ ! -d "$NFS_ROOT" ]; then
	echo "Directory '$NFS_ROOT' does not exist" 1>&2
	exit 4
fi

if [ -n "$ENABLE_PVFS" ]; then
	pcs_unregister_host_as_ds "$SRC_NODE_ID" "$PVFS_DS_REGISTRY_KVSTOR_KEY"
	rc=$?
	if [ $rc -ne 0 ]; then
		echo "Cannot unregister host $SRC_NODE_ID" \
			"as PVFS data server, error $rc" 1>&2
		exit 10
	fi
fi

if [ -n "$ENABLE_MPFS" ]; then
	pcs_unregister_services "mpfs" --host "$SRC_NODE_ID"
	if [ $? -ne 0 ]; then
		echo "Cannot unregister MPFS services on host $SRC_NODE_ID" 1>&2
		return 12
	fi
fi

exit 0
