#!/usr/bin/env tcsh
#-----------------------------------------------------------------------------
# UPchange script
#-----------------------------------------------------------------------------
# Copyright (C) 2000,2001
# Associated Universities, Inc. Washington DC, USA.
#
# 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.,
# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
#
# Correspondence concerning AIPS++ should be addressed as follows:
#        Internet email: casa-feedback@nrao.edu.
#        Postal address: AIPS++ Project Office
#                        National Radio Astronomy Observatory
#                        520 Edgemont Road
#                        Charlottesville, VA 22903-2475 USA
#
# $Id$
#
# UPchange script (must have x allowed)
#
set script = "UPchange"
set usage = 'UPchange [-t] [-c] [-r] [-u] [-d] [-n] <typ>'
set htxt  = ("Update all files given in the <typ>.found list (which is" \
	     "normally created by UPfind). Updating means that files" \
	     "will be checked out, changed and checked in again." \
	     "<typ>.done and <typ>.error will contain the files successfully" \
	     "done or not.")
set hsw   = ("-t will run a test mode, where files are checked out," \
	     "   changed, but not checked in again. A file <type>.diff will" \
	     "   be created showing what would have been changed.")
set huse  =  "<typ>.def, <typ>.found, <typ>.sed, <typ>.rcs"
set hcrea =  "<typ>.done, <typ>.error, <typ>.diff"
#
# Get general info
#
if (! $?CASAPATH) then
   echo "No CASAPATH environment set, initialize aips++ first"
   exit 1
endif
set x = "`which UPfind`"
if (-x "$x") then
   set uppath = $x:h
else
   set x = ( $CASAPATH )
   set uppath = $x[1]/$x[2]/bin
endif
unset x
if ("$uppath" == ".") then
    set uppath = `pwd`
endif
if (! -r $uppath/UPinfo) then
   echo "Cannot find the UPinfo source script in $uppath"
   echo "Check your aips++ installation"
endif
source $uppath/UPinfo
if (! $ok) exit 1
#
# Get specific data
#
if (! -r $ldir/${tp}.sed) then
   touch $ldir/${tp}.sed
   if (! $upswQ) then
     echo "Specify the sed patterns used to change the contents of the files "
     echo "in the files in the ${tp}.found list. "
     echo 'Example: s#/Measures/MV#/Quanta/MV#g'
     echo '         s#[.]ac[(][)]##g'
     echo '         s#at_cc[(]#(#g'
     echo '         s#\([^a-zA-Z_]\)assert\([^a-zA-Z_]\)#\1assure\2#g'
   endif
   while (1)
      echo -n 'Change pattern: '
      set x = "$<"
      if ("$x" == "") break
      echo "$x" >> $ldir/${tp}.sed
   end
   unset x
   if (-z $ldir/${tp}.sed) then
      echo "No change patterns specified in ${tp}.sed"
      if (-e $ldir/${tp}.sed) rm -f $ldir/${tp}.sed
      exit 1
   endif
endif

if (! -r $ldir/${tp}.rcs) then
    touch $ldir/${tp}.rcs
    if (! $upswQ) then
      echo "Specify the message lines for the RCS log"
      echo 'Example: Moved includes from Quanta to Measures'
    endif
    while (1)
       echo -n 'Text line: '
       set x = "$<"
       if ("$x" == "") break
       echo "$x" >> $ldir/${tp}.rcs
    end
    if (-z $ldir/${tp}.rcs) then
       echo "No RCS message specified in ${tp}.rcs"
       if (-e $ldir/${tp}.rcs) rm -f $ldir/${tp}.rcs
       exit 1
    endif
    echo "." >> $ldir/${tp}.rcs  
    unset x
endif

if ($upswn) exit 0
#
# Execute
#
cd $ldir

if (! -r $ldir/${tp}.found || -z $ldir/${tp}.found) then
    echo "An empty or no ${tp}.found present"
    exit 1
endif

if (-e $ldir/${tp}.done) rm -f $ldir/${tp}.done
if (-e $ldir/${tp}.error) rm -f $ldir/${tp}.error
touch $ldir/${tp}.done
touch $ldir/${tp}.error
#
# Do the real change or do a dry run of it
#
if (! $upswQE) then
  set a = (`wc $ldir/${tp}.found`)
  echo "$a[1] files will be changed and checked in/out"
  unset a
endif

set xtst = ""
if ($upswt) then
   echo "Note: in test mode -- no actual file check in after change"
   if (-e $ldir/${tp}.diff) rm -f $ldir/${tp}.diff
   touch $ldir/${tp}.diff
   set xtst = "-t $ldir/${tp}.diff"
endif

foreach i (`cat $ldir/${tp}.found`)
  if (! $upswQ) echo "$i started"
  cd $mdir/code/$i:h
  if (-w $i:t) then
     if (! $upswQ) echo "$i had writable local copy"
     echo "$i had writable local copy" >> $ldir/${tp}.error
  else
    if (-e $i:t) rm -f $i:t
    if (! $upswt) then
       $xao -l $i:t
    else
       $xao  $i:t
       if (-e $i:t) chmod 644 $i:t
    endif
    if (-e $i:t) then
       if ($upswt) then
          echo "***** File: $i" >> $ldir/${tp}.diff
       endif
       $uppath/UPsed $xtst $ldir/${tp}.sed $i:t $i:t
       if (! $upswt) then
          $xai $i:t < $ldir/${tp}.rcs
       endif
       echo "$i done" >> $ldir/${tp}.done
       if (-e $i:t) rm -f $i:t
    else
       if (! $upswQ) echo "$i could not be checked out"
       echo "$i could not be checked out" >> $ldir/${tp}.error
    endif
  endif
end

cd $ldir

if (! $upswQE) then
  set a = ( `wc $ldir/${tp}.done` )
  echo $a[1] files in ${tp}.done
  unset a

  set a = ( `wc $ldir/${tp}.error` )
  echo $a[1] files in ${tp}.error
  unset a
endif

exit 0
#
