3 # catmutt - wrap mutt allowing mboxes read from stdin.
5 # Copyright (C) 1998-1999 Moritz Barsnick <barsnick (at) gmx (dot) net>,
6 # 2009 William Trevor King <wking (at) drexel (dot) edu>
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.
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.
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
21 # developed from grepm-0.6
22 # http://www.barsnick.net/sw/grepm.html
24 PROGNAME=`basename "$0"`
25 export TMPDIR="${TMPDIR-/tmp}" # used by mktemp
28 if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
29 echo 1>&2 "Usage: ${PROGNAME} [--help] mutt-arguments"
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}"
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
41 trap "rm -f ${TMPFILE}; exit 1" 1 2 3 13 15
43 cat > "${TMPFILE}" || exit 1
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}'`"
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}" "$@"
56 echo 1>&2 "Empty mailbox input."
59 rm -f "${TMPFILE}" && echo 1>&2 "Deleted temporary mailbox file (${TMPFILE})."