#! /bin/sh
#
# $ Id: metamutt,v 1.17 2000/02/23 12:50:41 roland Exp $
#
# This should become a replacement of metamail, using the mail reader
# mutt. The options of metamail are not yet implemented, but it should
# work as a filter without any problems.
# 
# This script was written to use it in combination with the tin news
# reader, where you have to set the envionment variable
# METAMAIL=metamutt, but it should work with other programs, too.
#
# You need formail (from the procmail package) to use this script. If
# you don't want to install formail, you will find an alternative
# solution (commented out) below.
#
# Pressing "g" allows you to post a Followup to a newsgroup.
#
# Options:
#  -D allows splits a digest into separate mails by using formail
#     (Feature added by Martin Ramsch <m.ramsch@computer.org>)
#
# This script tries to find a working muttrc (which is sourced first)
# at the following places (descending priority): 
# 1) ~/.mutt/metamuttrc
# 2) ~/.mutt/muttrc
# 3) ~/.muttrc
#
# Most recent version can be found at http://www.spinnaker.de/mutt/metamutt
#
# Many thanks for reporting bugs and introducing new features to
# Moritz Barsnick, Ulli Horlacher, Urs Janßen, and Martin Ramsch.
#
##########################################################################
#
#   Copyright (C) 1997-2000  Roland Rosenfeld <roland@spinnaker.de>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
##########################################################################

umask 077

tmpdir=${TMPDIR-/tmp}/metamutt.$$
mkdir $tmpdir || exit 1
trap "rm -rf $tmpdir; exit" 0 1 2 3 15

# formail options:
fmopt=''

# do we read a digest?
digest='no'

# handle options (ignore most of them at the moment):
for parameter 
do
    case $1 in 
        -- ) shift; break;;
        -e|-p ) shift;;
        -m ) shift 2;;
	-D ) fmopt='-ds cat'
	     digest='yes'
	     shift;;
        * )  break;;
    esac
done

# create mbox message using formail (from procmail package):
cat "$@" | formail $fmopt > $tmpdir/mbox

# If you don't have formail installed, try this:
#echo "From PIPE `env LC_ALL=C date`" > $tmpdir/mbox
#cat $* - | sed 's/^From />From /' >> $tmpdir/mbox

# create special muttrc for metamutt:
for rcsuggestion in $HOME/.mutt/metamuttrc $HOME/.mutt/muttrc $HOME/.muttrc
do
    if [ -f $rcsuggestion ]
    then
	echo "source $rcsuggestion" > $tmpdir/muttrc
	break
    fi
done

cat >> $tmpdir/muttrc <<EOF
macro pager g ":set sendmail=$tmpdir/inews dsn_notify='' dsn_return=''<return>:my_hdr `formail -X Newsgroups < $tmpdir/mbox`<return>r"
EOF

# some additions, if we read exactly one article.
if [ "$digest" = "no" ]
then

cat >> $tmpdir/muttrc <<EOF
set pager_index_lines=0
set quit=yes
bind pager \t next-page
bind pager i quit
bind pager q quit
bind pager x quit
bind index q quit
push "<return>"
EOF

fi

# create special inews which removes mail only headers:
cat > $tmpdir/inews<<EOF
#! /bin/sh
formail -I To -I Cc -I Bcc -I Fcc -I From\  -I In-Reply-To \
    -R X-Mailer User-Agent | inews -h
EOF
chmod 700 $tmpdir/inews

# run mutt on mbox message with special muttrc:
mutt -F $tmpdir/muttrc -f $tmpdir/mbox </dev/tty >/dev/tty
