RCS -> VCS, BUGDIR_DISK_VERSION -> v1.2
[be.git] / interfaces / email / catmutt
1 #!/bin/sh
2
3 # catmutt - wrap mutt allowing mboxes read from stdin.
4 #
5 # Copyright (C) 1998-1999 Moritz Barsnick <barsnick (at) gmx (dot) net>,
6 #               2009 William Trevor King <wking (at) drexel (dot) edu>
7 #
8 #    This program is free software; you can redistribute it and/or
9 #    modify it under the terms of the GNU General Public License
10 #    version 2 as published by the Free Software Foundation.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program; if not, write to the Free Software
19 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21 # developed from grepm-0.6
22 #   http://www.barsnick.net/sw/grepm.html
23
24 PROGNAME=`basename "$0"`
25 export TMPDIR="${TMPDIR-/tmp}" # used by mktemp
26 umask 077
27
28 if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
29   echo 1>&2 "Usage: ${PROGNAME} [--help] mutt-arguments"
30   echo 1>&2 ""
31   echo 1>&2 "Read a mailbox file from stdin and opens it with mutt."
32   echo 1>&2 "For example: cat somefile.mbox | ${PROGNAME}"
33   exit 0
34 fi
35
36 # Note: the -t/-p options to mktemp are deprecated for mktemp (GNU
37 # coreutils) 7.1 in favor of --tmpdir but the --tmpdir option does not
38 # exist yet for my 6.10-3ubuntu2 coreutils
39 TMPFILE=`mktemp -t catmutt.XXXXXX` || exit 1
40
41 trap "rm -f ${TMPFILE}; exit 1" 1 2 3 13 15
42
43 cat > "${TMPFILE}" || exit 1
44
45 # Now that we've read in the mailbox file, reopen stdin for mutt/user
46 # interaction.  When in a pipe we're not technically in a tty, so use
47 # a little hack from "greno" at
48 # http://www.linuxforums.org/forum/linux-programming-scripting/98607-bash-stdin-problem.html
49 tty="/dev/`ps -p$$ --no-heading | awk '{print $2}'`"
50 exec < ${tty}
51
52 if [ `wc -c "${TMPFILE}" | awk '{print $1}'` -gt 0 ]; then
53   echo 1>&2 "Calling mutt on temporary mailbox file (${TMPFILE})."
54   mutt -R -f "${TMPFILE}" "$@"
55 else
56   echo 1>&2 "Empty mailbox input."
57 fi
58
59 rm -f "${TMPFILE}" && echo 1>&2 "Deleted temporary mailbox file (${TMPFILE})."