#!/bin/sh

 # g_font
 # ======

 # This file is part of Gerolf Markup Shredder,
 # written by G. D. Brettschneider (1999-2006).

 # GMSdateG_FONT=20060927
 
 # Prologue: ==================================================================

 # Not running:

 if [ "$GMS_SHELL" = "" ]; then
   echo "This is Gerolf Markup Shredder (g_font)."
   exit
 fi
   
 # Debug: ---------------------------------------------------------------------

 if [ "$GMS_DEBUG" = "X" -o "$GMS_DEBUG" = "Y" ]; then
   source "$GMS_SHELL/l_banner" -debug g_font $1 $2 $3 $4
 elif  [ "$GMS_DEBUG" = "Z" ]; then
   echo " g_font ($1) ($2) ($3) ($4) ($5) ($6) ($7) ($8) ($9)" >> "$Z"
 fi

 # Function: ..................................................................
 
 # Get lowercase encoding; 
 function get_lc_enc {
   echo "$1" > enc.txt
   sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;\
     s/^/export enc=/" < enc.txt > enc.sh
   if [ -f "enc.sh" ]; then source enc.sh; fi
   if [ -f "enc.sh" ]; then rm enc.sh; fi
   if [ -f "enc.txt" ]; then rm enc.txt; fi; }
 
 # Chapters: ==================================================================
        
 # Not defined:
 
 if [ "$1" = "" ]; then
  source "$GMS_SHELL/l_banner" -no_action g_font
  
 # Install fonts: -------------------------------------------------------------

 # TDS: ../texmf/fonts/fonttype/supplier/typeface
 # where fonttype = afm, tfm, type1 (for pfa and pfb), ttf, vf.

 # According to the TeX directory structure, afm and pfa / pfb as well as ttf
 # files are stored in different trees. - Copies of them must be collected at
 # a common place, the tfm directory. The ttf fonts need computing of afm fi-
 # les first, before tfm and vf files can be created. After processing of the
 # metrics, all vf files are moved to the vf directory, while the pfa/pfb and
 # afm copies are deleted.  The "core" font metrics get processed during this
 # procedure, too.
 # The tfm and vf metrics that have been processed algorithmically are  *not*
 # stored in supplier/typeface subfolders, but in the top level of the tfm or
 # vf tree.  At the beginning of the metric building process,  the top levels
 # of the tfm and vf trees are cleared, but files in subfolders remain.

 # Handle typeface: -----------------------------------------------------------

 elif [ "$1" = "-handle_typeface" ]; then
   # $2 fonttype, $3 supplier, $4 typeface
   t="$GMS_FONTS/tfm"
   if [ "$4" != "_ignore_" -a "$4" != "_IGNORE_" \
     -a "$4" != "cm" -a "$4" != "Cm" -a "$4" != "CM" ]; then
     if [ -d "$GMS_FONTS/$2/$3/$4" ]; then 
       cd "$GMS_FONTS/$2/$3/$4"
       if [ "$2" = "type1" ]; then        
         for i in *.PFA; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfa; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.PFB; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfb; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "ttf" ]; then 
         for i in *.TTF; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.ttf; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "afm" ]; then
         for i in *.AFM; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.afm; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       fi
     fi
   fi; unset t

 # Handle supplier: -----------------------------------------------------------

 elif [ "$1" = "-handle_supplier" ]; then
   # $2 fonttype, $3 supplier
   t="$GMS_FONTS/tfm"
   if [ "$3" != "_ignore_" -a "$3" != "_IGNORE_" ]; then
     if [ -d "$GMS_FONTS/$2/$3" ]; then 
       cd "$GMS_FONTS/$2/$3"
       if [ "$2" = "type1" ]; then        
         for i in *.PFA; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfa; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.PFB; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfb; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "ttf" ]; then 
         for i in *.TTF; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.ttf; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "afm" ]; then
         for i in *.AFM; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.afm; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       fi
       for i in *; do
         if [ -d "$i" ]; then 
           source "$GMS_SHELL/g_font" -handle_typeface $2 $3 $i
           if [ -d "$GMS_FONTS/$2/$3" ]; then cd "$GMS_FONTS/$2/$3"; fi
         fi
       done
     fi
   fi; unset t

 # Copy font files to tfm top level: ------------------------------------------

 elif [ "$1" = "-copy_to_tfm" ]; then 
   # $2 fonttype
   t="$GMS_FONTS/tfm"
   if [ "$2" != "_ignore_" -a "$2" != "_IGNORE_" ]; then
     if [ -d "$GMS_FONTS/$2" ]; then 
       cd "$GMS_FONTS/$2"
       if [ "$2" = "type1" ]; then        
         for i in *.PFA; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfa; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.PFB; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.pfb; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "ttf" ]; then 
         for i in *.TTF; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.ttf; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       elif [ "$2" = "afm" ]; then
         for i in *.AFM; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
         for i in *.afm; do if [ -f "$i" ]; then cp "$i" "$t"; fi; done;
       fi
       for i in *; do
         if [ -d "$i" ]; then
           source "$GMS_SHELL/g_font" -handle_supplier $2 $i
           if [ -d "$GMS_FONTS/$2" ]; then cd "$GMS_FONTS/$2"; fi
         fi
       done
     fi
   fi; unset t
 
 # Build any font: ------------------------------------------------------------

 elif [ "$1" = "-build" ]; then 
   # $2: glyph file name
   # $3: encoding
   # $4: core/eroc/embed/corefamily/embedfamily mark
   # $5: extension factor
   # $6: slanting factor   
   # $7: new name (or 'none')
   # $8: new suffix (optional)
   enc="$3"
   if [ "$2" != "*.$4" ]; then
     glyph="$2"
       # Get font face file base name from glyph file name: ...................
       echo "$glyph" > font_tmp.txt
       sed "s/\..*$//;s/^/export GMS_BASE=/" < font_tmp.txt > map_tmp.sh
       if [ -f "map_tmp.sh" ]; then source map_tmp.sh; fi
       # Modify name: .........................................................
       esab="$GMS_BASE"
       if [ "$7" != "" ]; then
         if [ "$7" = "none" ]; then esab="$esab$8"; else esab="$7"; fi
       fi
         # Change encoding for non-latin fonts: ...............................
         fo="$GMS_TEMP/fo.sh"
           if [ ! -f "$fo" ]; then
             fj="$GMS_SETTING/encoding.cfg"
             fp="$GMS_TEMP/fp.sh"
             fq="$GMS_TEMP/fq.sh"
               tr -d '\r' < "$fj" > "$fp"
               sed "s/\#.*$//;/^ *$/d;s/^ *//;s/$/; fi/;" < "$fp" > "$fq"
               sed \
       "s/:/\" ]; then export enc=/;s/=\ */=/;s/^/if \[ \"\$GMS_BASE\" = \"/" \
                 < "$fq" > "$fo"
               if [ -f "$fp" ]; then rm "$fp"; fi
               if [ -f "$fq" ]; then rm "$fq"; fi
             unset fp; unset fq; unset fj
           fi
           oldenc=$enc
           if [ -f "$fo" ]; then source "$fo"; fi
           if [ "$oldenc" != "$enc" -a "$GMS_DEBUG" = "Z" ]; then
             echo "   New encoding: $enc" >> "$Z"; 
           fi
	 unset oldenc; unset fo
	 get_lc_enc $enc
         # Run ttf2afm: .......................................................
         arg1="$GMS_BINARIES/ttf2afm"
         arg2="$GMS_ROOT/data/enc/$enc.enc"
         arg3="$GMS_BASE.afm"
         if [ -f "$GMS_BASE.ttf" -a "$glyph" = "$GMS_BASE.ttf" ]; then 
  	   arg4="$GMS_BASE.ttf"
           echo "\"$arg1\" -e \"$arg2\" -o \"$arg3\" \"$arg4\"" >> fm.log
           "$arg1" "-e" "$arg2" "-o" "$arg3" "$arg4"
         elif [ -f "$GMS_BASE.TTF" -a "$glyph" = "$GMS_BASE.TTF" ]; then
           arg4="$GMS_BASE.TTF"
           echo "\"$arg1\" -e \"$arg2\" -o \"$arg3\" \"$arg4\"" >> fm.log
           "$arg1" "-e" "$arg2" "-o" "$arg3" "$arg4"
         fi
         # Define afm2tfm arguments: ..........................................
         if [ -f "$GMS_BASE.afm" -o -f "$GMS_BASE.AFM" ]; then
           # A redirection symbol (" < CP1252.enc ") is included in afm2tfm's
           # output, so afm2tfm must be put into its argument variable:
           arg1="$GMS_BINARIES/afm2tfm"
           arg2="./$GMS_BASE.afm"
           arg3="$GMS_ROOT/data/enc/$enc.enc"
           arg4="$esab.vpl"
           arg5=
           arg6=
             # Extend typeface: ...............................................
             if [ "$5" != "" -a "$5" != 1 -a "$5" != 1.0 -a "$5" != 1.00 -a \
               "$5" != 1.000 -a "$5" != 1.0000 -a "$5" != 1.00000 -a \
               "$5" != hide ]; then
               arg5="-e $5"
             fi
             # Slant typeface: ................................................
             if [ "$6" != "" -a "$6" != 0 -a "$6" != 0.0 -a "$6" != 0.00 -a \
               "$6" != 0.000 -a "$6" != 0.0000 -a "$6" != 0.00000 ]; then
               arg6="-s $6"
             fi
             # Display font file base name: ...................................
             export REPLY_OFFSET=0; export REPLY_SIZE=80
             "$GMS_BINARIES/reply" -banner 19 ""
             arg="                                       "
               echo "$arg$arg"; echo "$arg$arg"
             unset arg
             export REPLY_OFFSET=43; export REPLY_SIZE=37
             "$GMS_BINARIES/reply" -banner 17 "($enc) $GMS_BASE"
             echo
             # Run afm2tfm, build map item, eventually add glyph: .............
             echo \
       "\"$arg1\" \"$arg2\" -T \"$arg3\" -v \"$arg4\" $arg5 $arg6 _$esab.tfm" \
               >> fm.log
           "$arg1" "$arg2" "-T" "$arg3" "-v" "$arg4" $arg5 $arg6 "_$esab.tfm" \
	       > item_tmp.map
             if [ "$4" != "core" -a "$4" != "eroc" ]; then
             # sed "s/$/ \<$glyph/" < item_tmp.map > meti_tmp.map
               sed "s/<.*$/<$enc.enc <$glyph/" < item_tmp.map > meti_tmp.map
               cat meti_tmp.map >> "$GMS_SETTING/unsort.map"
             else
               sed "s/<.*$/<$enc.enc/" < item_tmp.map > meti_tmp.map
             # cat item_tmp.map >> $GMS_SETTING/unsort.map
               cat meti_tmp.map >> "$GMS_SETTING/unsort.map"
             fi
           export arguments=
           #  Run vptovf, move processed core afm fonts to _trans_: ...........
           echo
           arg1="$GMS_BINARIES/vptovf"
           echo "\"$arg1\" \"$esab.vpl\" \"$esab.vf\" \"$esab.tfm\"" >> fm.log
           "$arg1" "$esab.vpl" "$esab.vf" "$esab.tfm"           
           if [ "$4" = "eroc" ]; then
             if [ -f "$GMS_BASE.afm" -a -d "../_trans_" ]; then 
               cp "$GMS_BASE.afm"  "../_trans_"; fi
             if [ -f "$GMS_BASE.AFM" -a -d "../_trans_" ]; then 
               cp "$GMS_BASE.AFM"  "../_trans_"; fi
             if [ -f "$GMS_BASE.afm" ]; then rm "$GMS_BASE.afm"; fi
             if [ -f "$GMS_BASE.AFM" ]; then rm "$GMS_BASE.AFM"; fi
           fi
         fi; echo >> fm.log
       export GMS_BASE=
       unset esab
     unset glyph
   fi; unset enc
  # Clean display lines: ......................................................
   export REPLY_OFFSET=0; export REPLY_SIZE=66 ###
  "$GMS_BINARIES/reply" -banner 20
  "$GMS_BINARIES/reply" -banner 21
  "$GMS_BINARIES/reply" -banner 22
   arg="///////////////////////////////////////" ###
     echo; echo " $arg$arg"
   arg=

 # Build specifically encoded fonts: ------------------------------------------

 elif [ "$1" = "-build_encode" ]; then 
   # $2: encoding
   # be="source "$GMS_SHELL/g_font" -build
   e=$2
   if [ "$2" != "" ]; then
     for i in $e*.afm; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e eroc; fi; done
     for i in $e*.AFM; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e eroc; fi; done
     for i in $e*.pfa; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
     for i in $e*.PFA; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
     for i in $e*.pfb; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
     for i in $e*.PFB; do if [ -f "$i" ]; then 
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
     for i in $e*.ttf; do if [ -f "$i" ]; then
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
     for i in $e*.TTF; do if [ -f "$i" ]; then
       source "$GMS_SHELL/g_font" -build $i $e embed; fi; done
   fi; unset e

 # Build transformed fonts: ---------------------------------------------------

 elif [ "$1" = "-build_trans" ]; then 
   # $2: base name 
   # $3: encoding
   # $4: core/embed mark
   # $5: extension factor
   # $6: slanting factor
   # $7: new name (or 'none')
   # $8: new suffix (optional)
   b=$2; e=$3; m=$4
   f="$5 $6 $7 $8"; myttf="1.0 0.0 $7 $8"   
   # Display font file base name: .............................................
   export REPLY_OFFSET=0; export REPLY_SIZE=80
   "$GMS_BINARIES/reply" -banner 19 ""
   arg="                                       "
     echo "$arg$arg"; echo "$arg$arg"
   unset arg
   export REPLY_OFFSET=43; export REPLY_SIZE=37
   get_lc_enc $3
   "$GMS_BINARIES/reply" -banner 17 "($enc) $2"
   #...........................................................................
   if [ "$m" != "" ]; then
     if [ "$m" = "core" ]; then
       # This is only possible if a type1 glyph file is present:
       if [ -f $b.afm ]; then 
         source "$GMS_SHELL/g_font" -build $b.pfa $e $m $f; fi
     fi
     if [ "$m" = "corefamily" ]; then 
       for i in $b*.afm; do if [ -f "$i" ]; then 
         source "$GMS_SHELL/g_font" -build $i $e embed $f; fi; done
     fi
     if [ "$m" = "embed" ]; then
       if [ -f $b.pfa ]; then 
         source "$GMS_SHELL/g_font" -build $b.pfa $e $m $f; fi
       if [ -f $b.pfb ]; then 
         source "$GMS_SHELL/g_font" -build $b.pfb $e $m $f; fi
       if [ -f $b.ttf ]; then 
         source "$GMS_SHELL/g_font" -build $b.ttf $e $m $myttf; fi
     fi
     if [ "$m" = "embedfamily" ]; then
       for i in $b*.pfa; do if [ -f "$i" ]; then 
         source "$GMS_SHELL/g_font" -build $i $e embed $f; fi; done
       for i in $b*.pfb; do if [ -f "$i" ]; then 
         source "$GMS_SHELL/g_font" -build $i $e embed $f; fi; done
       for i in $b*.ttf; do if [ -f "$i" ]; then 
         source "$GMS_SHELL/g_font" -build $i $e embed $f; fi; done
     fi
   fi; unset b; unset e; unset f; unset m; unset myttf
    
 # Build all fonts: -----------------------------------------------------------

 elif [ "$1" = "-build_all" ]; then 
   export BEFORE=$(date +%s)
   export REPLY_BANNER=15; export REPLY_TEXT=0
   if [ "$2" != "-quiet" ]; then
     setterm -clear -cursor off     
     echo " $GMS_FILE"; cat "$GMS_SETTING/desktop.scn"
     export REPLY_OFFSET=0; export REPLY_SIZE=82     
     "$GMS_BINARIES/reply" -banner 1 "font.map"
     "$GMS_BINARIES/reply" -banner 24 "GMS: Running font metric processor ..."
     "$GMS_BINARIES/reply" -banner 14
     "$GMS_BINARIES/reply" -banner 15 "Please remember to re-initialize the"
     export REPLY_OFFSET=38; export REPLY_SIZE=44
   "$GMS_BINARIES/reply" -banner 15 "format file after re-writing the font map"
   fi
   export REPLY_OFFSET=0; export REPLY_SIZE=82
   "$GMS_BINARIES/reply" -banner 22; "$GMS_BINARIES/reply" -banner 16
   "$GMS_BINARIES/reply" -banner 21; "$GMS_BINARIES/reply" -banner 17
   "$GMS_BINARIES/reply" -banner 20; "$GMS_BINARIES/reply" -banner 18
   "$GMS_BINARIES/reply" -banner 19
   # Delete old tfm and vf; get afm, ttf, pfa/pfb: ............................
   export REPLY_OFFSET=0; export REPLY_SIZE=32
     echo " ---------- Collecting fonts:" >> "$Z"
     "$GMS_BINARIES/reply" -banner 17 "Collecting:   Font files ..."
     echo; echo
     echo "               These processes may run faster if there is no file"
     echo "               manager task to be updated in the background."     
       if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
         for i in *.tfm *.TFM; do if [ -f "$i" ]; then rm "$i"; fi; done
       if [ -d "$GMS_FONTS/vf" ]; then cd "$GMS_FONTS/vf"; fi
         for i in *.vf *.VF; do if [ -f "$i" ]; then rm "$i"; fi; done
   export REPLY_OFFSET=14; export REPLY_SIZE=66
     "$GMS_BINARIES/reply" -banner 17 "*.ttf - True Type Fonts ..."
       source "$GMS_SHELL/g_font" -copy_to_tfm ttf
   "$GMS_BINARIES/reply" -banner 17 "*.pf* - Postscript Fonts ... Ascii/Binary"
       source "$GMS_SHELL/g_font" -copy_to_tfm type1
     "$GMS_BINARIES/reply" -banner 17 "*.afm - Adobe Font Metrics ..."
       source "$GMS_SHELL/g_font" -copy_to_tfm afm
   # Get new tfm and vf: ......................................................
   export REPLY_OFFSET=0; export REPLY_SIZE=14
     "$GMS_BINARIES/reply" -banner 17 Computing:
   export REPLY_OFFSET=14; export REPLY_SIZE=66
     "$GMS_BINARIES/reply" -banner 17 "*.tfm - TeX Font Metrics ..."
       if [ -f "$GMS_SETTING/font.map" ]; then 
         rm "$GMS_SETTING/font.map"; fi 
       if [ -f "$GMS_SETTING/unsort.map" ]; then 
         rm "$GMS_SETTING/unsort.map"; fi
       if [ -d "$GMS_FONTS" ]; then cd "$GMS_FONTS"; fi
       if [ -d "./_trans_" ]; then rm "./_trans_" -r; fi 
       if [ ! -d "./_trans_" ]; then mkdir "./_trans_"; fi 
       # Process font files by encoding: ......................................
     "$GMS_BINARIES/reply" -banner 19 ###
     "$GMS_BINARIES/reply" -banner 20 ###
       if  [ "$GMS_DEBUG" = "Z" ]; then 
         echo " ---------- Process font files by encoding:" >> "$Z"
       fi
       if [ -d "$GMS_ROOT/data/enc" ]; then cd "$GMS_ROOT/data/enc"; fi
       for i in *.enc *.ENC; do
         base=$(basename $i .enc)
         if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
         source "$GMS_SHELL/g_font" -build_encode $base
       done
       unset base  
       # Process font files by type: ..........................................
       if  [ "$GMS_DEBUG" = "Z" ]; then 
         echo " ---------- Process font files by type:" >> "$Z"
       fi
       if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
       c=$GMS_CODEPAGE
         if [ "$GMS_DEBUG" = "Z" ]; then echo "   -------- *.afm:" >> "$Z"; fi
         for i in pcr*.afm; do if [ -f "$i" ]; then 
           source "$GMS_SHELL/g_font" -build $i $c eroc; fi; done
         for i in phv*.afm; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c eroc; fi; done
         for i in ptm*.afm; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c eroc; fi; done
         if [ "$GMS_DEBUG" = "Z" ]; then echo "   -------- *.pf*:" >> "$Z"; fi
         for i in *.pfa; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
         for i in *.PFA; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
         for i in *.pfb; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
         for i in *.PFB; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
         if [ "$GMS_DEBUG" = "Z" ]; then echo "   -------- *.ttf:" >> "$Z"; fi


         for i in *.ttf; do if [ -f "$i" ]; then 
       	   source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
     
         for i in *.TTF; do if [ -f "$i" ]; then 
           source "$GMS_SHELL/g_font" -build $i $c embed; fi; done
       unset c
       # Process transformed files: ...........................................
       if  [ "$GMS_DEBUG" = "Z" ]; then 
         echo " ---------- Process transformed fonts files:" >> "$Z"
       fi
       if [ -d "$GMS_FONTS/_trans_" ]; then cd "$GMS_FONTS/_trans_"; fi         
         for i in *.afm *.AFM; do if [ -f "$i" ]; then cp $i ../tfm; fi; done
         for i in *.afm *.AFM; do if [ -f "$i" ]; then rm $i; fi; done
       if [ -d "$GMS_FONTS" ]; then cd "$GMS_FONTS"; fi
         if [ -d "$GMS_FONTS/_trans_" ]; then rm "$GMS_FONTS/_trans_" -r; fi
       if [ -d "$GMS_SETTING" ]; then cd "$GMS_SETTING"; fi
         if  [ -f font.cfg ]; then
           tr -d '\r' < font.cfg > font.gfc
           sed "s/%GMS_CODEPAGE%/\$GMS_CODEPAGE/" < font.gfc > tnof.gfc
           sed "s/^.*#.*$//;/^ *$/d" < tnof.gfc > snart.gfc
	   # Fixme:
           sed "s/^/source \"\$GMS_SHELL\/g_font\" -build_trans/" \
             < snart.gfc > trans.sh
           if [ -f font.gfc ]; then rm font.gfc; fi
           if [ -f tnof.gfc ]; then rm tnof.gfc; fi
           if [ -f snart.gfc ]; then rm snart.gfc; fi
           if [ -f trans.sh ]; then cp trans.sh "$GMS_FONTS/tfm"; fi
           if [ -f trans.sh ]; then rm trans.sh; fi
           if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
           if [ -f trans.sh ]; then source trans.sh; fi
           if [ -f trans.sh ]; then rm trans.sh; fi
         fi
       if [ -d "$GMS_TEMP" ]; then cd "$GMS_TEMP"; fi
         if [ -f fo.sh ]; then rm fo.sh; fi
       # Install virtual fonts (and clean up the tfm folder): .................
       export REPLY_OFFSET=0; export REPLY_SIZE=15
       "$GMS_BINARIES/reply" -banner 17 Installing:
       export REPLY_OFFSET=14; export REPLY_SIZE=68
     "$GMS_BINARIES/reply" -banner 17 "*.vf* - Virtual Fonts ... $GMS_CODEPAGE"
       arg="                                       "
       echo "$arg$arg"; echo "$arg$arg"; echo "$arg$arg"; echo "$arg$arg"
       unset arg
       if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
         for i in *.afm *.AFM; do if [ -f "$i" ]; then rm $i; fi; done
         for i in *.pfa *.PFA; do if [ -f "$i" ]; then rm $i; fi; done
         for i in *.pfb *.PFB; do if [ -f "$i" ]; then rm $i; fi; done
         for i in *.ttf *.TTF; do if [ -f "$i" ]; then rm $i; fi; done
         for i in *.vpl *.VPL; do if [ -f "$i" ]; then rm $i; fi; done
         for i in *.vf *.VF; do if [ -f "$i" ]; then 
           mv $i "$GMS_FONTS/vf"; fi; done
         if [ -f font_tmp.txt ]; then rm font_tmp.txt; fi
         if [ -f map_tmp.sh ]; then rm map_tmp.sh; fi
         if [ -f item_tmp.map ]; then rm item_tmp.map; fi
         if [ -f meti_tmp.map ]; then rm meti_tmp.map; fi
         if [ -f short.txt ]; then rm short.txt; fi
         if [ -f short.sh ]; then rm short.sh; fi
   # Build font map (and log): ................................................
     slashline="////////////////////////////////////////////////////////////"
       if [ -d "$GMS_SETTING" ]; then cd "$GMS_SETTING"; fi
         if [ -f encoding.sh ]; then rm encoding.sh ]; fi
           echo " % font.log - Warnings, errors, missing glyphs" > font.log
           echo " % $slashline$slashline" >> font.log
         arguments="$GMS_FONTS/[font-type]/[supplier]/[font-family]"
           echo " % font.map - $arguments" > head.map
           echo " % /////////$slashline$slashline///////////" >> head.map
         unset arguments
         echo >> head.map
         echo " % Generated by Gerolf Markup Shredder (www.Gerolf.org)" \
           >> head.map
         echo -n " % on " >> head.map
         date >> head.map
         echo >> head.map
         echo " %  _[1]                  Base  name of  font files" >> head.map
         echo " %   [2]                  PostScript font face name" >> head.map
       echo " % \" [3] ReEncodeFont \"   PostScript encoding  name" >> head.map
         echo " %  <[4]                  Encoding   file   [*.enc]" >> head.map
         echo " %  <[5]                  Glyph file [*.pf*, *.ttf]" >> head.map
         echo >> head.map
           if [ -f unsort.map ]; then sort < unsort.map > sort.map; fi
             if [ -f head.map ]; then 
               if [ -f sort.map ]; then 
                 cat head.map sort.map > font.map
               fi
             fi
             if [ -f sort.map ]; then rm sort.map; fi
             if [ -f head.map ]; then rm head.map; fi
           if [ -f unsort.map ]; then rm unsort.map; fi
         echo >> font.map
           echo " % Warnings, errors, missing glyph list: see font.log" \
             >> font.map
           get_runtime            
           echo " % GMS run time: $GMS_RUNTIME" >> font.map
           echo >> font.map
           echo " % $slashline$slashline////////////////////" >> font.map
         echo " % $GMS_FOLDER" >> font.map
       if [ -d "$GMS_FONTS/tfm" ]; then cd "$GMS_FONTS/tfm"; fi
       if [ -f fm.log ]; then mv fm.log "$GMS_SETTING/fontmap.log"; fi
     export slashline=
   export REPLY_OFFSET=0; export REPLY_SIZE=14
     "$GMS_BINARIES/reply" -banner 17 Writing:
   export REPLY_OFFSET=14; export REPLY_SIZE=68
     "$GMS_BINARIES/reply" -banner 17 "  $GMS_SETTING/font.map"
       if [ -d "$GMS_SETTING" ]; then cd "$GMS_SETTING"; fi
   setterm -cursor on
       
 # Not found: -----------------------------------------------------------------

 else 
   source "$GMS_SHELL/l_banner" -no_action g_font $1   
 fi
 
