#!/bin/sh
# check: report on users' mail
# @(#) check.sh 1.2 92/03/31
# 90/05/30 john h. dubois iii (john@armory.com)
# 90/11/14 removed ksh-specific code
# 90/11/30 changed to expect mail files in user's home dir
# 92/02/16 added help option
# 92/03/31 changed to say "last read" instead of "last read or received"
#          because mail now leaves the modify time alone.
#          May fail for other mail agents...
# 93/11/30 Made #!/bin/sh for server

name=check
if [ "$1" = -h ]; then
    echo \
"$name: check for whether users have mail.
Usage: $name [username ...]
For each named user whose mail spool file is in a readable directory,
$name will report the amount of waiting mail and the last time that mail
was read or received.
If no username is given, the report is done for the user invoking $name."
    exit 0
fi
[ $# -eq 0 ] && set -- $USER

for user in "$@"; do
	[ $user = root ] && homedir=/ || homedir=/u/$user/
    if [ -x $homedir ]; then
	mailbox=$homedir.mailbox
	if [ -f $mailbox ]; then
	    set -- `/bin/ls -og $mailbox`
	    sz=$3
	    shift 3
	    when=$*
	    echo \
	    "$user\thas $sz characters of mail, last received $1 $2 $3"
	else
	    if egrep \^$user: /etc/passwd > /dev/null; then
		echo "$user has no mail spool file."
	    else
		echo "$user: no such user."
	    fi
	fi
    else
	if egrep \^$user: /etc/passwd > /dev/null; then
	    echo "Can't access $user's home directory."
	else
	    echo "$user: no such user."
	fi
    fi
done
