#!/bin/sh
#
# cron script to perform monthly login accounting.
#
# Written by Ian A. Murdock <imurdock@gnu.ai.mit.edu>
# Modified by Dirk Eddelbuettel <edd@debian.org>   
# Modified by Tero Tilus <terotil@www.haapavesi.fi>
# Patch adopted by Christian Perrier <bubulle@debian.org> for #187538
# Modified by Marcos Fouces <marcos@debian.org>
# Modified by Andrew Bower <andrew@bower.uk>

# Since logrotate maintainer defined the rotation of wtmp file when it
# reach the size of 1 MB instead of doing it monthly, we also need to
# check rotated wtmp files in order to have all the records.

test -x /usr/sbin/accton || exit 0
	echo "####################################################################" > /var/log/wtmp.report
	echo "################# LOGIN ACCOUNTING MONTHLY REPORT ##################" >> /var/log/wtmp.report
	echo "####################################################################" >> /var/log/wtmp.report
	echo >> /var/log/wtmp.report

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

# Check the existence of wtmp.1 or wtmp.1.gz before trying to process them.
# In a recently installed system none of them should exist and thereby
# cron job gives an error. Check (#864242)
# The script process the content of wtmp.1 or wtmp.1.gz (if one of them exists)
# and also the data from current wtmp.

if test -f /var/log/wtmp.1 || test -f /var/log/wtmp.1.gz; then

	if [ -f /var/log/wtmp.1 ]
	then
		WTMP="/var/log/wtmp.1"
	elif [ -f /var/log/wtmp.1.gz ]
	then
		WTMP_WAS_GZIPPED="1"
		WTMP="`mktemp`"

		gunzip -c /var/log/wtmp.1.gz > "${WTMP}"
	fi
        echo "Data contained in rotated wtmp file [*]:" >> /var/log/wtmp.report
        echo >> /var/log/wtmp.report
	ac -f "${WTMP}" -p | sort -nr -k2 >> /var/log/wtmp.report
	echo >> /var/log/wtmp.report

	if [ -n "${WTMP_WAS_GZIPPED}" ]
	then
		# remove temporary file
		rm -f "${WTMP}"
	fi
fi
if test -f /var/log/wtmp; then
	echo "Data contained in current wtmp file [*]:" >> /var/log/wtmp.report
	ac -p | sort -nr -k2 >> /var/log/wtmp.report
	echo >> /var/log/wtmp.report
	printf "[*] data contained in legacy wtmp files is likely to be absent or incomplete\n\n" >> /var/log/wtmp.report
fi
if test -x /usr/bin/last; then
	echo "Data contained in wtmpdb database:" >> /var/log/wtmp.report
	month_ago="$(date --date='1 month ago' +%Y%m%d%H%M%S)"
	last -s "$month_ago" >> /var/log/wtmp.report
fi

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