#!/bin/sh

echo "yes" > tmp@

  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

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

echo $TEXMFCNF > tmp@
TEXMF=`
gawk '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `


  if test ! -s $TEXMF/tex/chinese/version; then
     Needupdate="y"
  fi


  
#  fi
  
  pwddir=`pwd`

if test "$LOGNAME" != "root"; then 
#clear
#echo " ";echo " "
#echo " "
#echo "         **********************************************"
#echo "         *   You are not super user 'root'            *"   
#echo "         *   and could not update chitex now          *"
#echo "         *                                            *"
#echo "         **********************************************"
#echo " "
#echo " "
exit 
fi



  cd /tmp
  if test ! -d chupdate; then
     mkdir chupdate
  fi   
  cd chupdate
  echo "lftp 140.115.25.3"> "ftpdat"
  echo "set passive off" >>"ftpdat"
  echo "cd chitex" >>"ftpdat"
  echo "cd unix" >>"ftpdat"
  echo "cd chitexupdate" >>"ftpdat"
  echo "get versionnow " >>"ftpdat"
  echo "get getupdate " >>"ftpdat"

  lftp -f ftpdat
  rm -f "NeedUpdate@" 
  echo "yes" > tmp@
  
 if test "$Needupdate" = ""; then  
  gawk '
  { chinese=texmf "/tex/chinese"
    version=chinese "/version"
    while (getline inl < version > 0)
      {if (index(inl,"versiontime") >0)
         {eq=index(inl,"=");versiontime=substr(inl,eq+1)}
      }
    while (getline inl < "versionnow" > 0)
      {if (index(inl,"versiontime") > 0)
         {eq=index(inl,"=");versiontimenow=substr(inl,eq+1)}
      }
    if (versiontime<versiontimenow)
       {print "NeedUpdate" > "NeedUpdate@"}
  }' texmf=$TEXMF tmp@
 fi 

#Needupdate="y"

  if test ! -s NeedUpdate@ -a "$Needupdate" = ""; then
     exit
  else   
   :
#     cp versionnow $TEXMF/tex/chinese/version
  fi   


InputYes="y"
while test "$InputYes" = "y"
do
clear
echo " ";echo " "
echo "      It will start to update chitex  "
echo "      Would you like to update now?"
echo "   "
echo "      c. Start to update. "
echo "      q. quit "
echo " "
echo $echomode "      Enter 'c' to start update or enter 'q' to quit: "$echocr
read inkey
case $inkey in
  "c") InputYes="n";;
  "q") InputYes="n"; rm -f *@;exit;;
    *) InputYes="y";;
esac
done


    
  if test -s NeedUpdate@ -o "$Needupdate" = "y"; then
     if test -s getupdate; then
        chmod +x getupdate
        ./getupdate
     fi   
  fi
  
  cd $pwddir
  rm -rf /tmp/chupdate
  
  exit
  
