#! /bin/sh
# fixscript will replace this line with code to load innshellvars

##  $Revision: 1.2 $
##  Tally/update the newgroup/rmgroup control log.
##  Merge in a log that contains newgroup/rmgroup control messages so that
##  the "control.log" file is updated to contain the new counts of how
##  often each group has been newgroup'd or rmgroup'd.  This is run by
##  scanlogs, which prepares this from the control-message handlers if
##  control.ctl specifies logging.

CONTROL=${MOST_LOGS}/control.log
CONTROL_NEW=${CONTROL}.new
CONTROL_OLD=${CONTROL}.old

PROGNAME=`basename $0`
LOCK=${LOCKS}/LOCK.${PROGNAME}

##  Lock.
trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15
shlock -f ${LOCK} -p $$ || {
    echo "$0: cannot lock ${LOCK}" 1>&2
    exit 1
}

##  Prepare the files.
if [ ! -f ${CONTROL} ]; then
    touch ${CONTROL}
    chmod 0660 ${CONTROL}
fi
rm -f ${CONTROL_NEW} ${CONTROL_OLD}
ln ${CONTROL} ${CONTROL_OLD}
touch ${CONTROL_NEW}
chmod 0660 ${CONTROL_NEW}

##  Grab the data.
${SED} -n -e 's/[	 ][	 ]*/ /g' -e 's/^ \(Control:.*\)$/1 \1/p' \
    | cat - ${CONTROL} \
    | ${SED} -e 's/ /#/g' -e 's/\([0-9][0-9]*\)#\(.*\)/\1 \2/' \
    | ${AWK} 'BEGIN {
	    ctl[0]=0;
	}
	{
	    ctl[$2] += $1;
	}
	END {
	    for (line in ctl) {
		if (line != 0) {
		    print ctl[line], line;
		}
	    }
	}' \
    | tr '#' ' ' \
    | sort -n -r >${CONTROL_NEW}
mv -f ${CONTROL_NEW} ${CONTROL}

##  All done.
rm -f ${LOCK}
exit 0
