#! /bin/bash
# This script reads all *.smi files in the current folder and tries to
# curate inaccuracies for a number of frequent functional groups that are
# not represented by Open Babel according to our conventions.
# The curation progress and the final result are presented on the screen
# for approval or not of the human editor, that may also perform small
# manual fixes.
# If the SMILES has not been modified, it is moved to the 'unchanged' folder.
# If changes have been made, it is moved to 'finished' if the human editor
# decides that the result is acceptable as final representation of the compound
# or to 'partial' otherwise. 

[ -d unchanged ] || mkdir unchanged
[ -d finished ] || mkdir finished
[ -d partial ] || mkdir partial
function chequear() {
  if [ "$tmp" == "$tmp1" ]; then
    return
  fi
  echo -n $texto
  echo $i
  echo $tmp1
  echo $tmp
  echo 'Accept change? (y/n/e for quick edit): '
  read answer
  if [ "$answer" == e ]; then
    echo $tmp > temporal
    vim temporal
    tmp=`cat temporal`
    tmp1=$tmp
  elif [ "$answer" != n ]; then
    tmp1=$tmp
  else
    tmp=$tmp1
  fi
}
for i in `ls *.smi` ; do
   tmp0=`cat $i | cut -f1`
   tmp1=$tmp0
   tmp=$tmp1
   key=`cat $i | cut -f2`
#### Perchlorate
   dato=`echo $tmp1 | obgrep 'Cl~O' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep [[]Cl[]]\([[]O[]]\)\([[]O[]]\)\([[]O[]]\)[[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Cl[]]\([[]O[]]\)\([[]O[]]\)\([[]O[]]\)[[]O[]]/Cl\(=O\)\(=O\)\(=O\)[O-]/`
     done
     while echo $tmp | grep [[]O[]][[]Cl[]]\([[]O[]]\)\([[]O[]]\)[[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]O[]][[]Cl[]]\([[]O[]]\)\([[]O[]]\)[[]O[]]/[O-]Cl\(=O\)\(=O\)=O/`
     done
     texto="Perchlorate: "
     chequear
   fi
### Tetrafluoruroborate
   dato=`echo $tmp1 | obgrep 'B(F)(F)(F)F' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep FB\(F\)\(F\)F > /dev/null; do   
        tmp=`echo $tmp | sed -e s/FB\(F\)\(F\)F/[B]\(F\)\(F\)\(F\)[F-]/`
     done
     while echo $tmp | grep B\(F\)\(F\)\(F\)F > /dev/null; do   
        tmp=`echo $tmp | sed -e s/B\(F\)\(F\)\(F\)F/[B]\(F\)\(F\)\(F\)[F-]/`
     done
     texto="Tetrafluoruroborate: "
     chequear
   fi
### Nitrate
   dato=`echo $tmp1 | obgrep 'N(~O)(~O)O' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep N\(=O\)\(=O\)[[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N\(=O\)\(=O\)[[]O[]]/N\(=O\)\(=O\)[O-]/`
     done
     while echo $tmp | grep O=N\(=O\)[[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/O=N\(=O\)[[]O[]]/O=N\(=O\)[O-]/`
     done
     texto="Nitrate: "
     chequear
   fi
### Hexafluorurophosphate
   dato=`echo $tmp1 | obgrep 'P(F)(F)(F)(F)(F)F' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep FP\(F\)\(F\)\(F\)\(F\)F > /dev/null; do   
        tmp=`echo $tmp | sed -e s/FP\(F\)\(F\)\(F\)\(F\)F/[P]\(F\)\(F\)\(F\)\(F\)\(F\)[F-]/`
     done
     while echo $tmp | grep P\(F\)\(F\)\(F\)\(F\)\(F\)F > /dev/null; do   
        tmp=`echo $tmp | sed -e s/P\(F\)\(F\)\(F\)\(F\)\(F\)F/[P]\(F\)\(F\)\(F\)\(F\)\(F\)[F-]/`
     done
     texto="Hexafluorurophosphate: "
     chequear
   fi
### Tetraphenylborate
   dato=`echo $tmp1 | obgrep 'B(c)(c)(c)c' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep Br > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Br/Wr/`
     done
     while echo $tmp | grep [[]B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]B/XX/`
     done
     while echo $tmp | grep B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/B/ZZ/`
     done
     while echo $tmp | grep ZZ > /dev/null; do   
        tmp=`echo $tmp | sed -e s/ZZ/[B-]/`
     done
     while echo $tmp | grep XX > /dev/null; do   
        tmp=`echo $tmp | sed -e s/XX/[B/`
     done
     while echo $tmp | grep Wr > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Wr/Br/`
     done
     texto="Tetraphenylborate: "
     chequear
   fi
### Imidazolylborate
   dato1=`echo $tmp1 | obgrep 'B(NN)(NN)' --`
   dato2=`echo $tmp1 | obgrep 'B(nn)(nn)' --`
   dato="$dato1""$dato2"
   if [ "$dato" != "" ]; then
     while echo $tmp | grep Br > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Br/Wr/`
     done
     while echo $tmp | grep [[]B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]B/XX/`
     done
     while echo $tmp | grep B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/B/ZZ/`
     done
     while echo $tmp | grep ZZ > /dev/null; do   
        tmp=`echo $tmp | sed -e s/ZZ/[BH]/`
     done
     while echo $tmp | grep XX > /dev/null; do   
        tmp=`echo $tmp | sed -e s/XX/[B/`
     done
     while echo $tmp | grep Wr > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Wr/Br/`
     done
     texto="Imidazolylborate: "
     chequear
   fi
### Acetonitrile
   echo $tmp1 | grep [[]N[]][[]C[]]C >/dev/null && {   
     while echo $tmp | grep [[]N[]][[]C[]]C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]N[]][[]C[]]C/[N]#CC/`
     done
     texto="Acetonitrile: "
     chequear
   }
### Azide
   echo $tmp1 | grep [[]N[]]=[[]N+[]]=N >/dev/null && {   
     while echo $tmp | grep [[]N[]]=[[]N+[]]=N > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]N[]]=[[]N+[]]=N/N#N=N/`
     done
     texto="Azide: "
     chequear
   }
   echo $tmp1 | grep N=[[]N+[]]=[[]N[]] >/dev/null && {   
     while echo $tmp | grep N=[[]N+[]]=[[]N[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N=[[]N+[]]=[[]N[]]/N=N#N/`
     done
     texto="Azide: "
     chequear
   }
### Coordinated chloride
   echo $tmp1 | grep [[]Cl-[]] | grep -v ^[[]Cl-[]][.] | grep -v [.][[]Cl-[]] >/dev/null && {   
     while echo $tmp | grep [[]Cl-[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Cl-[]]/Cl/`
     done
     texto="Coordinated chloride: "
     chequear
   }
### Coordinated bromide
   echo $tmp1 | grep [[]Br-[]] | grep -v ^[[]Br-[]][.] | grep -v [.][[]Br-[]] >/dev/null && {   
     while echo $tmp | grep [[]Br-[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Br-[]]/Br/`
     done
     texto="Coordinated bromide: "
     chequear
   }
### Coordinated iodide
   echo $tmp1 | grep [[]I-[]] | grep -v ^[[]I-[]][.] | grep -v [.][[]I-[]] >/dev/null && {   
     while echo $tmp | grep [[]I-[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]I-[]]/I/`
     done
     texto="Coordinated iodide: "
     chequear
   }
### Common tetrachlorometalates
   dato=`echo $tmp1 | obgrep '[Fe,Al,Co,Zn,Cd,Cu,Au,Pd,Pt](Cl)(Cl)(Cl)Cl' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep [[]Fe[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Fe[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Fe]\(Cl\)\(Cl\)\(Cl\)[Cl-]/`
     done
     while echo $tmp | grep [[]Al[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Al[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Al]\(Cl\)\(Cl\)\(Cl\)[Cl-]/`
     done
     while echo $tmp | grep [[]Au[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Au[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Au]\(Cl\)\(Cl\)\(Cl\)[Cl-]/`
     done
     while echo $tmp | grep [[]Co[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Co[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Co]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     while echo $tmp | grep [[]Zn[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Zn[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Zn]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     while echo $tmp | grep [[]Cd[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Cd[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Cd]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     while echo $tmp | grep [[]Pd[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Pd[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Pd]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     while echo $tmp | grep [[]Pt[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Pt[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Pt]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     while echo $tmp | grep [[]Cu[]]\(Cl\)\(Cl\)\(Cl\)Cl > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]Cu[]]\(Cl\)\(Cl\)\(Cl\)Cl/[Cu]\(Cl\)\(Cl\)\([Cl-]\)[Cl-]/`
     done
     texto="Tetrachlorometalate: "
     chequear
   fi
### Metal carbonyl
   echo $tmp1 | grep [[]C[]][[]O[]] >/dev/null && {   
     while echo $tmp | grep [[]C[]][[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]C[]][[]O[]]/C#[O]/`
     done
     texto="Metal carbonyl: "
     chequear
   }
   echo $tmp1 | grep [[]C[]]\([[]O[]]\) >/dev/null && {   
     while echo $tmp | grep [[]C[]]\([[]O[]]\) > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]C[]]\([[]O[]]\)/C\(#[O]\)/`
     done
     texto="Metal carbonyl: "
     chequear
   }
### Metal isocyanide
   echo $tmp1 | grep [[]C[]][[]N[]] >/dev/null && {   
     while echo $tmp | grep [[]C[]][[]N[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]C[]][[]N[]]/C#[N]/`
     done
     texto="Metal isocyanide: "
     chequear
   }
   echo $tmp1 | grep [[]N[]][[]C[]] >/dev/null && {   
     while echo $tmp | grep [[]N[]][[]C[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]N[]][[]C[]]/[N]#C/`
     done
     texto="Metal isocyanide: "
     chequear
   }
### Borane
   dato=`echo $tmp1 | obgrep 'B(B)(B)B' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep Br > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Br/Wr/`
     done
     while echo $tmp | grep [[]B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]B/XX/`
     done
     while echo $tmp | grep B > /dev/null; do   
        tmp=`echo $tmp | sed -e s/B/ZZ/`
     done
     while echo $tmp | grep ZZ > /dev/null; do   
        tmp=`echo $tmp | sed -e s/ZZ/[BH]/`
     done
     while echo $tmp | grep XX > /dev/null; do   
        tmp=`echo $tmp | sed -e s/XX/[B/`
     done
     while echo $tmp | grep Wr > /dev/null; do   
        tmp=`echo $tmp | sed -e s/Wr/Br/`
     done
     texto="Borane: "
     chequear
   fi
### Free imine
   echo $tmp1 | grep [[]CH[]][[]N[]][[]N[]][[]CH[]] >/dev/null && {   
     while echo $tmp | grep [[]CH[]][[]N[]][[]N[]][[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s#[[]CH[]][[]N[]][[]N[]][[]CH[]]#/C=N/N=C/#`
     done
     texto="Imine: "
     chequear
   }
   echo $tmp1 | grep [[]CH[]][[]N[]] >/dev/null && {   
     while echo $tmp | grep [[]CH[]][[]N[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s#[[]CH[]][[]N[]]#/C=N/#`
     done
     texto="Imine: "
     chequear
   }
   echo $tmp1 | grep [[]N[]][[]CH[]] >/dev/null && {   
     while echo $tmp | grep [[]N[]][[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s#[[]N[]][[]CH[]]#/N=C/#`
     done
     texto="Imine: "
     chequear
   }
### Diazo compound
   echo $tmp1 | grep [[]N[]][[]N[]] >/dev/null && {   
     while echo $tmp | grep [[]N[]][[]N[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s#[[]N[]][[]N[]]#/N=N/#`
     done
     texto="Diazo compound: "
     chequear
   }
   echo $tmp1 | grep [[]N[]]\([[]N[]] >/dev/null && {   
     while echo $tmp | grep [[]N[]]\([[]N[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]N[]]\([[]N[]]/N\(=N/`
     done
     texto="Diazo compound: "
     chequear
   }
### Nitroso group
   echo $tmp1 | grep [[]N[]][[]O[]] >/dev/null && {   
     while echo $tmp | grep [[]N[]][[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]N[]][[]O[]]/N=O/`
     done
     texto="Nitroso: "
     chequear
   }
### Coordinated DMF
   echo $tmp1 | grep O[[]CH[]]N\(C\)C >/dev/null && {   
     while echo $tmp | grep O[[]CH[]]N\(C\)C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/O[[]CH[]]N\(C\)C/[O]=CN\(C\)C/`
     done
     texto="Coordinated DMF: "
     chequear
   }
### DMSO
   dato=`echo $tmp1 | obgrep 'S(~O)(C)C' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep O[[]S[]]\(C\)C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/O[[]S[]]\(C\)C/[O]=S\(C\)C/`
     done
     while echo $tmp | grep [[]O[]][[]S[]]\(C\)C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]O[]][[]S[]]\(C\)C/[O]=S\(C\)C/`
     done
     while echo $tmp | grep [[]S[]]\([[]O[]]\)\(C\)C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]S[]]\([[]O[]]\)\(C\)C/S\(=O\)\(C\)C/`
     done
     while echo $tmp | grep C[[]S[]]\([[]O[]]\)\C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/C[[]S[]]\([[]O[]]\)C/S\(=O\)\(C\)C/`
     done
     while echo $tmp | grep S\([[]O[]]\)\(C\)C > /dev/null; do   
        tmp=`echo $tmp | sed -e s/S\([[]O[]]\)\(C\)C/[S]\(=O\)\(C\)C/`
     done
     texto="DMSO: "
     chequear
   fi
### Coordinated imine
   echo $tmp1 | grep [[]CH[]]N[123456789%] >/dev/null && {
     while echo $tmp | grep [[]CH[]]N1 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N1/C=[N]1/`
     done
     while echo $tmp | grep [[]CH[]]N2 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N2/C=[N]2/`
     done
     while echo $tmp | grep [[]CH[]]N3 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N3/C=[N]3/`
     done
     while echo $tmp | grep [[]CH[]]N4 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N4/C=[N]4/`
     done
     while echo $tmp | grep [[]CH[]]N5 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N5/C=[N]5/`
     done
     while echo $tmp | grep [[]CH[]]N6 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N6/C=[N]6/`
     done
     while echo $tmp | grep [[]CH[]]N7 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N7/C=[N]7/`
     done
     while echo $tmp | grep [[]CH[]]N8 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N8/C=[N]8/`
     done
     while echo $tmp | grep [[]CH[]]N9 > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N9/C=[N]9/`
     done
     while echo $tmp | grep [[]CH[]]N% > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]CH[]]N%/C=[N]%/`
     done
     texto="Coordinated imine: "
     chequear
   }   
   echo $tmp1 | grep N[123456789][[]CH[]] >/dev/null && {
     while echo $tmp | grep N1[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N1[[]CH[]]/[N]1=C/`
     done
     while echo $tmp | grep N2[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N2[[]CH[]]/[N]2=C/`
     done
     while echo $tmp | grep N3[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N3[[]CH[]]/[N]3=C/`
     done
     while echo $tmp | grep N4[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N4[[]CH[]]/[N]4=C/`
     done
     while echo $tmp | grep N5[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N5[[]CH[]]/[N]5=C/`
     done
     while echo $tmp | grep N6[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N6[[]CH[]]/[N]6=C/`
     done
     while echo $tmp | grep N7[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N7[[]CH[]]/[N]7=C/`
     done
     while echo $tmp | grep N8[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N8[[]CH[]]/[N]8=C/`
     done
     while echo $tmp | grep N9[[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N9[[]CH[]]/[N]9=C/`
     done
     texto="Coordinated imine: "
     chequear
   }   
   echo $tmp1 | grep N\([[]CH[]] >/dev/null && {
     while echo $tmp | grep N\([[]CH[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/N\([[]CH[]]/[N]\(=C/`
     done
     texto="Coordinated imine: "
     chequear
   }   
### Phosphine complexes
   echo $tmp1 | grep P >/dev/null && {   
     yuyu=`echo $tmp1 | sed -e s/[[]P[]]/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pd/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pt/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pb/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Po/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pr/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pa/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pm/xxx/g`
     yuyu=`echo $yuyu | sed -e s/Pu/xxx/g`
     dato=`echo $yuyu | grep P`
     if [ "$dato" != "" ]; then
       dato1=`echo $tmp1 | obgrep '[Li,Na,K,Rb,Cs]P' --`
       dato2=`echo $tmp1 | obgrep '[Be,Mg,Ca,Sr,Ba,Ra]P' --`
       dato3=`echo $tmp1 | obgrep '[Sc,Y,La,Ce,Pr,Nd,Pm,Sm]P' --`
       dato4=`echo $tmp1 | obgrep '[Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu]P' --`
       dato5=`echo $tmp1 | obgrep '[Ti,Zr,Hf,V,Nb,Ta]P' --`
       dato6=`echo $tmp1 | obgrep '[Cr,Mo,W,Mn,Tc,Re]P' --`
       dato7=`echo $tmp1 | obgrep '[Fe,Ru,Os,Co,Rh,Ir]P' --`
       dato8=`echo $tmp1 | obgrep '[Ni,Pd,Pt,Cu,Ag,Au]P' --`
       dato9=`echo $tmp1 | obgrep '[Zn,Cd,Hg,B,Al,Ga,In,Tl]P' --`
       dato10=`echo $tmp1 | obgrep '[Ge,Sn,Pb]P' --`
       dato="$dato1""$dato2""$dato3""$dato4""$dato5""$dato6""$dato7""$dato8""$dato9""$dato10"
       if [ "$dato" != "" ]; then
         while echo $tmp | grep [[]P > /dev/null; do   
            tmp=`echo $tmp | sed -e s/[[]P/XX/`
         done
         while echo $tmp | grep P > /dev/null; do   
            tmp=`echo $tmp | sed -e s/P/ZZ/`
         done
         while echo $tmp | grep ZZ > /dev/null; do   
            tmp=`echo $tmp | sed -e s/ZZ/[P]/`
         done
         while echo $tmp | grep XX > /dev/null; do   
            tmp=`echo $tmp | sed -e s/XX/[P/`
         done
         texto="Phosphine complex: "
         chequear
       fi
     fi
   }
### Sulfonate
   echo $tmp1 | grep S\(=O\)\(=O\)[[]O[]] >/dev/null && {   
     while echo $tmp | grep S\(=O\)\(=O\)[[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/S\(=O\)\(=O\)[[]O[]]/S\(=O\)\(=O\)[O-]/`
     done
     texto="Sulfonate: "
     chequear
   }
   echo $tmp1 | grep S\(=O\)\(=O\)\([[]O[]]\) >/dev/null && {   
     while echo $tmp | grep S\(=O\)\(=O\)\([[]O[]]\) > /dev/null; do   
        tmp=`echo $tmp | sed -e s/S\(=O\)\(=O\)\([[]O[]]\)/S\(=O\)\(=O\)\([O-]\)/`
     done
     texto="Sulfonate: "
     chequear
   }
### Insterstitial water
   echo $tmp1 | grep [.][[]O[]] >/dev/null && {   
     while echo $tmp | grep [.][[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[.][[]O[]]/.O/`
     done
     texto="Instertitial water: "
     chequear
   }
### Oxygen double bonded to some metals
   dato=`echo $tmp1 | obgrep '[V,Cr,Mo,W,Tc,Re,Th,U,Np,Pu]~O' --`
   if [ "$dato" != "" ]; then
     echo $tmp1 | grep [[]O[]]\) >/dev/null && {   
       while echo $tmp | grep [[]O[]]\) > /dev/null; do   
          tmp=`echo $tmp | sed -e s/[[]O[]]\)/=O\)/`
       done
     }
     echo $tmp1 | grep [[]O[]][.] >/dev/null && {   
       while echo $tmp | grep [[]O[]][.] > /dev/null; do   
          tmp=`echo $tmp | sed -e s/[[]O[]][.]/=O./`
       done
     }
     echo $tmp1 | grep [[]O[]][[] >/dev/null && {   
       while echo $tmp | grep [[]O[]][[] > /dev/null; do   
          tmp=`echo $tmp | sed -e s/[[]O[]][[]/O=[/`
       done
     }
     texto="Metal = O: "
     chequear
   fi
### Sulfoxides
   dato=`echo $tmp1 | obgrep 'S~O' --`
   if [ "$dato" != "" ]; then
     while echo $tmp | grep [[]O[]] | grep [[]S[]] > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]O[]]/=O/`
       tmp=`echo $tmp | sed -e s/[[]S[]]/S/`
     done
     while echo $tmp | grep [[]O[]] | grep S@@ > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]O[]]/=O/`
       tmp=`echo $tmp | sed -e s/[[]S@@H[]]/S/`
     done
     while echo $tmp | grep [[]O[]] | grep S@ > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]O[]]/=O/`
       tmp=`echo $tmp | sed -e s/[[]S@H[]]/S/`
     done
     texto="Sulfoxide: "
     chequear
   fi
### O-anion
   echo $tmp1 | grep [[]O[]] | grep -v C\#[[]O[]] >/dev/null && {   
     while echo $tmp | grep [[]O[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]O[]]/[O-]/`
     done
     texto="Anion with negative oxygen: "
     chequear
   }
### Complexes with ether-type oxygen
   echo $tmp1 | grep O@ >/dev/null && {   
     while echo $tmp | grep [[]O@H[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]O@H[]]/[O]/`
     done
     while echo $tmp | grep [[]O@@H[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]O@@H[]]/[O]/`
     done
     texto="Ether complex: "
     chequear
   }
### Complexes with thioether-type sulphur
   echo $tmp1 | grep S@ >/dev/null && {   
     while echo $tmp | grep [[]S@H[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]S@H[]]/[S]/`
     done
     while echo $tmp | grep [[]S@@H[]] > /dev/null; do   
        tmp=`echo $tmp | sed -e s/[[]S@@H[]]/[S]/`
     done
     texto="Thioether complex: "
     chequear
   }
### End of process
   if [ "$tmp0" == "$tmp1" ]; then
     mv $i unchanged
   else
     answer="e"   
     while [ "$answer" == e ]; do
       echo $i
       echo $tmp0
       echo $tmp1
       echo 'Perfect? (y, e for edit now, f for find/replace a string or n for edit in a future): '
       read answer
       if [ "$answer" == n ]; then
         echo -n $tmp1 > partial/$i
         echo -e "\t"$key >> partial/$i
         rm -f $i
       elif [ "$answer" == e ]; then
         echo $tmp > temporal
         vim temporal
         tmp=`cat temporal`
         tmp1=$tmp
       elif [ "$answer" == f ]; then
         echo -n 'String to find: '
         read findstr
	 echo -n 'String to replace: '
         read replstr
	 tmp=`echo $tmp | sed -e s/$findstr/$replstr/g`
         tmp1=$tmp
         answer="e"
       else
         echo -n $tmp1 > finished/$i
         echo -e "\t"$key >> finished/$i
         rm -f $i
       fi
     done  
   fi
done
