#!/bin/sh
#
#  $Id: newsq.in,v 1.1 1997/07/20 11:31:23 src Exp $
#
#  list the outgoing news queue
#
#  NOTE: don't change 'newsq', it is auto-generated.
#	 make changes in 'newsq.in instead.
#
BATCH=/var/spool/news/out.going
INN_MODE=1

st=0
wait=5

# Options.
all=
cont=
stat=
quiet=
hosts=
count=

while test ! "X$1" = "X" ; do
    case "$1" in
    -a)     all=y; shift ;;
    -c)     cont=y; stat=y; shift ;;
    -n)     count=y; quiet=y; shift ;;
    -q)     quiet=y; shift ;;
    -s)     stat=y; shift ;;
    -w)     wait=$2; cont=y; shift; shift ;;
    -*)     echo "$0: unknown option: $1"; exit 2 ;;
    *)	    hosts="${BATCH}/$1 ${hosts}"; shift ;;
    esac
done

if test "X$stat" = "Xy" ; then
    #
    # Running statistics
    #
    if test "X$cont" = "Xy" ; then
	while true; do
	    ps -ax | grep "newsx[(]"
	    sleep ${wait}
	done
    else
	ps -ax | grep "newsx[(]"
    fi
else
    #
    # Outgoing queue
    #
    st=1
    for f in ${BATCH}/*; do
	is_host=`( echo "${hosts}" |
		 sed "s!${f} !YES!" |
		 sed "s!${BATCH}/[^ ]* !!g" )`
	if test "X$hosts" = "X" ||
	   test "X$is_host" = "XYES" ; then
	    if test "${INN_MODE}" = "0"; then
		# C News mode
		togo=${f}/togo
	    else
		# INN mode
		togo=${f}
	    fi
	    if test -f ${togo}; then
		if test ! -r ${togo}; then
		    echo "${f}: no permission" | sed "s!${BATCH}/!!"
		elif test "X$all" = "Xy" || test -s ${togo}; then
		    if test ! "X$quiet" = "Xy" ; then
			echo "${f}:" | sed "s!${BATCH}/!!"
			cat ${togo}
		    fi
		    if test "X$count" = "Xy" ; then
			n=`cat ${togo} | wc -l`
			echo "${f}:" ${n} | sed "s!${BATCH}/!!"
		    fi
		    st=0
		fi
	    fi
	fi
    done
fi

exit $st
