BEGIN{
currPath=ENVIRON["PWD"];
}


{ 
  
  if (($1=="mol") && ( ($2=="new") || ($2=="addfile") ) ) {


   if (NF > 2) { #  if and only if a path is given
      # remove any space in the string :

      newline="";
      l=split($0,path,"/"); #split the current line with FS = "/"
      # for fields $2 ... $n-1: suppress any space
      for (i=2;i<=l-1;i++) {
         gsub(" ","",path[i]);
         newline=newline "/" path[i];}
      $0=path[1] "/" newline "/" path[l];

      newLine="";
      # split the 3rd field (the path) in order to be able to retrieve the last one (the filename!)
      # which will be pathName[l]
      l=split($3,pathName,"/"); 

      # loop over the fields of the currentlines (separated by spaces)
      # the 3rd one is the absolute path to be replaced with current one 
      for (i = 1; i <= NF; i++)  {
          # for every field except the 3rd one, get it and add it to the built newline
          if(i != 3) {newLine=newLine FS $i}
          else 
          # for the 3rd field of the currentline (absolute path) replace it with the 
          # real current path on this machine
          {   fileName=pathName[l]; # may be finished with "}" or not ...
              newLine=newLine FS "{"currPath"/"fileName;
              if (index(fileName,"}")==0) {newLine = newLine"}";}
          }  #end of else if(i != 3) {
       };  #end of for (i = 1; i <= NF; i++)
       printf("%s\n",newLine);

    } # end of if (NF > 2) {
    else {print $0;  }
   };  # end of if (($1=="mol") && ( ($2=="new") || ($2=="addfile") ) ) {

   if (($1=="set") && ($2=="charge_file") ) {
      newLine="";
      l=split($4,pathName,"/"); 
      for (i = 0; ++i <= NF;) {
          if(i != 4) {newLine=newLine FS $i}
          else {
              fileName=pathName[l]; # may be finished with "}" or not ...
              sqbracket=sub(/]/,"",fileName); # suppress the ] last character if it exists
              newLine=newLine FS "{"currPath"/"fileName;
              if (index(fileName,"}")==0) {newLine = newLine"}";}
              if (sqbracket==1) {newLine=newLine"]";}
               }  # end of else if(i != 4) 
      };  # end of for (i = 0; ++i <= NF;)
      printf("%s\n",newLine);
    } 
    else {
          if ( !(($1=="mol") && ( ($2=="new") || ($2=="addfile")) ) ) {print $0;} 
         }; 

}


END{}
