#!/usr/bin/env tcsh
#-----------------------------------------------------------------------------
# UPreident script
#-----------------------------------------------------------------------------
# Copyright (C) 2000,2001,2004
# 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$
#
# UPreident script (must have x allowed)
#
set script = "UPreident"
set usage = 'UPreident [-a] [-t] [-z] [-c] [-r] [-u] [-d] [-n] <typ>'
set htxt  = ("Do a reident for all the templates files in the casa" \
	     "library packages (unless -a switch)." \
	     "<typ>.done and <typ>.error will give the templates files" \
	     "done and not done.")
set hsw   = ("-a do all templates files in all packages" \
	     "-z use the -z switch on the reident program to renumber " \
	     "   all the templates files." \
	     "-t do a dry run, giving the differences in <type>.diff")
set huse  =  "<typ>.def, <typ>.tsel"
set hcrea =  "<typ>.done, <typ>.error, <typ>/log, <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
#
cd $ldir
if (! -r $ldir/${tp}.tsel) then
   if ($upswa) then
      $uppath/UPfind -P01 -E ${tp}_RE
   else
      $uppath/UPfind -P02 -E ${tp}_RE
   endif
   if (-e $ldir/${tp}_RE.found) cp -f $ldir/${tp}_RE.found $ldir/${tp}.tsel
endif
if (! -e $ldir/${tp}.tsel || -z $ldir/${tp}.tsel) then
   echo "No files specified in ${tp}.tsel"
   if (-e $ldir/${tp}.tsel) rm -f $ldir/${tp}.tsel
   exit 1
endif

if (! $upswQ) then
  set a = ( `wc $ldir/${tp}.tsel` )
  echo $a[1] templates files found in ${tp}.tsel
  unset a
endif

set ztxt = ""
if ($upswz) set ztxt = "with the -z switch"
if (! -r $ldir/${tp}.rcs) then
   cat >! $ldir/${tp}.rcs <<EOF
reident by the UPreident script $ztxt
EOF
endif
unset ztxt

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

if (! -r $ldir/${tp}.tsel || -z $ldir/${tp}.tsel) then
    echo "An empty or no ${tp}.tsel 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 (! $upswQ) then
  set a = (`wc $ldir/${tp}.tsel`)
  echo "$a[1] files will be changed and checked in/out"
  unset a
endif

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

set zmod = ""
if ($upswz) set zmod = "-z"
if (-e $ldir/${tp}.log) rm -f $ldir/${tp}.log
touch $ldir/${tp}.log
foreach i (`cat $ldir/${tp}.tsel`)
  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
       cvs update $i:t
    else
       cvs update $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
       echo "***** File: $i" >> $ldir/${tp}.log
       reident $zmod >>& $ldir/${tp}.log
       if (! $upswt) then
          cvs commit -F "$ldir/${tp}.rcs" $i:t 
       else
          diff -b $i:t $dir/code/$i >> $ldir/${tp}.diff
       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
unset zmod

cd $ldir

if (! $upswQ) 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
#
