#!/bin/bash

function usage() {
	echo "Usage : $0 [-h] [init]"
	echo "  -h    Display this message"
	echo "  init  Save current status as "
}

[ "$1" == "-h" ] && usage && exit 0

CRM_MON_BIN=/usr/sbin/crm_mon
STATUS="/var/cache/ee/check_crm_status_change.status"

function get_status() {
	$CRM_MON_BIN -1|grep -Ev "Last (updated|change):"
}

if [ "$1" == "init" ]
then
	get_status > $STATUS
	echo "Current status save in $STATUS."
	exit 0
fi

[ ! -f $STATUS ] && echo "UNKNOWN : Status file not initialized. Run $0 init" && exit 3

TMP=$( mktemp )
get_status > $TMP

if [ -n "$( diff $TMP $STATUS )" ]
then
	echo "WARNING : crm status change !"
	cat $TMP
	rm -f $TMP
	exit 1
fi

rm -f $TMP
$CRM_MON_BIN -s
exit $?
