#!/bin/sh
# Based on a cool script found on the mutt-Mailinglist. Unfortunately I don't
# know who sent the original script.
# This script opens up a new screen-window to edit and sent a mail via mutt. So
# you still are able to go back to your original mutt and read some other mails
# while you edit a new mail.
# Gerhard Siegesmund <jerri@jerri.de>

case $1 in
  # Output the configuration. You can use this script in the
  # mutt-configuration-files like this:
  # `compose.sh config`
  config)
    echo -n "set editor=\"vim -c 'set bs=1 tw=72 comments=nb:>' +/^To: +'silent! s/ignore@me//'\";"
    echo -n "macro index m \"<shell-escape>$0 newmail<enter>\";"
    echo -n "macro pager m \"<shell-escape>$0 newmail<enter>\";"
    echo -n "macro index r \"<pipe-message>$0 reply<enter>\";"
    echo -n "macro pager r \"<pipe-message>$0 reply<enter>\";"
    echo -n "macro index g \"<pipe-message>$0 group-reply<enter>\";"
    echo -n "macro pager g \"<pipe-message>$0 group-reply<enter>\";"
    echo -n "macro compose q \"<exit>\";"
    echo ""
    exit 0
    ;;

  # This is the action to create a new mail.
  newmail)
    screen -X screen -t "compose" sh -c "mutt 'ignore@me'; screen -X kill"
    exit 0
    ;;

  # And here are all other cases. Normally this would be reply or groupreply.
  *)
    BOX=/tmp/comp_mbox-${USER}-$$-$RANDOM
    {
      date "+From root  %a %b %d %H:%M:%S %Y";
      cat;
    } > "$BOX"
    screen -X screen -t "compose" sh -c "mutt -f \"$BOX\" -e \"macro compose y '<send-message><exit>'\" -e \"exec '$1'\"; rm -f \"$BOX\"; screen -X kill"
    ;;
esac
