#!/bin/sh
#  $Id: newsq.in,v 1.4 1998/09/09 07:32:12 src Exp $
#
#  list the outgoing news queue
#
#  NOTE: don't change 'newsq', it is auto-generated.
#        make changes to 'newsq.in' instead.
#
#  $Log: newsq.in,v $
#  Revision 1.4  1998/09/09 07:32:12  src
#  Version 1.1
#
#  Revision 1.3  1998/07/12 09:39:29  src
#  newsx version 1.0
#
BATCH=/var/spool/news/out.going
INN_MODE=1
PS_AWX="ps awx"
# some may need ax here

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_AWX | grep "newsx[(]"
	    sleep ${wait}
	done
    else
	$PS_AWX | 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
