#! /bin/sh
#
# Script for spawning VM under xemacs through muttzilla.
#
# by Claus Brunzema <chb@ossi.fho-emden.de>
# most code stolen from muttzilla's mzmail.sh

# If you already have an xemacs with gnuserv running,
# this script first tries to start VM using that xemacs.
# Otherwise a new xemacs is started. So, if you don't
# want a new xemacs for every mail, put something like
# (gnuserv-start) in your .emacs.

# Arguments to the script:
#   1  to
#   2  cc
#   3  bcc
#   4  subject
#   5  body file
#   6  org

MZTO=$1
MZCC=$2
MZBCC=$3
MZSUB=$4
MZBOD=$5
MZORG=$6

ARGS="-eval '(progn (vm-mail "

if [ -n "$MZTO" ] ; then
    ARGS="$ARGS \"$MZTO\""
fi

ARGS="$ARGS ) (save-excursion "

if [ -n "$MZSUB" ] ; then
    ARGS="$ARGS (mail-subject) (insert \"$MZSUB\") "
fi

if [ -n "$MZCC" ] ; then
    ARGS="$ARGS (mail-cc) (insert \"$MZCC\") "
fi

if [ -n "$MZBCC" ] ; then
    ARGS="$ARGS (mail-bcc) (insert \"$MZBCC\") "
fi

### body and org still missing ...


ARGS="$ARGS ))'"

if (gnuclient -eval '(progn)' 2>/dev/null) ; then
    eval gnuclient $ARGS
else
    eval xemacs $ARGS
fi

# This is an ugly hack, and to be honest I'm not even sure it does
# what I think it does, but I hadn't really forseen that you might
# want to spawn one of two programs depending on the results of some
# test.  I'm sure I'll hear from some enterprising VM users who will
# let me know a better way.
exit 0
