"****************************************************************************"
"updatetemplates(directory,source,<filename>,<mode>>) - Change a named file  "
"                      for every template directory in layout. Argument 1 is "
"                      the full path of the layout directory. Argument 2 is a"
"                      template directory in that layout that is the source  "
"                      of the named file. Argument 3 is the filename to be   "
"                      changed (default: 'acq.xml'). Argument 4 is the mode  "
"                      (default: 'update'):                                  "
"                         'update'- Alter Files in Exsiting Directories.     "
"                         'remove'- Remove files in Existing Directories.    "
"                         'add'   - Add Files to Empty Directories.          "
"                         'new'   - Add or Update Files in All Directories.  "
"                      The Source file remains unaltered and must exist,     "
"                      even for the remove mode.                             "
"                                                                            "
"Example: updatetemplate('home/myuser/vnmrsys/templates/layout',             "
"                                               'onepul','acq.xml','update') "
"****************************************************************************"

exists($1,'directory'):$e
if ($e < 0.5) then
   write('error','Input Layout Directory %s Does Not Exist',$1)
   abort
endif

$sourcepath = $1
if ($#>1)then 
  $sourcepath = $1 + '/' + $2
endif

exists($sourcepath,'directory'):$e
if ($e < 0.5) then
   write('error','Input Template Directory %s Does Not Exist',$2)
   abort
endif

// Set File Name 

$source = $1 + '/' + $2 + '/acq.xml'
if (($#>2)and($3<>'')) then
   $source = $1 + '/' + $2 + '/' +  $3
endif

// Set Mode

$mode = 'update'
if (($#>3)and(($4='update')or($4='remove')or($4='add')or($4='new'))) then 
   $mode=$4
endif 

write('line3','source = %s\n',$source)

exists($source,'file'):$e
if ($e < 0.5) then
   write('error','Source Directory %s Has No %s',$2,$3)
   abort
endif

$yes=''
write('line3','Change All %s With %s Mode? <y/n>:',$3,$mode)
input:$yes
if ($yes<>'y') then 
   write('line3','Abort Action: No Files Changed')  
   abort
endif

getfile($1):$entrys
write('line3','Layout Directory %s Has %d Entries',$1,$entrys)
$inode = 0
$filename = ''
$ext = ''
while ($inode < $entrys) do
   $inode = $inode + 1
   getfile($1,$inode):$filename,$ext
   $path = $1 + '/' + $filename
   write('line3','ENTRYNAME: %s\n',$filename)
   exists($path,'directory'):$e
   if ($e > 0.5) then 
      $acqfile = $path +  '/' + $3
      $e1=-1
      exists($acqfile,'file'):$e1
      write('line3','e1 = %d\n',$e1)
      if ($source<>$acqfile) then 
         if ($mode='update') then 
            if ($e1>0.5) then
               write('line3','source=%s\n',$source)
               write('line3','acqfile=%s\n',$acqfile)
               $res=-1
               cp($source,$acqfile):$res
               write('line3','UPDATE: %s Updated %d\n',$3,$res)
            else
               write('line3','UPDATE: %s Not in this Template\n',$3)
            endif
         endif
         if ($mode='add') then
            if ($e1<0.5) then 
               $res=-1
               cp($source,$acqfile):$res 
               write('line3','ADD: %s Added to Empty Template %d\n',$3,$res)
            else
               write('line3','ADD: %s Already Present\n',$3)
            endif
         endif
         if ($mode='remove') then 
            if ($e1>0.5) then 
               rm($acqfile)
               write('line3','REMOVE: %s Removed from this Template\n',$3)
            else 
               write('line3','REMOVE: %s Not in this Template\n',$3)
            endif
         endif 
         if ($mode='new') then 
            $res=-1
//            write('line3','source=%s\n',$source)
//            write('line3','acqfile=%s\n',$acqfile)
            cp($source,$acqfile):$res
            write('line3','NEW: %s Added to this Template %d\n',$3,$res)
         endif
      else 
         write('line3','Source File Unchanged')
      endif 
   else
      write('line3',' %s Is Not a Directory',$path)
   endif
endwhile


