#!/bin/sh
#
#
# Support:      linux-ha@lists.linux-ha.org
# License:      GNU General Public License (GPL)
#
#	This script is a test resource for introducing delay.
#
#	usage: $0  {start|stop|status|monitor|meta-data}
#
#	  OCF parameters are as below:
#		OCF_RESKEY_startdelay
#		OCF_RESKEY_stopdelay
#		OCF_RESKEY_mondelay
#
#	
#		OCF_RESKEY_startdelay defaults to 30 (seconds)
#		OCF_RESKEY_stopdelay defaults to $OCF_RESKEY_startdelay
#		OCF_RESKEY_mondelay defaults to $OCF_RESKEY_startdelay
#
#
#	This is really a test resource script.
#

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

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

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

usage() {
  cat <<-!
	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="Delay">
<version>1.0</version>

<longdesc lang="en">
This script is a test resource for introducing delay.
</longdesc>
<shortdesc lang="en">Delay resource agent</shortdesc>

<parameters>

<parameter name="startdelay" unique="0" required="1">
<longdesc lang="en">
How long in seconds to delay on start operation.
</longdesc>
<shortdesc lang="en">Start delay</shortdesc>
<content type="integer" default="30" />
</parameter>

<parameter name="stopdelay" unique="0"  required="1">
<longdesc lang="en">
How long in seconds to delay on stop operation.
Defaults to "startdelay" if unspecified.
</longdesc>
<shortdesc lang="en">Stop delay</shortdesc>
<content type="integer" default="30" />
</parameter>

<parameter name="mondelay" unique="0"  required="1">
<longdesc lang="en">
How long in seconds to delay on monitor operation.
Defaults to "startdelay" if unspecified.
</longdesc>
<shortdesc lang="en">Monitor delay</shortdesc>
<content type="integer" default="30" />
</parameter>
</parameters>

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

Delay_stat() {
	ha_pseudo_resource Delay_${OCF_RESOURCE_INSTANCE} monitor
}

Delay_Status() {
  if
    Delay_stat
  then
    ocf_log info "Delay is running OK"
    return $OCF_SUCCESS
  else
    ocf_log info "Delay is stopped"
    return $OCF_NOT_RUNNING
  fi
}

Delay_Monitor() {
  Delay_Validate_All -q
  sleep $OCF_RESKEY_mondelay
  Delay_Status
}

Delay_Start() {
  if
    Delay_stat
  then
    ocf_log info "Delay already running."
    return $OCF_SUCCESS
  else
    Delay_Validate_All -q
	ha_pseudo_resource Delay_${OCF_RESOURCE_INSTANCE} start
    rc=$?
    sleep $OCF_RESKEY_startdelay
    if
      [ $rc -ne 0 ]
    then
      return $OCF_ERR_PERM
    fi
    return $OCF_SUCCESS
  fi
}

Delay_Stop() {
  if
    Delay_stat
  then
    Delay_Validate_All -q
	ha_pseudo_resource Delay_${OCF_RESOURCE_INSTANCE} stop
    rc=$?
    sleep $OCF_RESKEY_stopdelay
    if
      [ $rc -ne 0 ]
    then
      return $OCF_ERR_PERM
    fi
    return $OCF_SUCCESS
  else
    ocf_log info "Delay already stopped."
    return $OCF_SUCCESS
  fi
}

# Check if all the arguments are valid numbers, a string is considered valid if:
# 1. It does not contain any character but digits and period ".";
# 2. The period "." does not occur more than once

Are_Valid_Numbers() {
  for i in "$@"; do
	echo $i |grep -v [^0-9.] |grep -q -v [.].*[.]
	if test $? -ne 0; then
	  return $OCF_ERR_ARGS
	fi
  done
  return $OCF_SUCCESS
}

Delay_Validate_All() {
# Be quiet when specified -q option _and_ validation succeded
  getopts "q" option

  if test $option = "q"; then
	quiet=yes
  else
	quiet=no
  fi
  shift $(($OPTIND -1))

  if Are_Valid_Numbers $OCF_RESKEY_startdelay $OCF_RESKEY_stopdelay \
	$OCF_RESKEY_mondelay; then
    if test $quiet = "no"; then
	echo "Validate OK"
    fi
# _Return_ on validation success
	return $OCF_SUCCESS
  else
	echo "Some of the instance parameters are invalid"
# _Exit_ on validation failure
	exit $OCF_ERR_ARGS
  fi
}

if [ $# -ne 1 ]; then
  usage
  exit $OCF_ERR_ARGS
fi

: ${OCF_RESKEY_startdelay=30}
: ${OCF_RESKEY_stopdelay=$OCF_RESKEY_startdelay}
: ${OCF_RESKEY_mondelay=$OCF_RESKEY_startdelay}

case $1 in
  meta-data)		meta_data
			exit $OCF_SUCCESS
			;;
  start)		Delay_Start
			;;
  stop)			Delay_Stop
			;;
  monitor)		Delay_Monitor
			;;
  status)		Delay_Status
			;;
  validate-all)		Delay_Validate_All
			;;
  usage)		usage
			exit $OCF_SUCCESS
			;;
  *)			usage
			exit $OCF_ERR_ARGS
			;;
esac
exit $?
