#! /bin/sh
#
# This file generates mathematical fonts via tex2gif.
# 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

cd `dirname $0`
#HOME_DIR=/var/wims
HOME_DIR=..

SRC_DIR=$HOME_DIR/src
HTML_DIR=$HOME_DIR/public_html
FONT_DIR=$HTML_DIR/mathfonts
tex2gif=$HTML_DIR/bin/tex..gif
texgifout=$HOME_DIR/tmp/texgif.out
wims_server_base=$HTML_DIR
texgif_tmpdir=$HOME_DIR/tmp
texgif_fontdir=$FONT_DIR/texgif
texgif_texheader=$FONT_DIR/header
export wims_server_base texgif_tmpdir texgif_fontdir texgif_texheader
export texgif_src texgif_outfile texgif_density
rm -f $texgifout 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
  texgif_density=$density
  mkdir -p $FONT_DIR/$density
  
  for font in `cat $SRC_DIR/mathfonts`
  do
    fname=$font.gif
    if [ ! -z "$underscore" ]; then
     test=`echo $font | grep '[A-Z]'`
     if [ ! -z "$test" ]; then fname=$font$underscore.gif; fi
    fi
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/$font
      texgif_src="\$ \\$font \$"
      texgif_outfile=$FONT_DIR/$density/$fname
      $tex2gif >>$texgifout
    fi
  done
  
  for font in x y z
  do
    if [ ! -s $FONT_DIR/$density/$font.gif ]; then
      echo $density/$font
      texgif_src="\$ $font \$"
      texgif_outfile=$FONT_DIR/$density/$font.gif
      $tex2gif >>$texgifout
    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.gif
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/$f$f
      texgif_src="\$ \\mathbb $f \$"
      texgif_outfile=$FONT_DIR/$density/$fname
      $tex2gif >>$texgifout
    fi
    fname=euf$f$underscore.gif
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/euf$f
      texgif_src="\$ \\goth $f \$"
      texgif_outfile=$FONT_DIR/$density/$fname
      $tex2gif >>$texgifout
    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.gif
    if [ ! -s $FONT_DIR/$density/$fname ]; then
      echo $density/euf$f
      texgif_src="\$ \\goth $f \$"
      texgif_outfile=$FONT_DIR/$density/$fname
      $tex2gif >>$texgifout
    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.gif ]; then
      echo "$density/$font"
      texgif_src="\$\$ $src \$\$"
      texgif_outfile=$FONT_DIR/$density/$font.gif
      $tex2gif >>$texgifout
    fi
   done
  done
 done
 
  for syn in `cat $SRC_DIR/mathfonts-synonyms`
  do
     j=`echo $syn | sed 's/,/ /'`
     j1=`echo $j | awk '{print $1}'`
     j2=`echo $j | awk '{print $2}'`
     fname=$j1.gif
     if [ ! -e $FONT_DIR/$density/$fname ]; then
       ln -s $j2.gif $FONT_DIR/$density/$fname
     fi
  done
done

rm -f $HOME_DIR/tmp/texgif.*

