#! /bin/bash
#
# 	$Id$
#
# 	Common helper functions for the OCF Resource Agents supplied by
# 	heartbeat.
#
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Br�e
#                    All Rights Reserved.
#
# Modified for linux-cluster 2005 by Lon Hohberger
#
# 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.
#
__SCRIPT_NAME=`basename $0`

# lhh - determine if we're a dumb terminal
consoletype &> /dev/null
if [ $? -eq 1 ]; then
	__SERIAL="yes"
fi

__LOG_PID=$PPID
__LOG_NAME=$(basename $(readlink /proc/$PPID/exe))

pretty_echo() {
	declare pretty
	declare n="[0m"
        declare __OCF_PRIO="$1"
        shift
        declare __OCF_MSG="$*"

	if [ -n "$__SERIAL" ]; then
		echo "<$__OCF_PRIO> $__OCF_MSG"
		return 0
	fi

	case $__OCF_PRIO in
	emerg)		pretty="[34;1;5m";;
	alert)		pretty="[34;1m";;
        crit|critical)	pretty="[34;1m";;
        err|error)	pretty="[0;34m";;
        warn|warning)	pretty="[35;1m";;
	note|notice)	pretty="[37;1m";;
        info)		pretty="[37;1m";;
        debug|dbg)	pretty="[0m";;
        *)		pretty="[37;1m";;
	esac

	echo "$n<$pretty$__OCF_PRIO$n> [10G$__OCF_MSG"
	return 0
}

__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

	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

	if [ -z "$OCF_ROOT" ]; then
		OCF_ROOT=$(dirname $0)
	fi
	if [ ! -d "$OCF_ROOT" ]; then
		ocf_log err "OCF_ROOT points to non-directory $OCF_ROOT."
		exit $OCF_ERR_GENERIC
	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
		ocf_log err "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
		ocf_log err "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
		ocf_log err "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

        declare __OCF_PRIO="$1"
	declare -i __OCF_PRIO_N

        shift

        declare __OCF_MSG="$*"

        case "${__OCF_PRIO}" in
	emerg)		__OCF_PRIO_N=0;; # Not in original ocf-shellfuncs
	alert)		__OCF_PRIO_N=1;; # Not in original ocf-shellfuncs
        crit|critical)	__OCF_PRIO_N=2;;
        err|error)	__OCF_PRIO_N=3;;
        warn|warning)	__OCF_PRIO_N=4;;
	note|notice)	__OCF_PRIO_N=5;; # Not in original ocf-shellfuncs
        info)		__OCF_PRIO_N=6;;
        debug|dbg)	__OCF_PRIO_N=7;;
        *)		__OCF_PRIO_N=5;; # Defaults to INFO
	esac

	pretty_echo $__OCF_PRIO "$__OCF_MSG"

	if [ -z "`which clulog 2> /dev/null`" ]; then
		return 0
	fi
	clulog -p $__LOG_PID -n $__LOG_NAME \
		-s $__OCF_PRIO_N "$__OCF_MSG"
}

__ocf_set_defaults "$@"
