#!/bin/sh
#
#
# Support:      users@clusterlabs.org
# License:      GNU General Public License (GPL)
#
#	This script is a resource for introducing a delay.
#
#	usage: $0 {start|stop|status|monitor|meta-data}
#

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

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Parameter defaults

OCF_RESKEY_startdelay_default="20"
OCF_RESKEY_stopdelay_default="30"
OCF_RESKEY_mondelay_default="30"

: ${OCF_RESKEY_startdelay=${OCF_RESKEY_startdelay_default}}
: ${OCF_RESKEY_stopdelay=${OCF_RESKEY_stopdelay_default}}
: ${OCF_RESKEY_mondelay=${OCF_RESKEY_mondelay_default}}

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

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>1.0</version>

<longdesc lang="en">
This script is a resource for introducing a delay.
</longdesc>
<shortdesc lang="en">Waits for a defined timespan</shortdesc>

<parameters>

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

<parameter name="stopdelay" unique="0"  required="0">
<longdesc lang="en">
How long in seconds to delay on stop operation.
</longdesc>
<shortdesc lang="en">Stop delay</shortdesc>
<content type="integer" default="${OCF_RESKEY_stopdelay_default}" />
</parameter>

<parameter name="mondelay" unique="0"  required="0">
<longdesc lang="en">
How long in seconds to delay on monitor operation.
</longdesc>
<shortdesc lang="en">Monitor delay</shortdesc>
<content type="integer" default="${OCF_RESKEY_mondelay_default}" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="30s" />
<action name="stop" timeout="40s" />
<action name="status" depth="0" timeout="40s" interval="10s" />
<action name="monitor" depth="0" timeout="40s" interval="10s" />
<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="5s" />
</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
	ocf_exit_reason "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

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 $?
