#!/bin/sh
#  $Id: newsq.in,v 1.5.2.5 2003/01/21 08:01:08 egil 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.5.2.5  2003/01/21 08:01:08  egil
#  Renamed to PACKAGE_ style
#
#  Revision 1.5.2.4  2003/01/21 07:10:47  egil
#  New name for BUGCATCHER
#
#  Revision 1.5.2.3  2002/09/30 18:16:38  egil
#  Patches from bug #103
#
#  Revision 1.5.2.2  2002/04/06 07:00:58  egil
#  Included support for -h
#
#  Revision 1.5.2.1  2002/02/06 19:54:49  egil
#
#  Debian Bug#84621: newsq --help
#
#  Revision 1.5  1999/04/19 03:51:03  src
#  Added config options for newsq
#
#  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/news/spool/outgoing
INN_MODE=1
PS_AWX="ps awx"
# some may need ax here

EXECUTABLE=newsx

st=0
wait=5

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

usage()
{
   cat << EOF
newsq - list news server outgoing queue

Usage: newsq [options] [hostname]
    -a    List all entries
    --batch batchdir   Use batchdir as the outgoing news article
	  spool directory instead of the default.
    -c    List continuously
    --cnews            Set to C News mode.
    --inn              Set to INN mode.
    -n    Show size of outgoing queue
    -q    Keep quiet
    -s    List state of any active newsx
    --version
    -w N  Continuous mode, wait for N seconds

For further information, try:
    man newsq
EOF
}

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

if test "X$version" = "Xy" ; then
    echo "newsq version 1.6"
    echo "Copyright 2003 Egil Kvaleberg <egil@kvaleberg.no>"
    echo "This is free software; the GNU General Public License applies."
    echo "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A"
    echo "PARTICULAR PURPOSE."
    exit 0
fi
if test "X$help" = "Xy" ; then
    usage
    echo "Report bugs to http://www.kvaleberg.com/bug/"
    exit 0
fi
if test "X$stat" = "Xy" ; then
    #
    # Running statistics
    #
    if test "X$cont" = "Xy" ; then
	while true; do
	    #Strip trailing blanks
	    $PS_AWX | grep "$EXECUTABLE[(]" | sed -e 's/ *$//'
	    sleep ${wait}
	done
    else
	$PS_AWX | grep "$EXECUTABLE[(]" | sed -e 's/ *$//'
    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
