#!/bin/sh
#
# Author: Zhao Kai <zhaokai@cn.ibm.com>
#
# License:      GNU General Public License (GPL)
# Support:      linux-ha@lists.linux-ha.org
#
#set -x
. @HA_HBCONF_DIR@/shellfuncs

OCF_ROOT_DIR=@OCF_ROOT_DIR@
OCF_RA_DIR=@OCF_RA_DIR@

OCF_RA_VERSION_MAJOR=1 ; export OCF_RA_VERSION_MAJOR
OCF_RA_VERSION_MINOR=0 ; export OCF_RA_VERSION_MINOR
OCF_ROOT=$OCF_ROOT_DIR ; export OCF_ROOT

LEGAL_ACTIONS="(start|stop|status|usage|meta-data)"

log_and_print(){
	ha_log "$*"
	echo "$*"
}

#
# rsc_id=rsc1 rsc_type=IPaddr2 provide=heartbeat start ip=192.168.0.1 .....
#
ra_execocf(){
    if [ "x${1}" = "x" ]; then
	log_and_print "ERROR: No action specfied."
	usage
	exit 1
    fi

    . ${OCF_RA_DIR}/heartbeat/.ocf-shellfuncs
    
    __ACTION=$1
    __SCRIPT_NAME="${OCF_RA_DIR}/heartbeat/${OCF_TYPE}"
    
    if [ "x${OCF_TYPE}" = "x" ]; then
	log_and_print "ERROR: Internal error. No value for OCF_TYPE specified"
	exit 1
    fi
    
    if [ ! -x $__SCRIPT_NAME ] 
    then
	log_and_print "ERROR: $__SCRIPT_NAME is not an executable file "
	exit 1
    fi

    # execute ocf ra and get return value
    case $__ACTION in
	start)		$__SCRIPT_NAME start;;
	stop)		$__SCRIPT_NAME stop ;;
	monitor)	$__SCRIPT_NAME monitor;;
	status)		$__SCRIPT_NAME monitor;;	# Mapping this to monitor is a bug
	
	usage|help)	usage;;
	*)		log_and_print "ERROR: Unknown operation: $__ACTION"
			usage
			exit 1;;
    esac
    
    ocf_return_status=$?
    case $ocf_return_status in
	$OCF_SUCCESS)
              case $__ACTION in
		  monitor|status)	log_and_print "INFO: $rsc_type Running OK";;
		  *)			log_and_print "INFO: $rsc_type Success";;
	      esac;;

	$OCF_ERR_GENERIC)
		log_and_print "ERROR: $rsc_type Generic error";;

	$OCF_ERR_ARGS)
		log_and_print "ERROR: $rsc_type Illegal argument";;

	$OCF_ERR_UNIMPLEMENTED)
		log_and_print "ERROR: $rsc_type Function unimplemented";;

	$OCF_ERR_PERM)
		log_and_print "ERROR: $rsc_type User had insufficient privilege";;

	$OCF_ERR_INSTALLED)
		log_and_print "ERROR: $rsc_type Program is not installed";;

	$OCF_ERR_CONFIGURED)
		log_and_print "ERROR: $rsc_type Program is not configured";;

	$OCF_NOT_RUNNING)
		log_and_print "INFO: $rsc_type Resource is stopped";;
	*)
		log_and_print "ERROR: $rsc_type Unknown error: $ocf_return_status"
		exit 1
		;;
	esac

	return $ocf_return_status
}
