#!/usr/bin/ksh
#
# assignment 3: write a shell script that generates as output
# its arguments in reverse order.  the script should take an -q argument
# which causes its outputs to be quoted so that the output can be eval'ed
#
# sample solution: may 23, 2000, bsy.
#
quote=0
while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-q)	quote=1; shift;;
		--)	shift; break;;
		-*)	echo "rev_args: illegal flag $1" >&2; exit 1;;
		*)	break;;
		esac;;
	esac
done
case $# in
0)
	exit 0;;
esac
set -A alist -- "$@"
i=${#alist[*]}
sep=''
while [[ $i -gt 0 ]]
do
	i=$(( $i - 1 ))
	case $quote in
	0)
		printf "%s%s" "$sep" "${alist[$i]}";;
	*)
#		q=$(printf "%s\n" "${alist[$i]}" | sed "s/'/'\\\\''/g")
		q=$(printf "%s\n" "${alist[$i]}" | sed "s/'/'\"'\"'/g")
		printf "%s'%s'" "$sep" "$q";;
	esac
	sep=' '
done
printf '\n'
