#! /bin/sh
#
# This file generates mathematical fonts via texgd.
# You must redefine the directory settings.

textest=`tex -v 2>/dev/null`
if [ -z "$textest" ]; then
 cat <<@

ERROR! TeX not found in your system.
WIMS will not work properly without TeX. Please check your installation.

@
 exit 1
fi

SRC_DIR=/usr/share/wims
FONT_DIR=$SRC_DIR/mathfonts
tex2gd=/usr/bin/texgd
texgdout=/tmp/texgd.out
wims_server_base=/usr/lib/wims
texgd_tmpdir=/tmp
texgd_fontdir=$FONT_DIR/texgd
texgd_texheader=$FONT_DIR/header
export wims_server_base texgd_tmpdir texgd_fontdir texgd_texheader
export texgd_src texgd_outfile texgd_density
rm -f $texgdout 2>/dev/null

# test case-sensitiveness of the file system.
# Note that there is a wims.c, but no WIMS.C in the directory.
if [ -f WIMS.C ]; then
 underscore="_"
else
 underscore=""
fi

leftpar1='('
leftpar2='\left(\matrix{\mathstrut\cr\mathstrut\cr}\right.'
leftpar3='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar4='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar5='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar6='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar7='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar8='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar9='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftpar10='\left(\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
rightpar1=')'
rightpar2='\left.\matrix{\mathstrut\cr\mathstrut\cr}\right)'
rightpar3='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar4='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar5='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar6='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar7='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar8='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar9='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
rightpar10='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right)'
leftbrace1='\{'
leftbrace2='\left\{\matrix{\mathstrut\cr\mathstrut\cr}\right.'
leftbrace3='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace4='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace5='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace6='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace7='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace8='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace9='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
leftbrace10='\left\{\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right.'
rightbrace1='\}'
rightbrace2='\left.\matrix{\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace3='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace4='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace5='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace6='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace7='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace8='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace9='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'
rightbrace10='\left.\matrix{\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr\mathstrut\cr}\right\}'

# for density in 100
for density in 63 69 76 83 91 100 109 120 131 144 158 173
do
  texgd_density=$density
  mkdir -p $FONT_DIR/$density
  
  for font in `cat $SRC_DIR/mathfonts.list`
  do
    fname=$font.png
    if [ ! -z "$underscore" ]; then
     test=`echo $font | grep '[A-Z]'`
     if [ ! -z "$test" ]; then fname=$font$underscore.png; fi
    fi
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/$font
      texgd_src="\$ \\$font \$"
      texgd_outfile=$FONT_DIR/$density/$fname
      $tex2gd >>$texgdout
    fi
  done
  
  for font in x y z
  do
    if [ ! -s $FONT_DIR/$density/$font.png ]; then
      echo $density/$font
      texgd_src="\$ $font \$"
      texgd_outfile=$FONT_DIR/$density/$font.png
      $tex2gd >>$texgdout
    fi
  done
  
  for f in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  do
    fname=$f$f$underscore.png
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/$f$f
      texgd_src="\$ \\mathbb $f \$"
      texgd_outfile=$FONT_DIR/$density/$fname
      $tex2gd >>$texgdout
    fi
    fname=euf$f$underscore.png
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/euf$f
      texgd_src="\$ \\goth $f \$"
      texgd_outfile=$FONT_DIR/$density/$fname
      $tex2gd >>$texgdout
    fi
  done
  for f in a b c d e f g h i j k l m n o p q r s t u v w x y z
  do
    fname=euf$f.png
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/euf$f
      texgd_src="\$ \\goth $f \$"
      texgd_outfile=$FONT_DIR/$density/$fname
      $tex2gd >>$texgdout
    fi
  done
  ###################
  #
  # Special: parentheses. Names: leftpar1, rightpar5, leftpar10, etc. 
  # Max size is 10.
  #
  ###################
 for type in par brace
 do
  for side in left right
  do
   for size in 1 2 3 4 5 6 7 8 9 10
   do
    font=$side$type$size
    export $font
    eval src='$'$font
    if [ ! -s $FONT_DIR/$density/$font.png ]; then
      echo "$density/$font"
      texgd_src="\$\$ $src \$\$"
      texgd_outfile=$FONT_DIR/$density/$font.png
      $tex2gd >>$texgdout
    fi
   done
  done
 done
 
  for syn in `cat $SRC_DIR/mathfonts-synonyms.list`
  do
     j=`echo $syn | sed 's/,/ /'`
     j1=`echo $j | awk '{print $1}'`
     j2=`echo $j | awk '{print $2}'`
     fname=$j1.png
     if [ ! -e $FONT_DIR/$density/$fname ]; then
       ln -s $j2.png $FONT_DIR/$density/$fname
     fi
  done
done

rm -f $HOME_DIR/tmp/texgd.*

