#!/bin/bash
#################################
#seicoriya - preprocessor for oriya (latex)
# Copyright (C) 2006 Somendra M. Bhattacharjee (somen@iopb.res.in)
#
#    Written by:
#       Somendra M. Bhattacharjee
#       Institute of Physics, Bhubaneswar 751 005, India
#       email:somen@iopb.res.in
#       Home page: http://www.iopb.res.in/~somen

# This is  release. v 0.1
# For the most recent version,  check the above URL (under Programs).

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
####################################


numl=5                                            # hash mark every "numl" line

#### help file  in english
function helpfile
{
cat <<EOF
  NAME:
     seicoriya - preprocessor for Oriya latex
  SYNOPSIS:
     seicoriya [OPTIONS] infile[.tex] [outfile[.tex]]

  DESCRIPTION:
      seicoriya converts an ASCII file to a file that canbe processed
	by latex to get proper oriya output.
      
      seicoriya makes appropriate conversion in regions marked by "%<ei>" and  
      "%</ei>".  
       The markups (%<ei>  and %</ei>)  can be anywhere in a line
      but note that the line containing "%<ei>" will be processed but not
      the one containing "%</ei>" .

         o  Comments (including inline comments) marked by %  
              (as in tex/latex) are not touched.  
         o  "{", "}" are treated as "letters", except for "{oi}". 
         o  Anything from a "\" till a blank space will be left untreated 
             like "\quad " or "\verb+kali+ ", "{\rm{bash}}" etc.
	 o *e, *i, *{oi} are not touched. 

      Works  with GNU BASH version 2 and up only. 
      It will not run with lower versions of BASH.

      This version of seicoriya: v$vrsn
      Get current version from http://www.iopb.res.in/~somen/Prog/seicoriya
	  
  OPTIONS: 
	-h : prints this help file and exits
	-ho : prints a help file in oriya (roman fonts) and exits
              (NOT YET:  TO DO)
	-lN or -l=N : puts a hash mark on the screen after every N lines. (default: N=5) 
 	-n or -d : deletes the lines containing '%<ei>', '%</ei>'
 	-v : prints the version number and exits
        infile : ".tex" is appended if no ".tex" is supplied
        outfile: output file 
           (if no outfile is given, default is infile_sei.tex in the present 
            working directory)
              More options to be added!


AUTHOR:
       Written by Somendra M. Bhattacharjee (SINP, Kolkata/Institute of Physics, Bhubaneswar 751 005, India).                

BUGS:
         Could have been faster!
 
REPORTING BUGS:
       Report bugs to <somen@iopb.res.in>.
 
COPYRIGHT:
       Copyright © 2006 Somendra M. Bhattacharjee     
       This is free software released under GPL.  There is NO warranty; 
       not even for MERCHANTABILITY or FITNESS FOR  A  PARTICULAR PURPOSE.
 

                     $dtd
EOF
exit 0
}

#### help file  in bangla (in roman fonts)
function help_ori 
{
cat <<EOF


     seicoriya - 
  byobohar:
     seicoriya [OPTIONS] infile[.tex] [outfile[.tex]] 
  byakhya:
             NOT YET -- to do
			 
     lekhok:
      Somendra M. Bhattacharjee (Institute of Physics, Bhubaneswar 751 005, India).

             Copyright © 2006 Somendra M. Bhattacharjee
       This is free software released under GPL.  There is NO warranty;
       not even for MERCHANTABILITY or FITNESS FOR  A  PARTICULAR PURPOSE.


	   $dtd            

				   
EOF
exit 0
}


### Main program starts
vrsn='0.1'    ## version number
dtd='August 16 2006.'  ## release date

##############################
while :                                          #: placeholder, true always!
do
        case "$1" in
        -v*) echo `basename $0` version $vrsn $dtd
	    exit 0;;
	-l*) numl="$1"
             numl=${numl/-l=/}
             numl=${numl/-l/}
	     
	      if [ $[numl+1] = "1" ]
	       then
	         numl=5
	       fi
		;;
  	-[nd]) no_ei=1;;     ### no %<ei>, %</ei> in output
 	-h) echo "          Help page in English       "
             helpfile|less
	     exit 0;;

 	-ho) echo "  Help in bangla                                   "
             help_ori |less
	     exit 0;;

	     
        *)  break ;;
        esac
        shift
done

if [ $# -lt 1 -o $# -gt 2 ]; then
        echo "Usage: `basename $0` [options...] input[.tex] [output[.tex]]" 1>&2
        exit 1
fi

# check if the program can run: check bash version 

 tstbsh=`echo $BASH_VERSION|cut -f1 -d'.'`
if [ "$[tstbsh]" -gt "1" ] 
then
  :
else 
  echo "E: Your bash does not allow susbtituion/array etc.  Upgrade your bash"
  echo "B: Ei bash ey hobe na - notun bash  darkar"
 exit 1
fi

# check/fix input output file names

infile=$1
if [ $# -gt 1 ]
  then

    outfile=$2
else
    outfile=$(basename $1)
    outfile=${outfile/.tex/}_sei
fi


    case "$infile" in
      *.tex) : 
	      ;;
       *) infile=${infile}.tex
              ;;
     esac
    case "$outfile" in
      *.tex) :
	      ;;
       *)      outfile=${outfile}.tex
              ;;
     esac
 if [ "$outfile" = "$infile" ]
    then
          echo '>>>>>>> E: Output file name changed'
          echo '>>>>>>> B: Output file er  nam paltano holo'
         outfile=${infile//.tex/_sei.tex}
 fi  
   
if [ ! -r "$infile" ]
 then
   echo "           "	
   echo " E: Aborting $infile does not exist or not readable"
   echo " B: cherhe dilam: $infile nei ba porha jacche na   "
   echo "           "	
   exit 1
fi

cat /dev/null >$outfile   # start afresh



function ei_thik

{
#  to put \*...* in right places for bangtex

unset -v fl_txt
ZZZ=$@


ZZZ_3=""
#################


x0="$ZZZ"


unset -v wrd xx wtst slsh
#xx=$1
xx=$(echo $x0|tr ' ' '^')  
xx=${xx//\{oi\}/oi}
xx=${xx//eou/ou}
xx=${xx//e\{ou\}/ou}
xx=${xx//\{aa\}/aa}
#xx=${xx//oi/P}
len=${#xx}
len1=$(echo $len)


#  construct array of letters
wlg=0

until [ $[len1] = 0 ]; do
 
 x1=${xx:0:1}                                     ## cut first letter 
  wrd[$wlg]=$x1  
  if [ "$[wlg]" = 0 ]
   then
     x1=${x1/e/\*e}                     ## protect *i  *e constructs
     x1=${x1/i/\*i}
#     x1=${x1/P/\*P}
   fi
 xx=${xx:1}
 case  "$x1" in 

  '*')                               # to identify *i,*e,*P
   x2=${xx:0:1}                                   ## cut next one
    case "$x2" in 
      [ie])                        ## identify *i etc as units
            wrd[$wlg]='*'$x2 
            xx=${xx:1}
            len1=$[len1-2]
            wlg=$[wlg+1]
          ;;
      [oa])                        ## identify *oi,*ou,*aa etc as units
            x3=${xx:0:2}                                   ## cut two 
             case "$x3" in 
                o[iu]|aa)                        ## identify oi,ou,aa as units
                     wrd[$wlg]='*'$x3 
                      xx=${xx:2}
                      len1=$[len1-3]
                       wlg=$[wlg+1]
                  ;;
                *)
                       wrd[$wlg]='*'$x2 
                       xx=${xx:1}
                       len1=$[len1-2]
                       wlg=$[wlg+1]
                    ;;
               esac
            ;;
       *) 
        len1=$[len1-1]
         wlg=$[wlg+1]
          ;;
     esac
    ;;

  o)
   x2=${xx:0:1}                                   ## cut next one
   case "$x2" in                        ## identify oi ou as  units
      [iu]) wrd[$wlg]=$x1$x2 
            xx=${xx:1}
            len1=$[len1-2]
            wlg=$[wlg+1]
        ;;
      *) 
        len1=$[len1-1]
         wlg=$[wlg+1]
        ;;
     esac
   ;;


 [sS])
   x2=${xx:0:1}                                   ## cut next one
   if [ "$x2" = "h" ]; then                       ## identify sh as a unit
      wrd[$wlg]=$x1$x2
       xx=${xx:1}
       len1=$[len1-2]
       wlg=$[wlg+1]
    else
        len1=$[len1-1]
        wlg=$[wlg+1]
   fi
  ;;


 '\')
   x2=${xx:0:1}                                   ## cut next one
   case "$x2" in
     '*')                                   ## identify \* as a unit
           wrd[$wlg]='\*'
           xx=${xx:1}
           len1=$[len1-2]
           wlg=$[wlg+1]
         ;;  
       
     [a-zA-Z'\']) nwlg=$[wlg]                ## isolate slash commands
               wrd[$nwlg]=${wrd[$[nwlg]]}$x2 
               xx=${xx:1}
               len1=$[len1-2]
               wlg=$[wlg+1]
              while :
               do
                 x3=${xx:0:1}                                   ## cut next one
                 if [ "$x3" = '^' ]
                   then 
		      slsh="$slsh $nwlg"
                      break 
                  else
                    xx=${xx:1}
                    len1=$[len1-1]
                    wrd[$nwlg]=${wrd[$[nwlg]]}$x3 
                  fi
		done
              ;;
        *)
               len1=$[len1-1]
               wlg=$[wlg+1]
              ;;
   esac 
   ;;
 

  N)                                   # for NN NNG
   x2=${xx:0:1}                                   ## cut next one
   case "$x2" in
     'N')                                   ## select   NN as a unit
         xx=${xx:1}
         x3=${xx:0:1}                                   ## cut next one
           case "$x3" in
             'G')                                   ## identify NNG as a unit
                   wrd[$wlg]='NNG'
                   xx=${xx:1}
                   len1=$[len1-3]
                   wlg=$[wlg+1]
                    ;;  
             *)                                   ## identify NN as a unit
                   wrd[$wlg]='NN'
                   len1=$[len1-2]
                   wlg=$[wlg+1]
                    ;;  
              esac
             ;;
        *)                                       ##go ahead  if only N
               len1=$[len1-1]
               wlg=$[wlg+1]
              ;;
   esac 
   ;;

 ':'|'/')                                          ## for hosonto

   x2=${xx:0:1}                                   ## cut next one
   if [ "$x2" = "/" ]; then                       ## identify :/, //  as  units
      wrd[$wlg]=$x1$x2 
       xx=${xx:1}
       len1=$[len1-2]
       wlg=$[wlg+1]
    else
        len1=$[len1-1]
        wlg=$[wlg+1]
   fi
   ;;

 [rR]) 

   x2=${xx:0:1}                                   ## cut next one
   if [ "$x2" = "R" ]; then                       ## identify rR, RR as units
      wrd[$wlg]=$x1$x2
       xx=${xx:1}
       len1=$[len1-2]
       wlg=$[wlg+1]
    else
        len1=$[len1-1]
        wlg=$[wlg+1]
   fi
  ;;

 k)

   x2=${xx:0:1}                                   ## cut next one
   if [ "$x2" = "K" ]; then                       ## identify kK as a unit
      wrd[$wlg]="kK" 
       xx=${xx:1}
       len1=$[len1-2]
       wlg=$[wlg+1]
    else
        len1=$[len1-1]
        wlg=$[wlg+1]
   fi
  ;;

 a)

   x2=${xx:0:1}                                   ## cut next one
   if [ "$x2" = "a" ]; then                       ## identify aa as a unit
      wrd[$wlg]="e" 
       xx=${xx:1}
       len1=$[len1-2]
       wlg=$[wlg+1]
    else
        len1=$[len1-1]
        wlg=$[wlg+1]
   fi
  ;;

 *) 
   len1=$[len1-1]
    wlg=$[wlg+1]
  ;;

 esac
  
done
 wrd[$wlg]=' '                                    ## identify  end

# slsh=${slsh:=$wlg}    #list of locations of slash commands, if unset, then wlg 
  slsh="$slsh $wlg"
# echo "${wrd[*]}"

#  echo $slsh 

# locate e and i then go back
slps=0
wlg=0
eipos=-1
idd=$[wlg]
len1=$len
                                                                               
until [ $[len1] = 0 ]; do
ind=-1
 

 case  "${wrd[$[wlg]]}" in

  [aiouAEIOUVK^]|ii|'*'[ie]|'*'o[iu]|'*'aa|NNG|[rR]R|':/'|'//')          ## identify the trivial ones
          eipos=$[wlg]                            ## e,i,oi cannot cross these
   ;; 


  e|o[iu])                                          # turn for e and oi, ou =P

#	  echo $wlg
           j0=$[eipos+1]                          ## default position but may 
                                                  ##  move forward
 	   yy=${wrd[$[j0]]} 
#           echo $yy
#### error detection
          if [ "$[j0]" -gt "$[wlg-1]" ]
            then

               echo  " ?ERROR:  $ZZZ "|tr "^" " " 1>&3 
               break
           fi
 
           until [ $[j0] = $[wlg-1] ]; do
           j0=$[j0+1]
 	   yy=$yy${wrd[$[j0]]}                    ## part of the word upto e,i,P
                                                  ## from defined pos. 
      	   done

#	    echo $eipos yy  "$yy" wrd ${wrd[$[eipos+1]]}

           case "$yy" in                          ## check for patterns  
 	   
           ?)                                     ## example: ki 
                     		 indx=$[wlg-1]
#echo 3
               ;;
           *)                                     ## or else .....
        
	   yy1=${yy//kK/X}                   ## replace kK by F
	   yy1=${yy1//[Ss]h/k}                   ## replace sh by k
           yy1=${yy1//[hRMLWYFK]/F}                 ## replace all fala's by F
	   yy1=${yy1//NN/F}                   ## replace chadrabinduu by F
	   yy1=${yy1//N[GJ]/kF}                   ## replace NG, NJ
	   yy1=${yy1//l~/kF}                   ## replace l~      
	   yy1=${yy1//[gcjJTDNtdnpbmrlHsSBwX]/k}   ## replace by k and kF

           case "$yy1" in                         #  
#rule 1
            *[^/][^F])                            ##  baki

                     	 indx=$[wlg-1]
#echo rule 1
               ;;

#rule 2
            kF|*[^/]kF)                           ##  khela, barhi, kosMin

                     	 indx=$[wlg-2]
#echo rule 2 $yy1
               ;;

#rule 3
           k[F/][kF]|*[^/]k[F/][kF])       ##  ShWe, g/Ye, k/ki  but not k/khWe

                     		 indx=$[wlg-3]
#echo rule 3 $yy1
               ;;

#rule 4
	       *[^/]k/kF|*[^/]kF/k)                ## k/khi, kh/ki, k/kWi
			 indx=$[wlg-4]
#echo rule 4 $yy1
	       ;;

#rule 5
	       *[^/]kF/kF)                      ## kh/khi, NJ/chi
			 indx=$[wlg-5]
#echo rule 5 $yy1 
	       ;;

#rule 6
	       *[^/]kF/kFF|*[^/]kFF/kF)             ## kh/khWi, khh/khi
			 indx=$[wlg-6]
#echo rule 6 $yy1
	       ;;

#rule 7
            *) #echo here                  ## when everything fails 
                     	 indx=$[eipos+1]
#echo rule 7 $yy1
		;;
            esac      
     esac

   wrd[$[indx]]=e${wrd[$[indx]]}
   wrd[$[wlg]]=`echo "${wrd[$[wlg]]}"|sed -e 's/e//g'`
   eipos=$[wlg]
   ;;
   *) #echo none ${wrd[$[wlg]]}
        
   ;;
  esac
   len1=$[len1-1]
    wlg=$[wlg+1]
    done
# echo $x0 ' ---> ' "${wrd[*]}"|tr -d " "|tr 'Z^' '* '


istrt="-1"

for ijk in $slsh
 do 
  unset fl_0
  if [ "$ijk" = '0' ]
    then
      istrt=1
    else
     yyy=          #assign null string
      until [ "$[istrt]" = "$[ijk-1]" ]
       do
        istrt=$[istrt+1]
        yyy=${yyy}${wrd[$[istrt]]}
       done

     fl_0=${yyy}              #//NNGe/eNNG}


#### position ref     properly

     ref='r/'
    echo "$fl_0"|grep "$ref">/dev/null
    sts=$?
     if [ "$sts" = "0" ]
      then


       for iph in kh gh NG ch jh NJ Th Dh th  dh  ph bh l\~  shh Sh  rhh kK 'g\/Y' 
        do

	  fl_0=${fl_0//${ref}${iph}\//${iph}${ref}/}

        done
       for iph in k g N c j n T D t d  p b m J l sh rh S H
        do

	  fl_0=${fl_0//${ref}${iph}\//${iph}${ref}/}
        done
          fl_0=${fl_0//${ref}\//\/${ref}}


        for iph in kh gh NG ch jh NJ Th Dh  th  dh  ph bh l\~  shh Sh  rhh kK 'g\/Y' 
         do

	    fl_0=${fl_0//${ref}${iph}/${iph}${ref}}
         done

        for iph in k g c j n T D t d  p b m J l sh rh S H  
         do

	   fl_0=${fl_0//${ref}${iph}/${iph}${ref}}
         done

             fl_0=${fl_0//${ref}N\/N/${ref}N}
             fl_0=${fl_0//${ref}N/N\/N${ref}}
             fl_0=${fl_0//${ref}W/W${ref}}
	     fl_0=${fl_0//${ref}M/M${ref}}  
             fl_0=${fl_0//${ref}uu/uu${ref}}
             fl_0=${fl_0//${ref}u/u${ref}}
             fl_0=${fl_0//${ref}i/\{\\CCCC171\}}
             fl_0=${fl_0//${ref}oi/\{\\CCCC246\}}
             fl_0=${fl_0//${ref}ou/\{\\CCCC19\}}

     fi

### end of ref block

#### halant (hosonto)  : normal or low

   fl_0=${fl_0//:\//+} 

## u, uu, rR,R etc placing below properly (verylow, low, center) others 
## are normal
#### 216: very low u ; 217: very low uu; 218:verylow rr;206: low r; 142: low halant

#for k/k k/T kKn gg           -VeryLow
#199 27 225 30 31 36 242 220 186 232 197 29 219 221 223 237 205 200 198 
#207 184 222 201 233 26 185 224 25 240 239 136 155

  for vlw in 'k\/k' 'k\/T' 'kK\/N' 'g\/g' 'g\/dh' 'j\/j' 'j\/jh' 'N\/T' 'N\/Th' 'N\/N' 'n\/tR' 'p\/s' 'b\/dh' 'l\/k' 'l\/g' 'l\/p' 'l\/ph' 'l\/l' 'lL' 'S\/c' 'Sh\/k' 'Sh\/T' 'Sh\/Th' 'Sh\/N' 'Sh\/p' 'Sh\/ph' 's\/kh' 's\/t' 's\/tR' 's\/p' 's\/ph' 'sM' 's\/m' 'HM' 

   do
     vlz=${vlw//\\/}
     fl_0=${fl_0//${vlw}uu/${vlz}\{\\CCCC217\}}

     fl_0=${fl_0//${vlw}u/${vlz}\{\\CCCC216\}}
     fl_0=${fl_0//${vlw}rR/${vlz}\{\\CCCC218\}}
     fl_0=${fl_0//${vlw}R/${vlz}\{\\CCCC206\}}
     fl_0=${fl_0//${vlw}:\//${vlz}\{\\CCCC142\}}
     fl_0=${fl_0//${vlw}W/${vlz}C0C0C0W}
   done

  fl_0=${fl_0//d\/bh/\{\\CCCC214\}}  #put the special ssymbol first
     fl_0=${fl_0//SRu/SR\{\\CCCC216\}}
     fl_0=${fl_0//\/bhu/\{\\CCCC17\}}
     fl_0=${fl_0//s\/tu/\{\\CCCC32\}}
     fl_0=${fl_0//s\/tuu/s\/t\{\\CCCC217\}}
   fl_0=${fl_0//n\/tR/\{\\CCCC232\}}
   fl_0=${fl_0//s\/tR/\{\\CCCC233\}}
     fl_0=${fl_0//n\/tW/n\/tC0C0C0W}



#for k/t  low

  for lw in jh y l H kK x 'k\/t' 'k\/s' 'D\/g' 'rh\/g' 'D\/D' 'N\/D' 'N\/Dh' 't\/n' 't\/p' 't\/m' 'tM' 'd\/g' 'n\/t' 'p\/t' 'm\/m' 'mM' 's\/t' 'sR' 'SR' 'shR'  'H\/b' 'HW' 
   do
      lwz=${lw//\\/}
     fl_0=${fl_0//${lw}uu/${lwz}\{\\CCCC212\}}
     fl_0=${fl_0//${lw}u/${lwz}\{\\CCCC211\}}
     fl_0=${fl_0//${lw}rR/${lwz}\{\\CCCC213\}}
     fl_0=${fl_0//${lw}R/${lwz}\{\\CCCC206\}}
     fl_0=${fl_0//${lw}:\//${lwz}\{\\CCCC142\}}

   done

# 206 VL after r-fala
     vlw='206'
     fl_0=${fl_0//CCCC${vlw}\}uu/CCCC${vlw}\}\{\\CCCC217\}}
     fl_0=${fl_0//CCCC${vlw}\}u/CCCC${vlw}\}\{\\CCCC216\}}

  
  for vlw in '\/n' '\/bh' '\/m' 'M' '\/l' 'L' '\/l\~' 

   do
      vlz=${vlw//\\/}
     fl_0=${fl_0//${vlw}uu/${vlz}\{\\CCCC217\}}
     fl_0=${fl_0//${vlw}u/${vlz}\{\\CCCC216\}}
     fl_0=${fl_0//${vlw}rR/${vlz}\{\\CCCC218\}}
     fl_0=${fl_0//${vlw}R/${vlz}\{\\CCCC206\}}
     fl_0=${fl_0//${vlw}+/${vlz}\{\\CCCC142\}}
   done


#center matras

  for cmw in c ch T Th D Dh r
 
   do
     fl_0=${fl_0//${cmw}uu/${cmw}\{\\CCCC8\}}
     fl_0=${fl_0//${cmw}u/${cmw}\{\\CCCC7\}}
     fl_0=${fl_0//${cmw}rR/${cmw}\{\\CCCC9\}}
   done
################


# juktakkhor
 fl_0=`echo "${fl_0}"|tr -d " "|sed -e 's/k\/k/{\\\\CCCC199}/g'`


   fl_0=${fl_0//k\/T/\{\\CCCC27\}}
   fl_0=${fl_0//k\/th/k\{\\CCCC195\}}
   fl_0=${fl_0//k\/t/\{\\CCCC182\}}
   fl_0=${fl_0//k\/r/\{\\CCCC176\}}
   fl_0=${fl_0//kR/\{\\CCCC176\}}
   fl_0=${fl_0//kK\/n/\{\\CCCC225\}}
#   fl_0=${fl_0//kK\/m/x\{\\CCCC136\}}
   fl_0=${fl_0//kK/x}
   fl_0=${fl_0//k\/s/\{\\CCCC203\}}
   fl_0=${fl_0//g\/g/\{\\CCCC30\}}
   fl_0=${fl_0//g\/dh/\{\\CCCC31\}}
   fl_0=${fl_0//g\/Y/\{\\CCCC179\}}
   fl_0=${fl_0//NG\/kh/\{\\CCCC159\}}
   fl_0=${fl_0//NG\/k/\{\\CCCC148\}} 
   fl_0=${fl_0//NG\/gh/\{\\CCCC202\}}
   fl_0=${fl_0//NG\/g/\{\\CCCC160\}}
   fl_0=${fl_0//NG\/t/f\{\\CCCC174\}}


   fl_0=${fl_0//c\/ch/\{\\CCCC210\}}


   fl_0=${fl_0//c\/c/\{\\CCCC152\}}
   fl_0=${fl_0//j\/jh/\{\\CCCC36\}}
   fl_0=${fl_0//j\/j/\{\\CCCC153\}}
   fl_0=${fl_0//j\/NJ/\{\\CCCC179\}}
   fl_0=${fl_0//NJ\/ch/\{\\CCCC230\}}
   fl_0=${fl_0//NJ\/c/\{\\CCCC172\}}
   fl_0=${fl_0//NJ\/jh/\{\\CCCC173\}}
   fl_0=${fl_0//NJ\/j/\{\\CCCC189\}}
   fl_0=${fl_0//T\/T/\{\\CCCC161\}}
   fl_0=${fl_0//D\/g/\{\\CCCC127\}}
   fl_0=${fl_0//D\/D/\{\\CCCC236\}}
   fl_0=${fl_0//N\/Th/\{\\CCCC220\}}
   fl_0=${fl_0//N\/T/\{\\CCCC242\}}
   fl_0=${fl_0//N\/Dh/\{\\CCCC188\}}
   fl_0=${fl_0//N\/D/\{\\CCCC187\}}
   fl_0=${fl_0//N\/N/\{\\CCCC186\}}



#   fl_0=${fl_0//n\/tR/\{\\CCCC232\}}
#   fl_0=${fl_0//s\/tR/\{\\CCCC233\}}
   fl_0=${fl_0//tR/\{\\CCCC177\}}
#   fl_0=${fl_0//t\/\//t\/}
   fl_0=${fl_0//t\/\/s\//t+s\/}

   fl_0=${fl_0//t\/k/k{\\CCCC151\}}
   fl_0=${fl_0//t\/th/th\{\\CCCC151\}}
   fl_0=${fl_0//t\/t/\{\\CCCC154\}}
   fl_0=${fl_0//t\/n/\{\\CCCC162\}}
   fl_0=${fl_0//t\/p/\{\\CCCC28\}}
   fl_0=${fl_0//t\/m/\{\\CCCC163\}}
#   fl_0=${fl_0//tW/b\{\\CCCC151\}}
#   fl_0=${fl_0//t\/b/b\{\\CCCC151\}}
   fl_0=${fl_0//t\/S/z\{\\CCCC151\}}
   fl_0=${fl_0//t\/sh/z\{\\CCCC151\}}
   fl_0=${fl_0//t\/s/\{\\CCCC241\}}
#   fl_0=${fl_0//t\//t+}
   fl_0=${fl_0//d\/g/\{\\CCCC234\}}
   fl_0=${fl_0//d\/dh/\{\\CCCC170\}}
   fl_0=${fl_0//d\/d/\{\\CCCC169\}}
#   fl_0=${fl_0//d\/bh/\{\\CCCC214\}}
   fl_0=${fl_0//n\/dh/\{\\CCCC166\}}
   fl_0=${fl_0//dhY/\{\\CCCC178\}}
   fl_0=${fl_0//n\/th/\{\\CCCC227\}}
   fl_0=${fl_0//n\/t/\{\\CCCC226\}}
   fl_0=${fl_0//n\/d/\{\\CCCC165\}}


   fl_0=${fl_0//p\/t/\{\\CCCC181\}}
   fl_0=${fl_0//p\/p/\{\\CCCC183\}}
   fl_0=${fl_0//p\/s/\{\\CCCC197\}}
   fl_0=${fl_0//b\/j/\{\\CCCC34\}}
   fl_0=${fl_0//b\/dh/\{\\CCCC29\}}
   fl_0=${fl_0//b\/d/\{\\CCCC167\}}
   fl_0=${fl_0//b\/b/\{\\CCCC168\}}
   fl_0=${fl_0//m\/N/\{\\C1C2C3\}}
   fl_0=${fl_0//m\/ph/\{\\CCCC191\}}
   fl_0=${fl_0//m\/p/\{\\CCCC190\}}
   fl_0=${fl_0//m\/bh/m\{\\CCCC239\}}
   fl_0=${fl_0//m\/m/\{\\CCCC215\}}
   fl_0=${fl_0//mM/\{\\CCCC215\}}
   fl_0=${fl_0//r\//\{\\CCCC147\}}
   fl_0=${fl_0//l\/k/\{\\CCCC219\}}
   fl_0=${fl_0//l\/g/\{\\C4C5C6\}}
   fl_0=${fl_0//l\/ph/\{\\CCCC223\}}
   fl_0=${fl_0//l\/p/\{\\CCCC221\}}
   fl_0=${fl_0//l\/l/\{\\CCCC237\}}
   fl_0=${fl_0//lL/\{\\CCCC237\}}
   fl_0=${fl_0//sh/S}


#(for ref)        -e 's/r\//{\\C0C0C0{C9}}{\\CCCC147}{\\C0C0C0{-C9}}/g' \

   fl_0=${fl_0//S\/ch/\{\\CCCC229\}}
   fl_0=${fl_0//S\/c/\{\\CCCC205\}}
   fl_0=${fl_0//S\/T/\{\\CCCC204\}} 
   fl_0=${fl_0//Sh\/k/\{\\CCCC200\}} 
   fl_0=${fl_0//Sh\/Th/\{\\CCCC207\}}
   fl_0=${fl_0//Sh\/T/\{\\CCCC198\}}
   fl_0=${fl_0//Sh\/N/\{\\CCCC16\}}
   fl_0=${fl_0//Sh\/ph/\{\\CCCC222\}}
   fl_0=${fl_0//Sh\/p/\{\\CCCC184\}}
   fl_0=${fl_0//s\/kh/\{\\CCCC26\}}
   fl_0=${fl_0//s\/k/\{\\CCCC201\}}
   fl_0=${fl_0//s\/th/\{\\CCCC228\}}
   fl_0=${fl_0//s\/t/\{\\CCCC180\}}
   fl_0=${fl_0//s\/ph/\{\\CCCC224\}}
   fl_0=${fl_0//s\/p/\{\\CCCC185\}}
   fl_0=${fl_0//s\/s/\{\\C7C8C9\}}

   fl_0=${fl_0//H\/[nN]/\{\\CCCC94\}}
   fl_0=${fl_0//[nN]\/H/\{\\CCCC94\}}
   fl_0=${fl_0//HW/\{\\CCCC95\}}
   fl_0=${fl_0//H\/b/\{\\CCCC95\}}
   fl_0=${fl_0//HM/\{\\CCCC25\}}
   fl_0=${fl_0//m\/H/\{\\CCCC25\}}
   fl_0=${fl_0//rh\/g/\{\\CCCC126\}}
 
   fl_0=${fl_0//W/\{\\CCCC164\}}
   fl_0=${fl_0//\/b/\{\\CCCC164\}}
   fl_0=${fl_0//\/n/\{\\CCCC240\}}
   fl_0=${fl_0//\/bh/\{\\CCCC239\}}
   fl_0=${fl_0//\/m/\{\\CCCC136\}}
   fl_0=${fl_0//M/\{\\CCCC136\}}
   fl_0=${fl_0//Y/\{\\CCCC138\}}
   fl_0=${fl_0//RR/\{\\CCCC128\}}
   fl_0=${fl_0//rR/\{\\CCCC129\}}
   fl_0=${fl_0//R/\{\\CCCC146\}}
   fl_0=${fl_0//\/r/\{\\CCCC146\}}
   fl_0=${fl_0//\/l/\{\\CCCC155\}}
   fl_0=${fl_0//L/\{\\CCCC155\}}

   fl_0=${fl_0//t\/\/s/\{\\CCCC241\}}
   fl_0=${fl_0//t\/\//t+}
####sworoborno - cinho


   fl_0=${fl_0//A/ZZZZ}
   fl_0=${fl_0//a/\{A\}}
   fl_0=${fl_0//ZZZZ/a}
   fl_0=${fl_0//ii/X}
   fl_0=${fl_0//uu/\{\\CCCC90\}}
       fl_0=${fl_0//NNG/Z-Z-Z-}      ##extra care for NNG
   fl_0=${fl_0//NNou/\{\\CCCC249\}}
   fl_0=${fl_0//ouNN/\{\\CCCC249\}}
   fl_0=${fl_0//ou/'*'}
   fl_0=${fl_0//u/\{\\CCCC93\}}
   fl_0=${fl_0//e/\<}
   fl_0=${fl_0//NNoi/\{\\CCCC247\}}
   fl_0=${fl_0//oiNN/\{\\CCCC247\}}
   fl_0=${fl_0//oi/\>}
   fl_0=${fl_0//NNi/\{\\CCCC243\}}
   fl_0=${fl_0//iNN/\{\\CCCC243\}}
   fl_0=${fl_0//i/\{\\CCCC91\}}
   fl_0=${fl_0//o/}

   fl_0=${fl_0//NN\{\\CCCC171\}/\{\\CCCC245\}}
   fl_0=${fl_0//\{\\CCCC171\}NN/\{\\CCCC245\}}
   fl_0=${fl_0//NN\{\\CCCC246\}/\{\\CCCC248\}}
   fl_0=${fl_0//\{\\CCCC246\}NN/\{\\CCCC248\}}
   fl_0=${fl_0//NN\{\\CCCC19\}/\{\\CCCC6\}}
   fl_0=${fl_0//\{\\CCCC19\}NN/\{\\CCCC6\}}


##### sworoborno

   fl_0=${fl_0//II/ZZZ}
   fl_0=${fl_0//UU/ZXZ}
   fl_0=${fl_0//OU/XZX}
   fl_0=${fl_0//U/u}
#   fl_0=${fl_0//RR/\{\\CCCC128\}}
   fl_0=${fl_0//E/e}
   fl_0=${fl_0//OI/E}
   fl_0=${fl_0//O/o}
   fl_0=${fl_0//I/i}
   fl_0=${fl_0//ZZZ/I}
   fl_0=${fl_0//ZXZ/U}
   fl_0=${fl_0//XZX/O}


###########bynjonborno

   fl_0=${fl_0//kh/K}
   fl_0=${fl_0//gh/G}
   fl_0=${fl_0//Z-Z-Z-/M}    ## set NNG now
   fl_0=${fl_0//NG/f}
   fl_0=${fl_0//NJ/F}
   fl_0=${fl_0//ch/C}
   fl_0=${fl_0//y/Y}
   fl_0=${fl_0//J/y}
   fl_0=${fl_0//l/L}
   fl_0=${fl_0//L\~/l}
   fl_0=${fl_0//jh/J}
   fl_0=${fl_0//Th/Q}
   fl_0=${fl_0//T/q}
   fl_0=${fl_0//Dh/W}
   fl_0=${fl_0//D/w}
   fl_0=${fl_0//th/T}
   fl_0=${fl_0//dh/D}
   fl_0=${fl_0//ph/P}
   fl_0=${fl_0//bh/B}
   fl_0=${fl_0//S/z}
   fl_0=${fl_0//sh/z}

#     -e 's/\.b/\{\\\\CCCC20}/g' \
   fl_0=${fl_0//Sh/S}
   fl_0=${fl_0//shh/S}
   fl_0=${fl_0//zh/S}
   fl_0=${fl_0//rhh/W\{\\CCCC144\}}
   fl_0=${fl_0//rh/w\{\\CCCC144\}}
   fl_0=${fl_0//NN//}
#   fl_0=${fl_0//:\//+}        ###  taken above
   fl_0=${fl_0//h/\{\\CCCC250\}}
   fl_0=${fl_0//H/h}
   fl_0=${fl_0//\~a/\{\\CCCC253\}}


# .b: petkata b; l~: other l; ~a: avogroHo;

#   fl_0=${fl_0//\.\./\.A}
#   fl_0=${fl_0//\`\`/\`\\\!\`}
#   fl_0=${fl_0//\'\'/\'\\\!\'}
#   fl_0=${fl_0//\"/\'\\\!\'}
#   fl_0=${fl_0//---/\{\\CCCC3\}}
#   fl_0=${fl_0//--/\{\\CCCC2\}}
   fl_0=${fl_0//\[/\{\\CCCC4\}}
   fl_0=${fl_0//\]/\{\\CCCC5\}}
#   fl_0=${fl_0//\\/\|}


   fl_0=${fl_0//CCCC/char}
   fl_0=${fl_0//C1C2C3/oSp\{B\}}
   fl_0=${fl_0//C4C5C6/oSp\{I\}}
   fl_0=${fl_0//C7C8C9/oSp\{H\}}
  fl_0=${fl_0//C0C0C0/\\lower .3ex\\hbox}

fi

# fl_0=`echo "${wrd[*]}"|tr -d " "`
# fl_0=${fl_0//\*P/\*\{oi\}}                # replace only *P 
# fl_txt="$fl_txt $fl_0"
  fl_txt="${fl_txt}${fl_0}${wrd[$[ijk]]}"

   istrt=$ijk
 done




#fl_txt=$(echo $fl_txt)  
#echo $fl_txt $ZZZ_3
echo $fl_txt

return
}



test_ei=0                                         # =1 => convert

klng=0

#be careful about \%, \\, ^ in the file - they should be preserved.
sed -e 's/\\%/\*VYZX25/g' -e 's/\\/\\\\/g' -e 's/\^/\*VYZX94/g' -e 's/ /'^'/g' $infile| \
    while read AAA
       do

          klng=$[klng+1]
	  if [ "$klng" = "$numl" ]
	   then
	     echo -n '#'
	     klng=0
	  fi


           case "$AAA" in 
            *'%<ei>'*) test_ei=1                  ## shuru
                       if test "$no_ei" = '1'
             		 then
			  test_ei=10
        		fi
        		;;	
            *'%</ei>'*) test_ei=0                  ## shesh
                       if test "$no_ei" = '1'
             		 then
			  test_ei=01
        		fi
        		;;	
            *) : ;;                                 ## continue
	   esac

	  case "$test_ei" in

  	  1)
            ZZZ_1=`echo "$AAA"|cut -f1 -d'%'`       ## left of '%' matters
            ZZZ_1=${ZZZ_1:+$ZZZ_1'^'}             ##if not empty add ^ for end 
            ZZZ_2=`echo "$AAA"|cut -s -f2- -d'%'`   ## store right of '%'
            ZZZ_2=${ZZZ_2:+%$ZZZ_2}               ## if no '%', ZZZ_2 is 
                                                  ##  empty, else insert '%'

             ZZZ_1=`ei_thik "$ZZZ_1"`
	     echo "$ZZZ_1""$ZZZ_2"|tr '^' ' ' |sed -e 's/\*VYZX25/\\%/g' -e "s/\*VYZX94/\^/g">>$outfile
	  ;;

	  0) echo "$AAA"|tr '^' ' '|sed -e 's/\*VYZX25/\\%/g' >>$outfile
     	   ;;
         
	  10) test_ei=1;;
	  01) test_ei=0;;

	 esac
        done  3>&1

echo " "
echo " done (seicoriya  version number: $vrsn)"
echo "output file: $outfile"
echo "check http://www.iopb.res.in/~somen/Prog/seicoriya  for current version of seicoriya"
exit 0