@echo off

rem 
rem    This is maketexp.btm, a batch file for 4DOS/4OS2 written by
rem    Werner Lemberg <a7621gac@awiuni11.bitnet>. 
rem    It makes a new TeX PK font, because one wasn't found. 
rem
rem    Its function is similar to the script file MakeTeXPK but calls mfjob of
rem    the emTeX package to compute METAFONT fonts. Under DOS it creates a
rem    batch file which will build the fonts later.
rem
rem    If a proper CJK ttf font is found instead, makettfp.btm is called.
rem
rem    Parameters are:
rem 
rem        name dpi bdpi magnification [mode]
rem 
rem    `name' is the name of the font, such as `cmr10'.  `dpi' is
rem    the resolution the font is needed at.  `bdpi' is the base
rem    resolution, useful for figuring out the mode to make the font
rem    in.  `magnification' is a string to pass to MF as the
rem    magnification.  `mode', if supplied, is the mode to use.
rem 
rem
rem    name=%1 dpi=%2 bdpi=%3 mag=%4 mode=%5
rem

set ttfpkdir=d:\china\pk
set ttfdir=d:\china\truetype

set plane=%@substr[%1, 1, -2]
set ttfname=%@substr[%1, 0, %@index[%1,%plane] + 1]


iff "%_dos" == "DOS" .and. not exist fonts.btm then
  echo ``
  echo ``
  echo Creating font.btm
  echo ``
  echo Run fonts.btm after dvips has finished.
  echo ``
  echo ``

  echo @echo off                                          > fonts.btm
  echo rem fonts.btm - created by maketexp               >> fonts.btm
  echo ``                                                >> fonts.btm
  echo iff exist dvips.mfj then                          >> fonts.btm
  echo   mfjob dvips                                     >> fonts.btm
  echo   iff %%? == 0 then                               >> fonts.btm
  echo     del dvips.mfj `>` nul                         >> fonts.btm
  echo   endiff                                          >> fonts.btm
  echo endiff                                            >> fonts.btm
  echo ``                                                >> fonts.btm
endiff

rem
rem    Check whether a TTF font exists.
rem

iff %@numeric[%plane] == 1 .and. exist %ttfdir\%ttfname.ttf then
  set tfmfile=%@filename[%@unique[]]
  del %tfmfile > nul
  iff "%_dos" == "DOS" then
    echo call makettfp %tfmfile %1.pk %plane %ttfname %2 >> fonts.btm
    echo del %tfmfile `>` nul                            >> fonts.btm
    echo iff %%? == 0 then                               >> fonts.btm
    echo   mkdir /s %ttfpkdir\%2dpi `>`& nul             >> fonts.btm
    echo   move %1.pk %ttfpkdir\%2dpi `>` nul            >> fonts.btm
    echo else                                            >> fonts.btm
    echo   echo ttf2pk failed producing %1.pk            >> fonts.btm
    echo endiff                                          >> fonts.btm
    echo ``                                              >> fonts.btm
  else
    call makettfp %tfmfile %1.pk %plane %ttfname %2
    del %tfmfile > nul
    iff %? == 0 then
      mkdir /s %ttfpkdir\%2dpi >& nul
      move %1.pk %ttfpkdir\%2dpi > nul
      quit 0
    else
      echo ttf2pk failed
      quit 1
    endiff
  endiff
else
  iff not exist dvips.mfj then
    echo Creating dvips.mfj
    echo ``

    echo %% dvips.mfj - created by dvips                    > dvips.mfj
    echo ``                                                >> dvips.mfj
    echo input [modes];                                    >> dvips.mfj
    echo ``                                                >> dvips.mfj
  endiff
  echo {                                                   >> dvips.mfj
  echo   base=plain;                                       >> dvips.mfj
  echo   font=%1;                                          >> dvips.mfj
  echo   mag=%4;                                           >> dvips.mfj
  echo   mode=%5[%3];                                      >> dvips.mfj
  echo   output=pk[c:\emtex\texfonts\pixel.lj4\@Rrdpi\@f]; >> dvips.mfj
  echo }                                                   >> dvips.mfj
  echo ``                                                  >> dvips.mfj

  iff "%_dos" == "OS2" then
    mfjob dvips
    del dvips.mfj > nul
  endiff
endiff
