#!/bin/sh
#
# This script is a part of ChiTeX
# By Hung-Yih Chen    
#           Last updated  July. 30, 1999
#
# This script is used to compile and preview('xdvi') both chinese (Big5)
# plain TeX or LaTeX source file
#
# Usage: chilatex [-option [option]] sourcefile
#
#         chilatex math2   (or chilatex math2.tex)
# Example:
#         chilatex math2   (or chilatex math2.tex)
# when you have math2.tex 
#

gawkloc=`which gawk|grep ":"`
if test "$gawkloc" = ""; then
   gawkloc=`which gawk|grep "no gawk"`
fi
if test "$gawkloc" != ""; then
   gawkbin="awk"
else
   gawkbin="gawk"
fi

option="0"; opno="0"

if test "$1" = "-big5"; then
   option="big5"; code="big5"
   opno="1"
   codeoption="y"
else
   if test "$1" = "-gb"; then
        option="gb";  code="gb"
        opno="1"
        codeoption="y"
   else 
    if test "$1" = "-e"; then
       option="e"
       opno="1"
       code="big5"
    else
      if test "$1" = "-batch"; then
        option="batch"
        opno="1"
        code="big5"
      else
       if test "$1" = "-nonstop"; then
         option="nonstop"
         opno="1"
         code="big5"
       else
         if test "$1" = "-c"; then    
            option="0"
            opno="1"
            code="big5"
         else
          if test "$1" = "-help"; then  
            option="-help"
            opno="1"
          else
            option="none"
            code="big5"
            opno="0"
          fi
         fi
       fi
      fi
    fi
   fi
fi



if test "$#" = "0" -o "$opno" = "1" -a "$#" = "1"  -o "$#" = "2" -a "$codeoptin" = "y" ; then
      option="help"
fi

if test "$option" = "help"; then
   echo " "; echo " "
   echo "  Usage: chilatex [option [option]] sourcefile "
   echo " "
   echo "  where  sourcefile is a TeX/LaTeX source file"
   echo "  option are:"
   echo "  -gb      for using in GB encoded chinese"
   echo "           this option must appear as the 1st option"
   echo "  -big5    for using in Big5 encoded chinese" 
   echo "           this option must appear as the 1st option"
   echo "           default is with option -big5"
   echo "  -e       with error message and .log file non-chineses"
   echo "  -c       with chinese error message and chinese .log file"
   echo "           default is with option -c"
   echo "  -batch   with chinese .log file generated at run time"
   echo "           but without prompt error message which is only suitable"
   echo "           for compiling in 'batchmode' or in 'nonstopmode' and"
   echo "           could spend less time."
   echo "           There is a command 'chilatexb' which is equivalent to"
   echo "           'chilatex -batch' and is used for 'lyx'."
   echo "  -nonstop  same as '-batch'"
   echo "  -help    help"
   echo " "
   exit
fi


if test "$codeoption" = "y"; then
       shift
fi

rm -f tmp@


if test "$code" = "big5"; then
   texfmt="chitex"
   latexfmt="chitexl"
else
   texfmt="chitexg"
   latexfmt="chitexlg"
fi   


option="0"; opno="0"

if test "$1" = "-e"; then
   option="e"
   opno="1"
else
   if test "$1" = "-batch"; then
        option="batch"
        opno="1"
   else
      if test "$1" = "-nonstop"; then
         option="nonstop"
         opno="1"
      else
         if test "$1" = "-help"; then
            option="help"
            opno="1"
         else
            if test "$1" = "-c"; then    
               option="0"
               opno="1"
            fi
         fi
      fi
   fi
fi


#if [ "$#" = "0" -o "$opno" = "1" -a "$#" = "1" ]; then
if test "$#" = "0" -o "$opno" = "1" -a "$#" = "1"; then
   option="help"
fi



#if [ "$opno" = "1" -a "$#" = "1" ]; then
#   option="help"
#fi


if test "$option" = "help"; then
   echo " "; echo " "
   echo "  Usage: chilatex [option] sourcefile "
   echo " "
   echo "  where  sourcefile is a LaTeX source file"
   echo "  option are:"
   echo "  -e       with error message and .log file non-chineses"
   echo "  -c       with chinese error message and chinese .log file"
   echo "           default is with option -c"
   echo "  -batch   with chinese .log file generated at run time"
   echo "           but without prompt error message which is only suitable"
   echo "           for compiling in 'batchmode' or in 'nonstopmode' and"
   echo "           could spend less time."
   echo "           There is a command 'chilatexb' which is equivalent to"
   echo "           'chilatex -batch' and is used for 'lyx'."
   echo "  -nostop  same as '-batch'"
   echo "  -help    help"
   echo " "
   exit
fi


if test $# -gt 1; then
if test "$2" = "-e"; then
   option="e"
else
   if test "$2" = "-batch"; then
      option="batch"
   else
      if test "$2" = "-nonstop"; then
         option="nonstop"
      else 
         if test "$2" = "-c"; then    
            option="0"
         fi
      fi
   fi
fi
fi

#if test "$1" = "-e" -o "$1" = "-batch" -o "$1" = "-nonstop" -o "$1" = "-c"
#then
#   shift 1
#fi

#if test "$1" = "-e" -o "$1" = "-batch" -a "$#" = "1"; then
if test "$1" = "-e"; then
    shift 1
else
    if test "$1" = "-batch"; then
       shift 1
    else
       if test "$1" = "-nonstop"; then
          shift 1
       else
          if test "$1" = "-c"; then
             shift 1
          fi
       fi
    fi
fi

TEXMF=`kpsexpand '$TEXMF'|cut -d! -f 3 `
VARTEXFONTS=`kpsexpand '$VARTEXFONTS' `
if test "$VARTEXFONTS" = ""; then
   VARTEXFONTS=`kpsexpand '$VARFONTS' `
   export VARFONTS
else
   export VARTEXFONTS
fi

#$gawkbin -v texmfcnf=$texmfcnf '

echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
$gawkbin  '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `

texmffile=`kpsexpand '$TEXMFDIST' `
if test "$texmffile" != ""; then
TEXMFDIR=` gawk '
   {lbr=index(texmffile,"{")
    sep=index(texmffile,",")
    dash=index(texmffile,"-")
    texmfpre=substr(texmffile,1,dash)
    if (lbr > 0)
       {texlive=substr(texmffile,lbr+1,sep-lbr-1) 
        texmf =texmfpre texlive
       }
    else
       {if (dash > 0)
           {texlive=substr(texmffile,dash+1) 
            texmf =texmfpre texlive
           } 
       }    
    print texmf
   }' texmffile=$texmffile tmp@ `
fi

if test "$TEXMFDIR" != "" -a -d "$TEXMFDIR"; then
   TEXMF=$TEXMFDIR
fi



#TEXMFCNF=$TEXMF/web2c
TTF2PKINPUTS=.:$TEXMF
TTF2TFMINPUTS=.:$TEXMF
TTFONTS=.:$TEXMF/fonts/chinese/ttf//
TFMFONTS=.:$TEXMF/fonts/tfm//:$VARTEXFONTS/tfm//
TEXMFDBS=$TEXMF:$VARTEXFONTS
TEXINPUTS=.:$TEXMF/tex//

export TEXMF
#export VARTEXFONTS
export TEXMFCNF
export TTF2PKINPUTS
export TTF2TFMINPUTS
export TTFONTS
export TFMFONTS
export TEXMFDBS
export TEXINPUTS

if test "$code" = "gb"; then
   GBENV="gbenv"
   export GBENV
else
   GBENV=""   
   export GBENV
fi


fileno=1
export fileno
inputno=0
export inputno
labelcount=0
export labelcount
bitemcount=0
export bitemcount

if test ! -d .tmp00; then
   mkdir .tmp00
fi


PWD=`pwd`  
/bin/rm -f *temp@ maintemp*@ maintmp*@ fileb@ *.@@@ *.temp$ *.temp$ trasl*@
/bin/rm -f addmac*@ filemap@ trancode@ *.tmp00 ch@export* ch@labelfile

if test ! -s $1.tex
then
 if test ! -s $1; then 
   filename=`find $TEXMF/tex -name $1 -print`
   if test "$filename" = ""; then
      filename=`find $TEXMF/tex -name $1.tex -print`
      if test "$filename" = ""; then
         exit
      fi
   fi
 else
   filename=$1
 fi
else 
 filename=$1.tex
fi

  bname=`basename $filename`                    
  filea=`echo $bname|cut -d. -f 1` 
  fileb=`echo $bname |cut -d. -f 2`

  filea=$filea
  export filea

echo $filename > texfile@


if test -s $filea.tex; then
   texfile=$filea.tex
else
   texfile=$filea
fi

#chitran $1
chifiltpre $1


#if test -s ch@labelfile; then
#   mv -f ch@labelfile ch@label
#fi



/bin/rm -f *temp@ maintemp*@ maintmp*@ fileb@ *.@@@ 


/bin/rm -f tmp@@ *.tmp@* *.temp$ *.tmp!* 
#/bin/rm -f transl@*  addmac*@   *@  ch@export* ch@labelfile
cp .tmp00/*.aux . >/dev/null 2>&1

fileno=
inputno=
labelcount=
bitemcount=
html=
export fileno
export inputno
export labelcount
export bitemcount
export html

exit

