#!/bin/sh -
#
#	lscan: scan of (estimated!) "active" accounts, shown in reverse
#	order of (estimated!) last login time, and fingerized for clarity.
#
#	usage: lscan <group>0+
#	If no groups are given all active accounts are considered.
#	*****Use sparingly***** in that form of usage,
#	as it does then rather strain the fileservers!
#
#	NOTE 1: now that e-mail delivery on most accounts creates and destroys
#	a lock-file in the home directory, the times are not really as useful
#	as they once were.
#
#	NOTE 2: the particular keywords and output parsings are probably
#	hopelessly specific to DoC. The requested fields are respectively
#	the username, the group, and the timestamp.
#
progname=`basename $0`
ls_time_style="+%b %e  %Y
%b %e %H:%M"


#
#	construct a suitable matching pattern for the groups
#
if [ $# -eq 0 ]; then	#pattern that matches all groups
	echo "(warning: scanning for all active accounts; may take some time.....)" >&2
	groups_match='/homes/'
else
	gids=""
	for group in $* ; do
		gid=`grep "^$group:" /etc/group | sed -e "s/^$group:[^:]*://" -e 's/:.*$//'`
		if [ `echo $gid | grep -c '^[0-9][0-9]*$'` -ne 1 ]; then
			echo "($group not a recognized group, ignoring)" >&2
		elif [ -z "$gids" ]; then
			gids="$gid"
		else
			gids="$gids|$gid"
		fi
	done

	if [ -z "$gids" ]; then
		echo "$progname: no recognized groups!" >&2
		exit 1
	fi

	groups_match="^[^:]*:[^:]*:[^:]*:($gids):.*/homes/"
fi


#
#	try to find active accounts in the given groups
#
dirs=`egrep "$groups_match" /etc/passwd | sed -e '/disabled-.*sh/d' -e '/\/true/d' -e '/\/false/d' -e '/[: ][aA]ccount[: ]/d' -e '/[: ][mM]aintenance[: ]/d' -e '/[: ][aA]dministration[: ]/d' -e '/[: ][sS]ystem[: ]/d' -e '/[: ][tT]est[: ]/d' -e '/[: ][aA]cct[: ]/d' -e '/[: ][nN]umber[: ]/d' -e '/::/d' -e 's/:.*$//' -e 's|^|/homes/|'`
if [ -z "$dirs" ]; then
	echo "$progname: no active accounts in the given group(s)!" >&2
	exit 1
fi


#
#	OK, now do the big scan!
#
ls --time-style="$ls_time_style" -laFLdt $dirs 2>&1 | egrep -v 'visitors| -> | not found|Permission denied|No such file or directory' | sed -e 's/^[^ 	][^ 	]*[ 	][ 	]*[^ 	][^ 	]*[ 	][ 	]*\([^ 	][^ 	]*\)[ 	][ 	]*\([^ 	][^ 	]*\)[ 	][ 	]*[^ 	][^ 	]*[ 	][ 	]*\([^ 	][^ 	]*[ 	][ 	]*[^ 	][^ 	]*[ 	][ 	]*[^ 	][^ 	]*\).*$/\1	\2	\3/' | fingerize
