#!/bin/csh
#  script which allows to rename the EFPs into unique names
#
#  usage:
#
#  name_efp filename offset
#
#  where the offset is the starting number for the EFP count
#  (default 1)
#
#  Note: Names of polarizable points need not to be unique; however,
#  the script renames them as well
#
#  The backslashes (\) are actually newline escapes because the
#  shell wants to see one command in a line
#
if ($#argv < 1) then
    echo "Usage: name_efp filename offset"
    exit 1
endif
if (! -r $1 || -d $1) then
    echo "File $1 does not exist or is not readable or it is a directory"
    exit 1
endif
# init is the offset
set OFFSET=$2
if (null$OFFSET == null) set OFFSET=1 
#
#  start the awk
#
awk '\
/COORD/{\
   print  #print COORDINATES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           number=i\
           i++\
           $1=sprintf("%.1s%-9s",$1,number)\
           print\
           getline\
           }\
        }\
/MONOPOLES/{\
   print  #print MONOPOLES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           number=i\
           i++\
           $1=sprintf("%.1s%-9s",$1,number)\
           print\
           getline\
           }\
        }\
/DIPOLES/{\
   print  #print DIPOLES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           number=i\
           i++\
           $1=sprintf("%.1s%-9s",$1,number)\
           print\
           getline\
           }\
        }\
/QUADRUPOLES/{\
   print  #print QUADRUPOLES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           if($6==">")\
               {\
                number=i\
                i++\
                $1=sprintf("%.1s%-9s",$1,number)\
                print\
                getline\
                }\
           else\
                {print\
                getline}\
           }\
        }\
/OCTUPOLES/{\
   print  #print OCTUPOLES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           if($6==">")\
               {\
                number=i\
                i++\
                $1=sprintf("%.1s%-9s",$1,number)\
                print\
                getline\
                }\
           else\
                {\
                print\
                getline\
                print\
                getline\
                }\
           }\
        }\
/POLARIZABLE POINTS/{\
   print  #print POLARIZABLE POINTS\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
                number=i\
                i++\
                $1=sprintf("CT%-9s",number)\
                print\
                getline\
                print\
                getline\
                print\
                getline\
                print\
                getline\
        }\
                    }\
# print the rest of the file\
                   {print}\
                          ' init=$OFFSET $1 > $1.renamed
echo "Output written to $1.renamed in the current directory."

