#! /bin/sh
# superkludge - implement the stupid Supersedes header for specific groups

# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/usr/lib/news/bin/config}

PATH=$NEWSCTL/bin:$NEWSBIN:$NEWSPATH ; export PATH
umask $NEWSUMASK
trap "rm -f /tmp/sup*$$ ; exit 0" 0 1 2

verbose=
case "$1" in
-v)	verbose=yes ; shift ;;
esac

for ng
do
	dir=`echo $ng | sed 's;\.;/;g'`
	if test ! -d $NEWSARTS/$dir
	then
		echo "$0: no directory for newsgroup \`$ng'" >&2
		exit 1
	fi
	cd $NEWSARTS/$dir
	>/tmp/sup$$
	for f in `ls | egrep '^[0-9]+$'`
	do
		awk 'BEGIN { mid = "xxx" }
			/^$/ { exit }	# goes to END
			/^Supersedes:[ 	]/ { sup = $2 }
			/^Message-ID:[ 	]/ { mid = $2 }
			END { print FILENAME, mid, sup ; exit }' $f >>/tmp/sup$$
	done
	awk 'NF > 3 || $2 !~ /^<.*>$/ || $3 !~ /^(<.*>)?$/' /tmp/sup$$ >/tmp/supx$$
	if test -s /tmp/supx$$
	then
		echo "$0: message-id format problems:" >&2
		cat /tmp/supx$$ >&2
		exit 1
	fi
	awk 'NF == 3 { print $3 }' /tmp/sup$$ | sort >/tmp/supd$$
	sort +1 /tmp/sup$$ -o /tmp/sup$$
	join -j2 2 -o 2.1 /tmp/supd$$ /tmp/sup$$ >/tmp/supx$$
	rm -f `cat /tmp/supx$$`
	if test " $verbose" = " yes"
	then
		echo "`wc -l </tmp/supx$$` superseded in $ng"
	fi
done
