#!/bin/sh

#cd $Curdir


PWDDIR=`pwd`

gawkbin=awk
export gawkbin

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


#

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

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

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



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

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


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

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

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

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

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

fi

#echo $unixname


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


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


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=`
awk '
{
  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 test "$texmffound" = ""; then
# (2) find path of TEXMF #
#
#kpsexpand=`which kpsexpand`

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

texmf=$texmfdir

 if test -d "/usr/share/texmf-texlive"; then
    texmf="/usr/share/texmf-texlive"
 fi

texmffile=`kpsexpand '$TEXMFDIST' `


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

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

export texmf
export TEXMF


 if test "$unixname" = "freebsd"; then
    gunzip -dc $Curdir/bsmi00lp.tgz|tar xvf -
    mv -f bsmi00lp.ttf $TEXMF/fonts/chinese/ttf/bsmi00lp.ttf
    gunzip -dc $Curdir/bkai00mp.tgz|tar xvf -
    mv -f bkai00mp.ttf $TEXMF/fonts/chinese/ttf/bkai00mp.ttf
    bsttfdir="$TEXMF/fonts/chinese/ttf"
 else
  if test -s "/usr/share/fonts/chinese/TrueType/bsmi00lp.ttf"; then
    bsttfdir="/usr/share/fonts/chinese/TrueType"
  else   
    if test -s "/usr/share/fonts/chinese/TrueType/uming.ttf"; then       
:      bsttfdir="/usr/share/fonts/chinese/TrueType"    
:      umingttf="y"
    else  
      if test -s "/usr/share/fonts/ttf/chinese/fireflysung.ttf"; then
         bsttfdir="/usr/share/fonts/ttf/chinese"          
         firflysung="y"
      else    
        if test -s "/usr/share/fonts/cjkunifonts-uming/uming.ttf"; then       
:           bsttfdir="/usr/share/fonts/cjkunifonts-uming"    
:           umingttf="y"
        else  
         echo " "
         echo "  Finding bsmi00lp.ttf, Wait!"
#         find /usr -type f -name 'bsmi00lp.ttf' > findfile
        fi 
      fi


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

#if test -s "/usr/share/fonts/cjkunifonts-ukai/ukai.ttf"; then       
#   kaibsttfdir="/usr/share/fonts/cjkunifonts-uming"    
#   ukaittf="y"
#fi  


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

 cd $PWDDIR

ln -s $TEXMF/fonts/chinese/ttf/nbsmi00lp.ttf $TEXMF/fonts/truetype/nbsmi00lp.ttf 
ln -s $TEXMF/fonts/chinese/ttf/refser.ttf $TEXMF/fonts/truetype/refser.ttf

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

if test -s "/usr/share/fonts/ttf/big5/bkai00mp.ttf"; then
   ln -s "/usr/share/fonts/ttf/big5/bkai00mp.ttf" $TEXMF/fonts/truetype/bkai00mp.ttf
   ln -s "/usr/share/fonts/ttf/big5/bkai00mp.ttf" $TEXMF/fonts/chinese/ttf/bkai00mp.ttf
fi

#  if test -s $TEXMF/fonts/chinese/ttf/uming.ttf; then
     rm -f $TEXMF/fonts/chinese/ttf/uming.ttf
#  fi 
#  if test -s $TEXMF/fonts/chinese/ttf/ukai.ttf; then
     rm -f $TEXMF/fonts/chinese/ttf/ukai.ttf
#  fi 

  cp -f $Curdir/uming.ttf.gz .
  gunzip uming.ttf.gz
#  mv -f uming.ttf $TEXMF/fonts/chinese/ttf/uming.ttf  
  if test ! -d /usr/share/fonts/truetype; then
     mkdir /usr/share/fonts/truetype
  fi
  if test ! -d /usr/share/fonts/truetype/arphic2; then
     mkdir /usr/share/fonts/truetype/arphic2
  fi

  mv -f uming.ttf /usr/share/fonts/truetype/arphic2/uming.ttf
  
  cp -f $Curdir/ukai.ttf.gz .
  gunzip ukai.ttf.gz

  mv -f ukai.ttf /usr/share/fonts/truetype/arphic2/ukai.ttf  

#  mv -f ukai.ttf $TEXMF/fonts/chinese/ttf/ukai.ttf  

  ln -s /usr/share/fonts/truetype/arphic2/uming.ttf $TEXMF/fonts/truetype/uming.ttf 
  ln -s /usr/share/fonts/truetype/arphic2/uming.ttf $TEXMF/fonts/chinese/ttf/uming.ttf
  ln -s /usr/share/fonts/truetype/arphic2/ukai.ttf $TEXMF/fonts/truetype/ukai.ttf 
  ln -s /usr/share/fonts/truetype/arphic2/ukai.ttf $TEXMF/fonts/chinese/ttf/ukai.ttf
  
  fc-cache -v -f   
  
#  ln -s $TEXMF/fonts/chinese/ttf/ukai.ttf $TEXMF/fonts/truetype/ukai.ttf   

  

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

#   if test -s "/usr/share/fonts/cjkunifonts-ukai/ukai.ttf"; then       
#      kaibsttfdir="/usr/share/fonts/cjkunifonts-ukai"    
#      ln -s $kaibsttfdir/ukai.ttf $TEXMF/fonts/truetype/ukai.ttf
#      ln -s $kaibsttfdir/ukai.ttf $TEXMF/fonts/chinese/ttf/ukai.ttf            
#   fi

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


if test "$unixname" = "linux"; then
echo " "
echo "  Finding gbsn00lp.ttf, Wait!"

if test -s "/usr/share/fonts/chinese/TrueType/gbsn00lp.ttf"; then
   bsttfdir="/usr/share/fonts/chinese/TrueType"
else    
   if test "$umingttf" = ""; then
   :
#      find /usr -type f -name 'gbsn00lp.ttf' > findfile
   fi   

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

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

 cd $PWDDIR


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

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

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

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



cd $TEXMF/fonts/truetype
ln -s $TEXMF/fonts/chinese chinese

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

#  if test -s "$bindir/dvipdfmx:; then
#     makedvipdfm="n"
#  else   
#     makedvipdfm="y"
#  fi

# echo "At 3"

makedvipdfm="y"

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

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

  } ' tmp@ ` 

  cd $PWDDIR

#  cp /bin/gawk /bin/gawk.org

## check gawk's version
#  gawk --version> v1
#  Newgawk=`
#  gawk '
#  {n=n+1
#   if (n==1)
#      {if (index($0,"3.1.5")>0)
#          {print "New"} 
#      }     
#  } ' v1 `

  
#  if test "$Newgawk" = "New"; then
#     gunzip -dc $Curdir/gawk-3.1.4.tar.gz|tar xvf -
#     cd gawk-3.1.4
#     ./configure 
#     make
#     if test -s gawk; then
#        mv -f /bin/gawk /bin/gawk-3.1.5
#        cp -f gawk /bin/gawk
#     fi    
#  fi

# echo "At 5"

  if test -d $TEXMF/fonts/truetype/chinese; then
     rm -rf $TEXMF/fonts/truetype/chinese
  fi
  
  ln -s $TEXMF/fonts/chinese/ttf $TEXMF/fonts/truetype/chinese

  if test ! -s $bindir/dvipdfmx; then
     installdvipdfm="y"
  else   
     if test ! -s $bindir/dvipdfmx.org; then
        cp $bindir/dvipdfmx $bindir/dvipdfmx.org
     fi    
  fi


  if test -d /usr/share/doc/fedora-release-11; then
#  if test -s /usr/bin/dvipdfmx; then 
     installdvipdfm="y"
  else
#     if test ! -s $bindir/dvipdfmx; then
#        installdvipdfm="y"   
#     else
        installdvipdfm="n"   
#     fi   
  fi

  prefix=`
  gawk -v bindir=$bindir '
  {llen=length(bindir)
   for (i; i<=llen; i++)
     {ch=substr(bindir,i,1)
      if (ch == "/")
         {sl=i}
     }
     prefix=substr(bindir,1,sl-1)
     print prefix
  } ' tmp@ `


 if test "$installdvipdfm" = "y"; then
 
#  gunzip -dc $Curdir/dvipdfmx-20031006.tar.gz|tar xvf -
  gunzip -dc $Curdir/dvipdfmx-20090522.tar.gz|tar xvf -
  cd dvipdfmx-20090522
#  ./configure --bindir=$bindir --datadir=$texmfshare 
#  ./configure --prefix=/usr
  ./configure --prefix=$prefix
  make check
  make install
  cp $bindir/dvipdfmx $bindir/dvipdfmx.org   
#  /bin/rm -rf bindir dvipdfmx-20050831
  
#  cp -f $bindir/dvipdfmx $bindir/dvipdfm
 fi

modify="y"

if test "$modify" = "y"; then
#  if test ! -s $bindir/dvipdfmx.bin; then
   if test -s $bindir/dvipdfmx.org; then
     cp -f $bindir/dvipdfmx.org $bindir/dvipdfmx.bin
##     cp -f $bindir/dvipdfm $bindir/dvipdfm.bin
##     cp -f $Curdir/chitex612/chitex612/data/dvipdfm $bindir/dvipdfm
#     cp -f $Curdir/chitex612/chitex612/data/dvipdfmx $bindir/dvipdfmx
##     chmod +x $bindir/dvipdfm
#     chmod +x $bindir/dvipdfmx
   fi  
#  fi   


#  cp $TEXMF/dvipdfm/config/cid-x.map.org $TEXMF/dvipdfm/config/cid-x.map
  if test -d $TEXMF/dvipdfm/config; then
     cp $TEXMF/dvipdfm/config/config.org $TEXMF/dvipdfm/config/config
  fi
fi


# Modify /texmf
#  cd $TEXMF 
#  chmod +w $TEXMF/tex/chinese/*

#  gunzip -dc $Curdir/texmf.tgz|tar xvf -
  chmod a+w $TEXMF/tex/chinese/*

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

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

  cd $PWDDIR

# Make Bgconv

  
# Modify pdftex.map
 
 cd $PWDDIR


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

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


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

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


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



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


# copy new binary files and modify chitex chitrancc

SAVEPWD=`pwd`

cd $tmpdir
echo "yes" > tmp@

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

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

  cd bindir


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

  cp -f chitranc@ chitranc

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

echo "yes" > tmp@

  cp -f chitranc $chitrancdir

#  chitexconfig -scfile
  chitexconfig -chitex.fdf

  cd $PWDDIR

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

#  rehash

  texhash

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

 exit

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

  
#exit



