#!/bin/sh

NEWSARTS=/afs/net/user/tytso/usenet
ACTIVE=/afs/net/user/tytso/usenet-demo/lib/active

cat << EOF > /tmp/bad_list.$$
americast
americast.ap
americast.latimes
americast.twt
nptn
nptn.campaign92
usa-today
EOF

find $NEWSARTS -type d -print | \
	sed -e 1d -e "s;^$NEWSARTS/;;" -e 's;/;.;g' | sort | \
	comm -13 /tmp/bad_list.$$ - | \
while read group 
do
        dir=`echo $group | tr . / `     # map ng name to directory name
        new=
	min=
        if test -d $NEWSARTS/$dir
        then
		ls $NEWSARTS/$dir | egrep '^[0-9]+$' | sort -n | \
			sed -n -e 1p -e '$p' > /tmp/ls.$$
		new=`tail -1 /tmp/ls.$$`
		min=`head -1 /tmp/ls.$$`
        fi
        case "$new" in
        "")     new=0        ;;      		# no files
        esac
        max="`expr 0000000000$new : '.*\(..........\)$'`"

        case "$min" in          # no files, so use max+1
        "")     min=`awk "END{ print $max + 1 }" /dev/null`     ;;
        esac
        case "$min" in
        [0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])  # short
                min=`expr 00000$min : '.*\(.....\)$'` ;;
        esac

        echo $group $max $min y
done >$ACTIVE.tmp

rm /tmp/bad_list.$$ /tmp/ls.$$

rm -f $ACTIVE.old
ln $ACTIVE $ACTIVE.old
mv $ACTIVE.tmp $ACTIVE
