#!/bin/sh

#   Curdir=`cd ../;pwd`
#   Curdir="/c/unix/6.1.2p10"


  tmpdir=`cat /tmp/tmpdir@`
  export tmpdir

  if test "$tmpdir" = ""; then
     tmpdir=`cd ../;pwd`
  else
     cd $tmpdir
  fi


##### Ϊ 23 (parts) ÿһһ function ִ
#     еڶ column Ϊ function 
#     ȶ 25 functions Ȼһִ 25 functions?
# Setenvironment                 # Part1
# FindUnixSystem                 # Part1.1

# FindpathofFmtTexmfVartexBinary # Part3
# ModifyReadme                 # Part4
# InstallAcrobatReader         # Part5.1
# MakeChineseFmt               # Part5.2

# 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
# InstallLatex2html            # Part19.1
# 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

SAVEPWDOLD=`pwd`


gawkbin=awk
export gawkbin

#  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 "        unix ϵͳΪ:"
echo " "
echo "       1. Linux"
echo "       2. FreeBSD"
echo "       3. Solaris"
echo "       4. SunOS"
echo "       q. ֹͣװ"
echo " "
echo $echomode "         Ӧ 1,..,4  q ֹͣװ: "$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" -a "$unixname" != "freebsd"; then
 if test "$unixname" != "linux"; then
   if test "$unixname" = "freebsd"; then
     cp chitex612/data/gawk.freebsd gawk
   else
     cp chitex612/data/gawk."$unixname".gz .
     gunzip gawk."$unixname".gz
   fi
   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" -o "$unixname" = "freebsd"; then
   gawkbin=awk
   export gawkbin
#fi
}



##### Part2
WelcomeandVerifysupuser(){

welcomea=""
if test "$welcomea" = "y"; then
#######    Welcome      #######
clear
echo " ";echo " ";echo " "
echo " "
echo "         ******************************************"
echo "         *                                        *"
echo "         *   ӭʹ  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.2p12mh, Sep. 23, 2005"
echo " "
echo $echomode "         Press enter key to continue "$echocr
read inkey
fi

clear
echo " ";echo " "
echo " "
echo "         *************************************************"
echo "         *                                               *"
echo "         *   װʽΪ㰲װ 6.1.2p12mh             *"
echo "         *                                               *"
echo "         *   װ ChiTeX6.1.2p12mh ֮ǰ, ϵͳ  *"
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 "         *   㲻 super user \"root\"                 *"   
echo "         *    teTeX Ŀ¼дȨ,   *"
echo "         *   ޷аװ                             *"
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo "             c. װ"
echo "             q. ֹͣװ" 
echo " "
echo $echomode "          '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 "         *   ڽʼװ  6.1.2p12mh           *"
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.2p12mh, Sep. 23, 2005"
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 #
#

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

fmt=$TEXMF/web2c
fmtname=$TEXMF/web2c
texmf=$TEXMF

#echo "         fmtdir=$fmt found"
#echo "         texmf=$texmf found"

export TEXMF

if "$texmffound" = ""; then
# (2) find path of TEXMF #
#
#kpsexpand=`which kpsexpand`

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




texmf=$texmfdir

# echo "         texmfdir=$texmfdir found"

# read xxxx

fi

# (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"

if test ! -d /usr/local/bin; then
   mkdir /usr/local/bin
fi
#if test ! -s /usr/local/bin/gawk; then

   if test "$unixname" = "freebsd"; then
     cp $SAVEPWDOLD/chitex612/chitex612/data/gawk.freebsd /usr/local/bin/gawk
#      cp -f $SAVEPWDOLD/gawk /usr/local/bin
      chmod +x /usr/local/bin/gawk
      mv -f /usr/bin/gawk /usr/bin/gawk.bak
      awk '{system("rehash")}' tmp@
   fi
#fi


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

checkmenu="y"
while test "$checkmenu" = "y"
do
clear
echo "         Ѳ⵽Ŀ¼:  "
echo "         1.  texmf Ŀ¼ (һΪ /usr/.../texmf) Ϊ:"
echo "               $texmf"
echo "         2.  *.fmt ڵĿ¼ (һΪ /usr/.../texmf/web2c) Ϊ:"
echo "               $fmt"
echo "         3.  .../texfonts Ŀ¼ (һΪ /var/tmp/texfonts "
echo "           /var/lib/texmf  /var/texfonts кĿ¼ tfm, pk ) Ϊ:"
echo "               $VARTEXFONTS"
echo "            ޴Ŀ¼, ܱ run 'texmfconfig' ȥ趨Ŀ¼"
echo "         4.  ִе latex ڵĿ¼Ϊ: "
echo "               $bindir "
echo " "
echo "         ϲ⵽Ŀ¼ǿĿ¼ȷ. "
echo "         "
echo "         ȫȷ "y" ,  '1'  '2',.. "
echo "         (ԱĿ֮Ŀ¼)"
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 ڵĿ¼Ϊ($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 "         ִе latex ڵĿ¼Ϊ($bindir)"
       echo $echomode "         :" $echocr
       read keydir
       if test "$keydir" != ""; then 
          bindir=$keydir
       fi
       ;;
   "y") 

   checksubmenu="y"
   while test "$checksubmenu" = "y"
   do
     echo " "
     echo "         ˵Ŀ¼ȫȷ."
     echo "         ټ "y"  ȷ,  'n' "
     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 "         ְװʽ:"
echo "         1.  ͣʷʽ"
echo "         2.  װ׵ķʽ(ͣ² fmt files)"
echo "         "
echo "          '1'  '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 $tmpdir/chitex612


SAVEPWD=`pwd`


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

if test ! -d $tmpdir/chitex612/docdir; then
   mkdir $tmpdir/chitex612/docdir
fi

 cd $tmpdir/chitex612/chitex612/doc

 NOWPWD="$tmpdir/chitex612"


# Move *.ps to ../ for the time being since we need not modify them
mv -f  *.ps $tmpdir/chitex612/docdir

docfiles=`/bin/ls `

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


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

cd $tmpdir/chitex612

# 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 -R +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 "         *            ʼװ, Ժ              *"
#echo "         *                                        *"
#echo "         ******************************************"
#echo " "
}  #End of ModifyReadme



##### Part5.1
InstallAcrobatReader(){
#if test "$unixname" = "linux" -o "$unixname" = "solaris"; then
rootmessage="y"
while test "$rootmessage" = "y" 
do
clear
echo " ";echo " "
echo " "
echo "         **********************************************"
echo "         *                                            *"
echo "         *   潫װ Acrobat Reader 5.0.6          *"   
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo "             c. װ Acrobat Reader 5.0.6"
echo "             q. ֹͣװ Acrobat Reader 5.0.6" 
echo " "
echo $echomode "          'c'  'q' : "$echocr
read inkey

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

#cd $Curdir

PWDDIR=$tmpdir 


  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" -o "$unixname" = "solaris"; then
       if test "$unixname" = "linux"; then
         gunzip -dc $Curdir/linux-506.tgz|tar xvf -
       else
         gunzip -dc $Curdir/solaris-506.tgz|tar xvf -
       fi
        ./INSTALL
       gunzip -dc $Curdir/chtfont.tgz|tar xvf -
       cd CHTKIT
        ./INSTLANG
     else
        cd /usr/local
        gunzip -dc $Curdir/acroread-5.08.tgz|tar xvf -
        gunzip -dc $Curdir/acroread5-commfont-2002.5.tgz|tar xvf -
        gunzip -dc $Curdir/zh-acroread5-chtfont-2002.5.tgz|tar xvf -
        echo "yes" > tmp@
        awk '{system("rehash")} ' tmp@
     fi
  fi

  cd $tmpdir/chitex612  

clear
echo " ";echo " "
echo " "
echo "         **********************************************"
echo "         *                                            *"
echo "         *                    Wait!                   *"   
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo " "
#echo $echomode "                                                         "$echocr
#read inkey

 if test ! -s /usr/local/Acrobat5/bin/acroread; then  

  find /usr -name 'Acrobat5' > acroread@

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

  acroreaddir=`cat acro@`
 else
  acroreaddir="/usr/local/Acrobat5"
 fi

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

#fi
}


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

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


#if test "$fmtdate" = ""; then
#  date --set "1998/01/01"
#fi 

### To enlarge memory 
if test -s $texmf/web2c/texmf.cnf; then
$gawkbin '
BEGIN{}
{ per=index($0,"%")
  rline=""
  if (per > 0)
     {inline=substr($0,1,per-1)
      rline=substr($0,per,length($0)-per+1)
     }
  else
     {inline=$0}

  if (inline != "")
     {gsub(/ /,"",inline)}

  mempos=index(inline,"main_memory=")
  if (mempos == 1)
     {print "% " $0 > "texmfcnf@"
      print "main_memory = 20000000 " rline > "texmfcnf@"}
  else
     {print $0 > "texmfcnf@"}
}
END{} ' $texmf/web2c/texmf.cnf
fi

   cp -f texmfcnf@ $texmf/web2c/texmf.cnf
   rm -f texmfcnf@

### Recompile latex,...

   latex -ini latex.ltx
   pdfinitex latex.ltx
   mv latex.fmt pdflatex.fmt
   pdfinitex plain \\dump
   mv plain.fmt pdftex.fmt
   cp -f *.fmt $texmf/web2c
   rm -f *.fmt
   initex latex.ltx
   initex plain \\dump
   cp -f *.fmt $texmf/web2c
   rm -f *.fmt

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

# 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 

### Renew date
#  date --set $nowdate 2> /dev/null>/dev/null
#  date --set $nowtime 2> /dev/null>/dev/null

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


  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

#  cp -f $Curdir/chitex/612/chitex612/data/wumap2.tex $texmf/tex/chinese
#  cp -f $Curdir/chitex/612/chitex612/data/bcwumap.tex $texmf/tex/chinese

# 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

 

 cd /tmp
 mkdir .tmp7675
 cd .tmp7675 
 gunzip -dc $tmpdir/chitex612/chitex612/data/ctexl.tgz|tar xvf -  2>&2 >/dev/null 
 latex -ini \&latex ttl "\dump" 
 
# echo "after latex"
# read xxxxxx
 
 
 mv -f ttl.fmt $texmf/web2c/chitexl.fmt
 latex -ini  \&latex ttlg "\dump" 
 mv -f ttlg.fmt $texmf/web2c/chitexlg.fmt
 pdftex -ini \&pdflatex ttl "\dump" 
 mv -f ttl.fmt $texmf/web2c/pdfchitexl.fmt
 pdftex -ini \&pdflatex ttlg "\dump" 
 mv -f ttlg.fmt $texmf/web2c/pdfchitexlg.fmt
 /bin/rm -f *.tex *.log *.fdfile *@

 gunzip -dc $tmpdir/chitex612/chitex612/data/ctex.tgz|tar xvf -  2>&2 >/dev/null 
 tex -ini \&plain chitex "\dump" 
 mv -f chitex.fmt $texmf/web2c
 tex -ini \&plain chitexg "\dump" 
 mv -f chitexg.fmt $texmf/web2c
 pdftex -ini \&pdftex chitex "\dump" 
 mv -f chitex.fmt $texmf/web2c/pdfchitex.fmt
 pdftex -ini \&pdftex chitexg "\dump" 
 mv -f chitexg.fmt $texmf/web2c/pdfchitexg.fmt
 /bin/rm -f *.tex *.log *.fdfile *@
 cd ../
 /bin/rm -rf /tmp/.tmp7675

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

# Test 

echo "yes">tmp@

echo $texmf/tex/chinese/chitex.fdf

   chmod +w $texmf/tex/chinese/chitex.fdf

   

   cp $tmpdir/chitex612/chitex612/data/chitex.fdf $tmpdir/chitex612/chitex612/tex/chitex.fdf
   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 "\134nonstopmode" > "t1.tex"
print "\134makeatletter" > "t1.tex"
print "\134def\134ch@delay{y}" > "t1.tex"
print "\134makeatother" > "t1.tex"
print "\134documentclass{article}" > "t1.tex"
print "\134begin{document}" > "t1.tex"
print "test" > "t1.tex"
print "\134end{document}" > "t1.tex"
}
END{} ' tmp@

  echo "cp $fmtname/chitexl.fmt" 
  
  cp  $fmtname/chitexl.fmt .
  
#  read xxxxxxx
  
  latex \&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/pk/chinese/moe_kai \
  fonts/tfm/chinese/moe_sung fonts/pk/chinese/moe_sung fonts/tfm/chinese/wafonts
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

gunzip -dc $Curdir/wutfm.tgz|tar xvf -
cp -f *.tfm $texmf/fonts/tfm/chinese/wafonts
/bin/rm -f *.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  # Delete 9/20/2005
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
   rm -rf $texmf/fonts/type1/chinese
fi
   mkdir -p $texmf/fonts/type1/chinese
   ln -s $VARTEXFONTS/type1/fonts $texmf/fonts/type1/chinese/fonts \
       2>&2 >/dev/null


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`
moekaifiles=`/bin/ls $Curdir/moe?pfb.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

   if test ! -d $VARTEXFONTS/tfm/chinese/moek; then
      mkdir -p $VARTEXFONTS/tfm/chinese/moek
   fi
   if test ! -d $VARTEXFONTS/tfm/chinese/moes; then
      mkdir -p $VARTEXFONTS/tfm/chinese/moes
   fi

   chmod a+w $VARTEXFONTS/tfm/chinese
   chmod a+w $VARTEXFONTS/tfm/chinese/moek
   chmod a+w $VARTEXFONTS/tfm/chinese/moes
#   cp -f *.tfm $VARTEXFONTS/tfm/chinese/moek
#   /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 $Curdir/wumpfb.tgz $Curdir/wufpfb.tgz $Curdir/wuhpfb.tgz $Curdir/msl.tgz $Curdir/mbf.tgz $Curdir/mbs.tgz $Curdir/moeksl.tgz $Curdir/moekbf.tgz $Curdir/moekbs.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/kaipfb.tgz") kaidir="ntuk" ;;
     "$Curdir/lipfb.tgz") kaidir="ntul" ;;
     "$Curdir/fspfb.tgz") kaidir="ntuf" ;;
     "$Curdir/mrpfb.tgz") kaidir="ntur" ;;
#     "$Curdir/idpfb.tgz") kaidir="id030_pfb" ;;
     "$Curdir/wumpfb.tgz") kaidir="wafonts" ;;
     "$Curdir/wufpfb.tgz") kaidir="wafonts" ;;
     "$Curdir/wuhpfb.tgz") kaidir="wafonts" ;;
     "$Curdir/msl.tgz") kaidir="msl" ;;
     "$Curdir/mbf.tgz") kaidir="mbf" ;;
     "$Curdir/mbs.tgz") kaidir="mbs" ;;
     "$Curdir/moeksl.tgz") kaidir="moeksl" ;;
     "$Curdir/moekbf.tgz") kaidir="moekbf" ;;
     "$Curdir/moekbs.tgz") kaidir="moekbs" ;;
   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
   if test "$kaidir" != "wafonts"; then
      cp -f *.tfm $VARTEXFONTS/tfm/chinese/$kaidir
      /bin/rm -f *.tfm
   fi
done

   cd $texmf

#   gunzip -dc $Curdir/tfm.tar.gz |tar xvf -

   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
if test ! -d $texmf/fonts/afm/chinese/wafonts; then
   mkdir -p $texmf/fonts/afm/chinese/wafonts
fi
gunzip -dc $Curdir/wuafm.tgz|tar xvf -
cp -f *.afm $texmf/fonts/afm/chinese/wafonts
rm -f *.afm
} # 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

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


cp -f chitex612/bin/chitranc $chitrancdir

cp -f chitex612/data/chitran0.awk $chitrancdir
cp -f chitex612/data/chitran1.awk $chitrancdir
cp -f chitex612/data/chitran11.awk $chitrancdir


sed "s#chitran0.awk#$bindir/chitran0.awk#g" $chitrancdir/chitranc > chitranc@
sed "s#chitran1.awk#$bindir/chitran1.awk#g" chitranc@ > chitranc@@
sed "s#chitran11.awk#$bindir/chitran11.awk#g" chitranc@@ > chitranc@@@
sed "s#chitran3a.awk#$bindir/chitran3a.awk#g" chitranc@@@ > chitranc@

sed "s#chitran1.awk#$bindir/chitran1.awk#g" $chitrancdir/chitranpc > chitranpc@
sed "s#chitran11.awk#$bindir/chitran11.awk#g" chitranpc@ > chitranpc@@
sed "s#chitranp1.awk#$bindir/chitranp1.awk#g" chitranpc@@ > chitranpc@

mv -f chitranc@ $chitrancdir/chitranc
mv -f chitranpc@ $chitrancdir/chitranpc

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

cp -f $SAVEPWD/chitex612/data/chitran3a.awk $bindir
cp -f $SAVEPWD/chitex612/data/chitran11.awk $bindir
cp -f $SAVEPWD/chitex612/data/chitranp1.awk $bindir

#step6="y"
#if test "$step6" = "y"; then
for cmd in chitran00 chitran01 chitran0 chitran1 chitran2 chitran3 chitran4 \
   chitran5 chitran5b chitran6 chitran7 chicodelistc  \
   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

echo after compile c


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
cp -f chitex612/data/chitex.fdf $texmf/tex/chinese/chitex.fdf


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 And cp cwtex.enc ######

if test -d $texmf/fonts/enc; then
   cp -f $tmpdir/chitex612/chitex612/data/cwtex.enc $texmf/fonts/enc/dvips/tetex
else
   cp -f $tmpdir/chitex612/chitex612/data/cwtex.enc $texmf/dvips/base
fi   

if test -s $texmf/dvips/config/psfonts.map; then
wafonts=`grep "%Wafonts begin" $texmf/dvips/config/psfonts.map`
wafontsa=`grep "%Wafontsa begin" $texmf/dvips/config/psfonts.map`
wafontsb=`grep "%Wafontsb begin" $texmf/dvips/config/psfonts.map`

$gawkbin '{ntupos=index($0,"ntu"); pfbh=index($0,"_pfb")
   if (ntupos == 0 || pfbh == 0)
      {print $0 > "psfonts@"}
}
' $texmf/dvips/config/psfonts.map

cp -f "psfonts@" $texmf/dvips/config/psfonts.map
rm -f "psfonts@" 

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
if test "$wafontsb" = ""; then
   cat chitex612/data/pfbaddb.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`
wafontsb=`grep "%Wafontsb 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/base/psfonts.map
fi
if test "$wafontsb" = ""; then
   cat chitex612/data/pfbaddb.map >> $texmf/dvips/base/psfonts.map
fi
fi


$gawkbin '{ntupos=index($0,"ntu"); pfbh=index($0,"_pfb")
   if (ntupos == 0 || pfbh == 0)
      {print $0 > "pdftex@"}
}
' $texmf/dvips/config/pdftex.map

cp -f "pdftex@" $texmf/dvips/config/pdftex.map
rm -f "pdftex@" 

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
if test "$wafontsb" = ""; then
  cat chitex612/data/pfbaddb.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


##### Part19.1
InstallLatex2html(){
   gunzip -dc $Curdir/latex2html-2002-2-1.tgz|tar xvf -
   prefix=`which tex|sed 's#/tex##'`
   cd latex2html-2002-2-1
   
   echo "   *********************************************************"
   echo "   *  In the following, Press 'Enter key' to continue,     *"
   echo "   *  if it appears an messgae 'checking dvips version... '   *"
   echo "   *********************************************************"   
   echo " "

   echo "   Press Enter key to continue "   
   read xxxxxxx
   
   ./configure --prefix=$prefix
   make
   make install
   if test -s $texmf/tex/chinese/l2hconfpm; then
      /bin/rm -f $texmf/tex/chinese/l2hconfpm
   fi
}

##### Part20
Modifylyx(){
##### Modify configure of lyx #########
rm -f config@

if test ! -s tmp; then
   mkdir tmp
fi   

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

cd $tmpdir

} # End of Modifylyx

##### Part20-2
Update(){
#### Update to have facility of dvipdfm and others ######
./UpdateNow
}

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

if test -s $bindir/updmap-sys; then
   cp -f $texmf/dvips/config/chinese.map $texmf/fonts/map/dvips/tetex
fi

rehash
texhash

cat $texmf/xdvi/xdvi.cfg > tmp@
$gawkbin '
{ inline=$0
  if (index(inline,"dvipsmap") > 0 && index(inline,"ps2pk.map") > 0)
     {print "dvipsmap ps2pk35.map" > "tmp1@"}
  else   
     {print $0 > "tmp1@"}
} ' tmp@

# echo "after xdvi.cfg"
# read xxxxxx

cp $texmf/xdvi/xdvi.cfg $texmf/xdvi/xdvi.cfg.bak
cp tmp1@ $texmf/xdvi/xdvi.cfg

cat $texmf/web2c/updmap.cfg > tmp@
$gawkbin '
{ inline=$0
  print $0 > "tmp1@" 
}
END{print "Map chinese.map" > "tmp1@" } ' tmp@

# echo "after updmap.cfg"
# read xxxxxx


cp $texmf/web2c/updmap.cfg $texmf/web2c/updmap.cfg.bak
cp tmp1@ $texmf/web2c/updmap.cfg
rm -f tmp1@
if test -s $bindir/updmap-sys; then
   updmap-sys
else
   updmap
fi

cp -f $SAVEPWD/chitex612/bin/chitex.utf8 $bindir/chitex
cp -f $SAVEPWD/chitex612/bin/chitexorg.utf8 $bindir/chitexorg
cp -f $SAVEPWD/chitex612/bin/chitexpsv.utf8 $bindir/chitexpsv
cp -f $SAVEPWD/chitex612/bin/chitexdvi.utf8 $bindir/chitexdvi
cp -f $SAVEPWD/chitex612/bin/chilatex.utf8 $bindir/chilatex

########### 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.*
/bin/rm -f /tmp/tmpdir@

#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


##### Part23a
GiveMessagea(){
clear
#echo " "
echo "        ° ChiTeX ¹:"
echo "        1. , :"
echo "           \\ming     % Ķϸ"
echo "           \\idming   % й"
echo "           \\moekai   % "
echo "           \\MING     % Ķϸ"
echo "           \\IDMING   % й Adobe Type1 "
echo "           \\MOEKAI   %  Adobe Type1  "
echo "           \\KAI      %  Adobe Type1 "
echo "           \\FS       %  Adobe Type1 "
echo "           \\LI       %  Adobe Type1 "
echo "           \\MR       % Բ Adobe Type1 "
echo "           \\WUMING   %  (cwtex) Adobe Type1 "
echo "           \\WUFS     %  (cwtex) Adobe Type1 "
echo "           \\WUHEI    %  (cwtex) Adobe Type1 "
echo "           \\songGB   %  (GB , ½ ¼)"
echo "           \\kaiGB    %  (GB , ½ ¼)"
echo "           \\SONGGB   %  (GB , ½ ¼) Adobe Type1 "
echo "           \\KAIGB    %  (GB , ½ ¼) Adobe Type1 "
echo "           (ʱָ chitex -fonts, ֪)"
echo "        2. ʹ pdftex  pdf  ''  'ǩ'"
echo "           (μ math2_pdftex.tex :ָ chitex -pdftex math2_pdftex "
echo "            ɲ math2_pdftex.pdf ( Acrobat Reader  pdf ))"
echo $echomode "        Press any key to continue"$echocr
read inline
}


##### 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 "        , ° ChiTeX Ҳṩй:"
echo "        1. ĵ֮ TeX/LaTeX ļ"
echo "        2. ṩµ mymacros.tex  input 趨ԼϲõĻ"
echo "           ṩµ samp2*.tex Ĳļ"
echo "        3. ȷ֧Ԯ ʹ chapterbib.sty  bibunits.sty"
echo "           ṩԵļ 'ѧ_bibunits.tex', ѧ_chapbib.tex"
echo "        4.  cmakeindex  ǿ cbibtex ʹ key Ҳȷ sort"
echo "            \"alpha\"  style  author-year ֮ key Ҳʵ"
echo "        5. ȷʹ֪ verbatim ָ"
echo "        6. Modify \"camsbook.sty\" ʹ \\documentclass{amsbook} ʱ"
echo "           ȷ title"
echo "        7. ֧Ԯ pdftex/pdflatex, ÷: chitex -pdftex math2_pdftex"
echo "            math2_pdftex.tex Ϊɺ pdftex ָ֮ļ( README.use* )"
echo "        8. Could have chinese \"boldface()\" and \
 \"outline(п)\","
echo "           if one use chinese Adobe type1 fonts commands."
echo "        9. Could have fascinating \"grading color's \
 effects(ɫЧ)\""
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 Բ, ٶһ(Գļ)"
echo "       11. ṩй cbibtex, cmakeindex  kay  \\label, "
echo "           \\cite, \\ref ʹõļ: mabibind.tex "
echo "           (ָ chitex -chitexhelp ѡ mabibind.tex)"
echo "       12. ʹ  Adobe Type1 , Linux  FreeBSD\
 ֮ϵͳ߿ compile"
echo "           ʱ Big5  GB ֮ TrueType βҪ֮\
 Adobe Type1"
echo "           (ʹ ttf2pfb). Linux ϵͳ߿Զ unicode ֮\
 TrueType"
echo "           , ת Big5  GB ֮ TrueType "
echo "       11. װʱ compile  fmt , ޷ teTeX 汾"
echo "           Ҳṩʽ chitexconfig  configure  ChiTeX "
echo "           Ҳ㰲װ ChiTeX ,  teTeX 汾, ٸ teTeX Ϊ"
echo "           °汾֮, ָ:"
echo "               chitexconfig -makefmt"
echo "           ɸ chitex*.fmt  pdfchitex*.fmt ʹ° 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 "        *** б message ָ:  message "
   echo " "
   echo "        κʱ, ҪҪ֪Ҫ chitex ָ, ֻҪָ:"
   echo "               chitex"
   echo " "
   echo "        κʱ, Ҫ֪ʹ֮ڽָ, ֻҪָ:"
   echo "               chitex -fonts "
   echo " "
   echo "        κʱ, Ҫ \" TeX ļ\", \"(help)\","
   echo "        \"Ķ README* \", ֻҪָ:"
   echo "               chitex -chitexhelp"
   echo " "
   echo "         \"Ķ TeX ļ\", \"\"  \
\"\""
   echo " "
   echo "        1. Ķ TeX ļ"
   echo "        2. (й ChiTeX) "
   echo "        b. صǰ "
   echo "        q. "
   echo "     "
   echo $echomode "        Type '1', '2', 'b' or 'q' to choose one of the above: " $echocr
   
   read inkeyc
#   export inkeyc
}

mkdir -p /tmp/temp
submenucno="y"
while test "$submenucno" = "y"
do
   submenuc 
   case "$inkeyc" in
      "1")  cd /tmp/temp; chitexhelp -big5 -test;cd $tmpdir; 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
rm -rf /tmp/temp
}


messageno="y"

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

exit
} # End of GiveMessage


Setenvironment                 # Part1

FindUnixSystem                 # Part1.1
WelcomeandVerifysupuser        # Part2
FindpathofFmtTexmfVartexBinary # Part3
ModifyReadme                 # Part4

InstallAcrobatReader         # Part5.1
MakeChineseFmt               # Part5.2

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
InstallLatex2html            # Part19.1
Modifylyx                    # Part20

#cd $tmpdir

$tmpdir/chitex612/UpdateNow  # Part20-2

TexhashandRmtmpfiles         # Part21
MakeVARTEXFONTSsubdir        # Part22
GiveMessagea                 # Part23a
GiveMessage                  # Part23
cd $SAVEPWD

exit

