[net.sources] *Improved* newsgroup peruser

oscar@utcsrgv.UUCP (Oscar M. Nierstrasz) (05/12/84)

Okay, this really *does* have some advantages over `readnews -l',
namely in listing the number of lines & the author of each article.
Also, this runs faster than before (but not as fast as it could
if awk had a `skip to next file' command!).  Changes were made by
an anonymous hacker.
------------------------------ cut here --------------------------------
#! /bin/sh
#	png [-ar] newsgroups ...
#	Peruse news group.
#	List subject headings of articles in listed newsgroups
#	starting at last-read article in $NEWSRC.
#	With the -a flag all articles are listed.
#	With the -r flag all articles are listed in reverse order.
#	Author : Oscar Nierstrasz @ utcsrgv!oscar
#	Modified by anonymous sprites ...

if test ! -r ${NEWSRC=$HOME/.newsrc}
then
	echo "$0: Can't find $NEWSRC" 1>&2
	exit
fi
n=/usr/spool/news
s='/^/ { if (FILENAME == fn) next }
/^From:/ { i=index ($0,"(")
	if (i) fr=substr ($0,i)
	else fr="(" $2 ")" }
/^Subject:/ { su=substr($0,index($0," ")+1) }
/^Lines:/ { li=$2 }
/^$/ { fn=FILENAME
	printf "%4d: %-50s", fn, substr(su,1,50)
	printf " %3d %s\n", li, substr(fr,1,20) }'

for i
do
	case "$i" in
	-a )	all=y ;;
	-r )	all=y
		rev=r ;;

	* )
		d=`echo $i | sed "s/\./\//g"`
		if test ! -d $n/$d
		then
			echo "$i: no such newsgroup" 1>&2
		else
			cd $n/$d
			echo
			echo "-------- $i --------"
			echo
			case "$all" in
			y )	f=all ;;
			* )	f=`sed -n "/^$i[:!]/s/.*-//p" $NEWSRC`
				case "$f" in
				"" )	f=all ;;
				* )	if test ! -r $f
					then
						f=all
					fi ;;
				esac ;;
			esac
			case "$f" in
			all )	awk "$s" `ls [0-9]* | sort -n$rev`;;
			* )	awk "$s" `ls [0-9]* |
				awk "\\\$0+0 > $f" | sort -n$rev`;;
			esac
		fi
	esac
done