#!/usr/bin/perl # # genmime - run mhn to produce MIME # # Usage: mostly the same as whatnow(1). See the output of # the -help option. # # Genmime is intended for use as an MH "whatnowproc", e.g.: # # comp -form mime-faq-pointer.mh -file $(DRAFT) -noedit \ # -whatnowproc genmime # # The "-noedit" in the command above is needed to prevent # the default editor (e.g. prompter) from being run instead # of mhn, since genmime chooses the editor to run as follows: # # 1. The editor specified with the -editor option. # # 2. The editor defined by the envar $mhedit. # (This is the default editor, unless -noedit # is specified in the invoking program.) # # 3. The editor defined by -automhnproc, # if $mhedit isn't defined. # # 4. mhn, if -automhnproc isn't defined. # # Note that mhn can be made to run automatically by specifying an # "automhnproc" component in the MH profile. However, genmime # allows multiple "automhnproc" components to exist: "-automhnproc". # The prefix for the component is whatever name by which this # program is invoked, e.g. "genmime-automhnproc". # # Profile components: # # -automhnproc: mhn -- program to produce MIME from a template # -next: (none) -- program to invoke after running automhnproc # # Environment variables: # # $MH -- MH profile path # $HOME -- fallback directory containing .mh_profile # $mhdraft -- used if no file is specified # $mheditor -- used if no -editor is specified # $mhaltmsg -- '@' link # # Author: Jerry Sweet # # $Id: genmime,v 1.3 1994/07/22 01:44:32 jsweet Exp $ # # Subprograms # sub bail { # Called when termination signals hit. exit 1; } sub bye { local($status) = @_; exit $status; } sub gripe { print STDERR "$prog: ", @_, "\n"; } sub barf { &gripe(@_); &bye(1); } sub cmd { local($_, $exit_on_error) = @_; local($st); if ($debug) { print "\t$_\n"; } $st = system "$_"; if ($st >> 8 && $exit_on_error) { split; &barf("$_[0] failed with status ", $st >> 8); } } sub shx { # Shell command, do NOT exit on error. local($_) = @_; &cmd($_, 0); } sub read_profile { # Defines associative array %PROFILE_COMPONENT indexed by lower case # component name. Continuation lines are tacked on. New # occurrences of a component wipe out previous occurrences of the # same component, but probably shouldn't. local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} : $ENV{'HOME'} . "/.mh_profile"; local($profile_component) = 'x-bogus'; local($_); &barf("can't open profile \"$profile\" - $!") unless open(PROFILE, "<$profile"); while () { /^(\S+):\s*/ && do { ($profile_component = $1) =~ tr/A-Z/a-z/; $PROFILE_COMPONENT{$profile_component} = $'; }; /^[ \t]/ && do { $PROFILE_COMPONENT{$profile_component} .= $_; }; } } sub wn_edit { local($ed) = @_; local($wd, $atlink, $atlink_exists, $st); return if ((! $ed) || ! $file); if (defined $ENV{'mhaltmsg'}) { ($wd = $file) =~ s|(.*/).*|$1|; $atlink = "$wd/@"; if (-w $wd && ! -e $atlink) { $st = system "ln $ENV{'mhaltmsg'} $atlink >/dev/null 2>&1"; # Create a symlink in the same directory as the draft $atlink_exists = ($st >> 8) == 0; if (! $atlink_exists) { $st = system "ln -s $ENV{'mhaltmsg'} $atlink >/dev/null 2>&1"; $atlink_exists = ($st >> 8) == 0; } } } &shx("$ed $file"); unlink $atlink if $atlink_exists; } # # Main program # ($prog = $0) =~ s%.*/%%; $SIG{'INT'} = $SIG{'HUP'} = $SIG{'TERM'} = 'bail'; # Check the profile. &read_profile; if (defined $PROFILE_COMPONENT{$prog}) { push(@Args, split(' ', $PROFILE_COMPONENT{$prog})); } # Check the command line arguments. push(@Args, @ARGV); while ($_ = shift @Args) { print "$_\n"; /^-(de|deb|debu|debug)$/ && do { ++$debug; next; }; /^-(e|ed|edi|edit|edito|editor)$/ && do { $editor = shift @Args; next; }; /^-no(e|ed|edi|edit)$/ && do { $editor = ''; next; }; /^-(h|he|hel|help)$/ && do { ++$help; next; }; /^[^\-]/ && do { $file = $_; next; }; &gripe("unrecognized command line option \"$_\" (ignored)"); } if ($help) { print <<"xxEndHelpxx"; $prog - run mhn to produce MIME Usage: $prog [-editor editor] [-noedit] [file] See whatnow(1) and mh-profile(5). xxEndHelpxx &bye(1); } if (! $editor) { $editor = $ENV{'mheditor'} ? $ENV{'mheditor'} : ( $PROFILE_COMPONENT{"$prog-automhnproc"} ? $PROFILE_COMPONENT{"$prog-automhnproc"} : ( $PROFILE_COMPONENT{'automhnproc'} ? $PROFILE_COMPONENT{'automhnproc'} : 'mhn' ) ); } if (! $file) { if (defined $ENV{'mhdraft'}) { $file = $ENV{'mhdraft'}; } else { &gripe("no draft file specified"); } } if ($editor) { &wn_edit($editor); } if ($PROFILE_COMPONENT{"$prog-next"}) { exec $PROFILE_COMPONENT{"$prog-next"}; } &bye(0);