#
# 	$Id: ocf-shellfuncs.in,v 1.18 2005/07/27 10:16:22 panjiam Exp $
#
# 	Common helper functions for the OCF Resource Agents supplied by
# 	heartbeat.
#
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Bre
#                    All Rights Reserved.
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 


# TODO: Some of this should probably split out into a generic OCF
# library for shell scripts, but for the time being, we'll just use it
# ourselves...
#

# TODO wish-list:
# - Generic function for evaluating version numbers
# - Generic function(s) to extract stuff from our own meta-data
# - Logging function which automatically adds resource identifier etc
#   prefixes
# TODO: Move more common functionality for OCF RAs here.
#
HA_D=/etc/ha.d
__SCRIPT_NAME=`basename $0`
. ${HA_D}/shellfuncs

__ocf_set_defaults() {
	__OCF_ACTION="$1"

	# Return to sanity for the agents...
	unset LANG
	LC_ALL=C
	export LC_ALL

	# TODO: Review whether we really should source this. Or rewrite
	# to match some emerging helper function syntax...? This imports
	# things which no OCF RA should be using...


	OCF_SUCCESS=0
	OCF_ERR_GENERIC=1
	OCF_ERR_ARGS=2
	OCF_ERR_UNIMPLEMENTED=3
	OCF_ERR_PERM=4
	OCF_ERR_INSTALLED=5
	OCF_ERR_CONFIGURED=6
	OCF_NOT_RUNNING=7

	OCF_CHECK_LEVEL=0
	# Strip the OCF_RESKEY_ prefix from this particular parameter
	if [ -z "$OCF_RESKEY_OCF_CHECK_LEVEL" ]; then
		: ${OCF_CHECK_LEVEL:=$OCF_RESKEY_OCF_CHECK_LEVEL}
	fi

	if [ -z "$OCF_ROOT" ]; then
		: ${OCF_ROOT=/usr/lib/ocf}
	fi
	if [ ! -d "$OCF_ROOT" ]; then
		ha_log "ERROR: OCF_ROOT points to non-directory $OCF_ROOT."
		exit $OCF_ERR_GENERIC
	fi

	if [ -z "$OCF_RESOURCE_TYPE" ]; then
		: ${OCF_RESOURCE_TYPE:=$__SCRIPT_NAME}
	fi

	if [ -z "$OCF_RA_VERSION_MAJOR" ]; then
		: We are being invoked as an init script.
		: Fill in some things with reasonable values.
		: ${OCF_RESOURCE_INSTANCE:="default"}
		return 0
        fi

	# TODO: Anything else we should be setting and thus checking?
        # There is nothing in this script which depends on the version
	# of the API.  TESTING THIS HERE IS A BUG.  THIS SHOULD BE
	# tested by the script that's invoked us.  FIXME!!
	if [ "x$OCF_RA_VERSION_MAJOR" != "x1" ]; then
		ha_log "ERROR: This script is OCF RA API 1.x compliant only!"
		exit $OCF_ERR_UNIMPLEMENTED
	fi
	# TODO: Should the minor level really be a number and not rather
	# a list of flags...?
        # AlanR says -- absolutely not -- a list of flags is good for a list
	# of implemented features, not a version compiliance
	# perhaps some future version might have such a list of
	# flags, but that would be _in addition to_ the minor version number
	if [ -z "$OCF_RA_VERSION_MINOR" ]; then
		ha_log "ERROR: No OCF RA minor version set."
		exit $OCF_ERR_UNIMPLEMENTED
	fi

	if [ "x$__OCF_ACTION" = "xmeta-data" ]; then
		OCF_RESOURCE_INSTANCE="undef"
	fi	

	if [ -z "$OCF_RESOURCE_INSTANCE" ]; then
		ha_log "ERROR: Need to tell us our resource instance name."
		exit $OCF_ERR_ARGS
	fi
}

ocf_log() {
	# TODO: Revisit and implement internally.
	if
          [ $# -lt 2 ]
        then
          ocf_log err "Not enough arguments [$#] to ocf_log."
        fi
        __OCF_PRIO="$1"
        shift
        __OCF_MSG="$*"

        case "${__OCF_PRIO}" in
          crit)	__OCF_PRIO="CRIT";;
          err)	__OCF_PRIO="ERROR";;
          warn)	__OCF_PRIO="WARNING";;
          info)	__OCF_PRIO="INFO";;
          debug)__OCF_PRIO="DEBUG";;
          *)	__OCF_PRIO=`echo ${__OCF_PRIO}| tr 'a-z' 'A-Z'`;;
	esac

	ha_log "${__OCF_PRIO}: $__OCF_MSG"
}

__ocf_set_defaults "$@"
