/* DVIPS exec:                                            */
/* This is version 2.0 as of 9 Nov. 1992.                 */
/* Written by Jim Hafner (hafner@almaden.ibm.com).        */
/*                                                        */
/* Command is:                                            */
/*    DVIPS dvifilename -options                          */
/*                                                        */
/* For a list of options type:                            */
/*    DVIPS                                               */

/* END OF HEADER (this line must be preceded by a blank line) */

/* This is part of the VM/CMS distribution of Rokicki's DVIPS program.
 * Installers may need to customize this exec extensively, though we
 * have tried to make it as generic as possible.
 *
 * You are welcome to modify this exec in any way you choose.
 *
 * See the file README VMCMS for more comments about the VMCMS
 * version of DVIPS and for some suggestions on modifying this EXEC.
 *
 * One suggestion for modifying this exec is to scan the arguments
 * looking for the -P PRINTER option.  You can use this to access
 * specific font disks for different printers, either by matching
 * the printer with a disk or reading the config.PRINTER file
 * for the 'M' (metafont mode) option and using that.
 *
 * This exec calls the DVIPS module with the appropriate libraries
 * linked.  It assumes   (SITE DEPENDENCIES)
 *   -- your font disks are linked
 *   -- your syntax is Unix-like
 *
 * 11/9/92:  Modified significantly by J. Hafner to add
 *   -- VM/CMS or Unix-like file name descriptors (the on-line
 *      help in the code also shows this).
 *   -- Note, dvifilename MUST be the first (not the last) command
 *      line argument for this exec to work.
 *   -- enhanced the TXTLIB and LOADLIB save and restore mechanism
 *      to handle large libraries.
 *   -- Now we do automatic font generation outside of the main
 *      module.  After the DVIPS MODULE has finished, this exec
 *      checks for a newly created MISSFONT LOG file.  If it exists,
 *      it prompts to see if the user wants to try to generate the
 *      missing fonts.  To do that, it queues the lines in MISSFONT LOG
 *      and 'EXEC's them, one by one.
 *   -- We have returned system call inside the code for font generation
 *      but we prevent this within this EXEC by adding the '-M' option
 *      when DVIPS MODULE is called.   Installers can remove this
 *      option if their versions of MF and GFtoPK can run with DVIPS
 *      still in memory.  Using the '-M0' option from the command line
 *      will also work.
 *
 * THANKS:
 *   Many thanks to Ronald Kappert (R.Kappert@uci.kun.nl) for sending
 *   me many suggestions to enhance the support for this program.
 *   Also, Michel Goossens (goossens@cernvm.cern.ch) for other ideas
 *   and helping with the testing.
 */
Address 'COMMAND';
parse source . . execfn execft execfm .
Trace 'ON'

/* Note: in the on-line help in the code, we have set the
         command line syntax to always put the filename information
         first.  It is NOT forced by the MODULE itself, but this
         EXEC will get confused otherwise. */
Parse Arg fn ft fm " -"Args
If ( fn = '' | fn = '?' | fn = '-?' | Args = '?') then signal Help;

If ft = '' then ft='DVI'
If fm = '' then fm='*'

/* If filename has a '.' in it, we assume that the name is given using
   Unix-style naming conventions so we use that.  Otherwise we build
   it in this form from the fn, ft, fm. */
If Index(fn,'.') ^= 0 then
      Infn = fn
   else
      Infn = fn'.'ft'.'fm

Upper Infn

If Args ^= '' then Args = '-'Args

/* remove any existing MISSFONT LOG A files */
'ESTATE MISSFONT LOG A'
If rc = 0 then 'ERASE MISSFONT LOG A'

call resetLibs       /* reset libraries for DVIPS */

/* Now call the real thing; note the prepending of -M option
   to prevent system calls for automatic font generation.
   This can be removed if it is safe to do so
   or it can be overriden at the command line with -M0 option */

'DVIPS' Infn '-M' Args

call restoreLibs     /* Restore old libraries */

/* Now we see if any missing fonts were found and we ask the user
   if they want us to try to generate them at this point */

failedfont = 0  /* this will be changed to 1 if MakeTeXPK fails */
'ESTATE MISSFONT LOG A'
if rc = 0 then do   /* I'd like to Clear-Screen for this but ... */
   call MissFontPrompt ;  /* we only come back if Yes */
   'MAKEBUF'
   'EXECIO * DISKR MISSFONT LOG A (FINIS'
   cnt=queued()
/* we erase MISSFONT LOG now and rebuild it if there are failures */
   'ERASE MISSFONT LOG A'
   do i=1 to cnt
      parse pull line
      parse var line EXEC mktxpk fontdata
      upper mktxpk
/* now we exec the MakeTeXPK command given by this line */
      EXEC mktxpk fontdata
         if rc ^= 0 then do
           call failfontmsg fontdata
           failedfont = 1 ;
           'EXECIO 1 DISKW MISSFONT LOG A (STR' line
           end
      end
     'DROPBUF'
     'FINIS MISSFONT LOG A'
   if failedfont = 1 then call fontfail
      else call redoDvipsPrompt Infn Args
   end /* rc = 0 from 'ESTATE MISSFONT LOG' */

Exit ;


Savelibs: procedure
   arg library
   'MAKEBUF';
   tmplibs = ''
   'QUERY' library '(STACK'
   do i = 1 to queued()
     pull Mode Eq_sign Tmptmplibs
     if Tmptmplibs = 'NONE' then tmplibs = ''
        else tmplibs = tmplibs Tmptmplibs
   end;
   'DROPBUF';
   return tmplibs  ;

resetLibs:
   /* get current status of library settings */
   Txtlibs=Savelibs(TXTLIB)
   Loadlibs=Savelibs(LOADLIB)
   /* Establish environment for dvips */
   'GLOBAL TXTLIB IBMLIB EDCBASE'
   'GLOBAL LOADLIB EDCLINK'
   return ;

restoreLibs:
   'GLOBAL TXTLIB' Txtlibs
   'GLOBAL LOADLIB' Loadlibs
   return ;

MissFontPrompt:
   say ''
   say 'One or more fonts were missing when DVIPS processed your'
   say 'file 'Infn'.'
   say 'Do you want to generate them now? (Please respond with'
   say '"y", "yes", "n" or "no" -- these are case insensitive.)'
   pull answer
   answer = 'SUBSTR'(answer,1,1) ;
   if ( answer ^= "Y" & answer ^= "N" ) then answer=BadReply() ;
   if answer = "N" then call MissFontReplyN ;
   return ;   /* answer was a 'Y' so we can return */

BadReply:
   say "What did you say? It wasn't one of the choices!"
   say 'Please try again and respond with "y", "yes", "n" or "no"'
   pull answer
   answer = 'SUBSTR'(answer,1,1) ;
/* we keeping asking until we get a 'yes' or 'no'.  Maybe we should
   put a counter on this? */
   if ( answer ^= "Y" & answer ^= "N" ) then answer=BadReply() ;
   return answer ;

MissFontReplyN:
   say "I'll save the MISSFONT LOG file so you can run"
   say "MAKETEXP EXEC yourself.  If you don't want to do that,"
   say "you may want to erase this file."
   exit 0

failfontmsg:
   arg MissFontData
   say " "
   say "I failed to complete a call to MAKETEXP EXEC for fontdata"
   say "     "MissFontData"."
   say " "
   return ;

fontfail:
   say "I failed to generate all the necessary fonts and so"
   say "will exit now.  You can use the MISSFONT LOG file to"
   say "try to correct the problem."
   exit 10

redoDvipsPrompt:
   arg Infn Args
   say "I've tried to generate all the fonts that were missing as"
   say "you requested.  Another call to DVIPS might be in order."
   say "Do you want me to do that NOW?"
   pull redoDvipsReply
   if 'ABBREV'(redoDvipsReply,"Y",1) then do
/* we call the module here instead of the exec since we don't want to
 * get into some silly loop trying to create fonts that we are failing
 * to generate; be sure '-M' flag is set here. We also have to get the
 * libraries restored. */
         call resetLibs ;
         'DVIPS' Infn Args '-M' ;
         dvipsrc = rc ;
         call restoreLibs ;
         exit dvipsrc ;
         end
      else exit 0

Help:
/* dvips now has on-line help if called with no filename */
  call resetLibs ;
  'DVIPS' ;
  call restoreLibs ;
  Exit 100;

