#!/usr/bin/env tcsh
#-----------------------------------------------------------------------------
# UPsed 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$
#
#
# UPsed script (must have x allowed)
#
set script = "UPsed"
set usage = 'UPsed [-t <log>] <sed> <filename in> [<filename out>]'
#
# Change a file according to a sed rule
# sed patterns will be in <sed> file
# file out will default to file in
#
# The script is meant to be for the use of other UP scripts
#
# Get general info
#
if ($# < 2) then
    echo "Usage: $usage"
    exit 1
endif
set xtst = 0
set tlog = ""
if ("$1" == "-t") then
    set xtst = 1
    shift
    set tlog = "$1"
    shift
endif
if ($# < 2) then
    echo "Usage: $usage"
    exit 1
endif
#
# Get specific data
#
set tp = $1
set fin = $2
if ($# < 3) then
    set fout = $fin
else
    set fout = $3
endif

set tmp = $$
if (-e $tmp.tmp) rm -f $tmp.tmp
if (! -e $tp) then
    echo "Cannot access $tp"
    exit 1
endif

sed -f $tp $fin >! $tmp.tmp

if ($xtst) then
   diff $fin $tmp.tmp >> $tlog
endif

mv -f $tmp.tmp $fout

unset tmp
unset tp
unset fin
unset fout
unset tlog

exit 0
#
