#!/usr/bin/env tcsh
#-----------------------------------------------------------------------------
# UPdup script
#-----------------------------------------------------------------------------
# Copyright (C) 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$
#
# UPdup script (must have x allowed)
#
set script = "UPdup"
set usage = 'UPdup [-s] [-c] [-r] [-u] [-d] [-n] <typ>'
set htxt  = ("Determine the template duplicates between the package and" \
	     "application/test programs. The result will be in <typ>.dup.")
set hsw   = ("-s show only the system template files that can cause" \
	     "   problems in the aips++ system")
set huse  =  "<typ>.def <typ>.pak"
set hcrea =  "<typ>.dup <typ>.pak"
#
# Top packages to check
#
set checkpack = (display trialdisplay appsglish contrib)
#
# 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
#
# Get package specific information
#
# Check list
#
foreach i (`'ls' -F $dir/code | grep '/' | sed -e 's;/;;'`)
  set found = 0
  foreach j ($casapack $skippack $checkpack)
    if ("$i" == "$j") then
      set found = 1
      break
    endif
  end
  if (! $found) then
    set checkpack = ($checkpack $i)
  endif
end
 
if (-e $ldir/${tp}.pak) then
    set x = (`cat $ldir/${tp}.pak`)
    set pack = ""
    set expect = 0
    set y = ""
    while ($#x > 0)
	if ("$x[1]" == "//") then
	    set expect = 1
	else if ($expect) then
	    set pack = $x[1]
	    set expect = 0
	else
	    set y = ($y $x[1])
	endif
	shift x
	if ($expect || $#x == 0) then
	    if ("$y" != "") then
	      if ("$pack" == "casapack") then
	        set casapack = ($y)
	      else if ("$pack" == "skippack") then
	        set skippack = ($y)
	      else if ("$pack" == "checkpack") then
	        set checkpack = ($y)
  	      endif
	    endif
	    set pack = ""
	    set y = ""
	endif
    end 
    unset x
    unset y
    unset pack
    unset expect
    rm -f $ldir/${tp}.pak
endif

touch $ldir/${tp}.pak
set x = ($casapack)
echo '//' casapack >> $ldir/${tp}.pak
while (1)
  if ($#x > 8) then
    echo $x[1-8] >> $ldir/${tp}.pak
    set x = ($x[9-])
  else
    echo $x[1-] >> $ldir/${tp}.pak
    break
  endif
end
unset x
set x = ($skippack)
echo '//' skippack >> $ldir/${tp}.pak
while (1)
  if ($#x > 8) then
    echo $x[1-8] >> $ldir/${tp}.pak
    set x = ($x[9-])
  else
    echo $x[1-] >> $ldir/${tp}.pak
    break
  endif
end
unset x
set x = ($checkpack)
echo '//' checkpack >> $ldir/${tp}.pak
while (1)
  if ($#x > 8) then
    echo $x[1-8] >> $ldir/${tp}.pak
    set x = ($x[9-])
  else
    echo $x[1-] >> $ldir/${tp}.pak
    break
  endif
end
unset x

if ($upswn) exit 0
#
# Execute
#
cd $dir/code

if (-e $ldir/${tp}.dup) rm -f $ldir/${tp}.dup
touch $ldir/${tp}.dup
set smod = ""
if ($upsws) set smod = "-s"

echo "******* Package lists used" >> $ldir/${tp}.dup
echo "******* " >> $ldir/${tp}.dup
cat $ldir/${tp}.pak >> $ldir/${tp}.dup
echo "************************************************ " >> $ldir/${tp}.dup

if (! $upswQ) echo "Starting casa base packages..."
echo "******* " >> $ldir/${tp}.dup
echo "******* Base casa packages repositories" >> $ldir/${tp}.dup
echo "******* " >> $ldir/${tp}.dup
set rtmp = ""
foreach i ($casapack)
  set rtmp = (-r $i $rtmp)
end
duplicates $smod $rtmp >>& $ldir/${tp}.dup

echo "******* " >> $ldir/${tp}.dup
echo "******* Base casa packages" >> $ldir/${tp}.dup
echo "******* " >> $ldir/${tp}.dup
set ptmp = ""
foreach i ($casapack)
  set ptmp = (-p $i $ptmp)
end
duplicates $smod $ptmp >>& $ldir/${tp}.dup

foreach i ($checkpack)
    if (! $upswQ) echo "Starting package $i..."
    echo "******* " >> $ldir/${tp}.dup
    echo "******* Package $i against casa base repositories" \
	    >> $ldir/${tp}.dup
    echo "******* " >> $ldir/${tp}.dup
    duplicates $smod -p $i $rtmp >>& $ldir/${tp}.dup
end

if (! $upswQ) echo "Starting all packages..."
echo "******* " >> $ldir/${tp}.dup
echo "******* All packages, all templates files" >> $ldir/${tp}.dup
echo "******* " >> $ldir/${tp}.dup
duplicates $smod -p all >>& $ldir/${tp}.dup

unset smod
unset ptmp
unset rtmp

exit 0
#
