#=============================================================================
#  
#  print_functions - print functions for ../bin/semaphore
#  semaphore - a Korn shell implementation of a counting semaphore
#  Copyright (C) 2004 Intel Corporation
#  
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#  
#  This program 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 General Public License
#  for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, see http://www.gnu.org/copyleft/gpl.html or
#  write to:
#  
#  The Free Software Foundation, Inc.,
#  59 Temple Place
#  Suite 330, Boston, MA 02111-1307
#  USA
#  
#  Author:
#  
#  John Spurgeon
#  5200 NE Elam Young Parkway
#  Hillsboro, OR 97124
#  
#  john.p.spurgeon@intel.com
#  
#=============================================================================

error()
{
	# Prints an error message prefixed by a function name to stderr.

	print "ERROR $FUNC_NAME: $1" >&2
}

info()
{
	# Prints an informative message prefixed by a function name to stderr.

	print "INFO $FUNC_NAME:\t$1" >&2
}

trace()
{
	# Prints a message when a function is called if debugging is turned on.

	print "BEGIN $@" >&2
}

print_queue()
{
	local FUNC_NAME=print_queue
	${TRACE:-trace $FUNC_NAME $@}

	local semaphore=$1
	local queue_dir=$HOME_DIR/$semaphore/$QUEUE_DIRNAME
	local queue_members=$(ls -1 $queue_dir)
	local pid=""
	local ppid=""
	local date_time_raw=""
	local year=""
	local month=""
	local day=""
	local hour=""
	local minute=""
	local second=""
	local date_time=""

	if [[ -n $queue_members ]]
	then
		print "Queue for semaphore ${semaphore}:"
		local i=0
		for queue_member in $queue_members
		do
			i=$(($i+1))

			date_time_raw=$(echo $queue_member | cut -d'.' -f1)
			year=$(echo ${date_time_raw} | cut -b1-2)
			month=$(echo ${date_time_raw} | cut -b3-4)
			day=$(echo ${date_time_raw} | cut -b5-6)
			hour=$(echo ${date_time_raw} | cut -b8-9)
			minute=$(echo ${date_time_raw} | cut -b10-11)
			second=$(echo ${date_time_raw} | cut -b12-13)
			date_time="20${year}-${month}-${day} ${hour}:${minute}:${second}"

			pid=$(echo $queue_member | cut -d'.' -f2)
			ppid=$(echo $(ps -p $pid -o ppid | tail -1))
			print "  ${i}. PID ${ppid} has been waiting since ${date_time}."
			print "     $(ps -p $ppid -o args | tail -1)"
		done
	fi
}

print_semaphore()
{
	local FUNC_NAME=print_semaphore
	${TRACE:-trace $FUNC_NAME $@}

	local semaphore=$1
	integer num_resources=$2
	integer resource_num=0

	if is_initialized $semaphore
	then
		print -n "\nSemaphore $semaphore has $num_resources resource"
		if (( $num_resources == 1 ))
		then
			print "."
		else
			print "s."
		fi
		while (( $resource_num < $num_resources ))
		do
			print -n "  Resource $resource_num is "
			pid=$(get_resource_holder $semaphore $resource_num)
			if [[ -n $pid ]]
			then
				print "taken by PID $pid."
				print "  $(ps -p $pid -o args | tail -1)"
			else
				print "available."
			fi
			resource_num=$(($resource_num+1))
		done
		print_queue $semaphore
	else
		${ERROR:-error "$semaphore is not a valid semaphore"}
	fi
}
