#! /bin/sh
# $Id: autoinc,v 1.4 1992/10/19 21:00:48 jerry book3 $
### autoinc - Incorporate new mail; refile from inbox automatically
### Usage: autoinc
# 
#                             NO WARRANTY
# 
#   BECAUSE THIS PROGRAM IS AVAILABLE FREE OF CHARGE, THERE IS NO WARRANTY
# FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
# TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
# 
#   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
# WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
# REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
# INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
# TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
# PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGES.


df=/bin/df  expr=/bin/expr  grep=/bin/grep  mailer=/usr/ucb/mail
quota=/usr/ucb/quota        sed=/bin/sed    tr=/bin/tr
error=xxx@yyy,aaa@bbb   # WHERE TO MAIL ERRORS
margin=300              # HOW MANY EXTRA KBYTES WE NEED
mh=/usr/local/mh        # WHERE MH PROGRAMS LIVE

# GET NAME OF FILESYSTEM WITH HOME DIRECTORY FROM LINES LIKE
# THIS.  TAKE LAST WORD STARTING WITH A SLASH ON SECOND LINE:
# Filesystem           kbytes    used   avail capacity  Mounted on
# hostname:/u1         842171  442588  315365    58%    /u1
homedir="`$df ${HOME?} | $sed -n '2s@.*\(/[^/]*\)$@\1@p'`"

# GET QUOTA VALUES.  USE sed TO GRAB LINE THAT STARTS WITH ${homedir}:
#    Disk quotas for yourname (uid 1234):
#    Filesystem   usage  quota  limit  timeleft  files  quota  limit
#    /u1          18451  20000  20000             1474      0      0
# AND PUT THE FIRST TWO NUMBERS ON THE LINE INTO SHELL VARIABLES:
eval `$quota -v |
$sed -n "s@^${homedir} *\([0-9][0-9]*\) *\([0-9][0-9]*\).*@used=\1 total=\2@p"`

# PUT x IN $1, NUMBER OF KBYTES IN $2, MAILBOX FILENAME IN $3.
# (NOTE: SOME ls -s OUTPUT IS IN 512-BYTE BLOCKS; NEED TO DOUBLE THOSE.)
set x `/bin/ls -s /usr/spool/mail/$USER`
wouldbe="`$expr $used + $2 + $margin`"
if [ $wouldbe -ge $total ]
then
    echo "used $used kytes, incoming mail $2 kbytes, quota is $total." |
    $mailer -s "ERROR -- no room to 'inc' mail!  Clean up now!" $error
    exit
fi

# FILTER inc OUTPUT THROUGH grep, PUT LEFTOVERS BACK ONTO STDERR.
# IF inc RETURNS NON-ZERO, EXIT.
$mh/inc 2>&1 | $grep -v '^inc: no mail to incorporate' 1>&2  || exit

set -e  # IF ANY ERRORS FROM NOW ON, EXIT

# SCAN inbox MESSAGES.  GET MSG. NUMBER, From: AND Sender: ADDRESS.
# TURN UPPER TO LOWER CASE.  THEN FEED TO STANDARD INPUT OF LOOP.
# INSIDE LOOP, refile MESSAGES WE KNOW HOW TO HANDLE; LEAVE OTHERS:
$mh/scan +inbox -format '%(msg) %(mbox{from}) %(mbox{sender})' |
$tr '[A-Z]' '[a-z]' |
while read msg from sender
do
    # FOR MOST, MATCH From: COMPONENT AND PUT INTO THAT FOLDER:
    case "$from" in
    aaaa|bbbbb|ccccc|ddddd|eeeeeeee|fffffff|ggggggg|hhhhhh)
        $mh/refile $msg +$from
        continue
        ;;
    iiiiii|jjjjj|kkkkkkkk|llllllll|mmmmmmm|nnnnnn|ooooooo)
        $mh/refile $msg +$from
        continue
        ;;
    # ... AND SO ON ...
    esac
    # THIS ONE IS BETTER TO MATCH ON Sender: COMPONENT:
    case "$sender" in
    ppppppp)
        $mh/refile $msg +ppppppp
        continue
        ;;
    esac
done
