#!/bin/sh
#
# General log and debug functions that can be used from all scripts
#
# usage: . THIS_SCRIPT
# 	 debugmsg STRING
# 	 errormsg STRING

#################
# Configuration #
#################

# Enable debug output here to monitor remote resource scripts
# *_DEST defines where output should be sent:
# syslog=0 , stdout=1, stderr=2
# Please note that syslog requires the 'logger' binary to be in the path.
# On most system it will be available in /usr/bin

DEBUG_INFO='1'
DEBUG_DEST='0'

INFO_DEST='0'

ERROR_DEST='0'

#############
# Functions #
#############

function debugmsg() {
	local DEST=''
	local LOG=''

	if [ -z ${DEBUG_INFO} -o ${DEBUG_INFO} -eq '0' ]; then
		return 1
	fi
	
	if [ -n ${DEBUG_DEST} -a ${DEBUG_DEST} -ne '0' ]; then
		LOG='echo'
		if [ ${DEBUG_DEST} -eq '1' ]; then
			# send output to stdout
			DEST='2>&1'
		fi
		if [ ${DEBUG_DEST} -eq '2' ]; then
			# send output to stderr
			DEST='1>&2'
		fi
	else
		LOG='logger'
	fi
	
	$LOG "DEBUG: $@" $DEST

	return 0
}

function infomsg() {
	local DEST=''
	local LOG=''
	
	if [ -n ${INFO_DEST} -a ${INFO_DEST} -ne '0' ]; then
		LOG='echo'
		if [ ${INFO_DEST} -eq '1' ]; then
			# send output to stdout
			DEST='2>&1'
		fi
		if [ ${INFO_DEST} -eq '2' ]; then
			# send output to stderr
			DEST='1>&2'
		fi
	else
		LOG='logger'
	fi
	
	$LOG "INFO: $@" $DEST

	return 0
}

function errormsg() {
	local DEST=''
	local LOG=''
	
	if [ -n ${ERROR_DEST} -a ${ERROR_DEST} -ne '0' ]; then
		LOG='echo'
		if [ ${ERROR_DEST} -eq '1' ]; then
			# send output to stdout
			DEST='2>&1'
		fi
		if [ ${ERROR_DEST} -eq '2' ]; then
			# send output to stderr
			DEST='1>&2'
		fi
	else
		LOG='logger'
	fi
	
	$LOG "ERROR: $@" $DEST

	return 0
}

#debugmsg "log functions initialized"
