#!/bin/sh

#------------------------------------------------------------------
# MHR - a shell-script frontend to the "MH" mail reader
# (c) Copyright Alec Muffett <alecm@crypto.dircon.co.uk>
#------------------------------------------------------------------

##################################################################

# configurable system things

unset DISPLAY # controversial

PGP=pgp
SENDMAIL=/usr/lib/sendmail
MORE=more
ROWS=23
TF=mhrtmp.$$

##################################################################

# prompt selection

bsdprompt()
{
    echo -n "$@ " # Linux, BSD
}

svprompt()
{
    echo "$@ \c" # Solaris, SV
}

pquery=`echo -n x`

if [ "$pquery" = "x" ]
then
    prompt=bsdprompt
else
    prompt=svprompt
fi

# banner

version="mhr v2.2 - 2 Feb 1999 <alecm@crypto.dircon.co.uk>"

# ignore SIGINT which is propagated by EMACS on ^G and for killing
# "mhn" when it goes wild

trap "" 2

# sensible umask

umask 077

# version

showver()
{
    echo ""
    echo "[$version]"
    echo ""
}

# help function

mhrhelp()
{
showver

cat <<EOF
Messages
--------
i       include new mail (present if "*" in prompt)
I       include new mail, maintain current reading position
s       show current message
.       show current message
M       show current message without MIME/mhn
b       show previous message
p       show previous message
n       show next message
d       delete current message
c       compose new message
m       compose new message
<       set backward reading direction
>       set forward reading direction
r       reply to current message
R       reply-all to current message (if not default)
f       forward current message to another user
z       list next few messages
Z       list previous few messages
a       list next (up to) 1000 messages, paged
A       list all messages, paged
^       point at first message in folder
$       point at last message in folder
<num>   show message number <num>

The default action of pressing "return" is shown in the prompt.

Folders
-------
l       list current folder name/info
,       list current folder name/info
F       list all mail folders
L       list all mail folders briefly
=foo    set current folder to folder "foo"
+foo    refile current message to folder "foo"

Misc
----
!cmd    shell "cmd"
|cmd    pipe message into "cmd"
h       help (this page)
?       help (this page)
Q       quit
x       quit

Advanced
--------
/foo ... "pick" with arguments "foo" ...
//foo .. "pick" with arguments "foo" and autoshow
zfoo ... "scan" with arguments "foo" ...
sfoo ... "show" with arguments "foo" ...
dfoo ... "rmm" with arguments "foo" ...
@foo      mark current message as "foo"; use "sfoo" to return
+foo ... "refile" to folder "foo" with arguments ...

Extended ( begin with # or ' )
--------
#d      set debugging on
#f      flush the mail queue
#i      equivalent to "=inbox"
#p      invoke PGP on a copy of the current message, which is later removed.
#q      list the mail queue
#r      renumber folder contents ("folder -pack")
#s      sort the current mailbox in the default manner

EOF
}

# prompts

dirn=">"
ncmd="show"

# find the mail directory

for mdir in /var/spool/mail /var/mail
do
    test -d $mdir && break
done

# start work

showver

folder || exit 0 # perhaps should mugtrap here in case of failure?

folder=`folder -nocreate -fast`

# erase the positional parameters

set --

# main loop

while :
do
    # if the folder is missing, create it

    if [ -z "$folder" ]
    then
	folder=`folder -nocreate -fast`
	echo "(error - resetting to folder $folder)"
    fi

    # if there is e-mail waiting, flag it

    if [ -s "$mdir/$USER" ]
    then
	mflag="*"
    else
	mflag=""
    fi

    # draw the prompt

    $prompt "$mflag$folder:$ncmd$dirn"

    # set xcmd to be the command to execute on dummy

    xcmd="$ncmd"
    read cmd
    ncmd=""

    # if nothing was typed

    if [ -z "$cmd" ]
    then
	if [ $# = 0 ]
	then
	    $xcmd
	    echo ""
	else
	    show $1
	    shift
	fi
    else
	# something was typed - wipe auto mode
	set --

	# else command is non-null
	case "$cmd" in
	    [0-9]*) show $cmd ; echo "" ;; # no quotes

	    A) ( scan ; folder ) | $MORE ;;
	    F) folders -recurse ;;
	    L) folders -recurse -fast ;;
	    M) show -noshowproc | $MORE ; echo "" ;;
	    Q|X|x) break ;; # "q" removed - clashes with 'more', etc
	    R) repl -cc all -nocc me ;;
	    Z) scan "cur:-$ROWS" ;;
	    a) ( scan cur:+1000 ; folder ) | $MORE ;; # cur:last not supported
	    b|p) prev ; echo "" ;;
	    c|m) comp ;;
	    d) rmm ;;
	    f) forw ;;
	    h|'?') mhrhelp | $MORE ;;
	    i) inc && ncmd="show" ; folder=`folder -nocreate -fast` ;;
	    I) folder=`folder -fast` ; pick cur -seq MHRINTERNAL ; inc ; folder +$folder MHRINTERNAL ;;
	    l|',') folder ;;
	    n) next ; echo "" ;;
	    r) repl ;;
	    s|'.') show ; echo "" ;;
	    z) scan "cur:+$ROWS" ;;

	    '<') dirn="<" ;;
	    '>') dirn=">" ;;
	    '^') folder first ; ncmd="show" ;;
	    '$') folder last ; ncmd="show" ;;
	    '!') ${SHELL:-sh} ;;

	    \/\/?*) x=`echo "$cmd" | sed -e 's/^..//'`  ; echo "pick $x" ; set - `pick $x` ;; # no quotes
	    \/?*) x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "pick $x" ; pick $x ;; # no quotes

	    \!?*) x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "!$x" ; eval "$x" ;;
	    \+?*) refile $cmd ;; # syntax cheat 8-)
	    \=?*) x=`echo "$cmd" | sed -e 's/^=/+/'` ; folder=`folder -nocreate -fast $x last` ;;
	    \@?*) x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "pick cur -seq $x" ; pick cur -seq $x ;; # no quotes
	    \|?*) x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "|$x" ; show -noshow | eval "$x" ;;
	    s?*)  x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "show $x" ; show $x ;; # no quotes
	    d?*)  x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "rmm $x" ; rmm $x ;; # no quotes
	    z?*)  x=`echo "$cmd" | sed -e 's/^.//'`  ; echo "scan $x" ; scan $x ;; # no quotes

	    [\'\#]d) set -x ;; # debug
	    [\'\#]f) $SENDMAIL -q ;; # flush
	    [\'\#]i) folder=`folder -nocreate -fast +inbox last` ;; # inbox
	    [\'\#]p) show -noshowproc >$TF ; $PGP $TF ; rm $TF ;; # pgp
	    [\'\#]q) $SENDMAIL -bp ;; # mailq
	    [\'\#]r) folder -pack ;; # renumber
	    [\'\#]s) sortm ;; # sort chronologically

	    '<num>') echo "ha ha very funny, try typing a number instead" ;;

	    *) echo "? $cmd - unrecognised command" ;;
	esac
    fi

    if [ -z "$ncmd" ]
    then
	if [ $# != 0 ]
	then
	    ncmd="[$#]"
	    
	elif [ "$dirn" = "<" ]
	then
	    ncmd="prev"
	else
	    ncmd="next"
	fi
    fi
done

# done

exit 0

#------------------------------------------------------------------
