#!/bin/sh
# @(#) from.sh 1.1 92/02/16
# from: check for mail; print From: and Subject: lines from message headers.
# 90/12/13 john h. dubois iii (john@armory.com)
# 91/06/09 added check for existance of mailbox
# 91/06/11 fixed check for existance of mailbox, added check for mail
# 91/09/27 added facility for specifying target users or files
# 91/09/28 changed search from egrep expression to sed script for header 
#         recognition; added separation between headers
# 91/11/27 use $MAIL if set
# 92/02/16 added help option

if [ "$1" = -h ]; then
    echo \
"$0: tell who mail is from.
Usage: $0 [username ...]
For each user named and whose mailbox is readable,
$0 searches the user's mailbox and lists the author
and subject of each message in the mailbox.
If no username is given, the invoking user's mailbox is checked."
    exit 0
fi

multi=0
case $# in
0)    set ${MAIL:-$HOME/.mailbox};;
1)    ;;
*)    multi=1;;
esac

while [ $# -gt 0 ]; do
    case $1 in
    [a-z0-9]*) home=`awk -F: 'user == $1 { print $6;}' user=$1 /etc/passwd`;
	mailbox=$home/.mailbox
        if [ -z "$home" ]; then
            echo "No user $1."
        elif [ ! -x $home -o -f $mailbox -a ! -r $mailbox ]; then
	    echo "Can't access $1's mailbox."
	else
            targ="$targ $mailbox"
	fi;;
    *)    targ="$targ $1";;
    esac
    shift
done

for i in $targ; do
    if [ -f $i ]; then
	if [ -s $i ]; then
	    [ $multi -eq 1 ] && echo "$nl$i":
	    sed '
/^/b inhdr
d
:inhdr
/^From /b phdr
/^Subject: /b phdr
b done
: phdr
x
/-----/p
s/-----//
x
p
: done
N
s/.*\n//
/^$/{
s/.*/-----/
h
d
}
b inhdr
' $i
	else
	    echo "${nl}No mail in $i."
	fi
    else
	if [ $multi -eq 1 ]; then
	    echo "$nl$i: no file."
	else
	    echo "No mailbox."
	fi
    fi
    nl="\n"
done
