#!/bin/sh

# set SAVEPWD to current dir. and set PATH to contain current dir.
Curdir=`pwd`
PATH=.:$PATH:$Curdir
export PATH
export Curdir
   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

#

if test "$LOGNAME" != "root"; then
rootmessage="y"
while test "$rootmessage" = "y"
do
clear
echo " ";echo " "
echo " "
echo "         **********************************************"
echo "         *                                            *"
echo "         *   You are not a super user 'root'          *"
echo "         *                                            *"
echo "         **********************************************"
echo " "
echo "             c. Continue to install."
echo "             q. quit."
echo " "
echo $echomode "         Type 'c' or 'q' : "$echocr
read inkey

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

Asktmpdir(){
message="y"
while test "$message" = "y"
do
clear
echo " ";echo " ";echo " "
echo "    In the following, it will start to install chitex "
echo "    Choose one of the following:"
echo " "
echo "    1. Use /usr/local/src/chitex612 as temporay files dir."
echo "    2. Choose other dir. as temporay files dir."
echo "    q. quit"
echo " "
echo " "   
echo $echomode "    Type '1', '2' or 'q' : "$echocr
#echo $echomode "         "$echocr
read inkey
case $inkey in
  "1") message="n";
       tmpdir="/usr/local/src/chitex612";;
  "2") message="n"
       clear
       echo " ";echo " ";echo " "
       echo "     Type the pathname of the temporay files dir."
       echo "     (Press enter key to go back)"
       echo " "
       echo $echomode "     The temporay files dir. : "$echocr
       read inkeya
  
       if test "$inkeya" = ""; then
          message="y"
       else
          message="n"
          tmpdir=$inkeya
       fi 
       ;;
  "q") message="n"; exit;;
    *) message="y";;
esac
done
}

messagea="y"
while test "$messagea" = "y"
do
   Asktmpdir;
   messageb="y"
   while test "$messageb" = "y"
   do
   clear
   echo " "
   echo " "
   echo "     The temporary files dir. you have chosen is: "
   echo "          $tmpdir "
   echo " "
   echo "     1. Accept"
   echo "     2. Cancell and redo it"
   echo ""
   echo ""
   echo $echomode "    Type '1' or '2': "$echocr
   read inkeyb
   case $inkeyb in
     "1") messageb="n";messagea="n" ;;
     "2") messageb="n";messagea="y" ;;
       *) messageb="y" ;;
   esac
   done
done

   if test ! -s $tmpdir; then 
      mkdir -p $tmpdir
   fi

   cd $tmpdir

   texmf=`kpsexpand '$TEXMFDIST' `
   TEXMFMAIN=`kpsexpand '$TEXMFMAIN' `


   if test -s /usr/share/texlive-base/pdftexconfig.tex; then
      cp /usr/share/texlive-base/pdftexconfig.tex .
   fi

   latex -ini latex.ltx
   tex -ini tex.ini "\dump"
   pdflatex -ini pdflatex.ini "\dump"       
 
   if test -s $texmf/tex/plain/config/pdfetex.ini; then
      pdftex -ini pdfetex.ini "\dump"          
      mv -f pdfetex.fmt pdftex.fmt
   else  
      pdftex -ini pdftex.ini "\dump"       
   fi

   lambda -ini lambda.ini "\dump"
   omega -ini omega.ini "\dump"

   cp -f *.fmt $TEXMFMAIN/web2c

   export tmpdir
   echo $tmpdir > /tmp/tmpdir@

   
  cd $tmpdir
  if test -s chitex612/data/chitran0nn.awk; then
     rm -f chitex612/data/chitran0nn.awk
  fi
  if test -s chitex612/data/chitran0nn3.awk; then
     rm -f chitex612/data/chitran0nn3.awk
  fi
  if test -s chitex612/data/chitranutf.awk; then
     rm -f chitex612/data/chitranutf.awk
  fi

  if test -d chitex612; then
     rm -rf chitex612
  fi

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


echo $TEXMFCNF > tmp@
TEXMF=`
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 -s /usr/share/texlive-base/pdftexconfig.tex; then
      cp /usr/share/texlive-base/pdftexconfig.tex .
   fi

if test $TEST = "test"; then
   latex -ini latex.ltx
   tex -ini tex.ini "\dump"
   pdflatex -ini pdflatex.ini "\dump"       
 
   if test -s $texmf/tex/plain/config/pdfetex.ini; then
      pdftex -ini pdfetex.ini "\dump"          
      mv -f pdfetex.fmt pdftex.fmt
   else  
      pdftex -ini pdftex.ini "\dump"       
   fi

   lamed -ini lambda.ini "\dump"
   mv -f lambda.fmt lamed.fmt
   aleph -ini omega.ini "\dump"
   mv omega.fmt aleph.fmt
   lambda -ini lambda.ini "\dump"
   omega -ini omega.ini "\dump"
   
fi
  
  
  gunzip -dc $Curdir/chitex6121.tar.gz|tar xvf -
  gunzip -dc $Curdir/chitex6122.tar.gz|tar xvf -

  cd chitex612
  ./chsetup
#  $Curdir/chitex612/chsetupbig5

#  updmap
  
  exit
