#!/usr/bin/perl
#!/space/imail/perl/bin/perl
#
# NAME
#   M_EditSend - edit mail message and send it
#
# SYMOPSIS
#   M_EditSend recipient... <file
#
# DESCRIPTION
#   This starts up an xterm with  $EDITOR  running  in  it.   When  the
#   $EDITOR  exits, we mail the resulting file.  There must be at least
#   one recognizable To:  or Cc:  or Bcc:  line in the message.
#
# AUTHOR
#   John Chambers <jc@trillian.mit.edu>

# First, determine some things from the environment:
#
$editor = $ENV{"VISUAL"} || $ENV{"EDITOR"} || 'vi';
$shell = $ENV{"SHELL"} || '/bin/sh';
$home = $ENV{HOME} || '/tmp';
$dir = "$home/M/out";
$user = $ENV{USER} || $ENV{LOGNAME} || 'unknown';
($host = `hostname`) =~ s/\s+$//;

# Next, create a file to hold the message:
#
system "mkdir -p $dir" if (! -d $dir);
($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$sitime = sprintf("%4d%02d%02d%02d%02d%02d",1900+$year,1+$mon,$mday,$hour,$min,$sec);
$msg = "$dir/$sitime";	# Build message with SI time as name.

# Now build the message:
#
open(R,">>$msg") || die "$0: Can't write \"$msg\" [$!]\n";

for $r (@ARGV) {	# Copy recipients into message.
	print R "To: $r\n";
	++$ToLines;
}
print R "To: \n" if !$ToLines;
print R "From: $user@$host\n";
print R "Subject: \n";
print R "\n";
while (<STDIN>) {print R}
close M;
close R;
close STDIN;

# Fire up an xterm running the user's editor:
#
system "xterm -e $shell -c '$editor $msg'";

# Finally, mail off the message:
#
system "Msnd $msg";

# Note that we don't delete the reply.  It is left in the folder, with
# the 'e' suffix, in case we want to refer to it later. So far, little
# is done with this fact, but it's there for later use.
