#!/bin/sh

root=/rtfm/faq_archiver
lock=$root/rkive_going
archived_max=$root/.archived_max
archived_list=$root/.archived

if [ -f $lock ]; then
	pid=`cat $lock`
	# Gross Perl hack because kill -0 doesn't work under Ultrix
	if /usr/athena/bin/perl -e "exit (kill(0, $pid) ? 0 : 1);"; then
		echo "Rkive already running as PID $pid."
		exit
	fi
	rm $lock
fi

echo $$ > $lock

if [ -s $archived_max ]; then
	rm -f $archived_list.new$$
	tail -100 $archived_list > $archived_list.new$$ && \
	mv $archived_list.new$$ $archived_list
fi

/rtfm/bin/rkive $* -f /rtfm/faq_archiver/rkive.cf 2>&1 | \
	grep -v 'No such newsgroup'

for file in /rtfm/ftp/pub/usenet/*/index \
   `find /rtfm/ftp/pub/usenet-by-hierarchy -type f -name index -print`; do
	rm -f $file~
	if grep -is '^Subject:[ 	]*index[ 	]*$' $file; then
		true
	elif grep -is '^[^ 	:]*archive-name:.*/index[ 	]*$' $file; then
		true
	elif /usr/spool/FAQ_archiver/sort-index.pl $file > $file.new; then
		rm -f $file && mv $file.new $file
		chmod g+w $file
	else
		rm -f $file.new
		echo "Sort of $file failed."
	fi
done

rm $lock
