#!/usr/bin/ksh
# autoloads for ksh
# these are the ksh functions accessed by this function
#
# Function compAnAttr: run pdls on multiple single-value
# attributes and compare the results to the answers that
# the caller expects. This command can take an indeterminate
# amount of command line arguments.
#
# Input positional parameters:
#     $1. the class of the object
#     $2. The target i.e. spooler or supervisor name
#     $3. The attribute you are searching for
#     $4. The answer that you expect
#     $5. the next attribute ...
#     $6. The answer that ...
#     and so forth ...
#
# This function returns 0 if success, else an error
#
# Author: Tim Unser
#
# 12/18/93 JRB Added support for
#		a. multi-valued attributes,
#		b. values with spaces,
#		c. error reports for all differences, rather than just first
#		d. reformatted error output to be easier to read
#		e. added pattern matching ability, for example:
#			$ compMultAttr server jbspl queues-supported "*myqueue*"
#			succeeds if myqueue is anywhere in the queues-supported list.
#			$ compMultAttr server jbspl queues-supported "myqueue*"
#			succeeds if myqueue is first in the queues-supported list.

#set -x
CLASS=$1
OBJECT=$2

shift 2
integer numOfParms=$# count=1 numOfAttrs=0 value=0 wcNum=0

if (( numOfParms%2==1 ))
then
  reportError "compMultAttr: ERROR: Wrong number of parameters"
  exit 1
fi

COMMAND='$PD_PATH/pdls -c $CLASS -g -s line -r '

if (( count < numOfParms ))
then
  COMMAND="$COMMAND \${$count}"
  count=count+2
  numOfAttrs=numOfAttrs+1
fi

while (( count < numOfParms ))
do
  COMMAND="$COMMAND,\${$count}"
  count=count+2
  numOfAttrs=numOfAttrs+1
done

COMMAND="$COMMAND \$OBJECT"
# echo $COMMAND
# echo ${1} ${3} ${5} ${7} ${9} ${11} ${13}

# run the pdls command,
# then massage pdls output for setting environment variables with eval
#	add double-quotes
#	eliminate spaces between attribute and value
#	remove dash(-) from attribute names
#       to create a legal environment variable name

eval $COMMAND > /tmp/cmdout.$$
if [ $? -ne 0 ]
then
	rm -f /tmp/cmdout.$$
	reportError "pdls command failed"
	exit 0
fi

{
	sed < /tmp/cmdout.$$ \
			-e 's/^[ ][ ]*/,/' \
			-e 's/$/\\/' \
			-e '/=/{
            	s/[ ]*= /=\\\
/
				s/^/\
/
				}' | sed -e '/=/s/-//g'
	echo
}  > /tmp/attrvals.$$

if [ $? -ne 0 ]
then
	reportError "sed failed"
	rm -f /tmp/attrvals.$$
	exit 0
fi


#echo ----
#echo /tmp/cmdout.$$
#echo ----
#cat /tmp/attrvals.$$
#echo ----

rm -f /tmp/cmdout.$$
#
# assign the values to the attributes in the environment
#
. /tmp/attrvals.$$
rm /tmp/attrvals.$$

count=1
while (( count < numOfParms ))
do
	# convert the attribute name from the command line
	# to the attribute name in the environment
	ATTR="$"`echo "${1}" | sed -e 's/att-//' -e 's/-//g'`

	# get the value(s) of the attribute from the environment
	# 1. rip out squiggly parens
	# 2. replace commands with newlines, if any,
	#    so that multi-value listed can be sorted
	# 3. sort the values
	# 4. put back the commas
	VALS=`( eval echo $ATTR ) | sed -e '{
		s/[{][ ]*//g
		s/[ ]*[}]//g
		s/[ ]*,[ ]*/\\
/g
}' | ( sort; echo ) | sed -e '{
:join
	N
	/\\n$/b
	s/\\n/,/
	b join
}'`

	# compare the value from the environment with
	# the value from the command line allowing pattern matching.
	case "${VALS}" in
		${2}) ;;
		*) reportError "---------- ERROR: compMultAttr ----------
	attribute: $1
	expected:  >$2<
	got:       >$VALS<";
		rc=2;;
    esac

	# prepare to get the next attribute/value pair from the command line
	count=count+2
	shift 2
done

exit 0
#
#    Version      Date     Time    Owner   Comment
# ------------- -------- -------- -------- ----------------------------
# V1.2          08/19/94 12:33:30 nrjbehrs new POSIX pdls -r syntax
# V1.3          08/30/94 16:41:20 nrjbehrs iut updates
# V1.4          11/17/94 17:48:09 nrjbehrs iut -g fix; media object validation
