#!/bin/sh

LOGROTATE="/etc/cron.daily/logrotate"

test -x /usr/sbin/accton || exit 0

echo "Login accounting for the month ended `date`:" > /var/log/wtmp.report
echo >> /var/log/wtmp.report

# The logrotate script happens to run before this one, effectively swallowing
# all information out of wtmp before we can use it. Hence, we need to use the
# previous file. Bad hack. Too bad we never heard from the logrotate maintainer
# about this ...

if [ -f "${LOGROTATE}" ] && [ -x /usr/sbin/logrotate ]
then
	# make sure wtmp.1 exists
	if [ -f /var/log/wtmp.1 ]
	then
		LOGFILE="/var/log/wtmp.1"
	fi
		if [ -f /var/log/wtmp.1.gz ]
	then
		LOGFILE2="`tempfile`"
	fi
		if [ -n "${LOGFILE}" ] && [ -n "${LOGFILE2}" ]
	then
		ac -f "${LOGFILE2}" -f "${LOGFILE}" -p | sort -nr -k2 >> /var/log/wtmp.report
		echo >> /var/log/wtmp.report
		last -f "${LOGFILE2}" >> /var/log/wtmp.report
	elif [ -n "${LOGFILE}" ] && [ -z "${LOGFILE2}" ]
	then
		ac -f "${LOGFILE}" -p | sort -nr -k2 >> /var/log/wtmp.report
		echo >> /var/log/wtmp.report
		last -f "${LOGFILE}" >> /var/log/wtmp.report
	fi
		rm -f "${LOGFILE2}"
else
	ac -p | sort -nr -k2 >> /var/log/wtmp.report
	echo >> /var/log/wtmp.report
	last >> /var/log/wtmp.report
fi

chown root:adm /var/log/wtmp.report
chmod 640 /var/log/wtmp.report
