#!/bin/sh
#
# Resource script for MailTo
#
# Author: Alan Robertson <alanr@unix.sh>
#
# Description: sends email to a sysadmin whenever a takeover occurs.
#
#	Note: This command requires an argument, unlike normal init scripts.
#
#	This can be given in the haresources file as:
#
#	You can also give a mail subject line or even multiple addresses
#		MailTo::alanr@unix.sh::BigImportantWebServer
#		MailTo::alanr@unix.sh,spoppi@gmx.de::BigImportantWebServer
#
#	This will then be put into the message subject and body.
#
#	  OCF parameters are as below:
#		OCF_RESKEY_email
#		OCF_RESKEY_subject
#
# License:  GNU General Public License (GPL)
#
# Copyright:	(C) 2005 International Business Machines

#######################################################################
# Initialization:

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

#######################################################################

ARGS="$0 $*"

us=`uname -n`

usage() {
  echo "Usage: $0 {start|stop|status|monitor|meta-data|validate-all}"
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="MailTo">
<version>1.0</version>

<longdesc lang="en">
This is a resource agent for MailTo. It sends email to a sysadmin whenever 
a takeover occurs.
</longdesc>
<shortdesc lang="en">MailTo resource agent</shortdesc>

<parameters>
<parameter name="email" unique="0" required="1">
<longdesc lang="en">
The email address of sysadmin.
</longdesc>
<shortdesc lang="en">Email address</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="subject" unique="0">
<longdesc lang="en">
The subject of the email.
</longdesc>
<shortdesc lang="en">Subject</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="10" />
<action name="stop" timeout="10" />
<action name="status" depth="0" timeout="10" interval="10" start-delay="10" />
<action name="monitor" depth="0" timeout="10" interval="10" start-delay="10" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
END
}

MailProgram() {
	$MAILCMD -s "$1" "$email"  <<EOF
        $Subject
 
        Command line was:
        $ARGS
EOF
	return $?
}

SubjectLine() {
  case $1 in
    ??*)	echo $1;;
    *)		echo "Resource Group";;
  esac
}


MailToStart() {

	Subject="`SubjectLine $subject` Takeover in progress at `date` on $us"

	MailProgram "$Subject" $1
	ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} start
}

MailToStop () {
	Subject="`SubjectLine $subject` Migrating resource away at `date` from $us"

	MailProgram "$Subject" $1
	ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} stop
}

MailToStatus () {
#	ocf_log warn "Don't stat/monitor me! MailTo is a pseudo resource agent, so the status reported may be incorrect"

	if ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} monitor
	then
		echo "running"
		return $OCF_SUCCESS
	else
		echo "stopped"
		return $OCF_NOT_RUNNING
	fi
}

MailToValidateAll () {

# $email may be a list of mail addresses separated by "," and " " and "\t"
# normalize it for ease of parse
	local local_email=`echo $email | tr ",\t" " " | tr -s " "`

#	ocf_log info "[$local_email]"
	for item in $local_email
	do
	    case $item in
		*?@?*)
		;; #possible valid email address
		*)
		getent passwd $item >/dev/null
		if [ $? -eq 0 ]; then
		: OK, mail to $item@localhost.localdomain
		else
		    ocf_log err "Invalid email address [$email]"
		    exit $OCF_ERR_ARGS
		fi
		;;
	    esac
	done

# Any subject is OK

	return $OCF_SUCCESS
}

# 
# See how we were called.
#
#	The order in which heartbeat provides arguments to resource
#	scripts is broken.  It should be fixed.
#

if
  ( [ $# -ne 1 ] )
then
  usage
  exit $OCF_ERR_GENERIC
fi

case $1 in
  meta-data)		meta_data
			exit $OCF_SUCCESS
			;;
  status|monitor)	MailToStatus
			exit $?
			;;
  usage)		usage
			exit $OCF_SUCCESS
			;;
  *)			;;
esac

if 
  [ -z "$OCF_RESKEY_email" ]
then
  ocf_log err "At least 1 Email address has to be given!"
#  usage
  exit $OCF_ERR_GENERIC
fi

email=$OCF_RESKEY_email
subject=$OCF_RESKEY_subject

case $1 in
  start)		MailToStart
			;;
  stop)			MailToStop
			;;
  validate-all)		MailToValidateAll
			;;
  *)			usage
			exit $OCF_ERR_UNIMPLEMENTED
			;;
esac
exit $?
