]> git.madduck.net Git - etc/mutt.git/blob - .mutt/bgrun

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

improved tempfile handling
[etc/mutt.git] / .mutt / bgrun
1 #!/bin/sh
2
3 SELF="${0##*/}"
4
5 if [ -z "$TMPDIR" ]; then
6   TMPDIR=/tmp
7 elif [ -d "${TMPDIR}/volatile" ]; then
8   TMPDIR="${TMPDIR}/volatile"
9 fi
10 export TMPDIR
11 TMPDIR=$(mktemp -dp "$TMPDIR" mutt.XXXXXXXXXX)
12 cleanup() { cd / && rm -rf "$TMPDIR"; }
13 trap cleanup 1 2 3 4 5 6 7 8 10 11 12 13 14 15
14
15 cd "$TMPDIR"
16
17 notify() {
18   if [ -x "$(command -v awesome-client)" ]; then
19     awesome-client <<-_eof
20         local naughty = require("naughty")
21         naughty.notify({ preset = naughty.config.presets.low,
22                 title  = "$3",
23                 text   = [[stdout:
24                 $(sed -e 's,\",\\\",g' "$1")
25                 stderr:
26                 $(sed -e 's,\",\\\",g' "$2")]]
27                 })
28         _eof
29   fi
30 }
31
32 case "$SELF" in
33   (bgrun)
34     # make a copy of the file, then launch a shell process in the background
35     # to divert to run-mailcap, after which the temporary directory gets
36     # cleaned up.
37     cp -a "$1" $TMPDIR
38     MIMETYPE="$2"
39     (
40       run-mailcap --action=view "$MIMETYPE":"$TEMPFILE"
41       if [ -s "$TMPDIR/output.stdout" ] || [ -s "$TMPDIR/output.stderr" ]; then
42         notify $TMPDIR/output.stdout $TMPDIR/output.stderr \
43           "Output from mutt/$SELF on $BASENAME"
44       fi
45       cleanup
46     ) &
47     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
48     ;;
49   (browserrun)
50     # hack to stay around until the browser has read the file: make a fifo and
51     # wait for the cat process to finish writing to it, which means that it
52     # must have been consumed on the other end.
53     mkfifo "$TEMPFILE"
54     INPUTFILE="$1"
55     cat "$INPUTFILE" > $TEMPFILE &
56     sensible-browser "$TEMPFILE" > $TMPDIR/output.stdout 2> $TMPDIR/output.stderr
57     wait
58     cleanup
59     ;;
60 esac