#!/bin/sh


##### U 23 (parts) C@H@ function 
#     UCĤG column  function 
#     wq 25 functions M~@_o 25 functions

# Setenvironment                 # Part1
# FindUnixSystem                 # Part1.1
# WelcomeandVerifysupuser        # Part2
# FindpathofFmtTexmfVartexBinary # Part3
# ModifyReadme                 # Part4
# MakeChineseFmt               # Part5
# Copytfmppkttf                # Part6
                               # Part6.1
                               # Part6.2
# ModifyConfigps               # Part7
# ModifySpecialmap             # Part8
# CopyPfbTtftfmfiles           # Part9
# Modifyttfontsmap             # Part10
# CopySfdAfm                   # Part11
# CopyPstfiles                 # Part12
# Backupbinaryfiles            # Part13
# Copybinaryfiles              # Part14
# Compileandcopycfiles         # Part15
# CopyTeXfdfpngstyfiles        # Part16
# Configurescfilechitexfdf     # Part17
# Updatepsfontsmappdftexmap    # Part18
# Set600dpi                    # Part19
# Modifylyx                    # Part20
# TexhashandRmtmpfiles         # Part21
# MakeVARTEXFONTSsubdir        # Part22
# GiveMessage                  # Part23
#


###### Part1
Setenvironment(){
# set SAVEPWD to current dir. and set PATH to contain current dir.
SAVEPWD=`pwd`
PATH=.:$PATH:$SAVWPWD
export PATH

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

#


#   gawkbin=awk
#   export gawkbin
#   unixname="linux"

} # End of Setenvironment

###### Part1.1
FindUnixSystem(){
# 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
##########
#
#if test "$unixname" = "linux"; then
#   chsetupbig5Linux
#   exit
#fi

# To unzip to have file 'gawk'
 if test "$unixname" != "linux"; then
  gunzip -dc chitex612/data/gawk."$unixname".gz > gawk
  chmod +x gawk
 fi

# To test if exist file 'gawk'
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

if test "$unixname" = "linux"; then
   gawkbin=awk
   export gawkbin
fi
}


##### Part2
WelcomeandVerifysupuser(){

welcomea=""
if test "$welcomea" = "y"; then
#######    Welcome      #######
clear
echo " ";echo " ";echo " "
echo " "
echo "         ******************************************"
echo "         *                                        *"
echo "         *   wϥ  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.2p7.9, March 19, 2002"
echo " "
echo $echomode "         Press enter key to continue "$echocr
read inkey
fi

clear
echo " ";echo " "
echo " "
echo "         *************************************************"
echo "         *                                               *"
echo "         *   w˵{NAw ChiTeX 6.1.2p7.9       *"
echo "         *                                               *"
echo "         *   w ChiTeX 6.1.2p7.9 e, Atζw  *"
echo "         *   ˦ teTeX, teTeX   >= 1.0            *"
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



install="all"

#updatemenu ( ) {
#######    Welcome      #######
clear
echo " ";echo " "
echo " "
echo "         ******************************************"
echo "         *                                        *"
echo "         *   {bN}lw  ChiTeX 6.1.2p7.9     *"
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.2p7.9, March 19, 2002"
echo " "
echo $echomode "         Press enter key to continue "$echocr
read inkey
#}

newttf2pk="y"

} # End of WelcomeandVerifysupuser


###### Part3
FindpathofFmtTexmfVartexBinary(){
echo "yes" > tmp@
# Find path of
#  (1) fmt  (2) TEXMF (3) VARTEXFONTS (4) binary files
#

# (1)  find path of fmt #
#
fmt=`kpsexpand '$WEB2C' `
if test "$fmt" = ""; then
   fmt=`kpsexpand '$TEXFORMATS' `
fi
fmt=`
$gawkbin '
BEGIN{}
{
 exc=index(fmt,"!!")
 if (exc > 0)
    {fmt=substr(fmt,exc+2,length(fmt)-exc-1)}
 print fmt   
}
END{} ' fmt="$fmt" tmp@ `

fmtname=$fmt
echo "         fmtdir=$fmt found"

# (2) find path of TEXMF #
#
#kpsexpand=`which kpsexpand`

texmfdir=`kpsexpand '$TEXMF'|cut -d! -f 3`

texmf=$texmfdir
echo "         texmfdir=$texmfdir found"

# (3) Find path of VARTEXFONTS
#
VARTEXFONTS=`kpsexpand '$VARTEXFONTS' `
if test "$VARTEXFONTS" = ""; then
   VARTEXFONTS=`kpsexpand '$VARFONTS' `
fi
if test "$VARTEXFONTS" = ""; then
   VARTEXFONTS="/var/tmp/texfonts"
   mkdir -p $VARTEXFONTS
   chmod a+rwx $VARTEXFONTS
fi
export VARTEXFONTS

# (4) find path of binary files #
#
# cd $SAVEPWD
if test -d .temp00$$;then
   /bin/rm -rf .temp00$$
fi
mkdir .temp00$$
cd .temp00$$
echo "yes" > tmp@

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

echo "         bindir=$bindir found"

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

checkmenu="y"
while test "$checkmenu" = "y"
do
clear
echo "         wΧAJؿ:  "
echo "         1.  texmf ؿ (@묰 /usr/.../texmf) :"
echo "               $texmf"
echo "         2.  *.fmt ɩҦbؿ (@묰 /usr/.../texmf/web2c) :"
echo "               $fmt"
echo "         3.  .../texfonts ؿ (@묰 /var/tmp/texfonts "
echo "           /var/lib/texmf  /var/texfonts 䤤tlؿ tfm, pk ) :"
echo "               $VARTEXFONTS"
echo "            YLؿ, Aiॲ run 'texmfconfig' h]wXؿ"
echo "         4.  i latex Ҧbؿ: "
echo "               $bindir "
echo " "
echo "         HW쪺ؿYOťؿNOT. "
echo "         "
echo "         YTJ "y" , _hJ '1'  '2',.. "
echo "         (HKsJӶؤ~ؿ)"
echo $echomode "         : " $echocr
read inkey

 
   case "$inkey" in
   "1") 
       echo " "
       echo "         texmf ؿ($texmf)"
       echo $echomode "         :" $echocr
       read keydir
       if test "$keydir" != ""; then 
          texmf=$keydir
       fi
       ;;
   "2") 
       echo " "
       echo "         *.fmt ɩҦbؿ($fmt)"
       echo $echomode "         :" $echocr
       read keydir
       if test "$keydir" != ""; then 
          fmt=$keydir
       fi
       ;;
   "3") 
       echo " "
       echo "          .../texfonts ؿ($VARTEXFONTS)"

       echo $echomode "         :" $echocr
       read keydir
       if test "$keydir" != ""; then 
          VARTEXFONTS=$keydir
       fi
       ;;
   "4") 
       echo " "
       echo "         i latex Ҧbؿ($bindir)"
       echo $echomode "         :" $echocr
       read keydir
       if test "$keydir" != ""; then 
          bindir=$keydir
       fi
       ;;
   "y") 

   checksubmenu="y"
   while test "$checksubmenu" = "y"
   do
     echo " "
     echo "         AؿT."
     echo "         ЦAJ "y"  T{, _hJ 'n' HAJ"
     echo $echomode "         : " $echocr
     read inkeya

     case "$inkeya" in
       "y")  checkmenu="n"; checksubmenu="n";;
       "n") checkmenu="y"; checksubmenu="n";;
       *)   checkmenu="y"; checksubmenu="y"; break ;;
     esac    
   done

     ;;
   *)  checkmenu="y"  ;;

   esac
done

Askstop="y"
while test "$Askstop" = "y"
do
clear
echo " "
echo "         ئwˤ覡:"
echo "         1.  ݤ覡"
echo "         2.  w˨쩳覡(|Uդ fmt files)"
echo "         "
echo "         J '1' M '2' "
echo " "
echo $echomode "         : " $echocr
read inkey
   case "$inkey" in
   "1") stopask="y"; Askstop="";;
   "2") stopask=""; Askstop="";;
   "*")  :  ;;
   esac
done


#done


} # End of FindpathofFmtTexmfVartexBinary


###### Part4
ModifyReadme(){
###### Modify files of README* ,...  and alt last put them
#      on $texmf/doc/chinese

# In the following we'll modify files of README*,... given by
# $SAVEPWD/chitex612/doc

cd $SAVEPWD

# first made dir. 'docdir' and then put modified files (modified from 
#  $SAVEPWD/chitex612/doc) into  dir. 'docdir'

if test ! -d docdir; then
   mkdir docdir
fi
cd $SAVEPWD/chitex612/doc

# Move *.ps to ../ for the time being since we need not modify them
mv -f  *.ps $SAVEPWD/chitex612
docfiles=`/bin/ls `

for readmefile in $docfiles
do
 sed "s#/usr/local/TeX/share/texmf#$texmf#g" $readmefile > $readmefile".tmp"
 mv $readmefile".tmp" $SAVEPWD/docdir/$readmefile
 if test -s $SAVEPWD/docdir/README.adv; then
    mv $SAVEPWD/docdir/README.adv $SAVEPWD/docdir/README.install.advance
 fi
 if test -s $SAVEPWD/docdir/README.adv.gb; then
     mv $SAVEPWD/docdir/README.adv.gb $SAVEPWD/docdir/README.install.advance.gb
 fi
done


# Move $SAVEPWD/chitex612/*.ps to  $SAVEPWD/chitex612/doc
mv -f $SAVEPWD/chitex612/*.ps $SAVEPWD/chitex612/doc

cd $SAVEPWD

# Make dir. $texmf/doc/chitex and put modified 'README*' files on it
#
if test ! -d $texmf/doc/chitex; then
   mkdir -p $texmf/doc/chitex
fi
chmod +w $texmf/doc/chitex/*    # Add Mar. 26, 2000


cp -f docdir/* $texmf/doc/chitex
/bin/rm -rf $SAVEPWD/doc
mv docdir doc
cp chitex612/doc/*.ps $texmf/doc/chitex

chmod -w $texmf/doc/chitex/README* 
chmod -w $texmf/doc/chitex/ANNOUNCE*
chmod -w $texmf/doc/chitex/ChangeLog*
chmod -w $texmf/doc/chitex/*.ps

echo "       End of modifying README*"
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echocr
   read inkey
fi

#clear
#echo " "
#echo " ";echo " "
#echo " "
#echo "         ******************************************"
#echo "         *                                        *"
#echo "         *            }lw, y              *"
#echo "         *                                        *"
#echo "         ******************************************"
#echo " "
}  #End of ModifyReadme



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

#   unixnamea="Redhat70"
  
   echo $echomode "." $echocr       

  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/nul
      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 "
  }

# if test "$unixname" != "linux"; then   
#      if test "$unixname" != "solaris"; then
#         gunzip -dc chitex612/fmt/chggfile.$unixname.tgz|tar xvf - 2>&2 >/dev/null
#      else
#         gunzip -dc chitex612/fmt/chggfile.sunos.tgz|tar xvf - 2>&2 >/dev/null
#      fi

# To make chinese fmt files
#  

#  ChmkFmtrtn
# else

#     gunzip -dc chitex612/fmt/chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null           
#     ChmkFmtrtn 

 if test "$unixname" != "linux"; then
      if test "$unixname" != "solaris"; then
         gunzip -dc chitex612/fmt/chggfile.$unixname.tgz|tar xvf - 2>&2 >/dev/null
      else
         gunzip -dc chitex612/fmt/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
    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 
         chmod +x chmk*
         ChmkFmtrtn
      fi  
   else
     gunzip -dc chitex612/fmt/chggfile.linux.tgz|tar xvf - 2>&2 >/dev/null
     ChmkFmtrtn 
   fi
 fi

## fi 

# Test 

echo "yes">tmp@

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


# The following now is only a backup of old procesure
  CopyFMTFiles(){
   gunzip -dc chitex612/fmt/chitex.fmt.gz > chitex.fmt
   gunzip -dc chitex612/fmt/chitexl.fmt.gz > chitexl.fmt
   cp -f chitex*.fmt $fmtname
   /bin/rm -f chitex*.fmt

   gunzip -dc chitex612/fmt/chitexg.fmt.gz > chitexg.fmt
   gunzip -dc chitex612/fmt/chitexlg.fmt.gz > chitexlg.fmt
   cp -f chitex*.fmt $fmtname
   /bin/rm -f chitex*.fmt
   gunzip -dc chitex612/fmt/pdfchitex.fmt.gz > pdfchitex.fmt
   gunzip -dc chitex612/fmt/pdfchitexl.fmt.gz > pdfchitexl.fmt
   cp -f pdfchitex*.fmt $fmtname
   /bin/rm -f pdfchitex*.fmt
   gunzip -dc chitex612/fmt/pdfchitexg.fmt.gz > pdfchitexg.fmt
   gunzip -dc chitex612/fmt/pdfchitexlg.fmt.gz > pdfchitexlg.fmt
   cp -f pdfchitex*.fmt $fmtname
   /bin/rm -f pdfchitex*.fmt
   echo "       End of copy fmt files "
  }

# fi

/bin/rm -f t1.* *.fmt

#if test "$stopask" = "y"; then
#   echo $echomode "       Press any key to continue " $echoc
#   read inkey                         
#fi

} # End of MakeChineseFmt


##### Part6
Copytfmppkttf(){
#####  Copy tfm. pk, ttf files      
#####  to $texmf/fonts/...  
#
# if test "$install" = "all" -o "$install" = "update" ; then
#                 # Remove  May 7, 2000

##### Part6.0
##### First Makedir .../texmf/fonts/chinese/...  
#     .../texmf/fonts/chinese/ttf link to .../texmf/fonts/ttf/chinese
#     if the latter exist otherwise make 'revervse ......' 


for texdir in tex/chinese fonts/chinese/ttf fonts/tfm/chinese/ntukai \
  fonts/tfm/chinese/fxntukai fonts/pk/chinese/fxntukai \
  fonts/tfm/chinese/fxntuli  fonts/tfm/chinese/fxntufs \
  fonts/tfm/chinese/nuwpstj fonts/pk/chinese/nuwpstj \
  fonts/tfm/chinese/moe_kai fonts/tfm/chinese/id030
do
   if test ! -d $texmf/$texdir; then
      if test "$texdir" = "fonts/chinese/ttf"; then
         if test -d "$texmf/fonts/ttf/chinese"; then
            ln -s $texmf/fonts/ttf/chinese $texmf/fonts/chinese/ttf 
         else
             mkdir -p $texmf/$texdir
         fi
      else
         mkdir -p $texmf/$texdir
      fi
   fi
#   if test  "$texdir" = "fonts/chinese/ttf"; then
#      if test ! -d $texmf/fonts/ttf/chinese; then
#        if test ! -d $texmf/fonts/ttf; then
#            mkdir -p $texmf/fonts/ttf
#        fi
#        ln -s $texmf/$texdir $texmf/fonts/ttf/chinese
#      fi
#   fi

done


##### Part6.1
##### Copy tfm files ########
cd $SAVEPWD
/bin/rm -rf tfm
mkdir tfm 
cd tfm
rm -rf pbktfm
mkdir pbktfm
cd pbktfm
gunzip -dc $SAVEPWD/chitex612/data/pbktfm.tgz|tar xvf -
if test ! -d $texmf/fonts/tfm/adobe/bookman; then
  mkdir -p $texmf/fonts/tfm/adobe/bookman
fi
cp *.tfm $texmf/fonts/tfm/adobe/bookman
cd ..
/bin/rm -rf pbktfm

gunzip -dc $Curdir/nuwpstfm.tgz|tar xvf -
cp -f nuwpstj*.tfm $texmf/fonts/tfm/chinese/nuwpstj
/bin/rm -f nuwpstj*.tfm
gunzip -dc $Curdir/moekatfm.tgz|tar xvf -
cp -f moe_kai*.tfm $texmf/fonts/tfm/chinese/moe_kai
/bin/rm -f moe_kai*.tfm

gunzip -dc $Curdir/idttftfm.tgz|tar xvf -
cp -f id030*.tfm $texmf/fonts/tfm/chinese/id030
/bin/rm -f id030*.tfm


cd $SAVEPWD
rmdir tfm
##### Part6.2
##### Copy pk files and ttf file ########
/bin/rm -rf pk
mkdir pk
cd pk
gunzip -dc $SAVEPWD/chitex612/data/fxntukai.tar.gz|tar xvf -
cp -f fxntukai*.*pk $texmf/fonts/pk/chinese/fxntukai
/bin/rm -f fxntukai*.*pk

cd $SAVEPWD
rmdir pk

###### Copy fx*'s tfm and link ttf files #######
tfm=$texmf/fonts/tfm
ttf=$texmf/fonts/chinese/ttf
/bin/cp chitex612/data/linkkai $tfm/chinese/fxntukai 
/bin/cp chitex612/data/linkfs $tfm/chinese/fxntufs
/bin/cp chitex612/data/linkli $tfm/chinese/fxntuli
#/bin/cp chitex612/data/fxntukai03.tfm $tfm/chinese/fxntukai 
/bin/cp chitex612/data/fxntukai03.tfm $tfm/chinese/fxntufs
/bin/cp chitex612/data/fxntukai03.tfm $tfm/chinese/fxntuli
/bin/cp chitex612/data/linkttf $ttf
#rm -f fxntukai03.tfm


cd $tfm/chinese/fxntukai 
/bin/rm -f fxntukai*.tfm
/bin/cp $SAVEPWD/chitex612/data/fxntukai03.tfm $tfm/chinese/fxntukai  
./linkkai 
/bin/rm -f linkkai
cd $tfm/chinese/fxntufs
/bin/rm -f fxntufs*.tfm
./linkfs
/bin/rm -f linkfs
cd $tfm/chinese/fxntuli
/bin/rm -f fxntuli*.tfm
./linkli
/bin/rm -f linkli

cd $ttf
/bin/rm -f fxntu*.ttf
./linkttf
/bin/rm -f linkttf
##### end of copying tfm. pk, ttf files

echo "       End of make copy tfm files "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
} # End of copytfmpkttf


##### Part7
ModifyConfigps(){
cd $SAVEPWD
##### Modify config.ps and copy XDvi.300 XDvi.600 $texmf/xdvi
#configps=$texmf/dvips/config/config.ps
configps=$texmf/dvips/config
$gawkbin '
BEGIN{}
{
   lpraloc=index($0,"o")
   vertloc=index($0,"|")
   lprbloc=index($0,"lpr")
   D600loc=index($0, "D 600")
   Modeloc=index($0, "M ljfour")
   D300loc=index($0, "D 300")
   Modeloca=index($0, "M cx")
  
   if (vertloc == 0 && lpraloc == 0 && lprbloc == 0 && D600loc == 0 \
       && Modeloc == 0 && D300loc == 0 && Modeloca == 0)
      {print $0 >  "config.ps.300"
       print $0 >  "config.ps.600"}
   else
    {
     if (lpraloc > 0 && lprbloc > 0 && vertloc > 0)
        {print "% " $0 > "config.ps.300"
         print "% " $0 > "config.ps.600"}
     else
        {if (D600loc > 0 || Modeloc > 0 || D300loc > 0 || Modeloca > 0)
            {print "% " $0 > "config.ps.300"
             print "% " $0 > "config.ps.600"
             if ((D600loc > 0 || D300loc > 0) && Dmodified == 0)
                {print "D 300" > "config.ps.300"
                 print "D 600" > "config.ps.600"
                 Dmodified=1}
             if ((Modeloc > 0 || Modeloca >0) && Mmodified == 0)
                {print "M cx" > "config.ps.300"
                 print "M ljfour" > "config.ps.600"
                 Mmodified=1
                 }
            }
         else
            {print $0 > "config.ps.300"
             print $0 > "config.ps.600"}
        }
    } 
}
END{}' $configps/config.ps

/bin/cp -f config.ps.300 config.ps.600 $configps
/bin/rm -f config.ps.300 config.ps.600
cd chitex612/data
/bin/cp -f XDvi.300 XDvi.600 $texmf/xdvi
cd $SAVEPWD
} # ModifyConfigps


##### Part8
ModifySpecialmap(){
###### Modify special.map ############
fontname=$texmf/fontname
chinesename=`grep "@c chinese fonts" $fontname/special.map`
if test "$chinesename" = ""; then
   cat chitex612/data/special.map.add >> $fontname/special.map
   cat chitex612/data/special.map.add2 >> $fontname/special.map
fi
/bin/rm -f chinesname@
###### end of modify specila.map  ###########

echo "       End of modifying file special.map  "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
# End of ModifySpecialmap
}



##### Part9
CopyPfbTtftfmfiles(){
#####  Makedir $VARTEXFONTS/type1/fonts
#####      and copy pfb files to here 
#####  Unzip ttf files and copy to $texmf/fonts/chinese/fonts  
##### 
cd $SAVEPWD 

# Make dir. for saving chineses pfb fonts  #########
if test ! -d $VARTEXFONTS/type1/fonts; then
   mkdir -p  $VARTEXFONTS/type1/fonts
fi
chmod a+rwx $VARTEXFONTS/type1/fonts

if test ! -d $texmf/fonts/type1/chinese; then
   mkdir -p $texmf/fonts/type1/chinese
   ln -s $VARTEXFONTS/type1/fonts $texmf/fonts/type1/chinese/fonts \
       2>&2 >/dev/null
fi



cd $SAVEPWD

#step3="y"
#if test "$step3" = "y"; then
#############
# unzip TrueType files in ../ and copy them into
# .../texmf/fonts/chinese/ttf
#

ttfgzfiles=`ls $Curdir/*.ttf.gz`

clear
echo " "
echo "           It will unzip TrueType files: "
echo " "
cd ..
ls $Curdir/*.ttf.gz
cd $SAVEPWD
#echo "              $ttfgzfiles "
echo " "
echo "           in $Curdir/ and move them into      "
echo "              $texmf/fonts/chinese/ttf"
echo " "
if test "$stopask" = "y"; then
   echo $echomode "           Press any enter key to continue" $echocr
   read inputkey
fi

for cmd in $ttfgzfiles
do
 #   cmda=`echo "$cmd"|cut -d/ -f 2|cut -d. -f 1`
 #   cmdnogz=$cmda".ttf"
    echo "unzip $cmd"
#    gunzip -dc $cmd > $cmdnogz
    cp -f $cmd .
    gunzip *.gz
#    echo "mv -f $cmdnogz $texmf/fonts/chinese/ttf"
#    /bin/rm -f $texmf/fonts/chinese/ttf$cmdnogz
#    cp -f $cmdnogz $texmf/fonts/chinese/ttf
    cp -f *.ttf $texmf/fonts/chinese/ttf
    /bin/rm -f *.ttf
done


##### Use name of "moe_kai.ttf" instead of old "moekai.ttf" ,...
if test -s $texmf/fonts/chinese/ttf/moekai.ttf; then
   ln -s $texmf/fonts/chinese/ttf/moekai.ttf \
           $texmf/fonts/chinese/ttf/moe_kai.ttf 
fi
if test -s $texmf/fonts/chinese/ttf/moesung.ttf; then
   ln -s $texmf/fonts/chinese/ttf/moesung.ttf \
           $texmf/fonts/chinese/ttf/moe_sung.ttf 
fi
########


###############
# unzip *tgz files in $Curdir/ into pfb and tfm files and copy them into
# and move pfb files into $VARTEXFONTS/type1/fonts
# and move tfm files into $VARTEXFONTS/tfm/chinese/...
#                             
#if test ! -d pfb; then
   /bin/rm -rf pfb
#fi
mkdir pfb
cd pfb
moekaifiles=`/bin/ls $Curdir/moekai*.tgz`
for tgzfile in $moekaifiles
do
   gunzip -dc $tgzfile|tar xvf -
   cp -f *.pfb $VARTEXFONTS/type1/fonts
   /bin/rm -f *.pfb
   if test ! -d $VARTEXFONTS/tfm/chinese/moe_kai_pfb; then
      mkdir -p $VARTEXFONTS/tfm/chinese/moe_kai_pfb
   fi
   chmod a+w $VARTEXFONTS/tfm/chinese
   chmod a+w $VARTEXFONTS/tfm/chinese/moe_kai_pfb
   cp -f *.tfm $VARTEXFONTS/tfm/chinese/moe_kai_pfb
   /bin/rm -f *.tfm
done

nwdsungfiles=`/bin/ls $Curdir/nwdsung*.tgz`
for tgzfile in $nwdsungfiles
do
   gunzip -dc $tgzfile|tar xvf -
   cp *.pfb $VARTEXFONTS/type1/fonts
   /bin/rm -f *.pfb
   if test ! -d $VARTEXFONTS/tfm/chinese/nbsmi00lp_pfb; then
      mkdir -p $VARTEXFONTS/tfm/chinese/nbsmi00lp_pfb
   fi
   chmod a+w $VARTEXFONTS/tfm/chinese/nbsmi00lp_pfb
   cp -f *.tfm $VARTEXFONTS/tfm/chinese/nbsmi00lp_pfb
   /bin/rm -f *.tfm
done

#ngbfiles=`/bin/ls $Curdir/nwdgkai.tgz;/bin/ls ../../nuwgsung.tgz`
ngbfiles="$Curdir/nwdgkai.tgz  $Curdir/nuwgsung.tgz"
for tgzfile in $ngbfiles
do
   if test "$tgzfile" = "$Curdir/nuwgsung.tgz"; then
      ngbdir="nuwpstj_pfb"
   else
      ngbdir="ngkai00mp_pfb"
   fi
   gunzip -dc $tgzfile|tar xvf -
   cp -f *.pfb $VARTEXFONTS/type1/fonts
   /bin/rm -f *.pfb
   if test ! -d $VARTEXFONTS/tfm/chinese/$ngbdir; then
      mkdir -p $VARTEXFONTS/tfm/chinese/$ngbdir
   fi
   cp -f *.tfm $VARTEXFONTS/tfm/chinese/$ngbdir
   /bin/rm -f *.tfm
done

#kaifiles=`/bin/ls $Curdir/kaipfb.tgz;/bin/ls ../../nuwgsung.tgz`
kaifiles="$Curdir/kaipfb.tgz $Curdir/lipfb.tgz $Curdir/fspfb.tgz $Curdir/mrpfb.tgz $Curdir/idpfb.tgz"
for tgzfile in $kaifiles
do
   case $tgzfile in
     "$Curdir/kaipfb.tgz") kaidir="ntukai_pfb" ;;
     "$Curdir/lipfb.tgz") kaidir="ntuli_pfb" ;;
     "$Curdir/fspfb.tgz") kaidir="ntufs_pfb" ;;
     "$Curdir/mrpfb.tgz") kaidir="ntumr_pfb" ;;
     "$Curdir/idpfb.tgz") kaidir="id030_pfb" ;;
   esac

   gunzip -dc $tgzfile|tar xvf -
   cp -f *.pfb $VARTEXFONTS/type1/fonts
   /bin/rm -f *.pfb
   if test ! -d $VARTEXFONTS/tfm/chinese/$kaidir; then
      mkdir -p $VARTEXFONTS/tfm/chinese/$kaidir
   fi
   cp -f *.tfm $VARTEXFONTS/tfm/chinese/$kaidir
   /bin/rm -f *.tfm
done



echo "       End of copying pfb files "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
cd $SAVEPWD
} # CopyPfbTtftfmfiles

#####

##### Part10
Modifyttfontsmap(){
##### Modify ttfonts.map or copy it #############
modifyttfontsmap="n"

# if test "$modifyttfontsmap = "y"; then
cp -f chitex612/data/ttfonts.map $texmf/ttfonts.map
if test ! -d $texmf/ttf2pk; then
   mkdir $texmf/ttf2pk
fi
cp -f chitex612/data/ttfonts.map $texmf/ttf2pk/ttfonts.map

$gawkbin '
BEGIN{
#ttfontsmap=texmf "/ttfonts.map"
#while (getline inline < ttfontsmap > 0)
#  {inlinea=inline; inlinea=gsub(" ", "",inlinea)
#   if (inlinea != "")
#   {
#   atpos=index(inline,"@")
#   if (atpos != 0)
#    {++k
#     ttfnamesys[k]=substr(inline,1,atpos-1)
#    }
#   }
#  }
#  ttfnamenosys=k; k=0
#  close(ttfontsmap)
}
{
if (NR==1)
{
ttfontsmap=texmf "/ttfonts.map"
while (getline inline < ttfontsmap > 0)
  {inlinea=inline; inlinea=gsub(" ", "",inlinea)
   if (inlinea != "")
   {
   atpos=index(inline,"@")
   if (atpos != 0)
    {++k
     ttfnamesys[k]=substr(inline,1,atpos-1)
    }
   }
  }
  ttfnamenosys=k; k=0
  close(ttfontsmap)
} # end of if (NR==1)


  inlinea=$0
   gsub(" ","",inlinea)
   if (inlinea != "")
    {
     atpos=index($1,"@")
     if (atpos != 0)
       {++k; 
        ttfname[k]=substr($1,1,atpos-1)
        wholeline[k]=$0
       }
    }
   ttfnameno=k
}
END{
   i=1
   for (i; i<=ttfnameno; i++)
     {k=1; meet=0
      for (k; k<=ttfnamenosys; k++)
        {if (ttfname[i] == ttfnamesys[k])
            {meet=1; break}
        }
      if (meet == 0)
        {gsub("/usr/local/TeX/share/texmf",texmf,wholeline[i])
         gsub("/var/tmp/texfonts",vartexfonts,wholeline[i])
        print wholeline[i] >> ttfontsmap}
     }

}' texmf=$texmf vartexfonts=$VARTEXFONTS chitex612/data/ttfonts.map

if test ! -s $texmf/ttfonts.bak;then
   cp -f $texmf/ttfonts.map $texmf/ttfonts.bak
fi

warning=`egrep "Warning!" $texmf/ttfonts.map `
shouldo=`egrep "After modifying this file, you should give command:" \
   $texmf/ttfonts.map `
if test "$warning" = "" -o "$shouldo" = ""; then
   echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" > appendfiles@
   echo "#                                       " >> appendfiles@
   echo "#  Warning! Warning!              " >> appendfiles@
   echo "#  After modifying this file, you should give command:" >> appendfiles@
   echo "#      chitexconfig -chitex.fdf " >> appendfiles@
   echo "#                                       " >> appendfiles@
   echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> appendfiles@
   echo " "
   echo "# Do not modify the above message" >> appendfiles@
   echo " " >> appendfiles@

   cat $texmf/ttfonts.map >> appendfiles@ 
   cp -f appendfiles@ $texmf/ttfonts.map
   /bin/rm -f appendfiles@
fi

  sed "s#/usr/local/TeX/share/texmf#$texmf#g" $texmf/ttfonts.map >ttfonts.map@
  cp -f ttfonts.map@ $texmf/ttfonts.map
  cp -f ttfonts.map@ $texmf/ttf2pk/ttfonts.map
  /bin/rm -f ttfonts.map@


echo "       End of modifying ttfonts.map files "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
} # End of Modifyttfontsmap


##### Part11 
CopySfdAfm(){
##### copy *.sfd  to $texmf, copy *.afm to $texmf/tex/chinese #####
cp -f chitex612/data/*.sfd $texmf
cp -f chitex612/data/*.sfd $texmf/ttf2pk
cp -f chitex612/data/*.afm $texmf/tex/chinese
} # End of CopySfdAfm


##### Part12
CopyPstfiles(){
cd $SAVEPWD
step5="y"
#if test "$step5" = "y"; then
#if test "$install" = "all"; then
##### copy pst's files 
/bin/rm -rf pst
mkdir pst
cd pst
gunzip -dc ../chitex612/data/pst.tar.gz|tar xvf -
cp -f *.sty *.tex *.con $texmf/tex/chinese
cp -f *.pro $texmf/dvips/base
/bin/rm -f *.sty *.tex *.con *.pro
cd $SAVEPWD
#fi

cd $SAVEPWD
##### update pstricks if tetex version <= .09 #####
echo "yes" > tmp@

   /bin/rm -rf pstricks
   mkdir pstricks
   cd pstricks
   gunzip -dc $SAVEPWD/chitex612/data/pstricks.tgz|tar xvf -
   if test ! -d $texmf/tex/generic/pstricks; then
      mkdir -p $texmf/tex/generic/pstricks
      cp -f * $texmf/tex/generic/pstricks
   fi
   /bin/rm -f *
   cd $SAVEPWD
   rmdir pstricks
} # CopyPstfiles



##### Part13
Backupbinaryfiles(){
##### Backup ttf2tfm, ttf2pk
   if test -s $bindir/ttf2tfm -a ! -s $bindir/ttf2tfm.bak; then 
       cp $bindir/ttf2tfm $bindir/ttf2tfm.bak
   fi
   if test -s $bindir/ttf2pk -a ! -s $bindir/ttf2pk.bak; then 
       cp $bindir/ttf2pk $bindir/ttf2pk.bak
   fi
   if test -s $bindir/ttf2pfb -a ! -s $bindir/ttf2pfb.bak; then 
       cp $bindir/ttf2pfb $bindir/ttf2pfb.bak
   fi


##### Backup binary files
####### backup old dvips and mv dvips to dvips.bin
######  Important
echo "yes" > tmp@
#step22="y"
#if test "$step22" = "y"; then

if test ! -s $bindir/dvips.bin; then
   cp $bindir/dvips $bindir/dvips.bin
   if test ! -s $bindir/dvips.bak; then
      cp $bindir/dvips $bindir/dvips.bak
   fi
fi
######## Backup mktextfm #########
if test "$newtextex" = "yes" -a "$newttf2pk" = "y"; then
  if test ! -s "$bindir/mktextfm.bak"; then
     cp $bindir/mktextfm $bindir/mktextfm.bak
  fi
#     cp -f chitex612/bin/mktextfm $bindir
fi
######## Backup mktexpk #########
if test "$newtextex" = "yes" -a "$newttf2pk" = "y"; then
  if test ! -s "$bindir/mktexpk.bak"; then
     cp $bindir/mktexpk $bindir/mktexpk.bak
  fi
#     cp -f chitex612/bin/mktexpk $bindir
fi
#fi # step5
} # Backupbinaryfiles

##### Part14
Copybinaryfiles(){
##### Copy binary files
##### Expand chitex612/data/ttf2*.$unixname.gz and copy ttf2tfm, ttf2pk
##### ttf2pfb, t1asm to $bindir
#
   echo $echomode "." $echocr       
   gunzip -dc chitex612/data/ttf2tfm.$unixname.gz> ttf2tfm
   chmod +x ttf2tfm
   if test "$unixname" != "linux"; then
      cp -f ttf2tfm $bindir
   else
      usage=`ttf2tfm --help|grep "Usage"`
      if test "$usage" != ""; then
         :
      else
         gunzip -dc chitex612/data/ttf2tfm.linux.new.gz> ttf2tfm         
         chmod +x ttf2tfm 
      fi
      cp -f ttf2tfm $bindir
   fi   
   /bin/rm -f ttf2tfm

   echo $echomode "." $echocr       
   gunzip -dc chitex612/data/ttf2pk.$unixname.gz> ttf2pk
   chmod +x ttf2pk
   if test "$unixname" != "linux"; then
      cp -f ttf2pk $bindir
   else
      usage=`ttf2pk --help|grep "Usage"`
      if test "$usage" != ""; then
         :
      else
         gunzip -dc chitex612/data/ttf2pk.linux.new.gz> ttf2pk
         chmod +x ttf2pk
      fi
      cp -f ttf2pk $bindir
   fi   
   /bin/rm -f ttf2pk

   echo $echomode "." $echocr       
   if test "$unixname" = "linux" -o "$unixname" = "freebsd"; then
      gunzip -dc chitex612/data/ttf2pfb.$unixname.gz> ttf2pfb
      chmod +x ttf2pfb
    if test "$unixname" != "linux"; then
      cp -f ttf2pfb $bindir
    else
      ttf2pfb --help 2> msgtmp
      usage=`grep "Usage" msgtmp`
      if test "$usage" != ""; then
         :
      else
         gunzip -dc chitex612/data/ttf2pfb.linux.new.gz> ttf2pfb
         chmod +x ttf2pfb
      fi
      cp -f ttf2pfb $bindir
    fi   
    /bin/rm -f ttf2pfb
    /bin/rm -f msgtmp
    echo $echomode "." $echocr       
   fi

   if test "$unixname" = "linux" -o "$unixname" = "freebsd"; then
      if test "$unixname" != "linux"; then
         cp $SAVEPWD/chitex612/data/t1asm.$unixname $bindir/t1asm
      else
         t1asm --help 2> msgtmp
         usage=`grep "Usage" msgtmp`
         if test "$usage" != ""; then
            :
         else
            cp $SAVEPWD/chitex612/data/t1asm.linux.new t1asm
            chmod +x t1asm
         fi
         cp -f t1asm $bindir
         /bin/rm -f t1asm
         /bin/rm -f msgtmp
      fi
    echo $echomode "." $echocr       
   fi

######
###### Copy chitex612/bin/* to $bindir 

/bin/rm -rf chitex612/bin/temp

# Copy chitex612/bin/* to $bindir
cp -f chitex612/bin/* $bindir

cp -f chitex612/data/chitran0.awk $bindir
cp -f chitex612/data/chitran1.awk $bindir

sed "s#chitran0.awk#$bindir/chitran0.awk#g" $bindir/chitranc > chitranc@
sed "s#chitran1.awk#$bindir/chitran1.awk#g" chitranc@ > chitranc@@
mv -f chitranc@@ $bindir/chitranc


if test "$unixname" != "linux"; then
   /bin/rm -f $bindir/ttf_edit $bindir/genttf
fi
} # Copybinaryfiles


##### Part15
Compileandcopycfiles(){
cd $SAVEPWD
# In the following it will compile '.c' files
# first copy $SAVEPWD/chitex612/data/*.c to  $SAVEPAWD and
# then unzip $SAVEPAWD/chitex612/data/cfile.tgz which have '.c' files
# into $SAVEPWD 
#


# Find 'gcc' and use 'cc' if not have 'gcc'
#

gccdir=`which gcc`
gccloc=`which gcc|grep ":"`

if test "$gccloc" = ""; then
  gawkloc=`which gawk|grep "no gcc"`
fi
if test "$gccloc" != ""; then
   CC="cc"
else
   CC="gcc"
fi


# Start to compile 'chitran0.c',...
# and move the resulted files into $bindir and its original place
# First it will compile awka
  gunzip -dc $SAVEPWD/chitex612/data/awka.tgz |tar xvf -
  cd awka-0.3b-50
  configure
  make
  make install
  cp -f $SAVEPWD/awka-0.3b-50/lib/libawka.a .
  cp -f $SAVEPWD/awka-0.3b-50/lib/libawka.h .
#  /bin/rm -rf awka-0.3b-50

echo "       End of compiling awka"
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
  

mkdir .temp00$$
cd .temp00$$
mv ../libawka.* .
cp -f $SAVEPWD/chitex612/data/*.c .
gunzip -dc $SAVEPWD/chitex612/data/cfile.tgz|tar xvf -
#cp -f chitran1.awk $texmf/tex/chinese
cp -f $SAVEPWD/chitex612/data/chitran3a.awk $texmf/tex/chinese


#step6="y"
#if test "$step6" = "y"; then
for cmd in chitran00 chitran01 chitran0 chitran1 chitran2 chitran3 chitran3a chitran4 \
   chitran5 chitran5b chitran6 chitran7 chicodelistc  \
   chitranp1 chitranp2 chitranp3 chitexsub1
do 
   eval $CC $cmd".c" -I./ -L./ -lawka -lm -o $cmd
   eval cmddir=\$$cmd"dir"
   if test "$cmddir" != ""; then 
      eval cp -f $cmd \$$cmd"dir"
      eval echo "cp -f $cmd" \$$cmd"dir"
   else
      eval echo "cp -f $cmd $bindir"
   fi
   
   eval cp -f $cmd $bindir
done

#fi  # end of step6


for cmd in chicode chifilt chadd chaddpfbname chicoder
do 
   $CC -o $cmd $cmd".c" 
   eval cmddir=\$$cmd"dir"
   if test "$cmddir" != ""; then 
      eval cp -f $cmd \$$cmd"dir"
      eval echo "cp -f $cmd" \$$cmd"dir"
   else
      eval echo "cp -f $cmd $bindir"
   fi
   eval cp -f $cmd $bindir
#   /bin/rm -f $cmd
done

/bin/rm -f *.c


cd $SAVEPWD

rm -rf .temp*

#cd $SAVEPWD
cd $bindir
chmod +x *
chmod +s chaddpfbname
chmod +s chadd

echo "       End of compiling some c files and copy to $bindir"
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
} # End of Compileandcopycfiles




##### Part16
CopyTeXfdfpngstyfiles(){
cd $SAVEPWD

##### copy (update) *.tex files #######

chmod +w $texmf/tex/chinese/*.fdf
chmod +w $texmf/tex/chinese/*.tex
chmod +w $texmf/tex/chinese/*.png
#chmod +w $texmf/tex/chinese/*.pdf
chmod +w $texmf/tex/chinese/*.bib
chmod +w $texmf/tex/chinese/*.sty
cp -f chitex612/tex/* $texmf/tex/chinese

if test ! -d $SAVEPWD/tex; then
   mkdir $SAVEPWD/tex
fi

cp -f chitex612/tex/math*.tex $SAVEPWD/tex
cp -f chitex612/tex/samp*.tex $SAVEPWD/tex
cp -f chitex612/tex/char*.tex $SAVEPWD/tex
cp -f chitex612/tex/*.png $SAVEPWD/tex
#cp -f chitex612/tex/*.pdf $SAVEPWD/tex
cp -f chitex612/tex/*.bib $SAVEPWD/tex

chmod a-w $texmf/tex/chinese/*.fdf
chmod a-w $texmf/tex/chinese/*.tex
chmod a-w $texmf/tex/chinese/*.png
#chmod a-w $texmf/tex/chinese/*.pdf
chmod a-w $texmf/tex/chinese/*.bib
chmod a-w $texmf/tex/chinese/*.sty


echo "       End of copy files to $texmf/tex/chinese "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey                         
fi
} # CopyTeXfdfpngstyfiles


##### Part17
Configurescfilechitexfdf(){
##### Configure chitex, chitexdvi,... ######
##### A. Make .../texmf/tex/chinese/chitex.fdfile from
#####     .../texmf/tex/chinese/chitex.fdf 
##### B. Configure chitex, chitexdvi,... such that TEXMF, VARTXFONTS
#####    to be set to speed the excution of those scripts chitex,.....

   chitexconfig -scfile
   chitexconfig -chitex.fdf
   /bin/rm -f chitex*.fdfile
} # Configurescfilechitexfdf


##### Part18
Updatepsfontsmappdftexmap(){
##### Update psfonts.map, pdftex.map ######

if test -s $texmf/dvips/config/psfonts.map; then
wafonts=`grep "%Wafonts begin" $texmf/dvips/config/psfonts.map`
wafonts=`grep "%Wafontsa begin" $texmf/dvips/config/psfonts.map`
if test "$wafonts" = ""; then
   cat chitex612/data/pfbadd.map >> $texmf/dvips/config/psfonts.map
fi
if test "$wafontsa" = ""; then
   cat chitex612/data/pfbadda.map >> $texmf/dvips/config/psfonts.map
fi
fi

if test -s $texmf/dvips/base/psfonts.map; then
wafonts=`grep "%Wafonts begin" $texmf/dvips/base/psfonts.map`
wafontsa=`grep "%Wafontsa begin" $texmf/dvips/base/psfonts.map`
if test "$wafonts" = ""; then
   cat chitex612/data/pfbadd.map >> $texmf/dvips/base/psfonts.map
fi
if test "$wafontsa" = ""; then
   cat chitex612/data/pfbadda.map >> $texmf/dvips/config/psfonts.map
fi
fi

wafonts=`grep "%Wafonts begin" $texmf/dvips/config/pdftex.map`
wafontsa=`grep "%Wafontsa begin" $texmf/dvips/config/pdftex.map`
if test "$wafonts" = ""; then
  cat chitex612/data/pdfadd.map >> $texmf/dvips/config/pdftex.map
fi
if test "$wafontsa" = ""; then
  cat chitex612/data/pfbadda.map >> $texmf/dvips/config/pdftex.map
fi


echo "       End of update  psfonts.map, pdftex.map "
if test "$stopask" = "y"; then
   echo $echomode "       Press any key to continue " $echoc
   read inkey
fi
##### end of Update psfonts.map, pdftex.map 
} # Updatepsfontsmappdftexmap



##### Part19
Set600dpi(){
###### set 600dpi for xdvi and dvips 
set600
} # End of Set600dpi


##### Part20
Modifylyx(){
##### Modify configure of lyx #########
rm -f config@
mkdir tmp
cd tmp
clyx=`which clyx`

if test "$clyx" != ""; then   #1st if
echo " "
echo "    Find  .../lyx/lyxrc.defaults                                     #"
echo "    Wait!            "
echo " "

   find /usr -path '*/lyx/lyxrc.defaults' > config@

if test -s config@; then   # 2nd if
lyxdir=`
awk '
BEGIN{}
{
  lyxrc=index($0,"/lyxrc.defaults")
  if (lyxrc > 0)
     {print substr($0,1,lyxrc-1)}
}
END{} ' config@ `

   echo "lyxdir=$lyxdir found"

if test ! -s $lyxdir/configure.bak; then
   cp -f $lyxdir/configure  $lyxdir/configure.bak
fi

modify=`
awk '
BEGIN{}
{
  if (modify==0) 
     {modify=index($0, "Modify to use with chitex")}
}
END{
  if (modify>0)
     {print "yes"}
} ' $lyxdir/configure `

rm -f tmp@

if test "$modify" = ""; then  # 3rd if
awk '
BEGIN{}
{ 
  inline=$0
  llen=length($0)
  haveexit=""
  if (index(inline,"exit") > 0)
     {for (i; i<=llen; i++)
          {ch=substr(inline,i,1)
           if (ch == " ")
              {continue}
           else
              {inlinenew=inlinenew ch}
          }
      if (inlinenew=="exit")
         {haveexit="y"}
     }
  if (haveexit == "")
     {print $0 > "tmp@"}
}
END{ } ' $lyxdir/configure

cat $SAVEPWD/chitex612/data/configure.add >> tmp@
cp -f tmp@ $lyxdir/configure
chmod +x $lyxdir/configure

 echo  'configure'  "has been modified"
 echo "The old  $lyxdir/configure have been backup as $lyxdir/configure.bak"

fi  # endof if test "$modify@: ...

else
   echo "can not find  lyxrc.defaults"
fi # end of if test "$config@" ...
fi # end of if test "$clyx" ...

if test "$clyx" != ""; then
##### Modify clyx ######
havecheck=`grep "check lyxrc.defaults" $clyx`

rm -f tmp@

 if test "$havecheck" = ""; then 

awk '
BEGIN{}
{ inline=""
  lyx=index($0,"lyx")
  if (lyx>0)
  {
    llen=length($0)
    i=1
    for (i; i<=llen; i++)
      {ch=substr($0,i,1)
       if (ch !=" ")
          {inline=inline ch}
      } 
  }

  if (lyx>0 && inline=="lyx")
     {
      print "# check lyxrc.defaults" > "tmp@"
      print "if test -s $HOME/.lyx/lyxrc.defaults; then" > "tmp@"
      print "   chilatexb=\`grep \"\\\\latex_command \\\"chilatexb\\\"\" $HOME/.lyx/lyxrc.defaults\`" > "tmp@"
      print "   usecjk=\`grep \"\\\\use_cjk\" $HOME/.lyx/lyxrc.defaults\`" > "tmp@"
      print "   if test \"$chilatexb\" = \"\" -o \"$usecjk\" != \"\"; then " > "tmp@"
      print "      rm -rf $HOME/.lyx" > "tmp@"
      print "   fi" > "tmp@"
      print "else" > "tmp@"   
      print "   rm -rf $HOME/.lyx" > "tmp@"
      print "fi" > "tmp@"
     }
  print $0 > "tmp@"    
}
END{}' $clyx

 cp tmp@ $clyx
 fi  # if test "$havecheck" = ""; then  
fi # 
##### end of modify configure for clyx #####
cd ..
} # End of Modifylyx


##### Part21
TexhashandRmtmpfiles(){ 
###### Must 'texhash' to take effect for all TeX setting
texhash

########### The main setup has been  completed ############


/bin/rm -f *.c
/bin/rm -rf temp
/bin/rm -rf tfm
if test -s gawk; then
   /bin/rm -f gawk
fi
/bin/rm -f *@
/bin/rm -rf fonts pfb pk pst tmp
/bin/rm -rf chitex612/bin/temp.*

#fi # testmessage
} # End of TexhashandRmtmpfiles(){ 


##### Part22
MakeVARTEXFONTSsubdir(){
##### Make VARTEXFONTS/tfm/chinese, VARTEXFONTS/type1/fonts writable 
chmod a+w $VARTEXFONTS/tfm/chinese
chmod a+w $VARTEXFONTS/type1/fonts
if test -f $VARTEXFONTS/ttf; then
   rm -f $VARTEXFONTS/ttf 
fi
if test ! -d $VARTEXFONTS/ttf; then
   mkdir $VARTEXFONTS/ttf 
   chmod a+rw $VARTEXFONTS/ttf
fi
} # End of MakeVARTEXFONTSsubdir



##### Part23
GiveMessage(){
############# The messages of end of upadte ##################
message ( ) {
menua (){
clear
echo " "
#echo " "
#echo "        If there are no error messages, then "
if test "$install" = "update"; then
   echo "        The 'update' must have been completed."
else
   echo "        The 'installation' must have been completed."
fi
echo " "
echo "        s ChiTeX UCs\:"
echo "        1. iΤɦW TeX/LaTeX "
echo "        2. ѷs mymacros.tex i input ӳ]wۤvߦn"
echo "           Bѷs samp2*.tex դ"
echo "        3. T䴩 ϥΤ chapterbib.sty  bibunits.sty"
echo "           BѴժҤl 'ƾ_bibunits.tex', ƾ_chapbib.tex"
echo "        4. ץ cmakeindex  ץ[j cbibtex Ϥ key ]QT sort"
echo "           Ӧb \"alpha\"  style  author-year  key ]ABz"
echo "        5. iTaϥΩҦw verbatim O"
echo "        6. Modify \"camsbook.sty\" ϥ \\documentclass{amsbook} "
echo "           T title"
echo "        7. 䴩 pdftex/pdflatex, Ϊkp: chitex -pdftex math2_pdftex"
echo "           䤤 math2_pdftex.tex it pdftex O(Ԩ README.use* )"
echo "        8. Could have chinese \"boldface(r)\" and \
 \"outline(Ŧr)\","
echo "           if one use chinese Adobe type1 fonts commands."
echo "        9. Could have fascinating \"grading color's \
 effects(C⺥hĪG)\""
echo "        More ....."
echo " "

echo $echomode "        Press enter key to continue" $echocr
read inkeya


menubno="y"
while test "$menubno" = "y"
do
    menub
    case $inkeyb in
    "b") menubno="n"; menuano="y";;
    "c") menubno="n"; menuano="n";;
      *) menubno="y";;
    esac
done
}

menub (){
clear
echo "       10. @ script { c y, t״@@HW()"
echo "       11. Ѧ cbibtex, cmakeindex  kay b \\label, "
echo "           \\cite, \\ref ϥΪҤl: mabibind.tex Ѵ"
echo "           (iUO chitex -chitexhelp Ūδ mabibind.tex)"
echo "       12. iϥ  Adobe Type1 r, Linux P FreeBSD\
 tΪ̥ib compile"
echo "           ɹ Big5  GB sX TrueType rβͥn\
 Adobe Type1"
echo "           r(ϥ ttf2pfb). Linux tΪ̥i۰ʱN unicode sX\
 TrueType"
echo "           r, ഫX Big5  GB sX TrueType r"
echo "       11. wˮɤ~ compile X fmt , iKLktX teTeX "
echo "           ]ѵ{ chitexconfig iΨӭs configure A ChiTeX "
echo "           ]ibAw ChiTeX , As teTeX , ӦAsA teTeX "
echo "           s, UO:"
echo "               chitexconfig -makefmt"
echo "           is chitex*.fmt  pdfchitex*.fmt ӨϯtXs teTeX"

echo " "
echo "        Give me messages if have problems"
echo "        "
echo "        Hung-Yih Chen ()"
echo "        Dept. of Mathematics, Nat'l Central Univ."
echo "        email: yih@math.ncu.edu.tw " 
echo " "
echo "        Type \"b\" to back"
echo "        or Type \"c\" to continue"
echo $echomode "        : " $echocr
read inkeyb
}

#menuno="y"
menuano="y";menubno="y"
#while test "$menu" = "y"
#do
  while test "$menuano" = "y"
  do
     menua
  done  
#done

submenuc (){
clear
echo " "
echo " "
   echo "        *** Is message ФUO:  message "
   echo " "
   echo "        bɭ, nnDDn chitex O, unUO:"
   echo "               chitex"
   echo " "
   echo "        bɭ, nDiϥΤؤrΫO, unUO:"
   echo "               chitex -fonts "
   echo " "
   echo "        bɭ, pn \"թҪ TeX Ҥl\", \"DU(help)\","
   echo "        \"\Ū README* \", unUO:"
   echo "               chitex -chitexhelp"
   echo " "
   echo "        {bAiH \"\Ūδ TeX Ҥl\", \"DU\"  \
\"\""
   echo " "
   echo "        1. \Ūδ TeX Ҥl"
   echo "        2. DU( ChiTeX) "
   echo "        b. ^̫e "
   echo "        q. "
   echo "     "
   echo $echomode "        Type '1', '2', 'b' or 'q' to choose one of the above: " $echocr
   
   read inkeyc
#   export inkeyc
}

submenucno="y"
while test "$submenucno" = "y"
do
  submenuc 
   case "$inkeyc" in
      "1")  chitexhelp -big5 -test; submenucno="y";;
      "2")  chitexhelp; submenucno="y" ;;
      "b")  messageno="y"; submenucno="n";;
      "q")  messageno="n"; submenucno="n"
           echo " "
           echo "        At any time give command:"
           echo "               chitex -help"
           echo "        to get help "
           echo " "
           exit;;
        *) messageno="n"; submenucno="y";;
   esac            
done
}


messageno="y"

while test "$messageno" = "y";     
  do
   message
  done

exit
} # End of GiveMessage


Setenvironment                 # Part1
FindUnixSystem                 # Part1.1
WelcomeandVerifysupuser        # Part2
FindpathofFmtTexmfVartexBinary # Part3
ModifyReadme                 # Part4
MakeChineseFmt               # Part5
Copytfmppkttf                # Part6
ModifyConfigps               # Part7
ModifySpecialmap             # Part8
CopyPfbTtftfmfiles           # Part9
Modifyttfontsmap             # Part10
CopySfdAfm                   # Part11
CopyPstfiles                 # Part12
Backupbinaryfiles            # Part13
Copybinaryfiles              # Part14

Compileandcopycfiles         # Part15
CopyTeXfdfpngstyfiles        # Part16
Configurescfilechitexfdf     # Part17
Updatepsfontsmappdftexmap    # Part18
Set600dpi                    # Part19
Modifylyx                    # Part20
TexhashandRmtmpfiles         # Part21
MakeVARTEXFONTSsubdir        # Part22
GiveMessage                  # Part23

cd $SAVEPWD

exit

