#! /bin/sh
# mkhistory - rebuild history file and friends

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

PATH=$NEWSCTL/bin:$NEWSBIN/expire:$NEWSBIN:$NEWSPATH ; export PATH
umask $NEWSUMASK

lock="$NEWSCTL/LOCK"
ltemp="$NEWSCTL/L.$$"
echo $$ >$ltemp
trap "rm -f $ltemp ; exit 0" 0 1 2 15
while true
do
	if newslock $ltemp $lock
	then
		trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
		break
	fi
	sleep 30
done

cd $NEWSARTS
find `ls | egrep -v '\.'` -type f -name '[0-9]*' -print | histinfo | sort |
	awk -f $NEWSBIN/expire/histdups | histslash >$NEWSCTL/history.n

cd $NEWSCTL
if egrep '^<[^>]*@trash>	' history.n >/dev/null
then
	echo "$0: (warning) empty/trash articles found, will expire at once" >&2
	echo "$0:     (grep history file for '@trash' to see them)" >&2
fi
mkdbm history.n
mv history history.o &&		# install new ASCII history file
mv history.n history &&
rm -f history.pag &&		# and related dbm files
rm -f history.dir &&
mv history.n.pag history.pag && mv history.n.dir history.dir
