#!/bin/sh

#cd $Curdir

PWDDIR=`pwd`

gawkbin=awk
export gawkbin

# Mw script
  echoprompt=`echo -n "."`
  if test "$echoprompt" = "."; then
     echomode="-n "; echocr=""
  else
     echoprompt=`echo  ".\c"`
     if test "$echoprompt" = "."; then
        echomode=""; echocr="\c"
     else 
        echomode="-e "; echocr="\c"
     fi
  fi


#

Welcom(){
# Welcome and check superuser
welcomea="y"
if test "$welcomea" = "y"; then
#######    Welcome      #######
clear
echo " ";echo " ";echo " "
echo " "
echo "         ******************************************"
echo "         *                                        *"
echo "         *   ws  ChiTeX/ChiLaTeX (UNIX )  *"
echo "         *                                        *"
echo "         ******************************************"
echo " "
echo " "
echo " "   
echo "         ChiTeX (ChiTeX/ChiLaTeX) written by:"
echo "             Hung-Yih Chen () (yih@math.ncu.edu.tw) "
echo " "
echo "         This version: ChiTeX 6.1.2p12, Sep. 1, 2002"
echo " "
echo $echomode "         Press enter key to continue "$echocr
read inkey
fi

clear
echo " ";echo " "
echo " "
echo "         *************************************************"
echo "         *                                               *"
echo "         *   {NAs 6.1.2p11 6.1.2p12        *"
echo "         *                                               *"
echo "         *   s ChiTeX6.1.2p12 e, A ChiTeX   *"
echo "         *    6.1.2p11                               *"
echo "         *                                               *"
echo "         *   Y < 6.1.2p11   Ctrl+C w     *"
echo "         *                                               *"
echo "         *************************************************"
echo $echomode "         Press enter key to continue "$echocr
read inkey

clear
echo " ";echo " "
echo " "
echo '         *************************************************'
echo '         *                                               *'
echo '         *   ssW\:                           *'
echo '         *                                               *'
echo '         *   1. `CPrΤ]w             *'
echo '         *   2. w˨ϥ dvipdfm-cjk                     *'
echo '         *      (b Redhat WչL b Lҥ)   *'
echo '         *      ϥ dvipdfm pgOrΦp:       *'
echo '         *        \\moekai \\moesung ( )         *'
echo '         *        \\wdkai \\wdming   (幩 ө)   *'
echo '         *        \\idming           (s)      *'
echo '         *      iH:                                    *'
echo '         *      MƻsPKW(ϥ Adobat Reader )    *'
echo '         *                                               *'
echo "         *************************************************"
echo $echomode "         Press enter key to continue "$echocr
read inkey



if test "$LOGNAME" != "root"; then
rootmessage="y"
while test "$rootmessage" = "y"
do
clear
echo " ";echo " "
echo " "
echo "         **********************************************"
echo "         *                                            *"
echo "         *   AO super user \"root\"                 *"   
echo "         *   DAb teTeX ؿWgvQ, _h  *"
echo "         *   Lkiw                             *"
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo "             c. ~w"
echo "             q. w" 
echo " "
echo $echomode "         J 'c'  'q' : "$echocr
read inkey

case $inkey in
  "c") rootmessage="n";;
  "q") rootmessage="n"; rm -f *@;exit;;
    *) rootmessage="y";;
esac
done
fi
}


# Find name of unix system
unixname=`uname|$gawkbin '{print $0}'|tr '[A-Z]' '[a-z]'`
unixver=`uname -r|cut -d. -f 1`
if test "$unixname" = "sunos"; then
   if test "$unixver" = "5" -o "$unixver" != "4" -a "$unixver" != "3"; then
      unixname="solaris"
   else
      unixname="sunos" 
   fi
fi

# if autodetection of unix system's name fails then ask users about ...

if test "$unixname" != "linux" -a "$unixname" != "freebsd" \
  -a "$unixname" != "solaris" -a "$unixname" != "sunos"; then

chooseunix="y"
while test "$chooseunix" = "y"
do
clear
echo " ";echo " ";echo " "
echo " "
echo "       A unix tά:"
echo " "
echo "       1. Linux"
echo "       2. FreeBSD"
echo "       3. Solaris"
echo "       4. SunOS"
echo "       q. w"
echo " "
echo $echomode "         J̹Ʀr 1,..,4  q w: "$echocrread
read inkey              

chooseunix=""
case "$inkey" in
  "1") unixname="linux";;
  "2") unixname="freebsd";;
  "3") unixname="solaris";;
  "4") unixname="sunos";;
  "q") exit ;;
   *) chooseunix="y"
esac
done

fi

#echo $unixname


# Find bindir
echo yes > tmp@
bindir=`which tex`
bindir=`
gawk -v bindir=$bindir '{ gsub(/\/tex/,"",bindir); print bindir}' tmp@ `


#####################################

InstallAcrobatReader(){
if test "$unixname" = "linux" -o "$unixname" = "solaris"; then
rootmessage="y"
while test "$rootmessage" = "y" 
do
clear
echo " ";echo " "
echo " "
echo "         **********************************************"
echo "         *                                            *"
echo "         *   UNw Acrobat Reader 5.0.6          *"   
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo "             c. ~w Acrobat Reader 5.0.6"
echo "             q. w Acrobat Reader 5.0.6" 
echo " "
echo $echomode "         J 'c'  'q' : "$echocr
read inkey

case $inkey in
  "c") rootmessage="n"; acroinst="y" ;;
  "q") rootmessage="n";;
    *) rootmessage="y";;
esac
done


  if test "$acroinst" = "y"; then
     cd /usr/local/src
#     if test ! -d /usr/local/src/Acrobat; then
#        mkdir /usr/local/src/Acrobat
     if test ! -d $PWDDIR/Acrobat; then
        mkdir $PWDDIR/Acrobat
     fi
     cd $PWDDIR/Acrobat
     if test "$unixname" = "linux"; then
#        gunzip -dc $PWDDIR/linux-506.tgz|tar xvf -
        gunzip -dc $Curdir/linux-506.tgz|tar xvf -
     else
        if test "$unixname" = "solaris"; then        
#          gunzip -dc $PWDDIR/solaris-506.tgz|tar xvf -
          gunzip -dc $Curdir/solaris-506.tgz|tar xvf -
        fi
     fi

     ./INSTALL
#     gunzip -dc $PWDDIR/chtfont.tgz|tar xvf -
     gunzip -dc $Curdir/chtfont.tgz|tar xvf -
     cd CHTKIT
     ./INSTLANG
  fi
  

  find /usr -name 'Acrobat5' > acroread@

  $gawkbin '
  {
    if (index($0,"Acrobat5")>0) then
       {print $0 > "acro@"}
  } ' acroread@

  acroreaddir=`cat acro@`

  if test -s $acroreaddir/bin/acroread; then
     acroread="$acroreaddir/bin/acroread"
     cp -f $acroread $bindir
  fi

fi
}

########################################



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@ `


MakeFmt(){
##### Make chinese fmt files and copy to $fmtname

makechinesefmt="y"

if test "$makechinesefmt" = "y"; then

### Find date of latex.ltx
nowdate=`date +%D`
nowtime=`date +%T`
if test -s $texmf/tex/latex/base/latex.ltx; then  #
   gawk -v nowdate=$nowdate '
   BEGIN{
    lyear=substr(nowdate,length(nowdate)-1)
    nowdate="20" lyear "/" substr(nowdate,1,length(nowdate)-3)
  }
  { fmtvpos=index($0,"def\134fmtversion{")
    if (fmtvpos>0)
       {
        rbr=index($0,"}")
        lline=substr($0,1,fmtvpos+14)
        rline=substr($0,rbr)
        outline=lline nowdate rline
       }
    else
       {outline=$0}
    print outline > "latexltx@"
  }
  END{} ' $texmf/tex/latex/base/latex.ltx
  cp -f latexltx@ $texmf/tex/latex/base/latex.ltx
  rm -f latexltx@
fi

if test -s $texmf/tex/latex/base/ltpatch.ltx; then  #
   gawk -v nowdate=$nowdate '
   BEGIN{
    lyear=substr(nowdate,length(nowdate)-1)
    nowdate="20" lyear "/" substr(nowdate,1,length(nowdate)-3)
  }
  { fmtvpos=index($0,"fmtversion@topatch{")
    if (fmtvpos>0)
       {
        rbr=index($0,"}")
        lline=substr($0,1,fmtvpos+18)
        rline=substr($0,rbr)
        outline=lline nowdate rline
       }
    else
       {outline=$0}
    print outline > "ltpatchltx@"
  }
  END{} ' $texmf/tex/latex/base/ltpatch.ltx
  cp -f ltpatchltx@ $texmf/tex/latex/base/ltpatch.ltx
  rm -f ltpatchltx@
fi

# Modify texmfcnf

$gawkbin '
{
  inline=$0
#  print inline > "texmfcnf@"
  mm=index(inline,"main_memory")
  extra=""
  if (mm >0)
    {
     inlinenosp=inline
     gsub(/ /,"",inlinenosp)
     if (index(inlinenosp,"main_memory=") == 1)
        {
         inlineextra="main_memory = 1100000 % words of inimemory available; also applies to inimf&mp"
         print inlineextra > "texmfcnf@"
         extra="y"
        }
    }
  if (extra == "y")
     {print "%" inline > "texmfcnf@"}
  else
     {print inline > "texmfcnf@"}


}' $TEXMF/web2c/texmf.cnf
  
 cp -f texmfcnf@ $TEXMF/web2c/texmf.cnf
 rm -f texmfcnf@
 fmtname=$TEXMF/web2c


# Recompile latex pdflatex
   initex latex.ltx \\dump 
   cp -f latex.fmt $fmtname/latex.fmt

   pdfinitex latex.ltx \\dump
   cp -f latex.fmt $fmtname/pdflatex.fmt

   /bin/rm -f latex.fmt


# Make chinese fmt files


  ChmkFmtrtn(){
   ./chmkfmtp; ./chmkfmt
   echo $echomode "." $echocr       

   cp chitex.fmt $fmtname/chitex.fmt 2> errlog@


   if test -s chitex.fmt; then
      cp chitexl.fmt $fmtname/chitexl.fmt
      cp chitexg.fmt $fmtname/chitexg.fmt
      cp chitexlg.fmt $fmtname/chitexlg.fmt
      cp -f chitex.fmt $fmtname/chitex.fmt
      cp -f chitexl.fmt $fmtname/chitexl.fmt
      cp -f chitexg.fmt $fmtname/chitexg.fmt
      cp -f chitexlg.fmt $fmtname/chitexlg.fmt
      /bin/rm -f *.fmt

      ./chmkfmtp; ./chmkfmt2 2>/dev/null
      echo $echomode "." $echocr          

      cp pdfchitex.fmt $fmtname
      cp pdfchitexl.fmt $fmtname
      cp pdfchitexg.fmt $fmtname
      cp pdfchitexlg.fmt $fmtname
      cp -f pdfchitex.fmt $fmtname
      cp -f pdfchitexl.fmt $fmtname
      cp -f pdfchitexg.fmt $fmtname
      cp -f pdfchitexlg.fmt $fmtname

      /bin/rm -f *.fmt
   fi
  
   echo $echomode "." $echocr       
   if test -s chmkfmt; then
      cp -f chmkfmt* $bindir      # move later
      /bin/rm -f chmkfmt*
   fi
   echo "       End of make fmt files "
  }

cd $PWDDIR

# To make chinese fmt files
#  
cd $PWDDIR

#  cp -f $tmpdir/chitex612/chitex612/data/wumap2.tex .
#  cp -f $tmpdir/chitex612/chitex612/data/bcwumap.tex .

#  if test ! -d $texmf/tex/chinese; then
#     mkdir -p $texmf/tex/chinese
#  fi

 if test "$unixname" != "linux"; then
      if test "$unixname" != "solaris"; then
#         gunzip -dc chitex612/fmt/chggfile.$unixname.tgz|tar xvf - 2>&2 >/dev/null
         gunzip -dc chggfile.$unixname.tgz|tar xvf - 2>&2 >/dev/null
      else
#         gunzip -dc chitex612/fmt/chggfile.sunos.tgz|tar xvf - 2>&2 >/dev/null
         gunzip -dc chggfile.sunos.tgz|tar xvf - 2>&2 >/dev/null
      fi
      ChmkFmtrtn 
 else
#   gunzip -dc chitex612/fmt/chggfile.Redhat70.tgz|tar xvf - 2>&2 >/dev/null
   gunzip -dc chggfile.Redhat70.tgz|tar xvf - 2>&2 >/dev/null
    chmod +x chmk*
    ./chmkfmtp 2> usagefile
    useage=`cat usagefile`
    rm -f usagefile

   if test "$useage" = ""; then
      ChmkFmtrtn 
      if test -s err1@; then
          rm -f err1@
      fi
      grep "No such file" errlog@|grep "chitex.fmt" > err1@
      if test -s err1@; then
#         gunzip -dc chitex612/fmt/chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null 
         gunzip -dc chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null 
         chmod +x chmk*
         ChmkFmtrtn
      fi  
   else
#     gunzip -dc chitex612/fmt/chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null
     gunzip -dc chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null
     ChmkFmtrtn 
   fi
 fi


# rm -f wumap.tex
# rm -f bcwumap.tex

# Test 

echo "yes">tmp@

#  cp $tmpdir/chitex612/chitex612/tex/chitex.fdf chitex.fdfile
  echo "     "
  echo "   The following is to test whether "
  echo "   the chinese fmt files work well"
  echo "  "
  echo "   If it fails, just type 'x' or 'control z' to exit"
  echo "   (Messages will contain characters ^^ which are not error message.)"
  echo "   and then it will try to make up the installation."
  echo " "
  echo "   Press Enter key to continue!"
  read xxxxxxx

gawk '
BEGIN{}
{
print "\\nonstopmode" > "t1.tex"
print "\\makeatletter" > "t1.tex"
print "\\def\\ch@delay{y}" > "t1.tex"
print "\\makeatother" > "t1.tex"
print "\\documentclass{article}" > "t1.tex"
print "\\begin{document}" > "t1.tex"
print "test" > "t1.tex"
print "\\end{document}" > "t1.tex"
}
END{} ' tmp@


#  cp  $fmtname/chitexl.fmt .
  virtex \&chitexl t1
  fdfilemsg=`grep ".fdfile" t1.log`
  if test "$fdfilemsg" = ""; then
#     gunzip -dc chitex612/fmt/chggfile.Redhat70.tgz|tar xvf - 2>&2 >/dev/null
     gunzip -dc chggfile.Redhat70.tgz|tar xvf - 2>&2 >/dev/null
     ChmkFmtrtn 
     echo "     "
     echo "   The following is to test again whether "
     echo "   the chinese fmt files work well"
     echo "  "
     echo "   If it fails, just type 'x' or 'ctl z' to exit"
     echo "   (Messages will contain characters ^^ which are not error message.)"
     echo "   and then it will try to make up the installation."
     echo " "
     echo "   Press Enter key to continue!"
     read xxxxxxx
  
#     cp  $fmtname/chitexl.fmt .
     virtex \&chitexl t1
     fdfilemsg=`grep ".fdfile" t1.log`
     if test "$fdfilemsg" = ""; then
        echo " "
        echo "  It still fail to make chinese fmt files."
        echo "  After the completion of installtion, please follow the help"
        echo "  of file 'README.Makefmt' to make them manually. "
        echo "  "
        echo "  Press Enter key to continue!"
       read xxxxxxxxxxx
     else
       echo " "
       echo "  It has made chinese fmt files successfully."
       echo " "
#       echo "   Press Enter key to continue!"
#       read xxxxx
     fi
  else
    echo " "
    echo "  It has made chinese fmt files successfully."
    echo " "
#    echo "   Press Enter key to continue!"
#    read xxxxx
  fi


fi #  if test "$makechinesefmt" = "y"; then

}

#  Makefmt


 if test "$unixname" = "freebsd"; then
    gunzip -dc $Curdir/bsmi00lp.tgz|tar xvf -
    mv -f bsmi00lp.ttf $TEXMF/fonts/chinese/ttf/bsmi00lp.ttf
    gunzip -dc $Curdir/bkai00mp.tgz|tar xvf -
    mv -f bkai00mp.ttf $TEXMF/fonts/chinese/ttf/bkai00mp.ttf
    bsttfdir="$TEXMF/fonts/chinese/ttf"
 else
    echo " "
    echo "  Finding bsmi00lp.ttf, Wait!"
    find /usr -type f -name 'bsmi00lp.ttf' > findfile

bsttfdir=`
gawk ' {
b1=index($0,"bsmi00lp.ttf")
if (b1>0)
{bstfile=substr($0,1,b1-1)
   print bstfile
   exit
}
} ' findfile `
  fi

if test ! -d $TEXMF/fonts/truetype; then
   mkdir $TEXMF/fonts/truetype
fi

 cd $PWDDIR


if test -s $bsttfdir/bsmi00lp.ttf; then
   ln -s $bsttfdir/bsmi00lp.ttf $TEXMF/fonts/truetype/bsmi00lp.ttf
   ln -s $bsttfdir/bkai00mp.ttf $TEXMF/fonts/truetype/bkai00mp.ttf
   if test "$unixname" = "linux"; then
      ln -s $bsttfdir/bsmi00lp.ttf $TEXMF/fonts/chinese/ttf/bsmi00lp.ttf
      ln -s $bsttfdir/bkai00mp.ttf $TEXMF/fonts/chinese/ttf/bkai00mp.ttf
   fi
fi

if test "$unixname" = "linux"; then

echo " "
echo "  Finding gbsn00lp.ttf, Wait!"

find /usr -type f -name 'gbsn00lp.ttf' > findfile

bsttfdir=`
gawk ' {
b1=index($0,"gbsn00lp.ttf")
if (b1>0)
{bstfile=substr($0,1,b1-1)
   print bstfile
   exit
}
} ' findfile `

fi

if test ! -d $TEXMF/fonts/truetype; then
   mkdir $TEXMF/fonts/truetype
fi

 cd $PWDDIR


if test -s $bsttfdir/gbsn00lp.ttf; then
   ln -s $bsttfdir/gbsn00lp.ttf $TEXMF/fonts/truetype/gbsn00lp.ttf
   ln -s $bsttfdir/gbsn00lp.ttf $TEXMF/fonts/chinese/ttf/gbsn00lp.ttf
fi

if test -s $bsttfdir/gkai00mp.ttf; then
   ln -s $bsttfdir/gkai00mp.ttf $TEXMF/fonts/truetype/gkai00mp.ttf
fi

gunzip -dc $Curdir/moe_sung.tgz|tar xvf -
/bin/cp -f moe_sung.ttf $TEXMF/fonts/chinese/ttf
rm -f moe_sung.ttf



cd $TEXMF/fonts/truetype
ln -s $TEXMF/fonts/chinese/ttf/moe_sung.ttf moe_sung.ttf
ln -s $TEXMF/fonts/chinese/ttf/moe_kai.ttf moe_kai.ttf
ln -s $TEXMF/fonts/chinese/ttf/nuwpstj.ttf nuwpstj.ttf
ln -s $TEXMF/fonts/chinese/ttf/ngkai00mp.ttf ngkai00mp.ttf

makedvipdfm="y"

if test "$makedvipdfm" = "y"; then

# Make dvipdfm
  echo "yes" > tmp@
  texmfshare=`
  gawk -v texmf=$TEXMF '
  {
    llen=length(texmf)
    i=1
    for (i; i<=llen; i++)
       { ch=substr(texmf,i,1)
         if (ch == "/")
            {sl=i}
       } 
    texmfshare=substr(texmf,1,sl-1)
    print texmfshare

  } ' tmp@ ` 

  cd $PWDDIR

  gunzip -dc $Curdir/dvipdfmx-20031006.tar.gz|tar xvf -
  cd dvipdfmx-20031006
  ./configure --bindir=$bindir --datadir=$texmfshare 
  make check
  make install
  /bin/rm -rf bindir dvipdfmx-20031006
  
  cp -f $bindir/dvipdfmx $bindir/dvipdfm

  cd $TEXMF
  gunzip -dc $Curdir/texmflocal.tgz|tar xvf -



# Modify /texmf
  cd $TEXMF
  gunzip -dc $Curdir/texmf.tgz|tar xvf -


fi # if test "$makedvipdfm" = "y"; then

  
# Modify /texmf/dvips/config/psfonts.map

  cd $PWDDIR


# Modify pdftex.map
 
 cd $PWDDIR


 if test -s $TEXMF/dvips/config/pdftex.map; then

$gawkbin '
{
 print $0 > "pdftexmap@"
 if (index($0,"%Wafonts begin") > 0)
    {exit} 
} ' $TEXMF/dvips/config/pdftex.map
  
  cp -f "pdftexmap@"  $TEXMF/dvips/config/pdftex.map


$gawkbin '
{
 print $0 > "pdftexcfg@"
 if (index($0,"map +chinese.map") > 0)
    {Havechinese="y"} 
}
END{
if (Havechinese == "")
   {print "map +chinese.map" > "pdftexcfg@"}

} ' $TEXMF/pdftex/config/pdftex.cfg
  


  cp -f "pdftexcfg@"  $TEXMF/pdftex/config/pdftex.cfg



 fi  # if test -d $TEXMF/dvips/config/pdftex.map; then


# copy new binary files and modify chitex chitrancc

SAVEPWD=`pwd`

cd $tmpdir
echo "yes" > tmp@

chitranc=`which chitranc`
chitrancdir=`
$gawkbin '
BEGIN{}
{texloc=index(chitranc,"/chitranc")
 bindir=substr(chitranc,1,texloc-1)
 print bindir
}
END{} ' chitranc="$chitranc" tmp@ `

cd $SAVEPWD
  
  gunzip -dc $Curdir/bindir.tgz|tar xvf -

  cd bindir


  
$gawkbin -v chitrancdir=$chitrancdir '
{ inline=$0
  if (index(inline,"chitran0.awk")>0)
     {chitranawk=chitrancdir "/chitran0" 
       gsub(/chitran0/,chitranawk,inline)
     }
  if (index(inline,"chitran1.awk")>0)
     {chitranawk=chitrancdir "/chitran1" 
       gsub(/chitran1/,chitranawk,inline)
     }
  if (index(inline,"chitran11.awk")>0)
     {chitranawk=chitrancdir "/chitran11" 
       gsub(/chitran11/,chitranawk,inline)
     }
  print inline > "chitranc@"
} ' chitranc

  cp -f chitranc@ chitranc

  /bin/rm -f *@
  cp -f * $bindir

echo "yes" > tmp@

  cp -f chitranc $chitrancdir

  chitexconfig -scfile
  chitexconfig -chitex.fdf

  cd $PWDDIR

  ln -s $TEXMF/fonts/chinese/ttf/id030.ttf $TEXMF/fonts/truetype/id030.ttf

#  rehash

  texhash

  /bin/rm -f  *@ *.tex t1.* *.aux *.ttf *.log findfile
  /bin/rm -rf bindir dvipdfmx-20031006

 exit

clear
echo " ";echo " "
echo " "
echo '            sw                                  '
echo '                                                        '
echo '            ssW\:                           '
echo '                                                        '
echo '            1. `CPrΤ]w             '
echo '            2. w˨ϥ dvipdfm-cjk                     '
echo '               (b Redhat WչL b LҬ)   '
echo '               ϥ dvipdfm pgOrΦp:       '
echo '                 \\moekai \\moesung ( )         '
echo '                 \\wdkai \\wdming   (幩 ө)   '
echo '                 \\idming           (s)      '
echo '               iH:                                    '
echo '               MƻsPKW(ϥ Adobat Reader )    '
echo '                                                        '
echo '            (аѨ:                                    '
echo "            ( $TEXMF/tex/chinese/math2a_dvipdfm.tex   "
echo '              math2a_dvipdfm.pdf ( Acrobat Reader ) '
#echo'         ************************************************'
echo " "
echo $echomode "         Press enter key to continue "$echocr
read inkey

  
#exit



