::
::  This is dvidrv.btm, a batch file for 4DOS/4OS2 written by
::  Werner Lemberg <wl@gnu.org> partially based on the
::  dvidrv.btm file of the 4allTeX package written by Phons Bloemen.
::
::  It is a replacement for dvidrv.exe of the emTeX package.
::
::  Additional features: support of ps2pk
::                       support of ttf2pk
::                       support of hbf2gf for HBFs (Hanzi bitmap fonts)
::
::      Only quadratic printer modes are supported for ttf2pk!
::
::  All needed binaries will be searched in the path.

iff %# lt 2 then
  echo ``
  echo Usage: %0 dvi-driver dvi-file [parameters]
  quit
endiff

setlocal
  :: we set a default value only if the corresponding environment variable
  :: is empty. The `=' must follow the variable name immediately.
  alias set_def `iff "%[%@word["=",0,%1]]" eq "" then %+ set %& %+ endiff`

  :: =========================================
  :: ======== User defined variables. ========
  :: =========================================
  ::
  :: Can be overridden in the environment (except %ps2pk, %ttf2pk, %hbf2gf,
  :: %pre_dpi, and %post_dpi).

  :: Set these values to `no' for  the programs you don't want to use.
  set ps2pk=yes
  set ttf2pk=yes
  set hbf2gf=yes

  :: where the PK files will be installed created by ps2pk, ttf2pk, and
  :: hbf2gf.
  :: dvidrv.btm adds `\modeless\XXXdpi' to this string (see below for the
  :: exact string).
  set_def pkdir=%emtexdir\pixel\tmp\pk

  :: where the TrueType fonts reside. Supports trailing `!' and `!!'.
  set_def ttfonts=%emtexdir\fonts\truetype
  :: where auxiliary files of ttf2pk are located. Supports trailing `!'
  :: and `!!'.
  set_def ttfcfg=%emtexdir\ttf2pk

  :: where the HBF files reside. Supports trailing `!' and `!!'.
  set_def hbfonts=%emtexdir\fonts\hbf
  :: where the hbf2gf config files are. Supports trailing `!' and `!!'.
  set_def hbfcfg=%emtexdir\hbf2gf

  :: the dpi-subdirectory has various forms; the most common are XXXdpi and
  :: dpiXXX, e.g. 300dpi and dpi300. Here we define two variables which
  :: control this behaviour.
  :: [dpiXXX is used for TDS compatible TeX trees.]
  set pre_dpi=dpi
  set post_dpi=

  :: ================================================
  :: ======== End of user defined variables. ========
  :: ================================================

  :: the base name for log files etc.
  set basename=%@name[%1]

  :: do we run dvips?
  iff "%basename" ne "dvips" then
    goto nodvips1
  endiff

  :: we delete dvips.mfj if it exists.
  del dvips.mfj >& nul

  :: now we run dvips with all supplied parameters.
  %&

  :: do we have to generate fonts?
  iff exist dvips.mfj then
    goto generate_fonts
  endiff

  :: else all is done.
  goto end


:nodvips1
  :: here we call the dvi driver with all supplied parameters.
  %& -pj:%basename.mfj

  :: do we have to generate fonts?
  iff errorlevel 8 then
    goto generate_fonts
  endiff

  :: else all is done.
  goto end


:generate_fonts
  gosub call_mfjob

  :: do we run dvips?
  iff "%basename" ne "dvips" then
    goto nodvips2
  endiff

  :: we now call dvips a second time
  %&

  :: since no fonts will be generated in the second run we remove dvips.mfj
  del dvips.mfj >& nul

  goto end


:nodvips2
  :: we call the dvi driver a second time (without font generation).
  %& -pj -fm

  del %basename.mfj >& nul

  goto end


:: this is the main subroutine which calls mfjob and then parses the mfjob
:: file for fonts which can be handled by ps2pk, ttf2pk, or hbf2gf.
:call_mfjob
  :: this alias prints a message on the screen and writes it into the logfile.
  alias echolog=`echo %& | tee /a %basename.mlg`

  echo ======== DVIDRV.BTM logfile ========  > %basename.mlg
  echo Commandline: dvidrv.btm %&           >> %basename.mlg
  echo ``                                   >> %basename.mlg
  echo basename=%basename                   >> %basename.mlg
  echo emtexdir=%emtexdir                   >> %basename.mlg
  echo dvidrvfonts=%dvidrvfonts             >> %basename.mlg
  echo mfinput=%mfinput                     >> %basename.mlg
  echo mfjobopt=%mfjobopt                   >> %basename.mlg
  echo textfm=%textfm                       >> %basename.mlg
  echo psfonts=%psfonts                     >> %basename.mlg
  echo ttfonts=%ttfonts                     >> %basename.mlg
  echo ttfcfg=%ttfcfg                       >> %basename.mlg
  echo hbfonts=%hbfonts                     >> %basename.mlg
  echo hbfcfg=%hbfcfg                       >> %basename.mlg
  echo ``                                   >> %basename.mlg

  iff "%@search[mfjob]" eq "" then
    echolog MFJOB not found in the path.
    echolog Can't generate PK fonts from METAFONT automatically.
    echo ``
    quit
  endiff

  :: now we call mfjob.
  echolog mfjob %mfjobopt -g%basename.mfl %basename.mfj
  mfjob %mfjobopt -g%basename.mfl %basename.mfj
  iff %? gt 0 then
    echolog ``
    echolog Something went wrong while running METAFONT.
    echolog Look into the log files...
    echolog ``
  endiff

  :: after running mfjob we scan the mfjob input file for fonts which can
  :: be handled by ps2pk, ttf2pk, or hbf2gf.
  set lnnr=0
  set totlines=%@lines[%basename.mfj]

  do while %lnnr le %totlines
    :: input a line.
    set scratch=%@line[%basename.mfj,%lnnr]
    set lnnr=%@inc[%lnnr]

    :: get resolutions (METAFONT mode will be ignored).
    :: example:
    ::   mode=lqlores[180 180];
    iff %@index[%scratch,mode] ge 0 then
      set temp=%@word["[]",1,%scratch]
      set xdpi=%@word[0,%temp]
      set ydpi=%@word[1,%temp]
    endiff

    :: get fontname and magnification; we then compute the font resolutions
    :: (rounded to the nearest integer---there is no necessity to adjust
    :: these values further because of possible rounding errors since both
    :: emTeX's dvi drivers and dvips check neighbored values too).
    :: example:
    ::   {font=cmr10; mag=0.5;}
    iff %@index[%scratch,{font] ge 0 then
      set testfont=%@word["=;",1,%scratch]
      set temp=%@word["=;",3,%scratch]
      :: we use highest arithmetic precision for these calculations.
      :: Additionally we write 1/2 instead of 0.5 to avoid problems with
      :: countries which use a comma instead of a colon as the decimal
      :: separator.
      set fontresx=%@int[%@eval[%xdpi*%temp+1/2=8]]
      set fontresy=%@int[%@eval[%ydpi*%temp+1/2=8]]
      set pksubdir=modeless\%[pre_dpi]%[fontresx]%post_dpi

      set success=0

      :: check whether %testfont leads to a PS font (we call ps2pkmfj).
      iff %success == 0 .and. "%ps2pk" eq "yes" then
        gosub call_ps2pk
      endiff

      :: check whether %testfont leads to a TrueType font.
      iff %success == 0 .and. "%ttf2pk" eq "yes" then
        gosub call_ttf2pk
      endiff

      :: check whether %testfont leads to a HBF file.
      iff %success == 0 .and. "%hbf2gf" eq "yes" then
        gosub call_hbf2gf
      endiff
    endiff
  enddo

  iff exist %basename.mfp then
    echo ``                              >> %basename.mlg
    echo ``                              >> %basename.mlg
    echo ======== PS2PK logfile ======== >> %basename.mlg
    echo ``                              >> %basename.mlg
    type %basename.mfp                   >> %basename.mlg
    echo ``                              >> %basename.mlg
    del %basename.mfp >& nul
  endiff

  iff exist %basename.mfl then
    echo ``                              >> %basename.mlg
    type %basename.mfl                   >> %basename.mlg
    echo ``                              >> %basename.mlg
    del %basename.mfl >& nul
  endiff

  return


:call_ttf2pk
  iff "%@search[ttf2pk]" eq "" then
    echolog TTF2PK not found in the path.
    echolog Can't generate PK fonts from TrueType fonts automatically.
    echo ``
    quit
  endiff

  echolog ttf2pk -q -n %testfont %fontresx
  ttf2pk -q -n %testfont %fontresx

  iff %? == 0 then
    echolog Font %pkdir\%pksubdir\%testfont.pk generated.
    mkdir /s %pkdir\%pksubdir >& nul
    move %testfont.pk %pkdir\%pksubdir >& nul
    set success=1
  elseiff %? == 2 then
    echolog (%lnnr/%totlines): Font %testfont is no TrueType font.
    set success=0
  else
    echolog Error running TTF2PK for font %testfont!
  endiff

  return


:call_hbf2gf
  iff "%@search[hbf2gf]" eq "" then
    echolog HBF2GF not found in the path.
    echolog Can't generate PK fonts from HBF files automatically.
    echo ``
    quit
  endiff

  echolog hbf2gf -q -p -n %testfont %fontresx %fontresy
  hbf2gf -q -p -n %testfont %fontresx %fontresy

  iff %? == 0 then
    echolog Font %pkdir\%pksubdir\%testfont.pk generated.
    mkdir /s %pkdir\%pksubdir >& nul
    gftopk %testfont.gf %testfont.pk
    move %testfont.pk %pkdir\%pksubdir >& nul
    del %testfont.gf >& nul
    set success=1
  elseiff %? == 2 then
    echolog (%lnnr/%totlines): Font %testfont is no HBF.
    set success=0
  else
    echolog Error running HBF2GF for font %testfont!
  endiff

  return


:call_ps2pk
  iff not exist %pkdir\%pksubdir\%testfont.pk then
    iff "%@search[ps2pkmfj]" eq "" .or. "%@search[ps2pk]" eq "" then
      echolog PS2PKMFJ and/or PS2PK not found in the path.
      echolog Can't generate PK fonts from PostScript fonts automatically.
      echo ``
      quit
    endiff

    echolog ps2pkmfj -X%fontresx -Y%fontresy %testfont %testfont.pk
    ps2pkmfj -X%fontresx -Y%fontresy %testfont %testfont.pk | input %%pspkline

    iff errorlevel != 1 then
      %pspkline >> %basename.mfp
      iff %? == 0 then
        echolog Font %pkdir\%pksubdir\%testfont.pk generated.
        mkdir /s %pkdir\%pksubdir >& nul
        move %testfont.pk %pkdir\%pksubdir >& nul
        success=1
      else
        echolog Error running PS2PK for font %testfont!
      endiff
    else
      echolog (%lnnr/%totlines): Font %testfont is no PS font.
      success=0
    endiff
  else
    echolog Font %pkdir\%pksubdir\%testfont.pk already exists.
  endiff

  return


:end

endlocal

:: ==== end of dvidrv.btm ====
