#!/bin/sh

#
# Generate per-user cpu time stats.  Stats are kept in $acctdir/hostname/date.
# Note that since we do this a few minutes after midnight, date is yesterday,
# not today.  If $acctdir/hostname doesn't exist, we create it.
#

#
# Change these to put things in different places
#
pracct=/usr/local/etc/pracct
yesterday=/usr/local/etc/yesterday
acctdir=/usr/local/acct

#
# Make a copy of the accounting file, then do the normal daily summary
# and truncate the accounting file.  Accounting is temporarily turned
# off while we snarf a copy of the file and do the statistics to keep
# things in sync.
#
/usr/etc/accton
cp /usr/adm/acct /tmp/acct1.$$
/usr/etc/sa -s > /dev/null
rm /usr/adm/acct
touch /usr/adm/acct
/usr/etc/accton /usr/adm/acct

#
# Generate the per-user stats.
#
$pracct -a /tmp/acct1.$$ -f Ux | awk '
		{time[$1] = time[$1] + $2}
		END {for (name in time) print name, time[name]}
	' > /tmp/acct2.$$

rm -f /tmp/acct1.$$

#
# Make sure a directory exists for this machine.  If not, try to create it.
# Move daily summary temp file to accounting directory.  This must be done
# as user "acct" so we can write on the common accounting directory.
#
su acct << EOF_SU_ACCT
	if test ! -d $acctdir/`hostname`
	then
		mkdir $acctdir/`hostname`
	fi
	cp /tmp/acct2.$$ $acctdir/`hostname`/`$yesterday | tr ' ' '-'`
EOF_SU_ACCT

rm -f /tmp/acct2.$$
