#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -r-xr-xr-x 1 jsweet 4919 Nov 12 02:04 mhpped
# -r--r--r-- 1 jsweet 3976 Nov 11 17:53 mhpped.l
#
echo 'x - mhpped'
if test -f mhpped; then echo 'shar: not overwriting mhpped'; else
sed 's/^X//' << '________This_Is_The_END________' > mhpped
X#!/usr/bin/perl
X#
X# mhpped - process backticks in comp, repl, and forw components
X#
X# Taken from "testedit" by Jerry Peek <jerry@ora.com>;
X# abuser: Jerry Sweet <jsweet@irvine.com>
X#
X# $Header: /usr/t/users/jsweet/src/mh-front/RCS/mhpped,v 1.5 1992/11/12 10:03:47 jsweet Exp $
X
X#
X# Subprograms
X#
X
Xsub bail {
X  unlink $temp;
X  exit 1;
X}
X
Xsub gripe {
X  print STDERR "$prog: ", @_, "\n";
X}
X
Xsub barf {
X  &gripe(@_);
X  &bail;
X}
X
Xsub check {
X  local($status) = @_;
X  &barf("problem writing to temporary draft file \"$temp\"- $!")
X	unless $status;
X}
X
Xsub sh {
X  local($_) = @_;
X  local($st);
X
X  if ($verbose) {
X    print "\t$_\n";
X  }
X
X  return if $debug;
X
X  $st = system "$_";
X
X  if ($st >> 8) {
X    split;
X    &barf("$_[0] failed with status ", $st >> 8);
X    exit 1;
X  }
X}
X
Xsub read_profile {
X  # Defines associative array %PROFILE_COMPONENT indexed by lower case
X  # component name.  Continuation lines are tacked on.  New
X  # occurrences of a component wipe out previous occurrences of the
X  # same component, but probably shouldn't.
X
X  local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} : 
X					 $ENV{'HOME'} . "/.mh_profile";
X  local($profile_component) = 'x-bogus';
X  local($_);
X  
X  &barf("can't open profile \"$profile\" - $!")
X	unless open(PROFILE, "<$profile");
X
X  while (<PROFILE>) {
X    /^(\S+):\s*/ && do {
X      	  ($profile_component = $1) =~ tr/A-Z/a-z/;
X      	  $PROFILE_COMPONENT{$profile_component} = $';
X        };
X  
X    /^[ \t]/ && do {
X      	  $PROFILE_COMPONENT{$profile_component} .= $_;
X        };
X  }
X}
X
X#
X# Main
X#
X
Xumask 077;  # This should probably be adjustable, but isn't at the moment.
X
X($prog = $0) =~ s|.*/||;	# Get the invocation name of this program
X
X# Check the profile
X
X&read_profile;
Xif (defined $PROFILE_COMPONENT{$prog}) {
X  push(@Args, split(' ', $PROFILE_COMPONENT{$prog}));
X}
X
X# Check the command line arguments
X
Xpush(@Args, @ARGV);
X
Xwhile ($_ = shift @Args) {
X  /^-debug$/ && do { ++$debug; next; };
X
X  /^-editor$|^-edit$/ && do { $editor = shift @Args; next; };
X
X  /^-entire$/ && do { ++$entire; next; };
X
X  /^-help$/ && do {
X      print <<"xxEndHelpxx";
X$prog - pre-process an MH composition template
XUsage: $prog [-(no)debug] [-editor editor] [-noeditor] [-(no)entire] [-help]
X                [-(no)verbose] [file]
XOptions:
X  -debug          print the message draft and invoke default whatnow
X  -editor editor  invoke the specified editor following processing
X  -entire         pre-process the entire message, including the body
X  -help           print this message
X  -nodebug        negates the -debug option
X  -noeditor       disables automatic invocation of an editor
X  -noentire       negates the -entire option; only the header is processed
X  -noverbose      negates the -verbose option
X  -verbose        print what's going on as it's being done
X  file            if omitted, envar \$mhdraft names the draft file
XxxEndHelpxx
X
X      exit 1;
X    };
X
X  /^-nodebug$/ && do { --$debug; next; };
X  /^-noeditor$|^-noedit$/ && do { $editor = ''; next; };
X  /^-noentire$/ && do { --$entire; next; };
X  /^-noverbose$/ && do { --$verbose; next; };
X
X  /^-verbose$/ && do { ++$verbose; next; };
X
X  /^[^\-]/ && do { $file = $_; next; };
X
X  &barf("unrecognized command line option \"$_\"");
X}
X
X$debug = $debug > 0;
X$entire = $entire > 0;
X$verbose = $verbose > 0;
X
X# Do some initial setup.
X
X$temp = "/tmp/$prog.$$";
X$SIG{'HUP'} = $SIG{'INT'} = $SIG{'TERM'} = 'bail';
X
X# Figure out the name of the draft file
X
Xif (! $file) {
X  if (defined $ENV{'mhdraft'}) {
X    $file = $ENV{'mhdraft'};
X  }
X  else {
X    &barf("no draft file specified");
X  }
X}
Xelse {
X  $ENV{'mhdraft'} = $file;
X}
X
X# Let's start running our dirty fingers over the draft file...
X
X&barf("can't open draft file \"$file\" - $!") 
X	unless open(DRAFT, "<$file");
X&barf("can't create temporary draft file \"$temp\" - $!")
X	unless open(TEMP, ">$temp");
X
Xwhile (<DRAFT>) {
X  last if (/^$|^-/ && ! $entire);
X
X  if (/\`(.+)\`/) {
X    $torun = $1;
X    $prefix = $`;
X    $suffix = $';
X
X    print("\t$torun\n") if ($verbose || $debug);
X    chop($result = `$torun`);
X    if ($? >> 8) {
X      &gripe("$file: line $.: command \"$torun\" returned error status ",
X		$? >> 8);
X    }
X    next if ($prefix =~ /^\s*$|^\S+:\s*$/) && 
X	    ($suffix =~ /^\s*$/) && 
X	    ! $result;
X		# If there was no result, and there was nothing important
X		# before or after the backtick construct, then don't even
X		# bother printing a line.  This allows us to omit
X		# header components programmatically.
X
X    &check(print(TEMP $prefix, $result, $suffix));
X  }
X  else {
X    &check(print TEMP $_);
X  }
X}
X
Xif (! $entire) {
X  &check(print TEMP "-------\n");
X  grep(&check(print TEMP $_) && 0, <DRAFT>);
X}
X
X&barf("problem closing temporary draft file \"$temp\" - $!")
X	unless close(TEMP);
X
Xclose(DRAFT);
X
X&sh("cp $temp $file");
Xsystem('/bin/cat', $temp) if $debug;
X
Xprint("\trm $temp\n") if ($verbose || $debug);
Xunlink $temp;
X
X&sh("$editor $file") if $editor;
________This_Is_The_END________
if test `wc -l < mhpped` -ne 213; then
	echo 'shar: mhpped was damaged during transit (should have been 213 bytes)'
fi
fi		; : end of overwriting check
echo 'x - mhpped.l'
if test -f mhpped.l; then echo 'shar: not overwriting mhpped.l'; else
sed 's/^X//' << '________This_Is_The_END________' > mhpped.l
X.\" $Header: /usr/t/users/jsweet/src/mh-front/RCS/mhpped.l,v 1.2 1992/11/12 01:53:28 jsweet Exp $
X.\"
X.\" These definitions were borrowed from the MH docs.
X.\"
X..fc ^ ~
X.\" I pity the fool who tampers with the next line...
X.ds ZZ -man
X.de SC					\" Title section
X.TH \\$1 \\$2 MHPPED
X..
X.de NA					\" Name section
X.SH NAME
X..
X.de SY					\" Synopsis section
X.SH SYNOPSIS
X.in +.5i
X.ti -.5i
X..
X.de DE					\" Description section
X.in -.5i
X.SH DESCRIPTION
X..
X.de De					\" Defaults section
X.SH "DEFAULTS"
X.nf
X..
X.de Bu					\" Bugs section
X.fi
X.SH BUGS
X..
X.de En
X..
X.de ip
X.IP "\\$1" \\$2
X..
X.SC MHPPED 1 local
X.NA
Xmhpped \- pre-process backticks in MH composition templates
X.SY
Xmhpped
X\%[\-(no)debug]
X\%[\-editor editor]
X\%[\-noeditor]
X\%[\-(no)entire]
X\%[\-(no)verbose]
X\%[file]
X.DE
XMhpped pre-processes shell-like backticks (``) in
Xe-mail message composition templates used by comp, repl, and forw.
XEach shell command inside a pair of backticks is executed via /bin/sh,
Xand the resulting output is substituted into the header component.
X
XIf the result of a backtick construct is empty, and nothing
X"important" surrounds the backtick construct, then the entire header
Xcomponent is omitted.  
XThe criterion for an "important" string preceding the backtick
Xconstruct is something that looks neither like a header component nor
Xlike whitespace.  
XThe criterion for an "important" string following the backtick
Xconstruct is something that isn't whitespace.
X
XOnly the header components are processed unless -entire
Xis specified.
XIf -entire is specified then backticks in the message composition template
Xbody as well as in the header are processed.
Xthe -noentire option negates the -entire option.
XThe default is -noentire.
X
XThe -editor option specifies an editor program to invoke after processing.
XThe -noedit option negates the -editor option.
XThe default is -noedit.
XIf the MH profile component "mhpped-next" is present, 
Xthen whatnow(1) will use the editor specified in that component when "edit"
Xis typed at the "What now?" prompt.
X
XThe -help option prints a help message and exits.
X
XThe -verbose option causes mhpped to print the commands being executed
Xin the backticks, as well as miscellaneous shell commands executed
Xby mhpped.
XThe -noverbose option negates the -verbose option.
XThe default is -noverbose.
X
XThe -debug option also prints the commands, but does not execute them.
XThe -nodebug option negates the -debug option.
XThe default is -nodebug.
X
X.SH EXAMPLE
X
XYou may install mhpped in any directory occurring in your $PATH
Xenvironment variable.  My copy is installed in my personal bin
Xdirectory, "~/bin".
X
XOnce mhpped has been installed, 
Xyou may specify an "\-editor\ mhpped" option for comp, forw, or repl.
X
XYou may also invent your own mail composition commands, and specify "mhpped"
Xas the default editor in the appropriate MH profile component.
X
XHere is an example test configuration using mhpped with a version of comp
Xnamed "ppcomp".
XFirst, run these csh commands to create the "ppcomp" command:
X.PP
X.in +.5i
X    % ln -s `which comp` ~/bin/ppcomp
X.br
X    % rehash
X.in -.5i
X.PP
XThen in your .mh_profile, add these lines:
X.PP
X.in +.5i
Xppcomp:\ \ \ \ \ \ \ \ \ \-editor\ mhpped\ \-form\ components.t
X.br
Xmhpped:\ \ \ \ \ \ \ \ \ \-entire\ \-verbose\ \-editor\ prompter
X.in -.5i
X.PP
XThen in your Mail directory, create a file named "components.t"
Xwith these example lines:
X.PP
X.in +.5i
XX-nothing: `echo ""`
X.br
XTo: 
X.br
XCc: 
X.br
XFcc: `folder -fast`
X.br
XX-local-time: `date`
X.br
XSubject:
X.br
X-----------
X.br
X 
X.br
XThis should not be expanded unless -entire is given: `date`
X.br
X.in -.5i
X.PP
X
XThe first line of the "components.t" file, which returns an empty
Xstring, is there as an example of a line that would be omitted in the
Xprocessed composition file.
X
XWhen everything is ready, invoke "ppcomp" to see what happens.
X
X.SH AUTHOR
X
XJerry Sweet <jsweet@irvine.com>.  
X
XMhpped was taken from "bipp", a slightly simpler shell/awk program
Xby Jerry Peek <jpeek@ora.com>.
________This_Is_The_END________
if test `wc -l < mhpped.l` -ne 154; then
	echo 'shar: mhpped.l was damaged during transit (should have been 154 bytes)'
fi
fi		; : end of overwriting check
exit 0
