#! /bin/sh

# Feed some articles from news.admin.net-abuse.sightings to the DCC.

#   This script works with the BSD Bourne shell, GNU grep, and other tools.
#   It might not work on other systems without changes.

# Copyright (c) 2003 by Rhyolite Software
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#	Rhyolite Software DCC 1.2.16-1.6 $Revision$


DCC_HOMEDIR=/var/lib/dcc
DEBUG=
VERBOSE=
# check the args once to get the home directory
while getopts "xnvh:a:g:l:o:s:" c; do
    case $c in
	x) set -x; DEBUG=-x;;
	v) VERBOSE=-v;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf

LDIR=`echo $DCCM_LOGDIR | sed -e "s@^[^/]@$DCC_HOMEDIR/&@"`
AGE=3
SDIR=/var/spool/news
NG=news/admin/net-abuse/sightings

USAGE="`basename $0`: [-xnv] [-h homedir] [-a age] [-l logdir] [-o dccargs] [-s spooldir] [-g group]"
OPTIND=1
while getopts "xnvh:a:g:l:o:s:" c; do
    case $c in
	x) ;;
	n) NOP=-n;;
	v) ;;
	h) ;;
	a) AGE="$OPTARG";;
	l) LDIR=`echo "$OPTARG" | sed -e "s@^[^/]@$CWD/&@"`
	    NA_ARGS="$NA_ARGS -l $LDIR";;
	o) NA_ARGS="$NA_ARGS -o $OPTARG";;
	s) SDIR=`echo "$OPTARG" | sed -e "s@^[^/]@$CWD/&@"`
	    NA_ARGS="$NA_ARGS -s $SDIR";;
	g) NG=`echo "$OPTARG" | sed -e 's@\.@/@g'`;;
	*) echo 1>&2 "$USAGE"; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    echo 1>&2 "$USAGE"
    exit 1
fi

cd $DCC_HOMEDIR
MARKER=$LDIR/ng-mark
if test -f "$MARKER"; then
    NEWER="-newer $MARKER"
fi

find "$SDIR/$NG" -type f -name '[0-9]*' -mtime -$AGE $NEWER	\
    | xargs $DCC_LIBEXEC/na-spam $DEBUG $VERBOSE $NOP -h $DCC_HOMEDIR $NA_ARGS

if test "$NOP" = ""; then
    touch $MARKER
fi
