##########################################################################
# Title      :	newscan - scan news spool directory for search patterns
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1995-06-01
# Requires   :	
# Category   :	News
# SCCS-Id.   :	@(#) newscan	1.2 03/12/19
##########################################################################
# Notes
#    o	Uses "ngrep" to search news articles, ignoring headers
##########################################################################

PN=`basename "$0"`			# program name
VER='1.2'

ID=`expr "\`LANG=C id\`" : 'uid=[0-9][0-9]*(\([^)]*\)).*'`
: ${USER:=${ID:-postmaster}}
: ${NEWSPATH:=/var/spool/news}
: ${NewsTopics:=$HOME/.topics}

# Determine mail program knowing "-s" argument
Mail=mail
[ -x /bin/mailx ] && Mail=mailx

# Search local directories for commands
PATH=$PATH:$HOME/cmds:$HOME/bin		export PATH

FoundList=${TMPDIR:=/tmp}/ns$$.f
Errors=$TMPDIR/ns$$.e

usage () {
    echo >&2 "$PN - search news articles for search patterns, $VER (stv '95)
usage: $PN

File with patterns to search:	$NewsTopics"
    exit 1
}

msg () {
    for i
    do echo "$PN: $i" >&2
    done
}

fatal () { msg "$@"; exit 1; }

Exit () {

    Ret=$?
    # Matches or Errors (ignoring blank lines)?
    if [ `cat $FoundList $Errors | grep . | wc -l` -gt 0 ]
    then
    (
	if [ -s "$FoundList" ]
	then
	    cat - $FoundList <<!
$PN ended `date`

Searched Directories:
    $NEWSPATH

Searched Pattern:
    $Search

MATCHES:
!
	fi

	# Error messages (ignore blank lines)?
	if [ -s "$Errors" ]
	then
	    cat - "$Errors" <<!

ERRORS:
!
	fi

	) | $Mail -s "$PN - status " $USER
    fi

    # Mail the matching articles to the user
    if [ -s "$FoundList" ]
    then
	while read File	Match
	do
	    echo "! START ARTICLE $File"
	    echo "! MATCH <$Match>"
	    cat $File
	    echo "! END ARTICLE $File"
	    echo
	done < $FoundList |
	    $Mail -s "$PN - results" $USER
    fi

    rm -f "$FoundList" "$Errors" > /dev/null 2>&1
    exit $Ret
}

while [ $# -gt 0 ]
do
    case "$1" in
					# your flags here
	--)	shift; break;;
	-h)	usage;;
	-*)	usage;;
	*)	break;;			# first file name
    esac
    shift
done

[ -d "$NEWSPATH" -a -r "$NEWSPATH" -a -x "$NEWSPATH" ] ||
    fatal "cannot read news spool directory $NEWSPATH"

[ -f "$NewsTopics" -a -r "$NewsTopics" ] ||
    fatal "cannot read topics file $NewsTopics"

# Build one egrep line containing all topics
Search=
exec 3<&0 0<$NewsTopics
while read Pattern
do
    case "$Pattern" in
	\#*)	continue;;		# Ignore comments
	"")	continue;;		# Ingnore blank lines
    esac

    Search="${Search:+$Search|}$Pattern"
done
exec 0<&3 3<&-

SearchDirs=
for Path in `echo "$NEWSPATH" | sed 's/:/ /g'`
do
    [ -d "$Path" -a -r "$Path" ] &&
	SearchDirs="$SearchDirs $Path"
done

[ -z "$SearchDirs" ] &&
    fatal "No readable directories in NEWSPATH" "(NEWSPATH=$NEWSPATH)"

msg "Results will be mailed to user $USER"
sleep 5					# Last chance to abort...

exec > $FoundList 2> $Errors

trap 'Exit' 0
trap 'echo "*** $PN aborted at `date` - sorry" >&2 ; exit 2' 1 2 3 15

# Only search directories not containing a "." (i.e. "in.coming", ...)
find $SearchDirs \( \( -name '*.*' -o -name '.[!.]*' -o -name 'lost+found' \) \
		-prune \) -o \
	-follow -type f -newer $NewsTopics -print |
    xargs ngrep "$Search"

touch $NewsTopics
exit 0
