home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / mail / mh / contrib / multimedia / genmime.Z / genmime
Encoding:
Text File  |  1994-07-20  |  4.9 KB  |  235 lines

  1. #!/usr/bin/perl
  2. #
  3. # genmime - run mhn to produce MIME
  4. #
  5. # Usage: mostly the same as whatnow(1).  See the output of
  6. #     the -help option.
  7. #
  8. #        Genmime is intended for use as an MH "whatnowproc", e.g.:
  9. #
  10. #     comp -form mime-faq-pointer.mh -file $(DRAFT) -noedit \
  11. #            -whatnowproc genmime
  12. #
  13. #      The "-noedit" in the command above is needed to prevent
  14. #     the default editor (e.g. prompter) from being run instead 
  15. #     of mhn, since genmime chooses the editor to run as follows:
  16. #
  17. #        1. The editor specified with the -editor option.
  18. #
  19. #        2. The editor defined by the envar $mhedit.
  20. #           (This is the default editor, unless -noedit
  21. #           is specified in the invoking program.)
  22. #
  23. #        3. The editor defined by <prog>-automhnproc,
  24. #           if $mhedit isn't defined.
  25. #
  26. #        4. mhn, if <prog>-automhnproc isn't defined.
  27. #
  28. # Note that mhn can be made to run automatically by specifying an
  29. # "automhnproc" component in the MH profile.  However, genmime 
  30. # allows multiple "automhnproc" components to exist: "<prog>-automhnproc".
  31. # The <prog> prefix for the component is whatever name by which this
  32. # program is invoked, e.g. "genmime-automhnproc".
  33. #
  34. # Profile components:
  35. #
  36. #   <prog>-automhnproc: mhn    -- program to produce MIME from a template
  37. #   <prog>-next: (none)        -- program to invoke after running automhnproc
  38. #
  39. # Environment variables:
  40. #
  41. #   $MH                -- MH profile path
  42. #   $HOME            -- fallback directory containing .mh_profile
  43. #   $mhdraft            -- used if no file is specified
  44. #   $mheditor            -- used if no -editor is specified
  45. #   $mhaltmsg            -- '@' link
  46. #
  47. # Author: Jerry Sweet <jsweet@irvine.com>
  48. #
  49. # $Id: genmime,v 1.3 1994/07/22 01:44:32 jsweet Exp $
  50.  
  51. #
  52. # Subprograms
  53. #
  54.  
  55. sub bail {
  56.   # Called when termination signals hit.
  57.  
  58.   exit 1;
  59. }
  60.  
  61. sub bye {
  62.   local($status) = @_;
  63.   exit $status;
  64. }
  65.  
  66. sub gripe {
  67.   print STDERR "$prog: ", @_, "\n";
  68. }
  69.  
  70. sub barf {
  71.   &gripe(@_);
  72.   &bye(1);
  73. }
  74.  
  75. sub cmd {
  76.   local($_, $exit_on_error) = @_;
  77.   local($st);
  78.  
  79.   if ($debug) {
  80.     print "\t$_\n";
  81.   }
  82.  
  83.   $st = system "$_";
  84.  
  85.   if ($st >> 8 && $exit_on_error) {
  86.     split;
  87.     &barf("$_[0] failed with status ", $st >> 8);
  88.   }
  89. }
  90.  
  91. sub shx {
  92.   # Shell command, do NOT exit on error.
  93.  
  94.   local($_) = @_;
  95.   &cmd($_, 0);
  96. }
  97.  
  98. sub read_profile {
  99.   # Defines associative array %PROFILE_COMPONENT indexed by lower case
  100.   # component name.  Continuation lines are tacked on.  New
  101.   # occurrences of a component wipe out previous occurrences of the
  102.   # same component, but probably shouldn't.
  103.  
  104.   local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} : 
  105.                      $ENV{'HOME'} . "/.mh_profile";
  106.   local($profile_component) = 'x-bogus';
  107.   local($_);
  108.   
  109.   &barf("can't open profile \"$profile\" - $!")
  110.     unless open(PROFILE, "<$profile");
  111.  
  112.   while (<PROFILE>) {
  113.     /^(\S+):\s*/ && do {
  114.             ($profile_component = $1) =~ tr/A-Z/a-z/;
  115.             $PROFILE_COMPONENT{$profile_component} = $';
  116.         };
  117.   
  118.     /^[ \t]/ && do {
  119.             $PROFILE_COMPONENT{$profile_component} .= $_;
  120.         };
  121.   }
  122. }
  123.  
  124. sub wn_edit {
  125.   local($ed) = @_;
  126.   local($wd, $atlink, $atlink_exists, $st);
  127.  
  128.   return if ((! $ed) || ! $file);
  129.  
  130.   if (defined $ENV{'mhaltmsg'}) {
  131.     ($wd = $file) =~ s|(.*/).*|$1|;
  132.     $atlink = "$wd/@";
  133.     if (-w $wd && ! -e $atlink) {
  134.       $st = system "ln $ENV{'mhaltmsg'} $atlink >/dev/null 2>&1";
  135.     # Create a symlink in the same directory as the draft
  136.       $atlink_exists = ($st >> 8) == 0;
  137.       if (! $atlink_exists) {
  138.         $st = system "ln -s $ENV{'mhaltmsg'} $atlink >/dev/null 2>&1";
  139.     $atlink_exists = ($st >> 8) == 0;
  140.       }
  141.     }
  142.   }
  143.  
  144.   &shx("$ed $file");
  145.  
  146.   unlink $atlink if $atlink_exists;
  147. }
  148.  
  149.  
  150. #
  151. # Main program
  152. #
  153.  
  154. ($prog = $0) =~ s%.*/%%;
  155.  
  156. $SIG{'INT'} = $SIG{'HUP'} = $SIG{'TERM'} = 'bail';
  157.  
  158. # Check the profile.
  159.  
  160. &read_profile;
  161. if (defined $PROFILE_COMPONENT{$prog}) {
  162.   push(@Args, split(' ', $PROFILE_COMPONENT{$prog}));
  163. }
  164.  
  165. # Check the command line arguments.
  166.  
  167. push(@Args, @ARGV);
  168.  
  169. while ($_ = shift @Args) {
  170.   print "$_\n";
  171.  
  172.   /^-(de|deb|debu|debug)$/ && do {
  173.     ++$debug; 
  174.     next; 
  175.     };
  176.   /^-(e|ed|edi|edit|edito|editor)$/ && do { 
  177.     $editor = shift @Args; 
  178.     next;
  179.     };
  180.   /^-no(e|ed|edi|edit)$/ && do { 
  181.     $editor = ''; 
  182.     next; 
  183.     };
  184.   /^-(h|he|hel|help)$/ && do { 
  185.     ++$help; 
  186.     next; 
  187.     };
  188.   /^[^\-]/ && do { 
  189.     $file = $_; 
  190.     next; 
  191.     };
  192.  
  193.   &gripe("unrecognized command line option \"$_\" (ignored)");
  194. }
  195.  
  196. if ($help) {
  197.   print <<"xxEndHelpxx";
  198. $prog - run mhn to produce MIME
  199. Usage: $prog [-editor editor] [-noedit] [file]
  200. See whatnow(1) and mh-profile(5).
  201. xxEndHelpxx
  202.  
  203.   &bye(1);
  204. }
  205.  
  206. if (! $editor) {
  207.   $editor = $ENV{'mheditor'} ? $ENV{'mheditor'} : (
  208.         $PROFILE_COMPONENT{"$prog-automhnproc"} ? 
  209.             $PROFILE_COMPONENT{"$prog-automhnproc"} : (
  210.             $PROFILE_COMPONENT{'automhnproc'} ? 
  211.                 $PROFILE_COMPONENT{'automhnproc'} : 
  212.                 'mhn'
  213.               )
  214.           );
  215. }
  216.  
  217. if (! $file) {
  218.   if (defined $ENV{'mhdraft'}) {
  219.     $file = $ENV{'mhdraft'};
  220.   }
  221.   else {
  222.     &gripe("no draft file specified");
  223.   }
  224. }
  225.  
  226. if ($editor) {
  227.   &wn_edit($editor);
  228. }
  229.  
  230. if ($PROFILE_COMPONENT{"$prog-next"}) {
  231.   exec $PROFILE_COMPONENT{"$prog-next"};
  232. }
  233.  
  234. &bye(0);
  235.