#!/bin/sh
#
# This script is a part of ChiTeX
# By Hung-Yih Chen    
#           Last updated  May. 12, 1999
#
# This script is used for removing tfm or pk files."
#
# Usage: rmchpk [-option]
#

/bin/rm -f *@

SAVEPWD=`pwd`

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
export $gawkbin

if test "$1" = "-big5"; then
   option="big5"   
else
   if test "$1" = "-gb"; then
        option="gb"
   else
      if test "$1" = "-help"; then  
         option="help"
      else
         option="big5"
      fi
   fi
fi

# no newline 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 "$option" = "help"; then
   echo " "; echo " "
   echo "  Usage: rmchpk [option]"
   echo " "
   echo "  This script is used for removing tfm or pk files."
   echo "  option are:"
   echo "  -gb      for using in GB encoded chinese"
   echo "  -big5    for using in Big5 encoded chinese" 
   echo "           default is with option -big5"
   echo "  -help    help"
   echo " "
   exit
fi


rm -f tmp@

if test "$option" = "big5"; then

  clear
  echo " "
  echo "        ******************************************"
  echo "        *                                        *"
  echo "        *  This is for Big5 encoded chinese.     *"  
  echo "        *  Use command:                          *"
  echo "        *        rmchpk -gb                      *"
  echo "        *  if you are unsing GB encoded chinese  *"
  echo "        *                                        *"
  echo "        ******************************************"
  echo " "
  echo "        Press enter key to continue or Press 'Ctrl C' to exit."
  echo $echomode "        " $echocr
  read inkey
  


clear
echo " ";echo " ";echo " "
echo " "
echo "         *************************************************"
echo "         *                                               *"
echo "         *   {NAMAƱM tfm/pk   *"
echo "         *   تOMª ttf2pk ͪ tfm/pk     *"
echo "         *   HϩҦrΤ@P                            *"
echo "         *                                               *"
echo "         *************************************************"
echo " "
echo $echomode "         Press enter key to continue" $echocr
read inkey



clear

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

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

texmf=$TEXMF
echo "directory of 'texmf': " $texmf " found"


# ****************
inkeyno="0"
inkeytmp="0"
while test "$inkeyno" = "0"
do
clear
    echo " ";echo " "
    echo " "
    echo "       ҧΧA s teTeX ؿ(@W٬ texmf, Ulؿ "
    echo "        tex, web2c, fonts ):  "
    echo "                  $texmf  "
    echo " "
    echo "       T{Wz줧ؿ, J 'y', _hJ 'n' HKsJ"
    echo "         Ctrl C { (YDťؿݭsJ)" 
    echo " " 
    echo $echomode "       J 'y' T{Wz, J 'n' HKsJ  ,,,: " $echocr
    read inkey

  if test "$inkey" != "y" -a "$inkey" != "n"; then
        inkeyno="0"
        inkeytmp="1"
  else
        inkeytmp="0"   
        if test "$inkey" = "n"; then
           echo " "; echo " "
           echo " "
           echo    "       s teTeX ؿ "
           echo $echomode "       :  " $echocr
           read texmftmp
           if test "$texmftmp" != "";then
              texmf=$texmftmp 
           fi
       else
         inkeyno=1
       fi
        inkeyno="1"
  fi

done

rm -f tmpfmark@

inkeyno="0"
while test "$inkeyno" = "0"
do
    menu="0"
    clear
    echo " "; echo " "
#    echo "        1. MҦrΤ tfm "
#    echo " "
#    echo "        2. MҦrΤ pk "
#    echo "  "
    echo "        1. ܩҭnM tfm ɤ r" 
    echo " "
    echo "        2. ܩҭnM pk ɤ r" 
    echo " "
    echo "        q. 楻{"
    echo " "

    echo $echomode "       J '1', '2'  'q' : " $echocr
    read inkey

 if test "$inkey" != ""; then
  if test "$inkey" = "q"; then
      inkeyno="1"
  else
   menu="1"
   while test "$menu" = "1"
   do
    if test "$inkey" = "1" -o "$inkey" = "2"; then
#         inkeyno="1"
         clear
         echo " "; echo " "
       if test "$inkey" = "1"; then 
         echo "     JAnM tfm ɤ rΦW, Ҧp"
         echo "     nM tfm ɬ 'ntukai*.tfm' hJ 'ntukai'"
         echo "     (b $texmf/fonts/chinese/ttf ҹ TrueType"
         echo "      rɬ ntukai.ttf) "
         echo " "
         echo $echomode "     nM tfm ɤ rΦW: " $echocr
       else
         echo "     JAnM pk ɤ rΦW, Ҧp"
         echo "     nM pk ɬ 'ntukai*.*pk' hJ 'ntukai'"
         echo "     (b $texmf/fonts/chinese/ttf ҹ TrueType r"
         echo "       ntukai.ttf) "
         echo " "
         echo $echomode "     nM pk ɤ rΦW: " $echocr
       fi

       read pkfont

       echo "yes" > tmp@

#       gawk -v pkfont=$pkfont '{gsub(" ","",pkfont);print pkfont}' tmp@ `

       pkfont=`
       $gawkbin '{gsub(" ","",pkfont);print pkfont}' pkfont=$pkfont tmp@ `

       if test "$pkfont" != ""; then
         menu="0"
         if test "$inkey" = "1"; then
            pktfm="tfm"
            pktfmname="*.tfm"
         else 
            pktfm="pk"
            pktfmname="*.*pk"
         fi 
  
         /bin/rm -f $texmf/fonts/$pktfm/chinese/$pkfont/*
         /bin/rm -f tmpfmarka@
         /bin/rm -f tmpfmark@
#         echo "yes" > tmp@

#         awk -v texmf=$texmf -v pkname=$pkfont -v pktfm=$pktfm -v pktfmname=$pktfmname '         

         $gawkbin  '
         BEGIN{}
         {print  pkname pktfmname
          abstra="find " texmf "/fonts/" pktfm " -follow -name \47" pkname pktfmname \
            "\47 > tmppka@"
          print abstra > "finda@"
          abstrb="find /var -follow -name " pkname pktfmname " > tmppk@"
          print abstrb > "findb@"
         }
         END{}' texmf=$texmf pkname=$pkfont pktfm=$pktfm pktfmname=$pktfmname tmp@

         chmod +x finda@; finda@
         chmod +x findb@; findb@

         $gawkbin '
         BEGIN{}
         {rmfile=$0
          system("/bin/rm -f " rmfile)
         }
         END{}' tmppka@  

         $gawkbin '
         BEGIN{}
         {rmfile=$0
          system("/bin/rm -f " rmfile)
         }
         END{}' tmppk@  
       fi
    fi
   done
  fi
 fi
done                 

else
clear
echo " ";echo " ";echo " "
echo " "
echo "         *************************************************"
echo "         *                                               *"
echo "         *   ʽΪϣ tfm/pk   *"
echo "         *   ĿΪɰ ttf2pk  tfm/pk     *"
echo "         *   ʹһ                            *"
echo "         *                                               *"
echo "         *************************************************"
echo " "
echo $echomode "         Press enter key to continue" $echocr
read inkey



clear

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

echo "directory of 'texmf': " $texmf " found"


# ****************
inkeyno="0"
inkeytmp="0"
while test "$inkeyno" = "0"
do
clear
    echo " ";echo " "
    echo " "
    echo "       ҵѡȡ  teTeX Ŀ¼(һΪ texmf, ֮Ŀ¼ "
    echo "        tex, web2c, fonts )Ϊ:  "
    echo "                  $texmf  "
    echo " "
    echo "       ȷҵ֮Ŀ¼,  'y',  'n' Ա"
    echo "         Ctrl C ֹͣʽִ֮ (ǿĿ¼)" 
    echo " " 
    echo $echomode "        'y' ȷ,  'n' Ա  ,,,: " $echocr
    read inkey

  if test "$inkey" != "y" -a "$inkey" != "n"; then
        inkeyno="0"
        inkeytmp="1"
  else
        inkeytmp="0"   
        if test "$inkey" = "n"; then
           echo " "; echo " "
           echo " "
           echo    "        teTeX Ŀ¼ "
           echo $echomode "       Ϊ:  " $echocr
           read texmftmp
           if test "$texmftmp" != "";then
              texmf=$texmftmp 
           fi
       else
         inkeyno=1
       fi
        inkeyno="1"
  fi

done

rm -f tmpfmark@

inkeyno="0"
while test "$inkeyno" = "0"
do
    menu="0"
    clear
    echo " "; echo " "
#    echo "        1. ֮ tfm "
#    echo " "
#    echo "        2. ֮ pk "
#    echo "  "
    echo "        1. ѡҪ tfm ֮ " 
    echo " "
    echo "        2. ѡҪ pk ֮ " 
    echo " "
    echo "        q. ִֹͣбʽ"
    echo " "

    echo $echomode "        '1', '2'  'q' : " $echocr
    read inkey

 if test "$inkey" != ""; then
  if test "$inkey" = "q"; then
      inkeyno="1"
  else
   menu="1"
   while test "$menu" = "1"
   do

    if test "$inkey" = "1" -o "$inkey" = "2"; then
#         inkeyno="1"
         clear
         echo " "; echo " "
       if test "$inkey" = "1"; then 
         echo "     Ҫ tfm ֮ , "
         echo "     Ҫ֮ tfm Ϊ 'ntukai*.tfm' Ӧ 'ntukai'"
         echo "     ( $texmf/fonts/chinese/ttf Ӧ֮ TrueType"
         echo "      εΪ ntukai.ttf) "
         echo " "
         echo $echomode "     Ҫ tfm ֮ : " $echocr
       else
         echo "     Ҫ pk ֮ , "
         echo "     Ҫ֮ pk Ϊ 'ntukai*.*pk' Ӧ 'ntukai'"
         echo "     ( $texmf/fonts/chinese/ttf Ӧ֮ TrueType ε"
         echo "      Ϊ ntukai.ttf) "
         echo " "
         echo $echomode "     Ҫ pk ֮ : " $echocr
       fi

       read pkfont

       echo "yes" > tmp@

#       gawk -v pkfont=$pkfont '{gsub(" ","",pkfont);print pkfont}' tmp@ `

       pkfont=`
       $gawkbin '{gsub(" ","",pkfont);print pkfont}' pkfont=$pkfont tmp@ `

       if test "$pkfont" != ""; then
         menu="0"
         if test "$inkey" = "1"; then
            pktfm="tfm"
            pktfmname="*.tfm"
         else 
            pktfm="pk"
            pktfmname="*.*pk"
         fi 
  
         /bin/rm -f $texmf/fonts/$pktfm/chinese/$pkfont/*
         /bin/rm -f tmpfmarka@
         /bin/rm -f tmpfmark@
#         echo "yes" > tmp@
         
#         awk -v texmf=$texmf -v pkname=$pkfont -v pktfm=$pktfm -v pktfmname=$pktfmname '

         $gawkbin '
         BEGIN{}
         {print  pkname pktfmname
          abstra="find " texmf "/fonts/" pktfm " -follow -name \47" pkname pktfmname \
            "\47 > tmppka@"
          print abstra > "finda@"
          abstrb="find /var -follow -name " pkname pktfmname " > tmppk@"
          print abstrb > "findb@"
         }
         END{}' texmf=$texmf pkname=$pkfont pktfm=$pktfm pktfmname=$pktfmname tmp@

         chmod +x finda@; finda@
         chmod +x findb@; findb@

         $gawkbin '
         BEGIN{}
         {rmfile=$0
          system("/bin/rm -f " rmfile)
         }
         END{}' tmppka@  

         $gawkbin '
         BEGIN{}
         {rmfile=$0
          system("/bin/rm -f " rmfile)
         }
         END{}' tmppk@  
       fi
    fi
   done
  fi
 fi
done                 


fi


/bin/rm -f *@  *@*
exit

  
