#!/usr/local/bin/perl

# Usage: m.pl template.tex files
# where files is a file containing the name of
# all the files to format.

   $tpl = shift ;
   $usr = shift ;
   open(T,$tpl) || die "Can't open $tpl" ;
   open(U,$usr) || die "Can't open $usr" ;

   # mode = 0  read prolog from template
   #        1  insert title
   #        2  continue prolog from template
   #        3  insert text
   #        4  write epilog from template

   $mode = 0 ;

   while ($s=<T>) {
      if ( $mode==0 )
      {
         if ( $s =~ /^TITLE/ )
         {
            $mode = 1 ;
            $usr_fixed = $usr ;
            $usr_fixed =~ y/_/-/ ;
            printf "%s\n", $usr_fixed ;
            $mode= 2 ;
         }
         else
         {
            printf "%s", $s ;
         }
       }
       elsif ( $mode==2 )
       {
         if ( $s =~ /^TEXT/ )
         {
            $mode = 3 ;
            # read text from U
            while ($s =<U>)
            {
               printf "%s", $s ;
            }
            $mode = 4 ;
         }
         else
         {
            printf "%s", $s ;
         }
       }
       elsif ( $mode==4 )
       {
          printf "%s", $s ;
       }
   }
