#!/bin/csh -f

set whatami = `basename $0`

if ($#argv == 0) then
  echo "${whatami}: You must specify an action and one or more files."
  echo "Usage: $whatami action file"
  exit 1
else if ($#argv == 1) then
  echo "${whatami}: You must specify one or more files."
  echo "Usage: $whatami action file"
  exit 1
endif

set action = "$1"
shift

foreach file ($argv)
  if (! -r $file) then
    echo "${whatami}: $file is unreadable. Skipping."
  else if (! -w $file) then
    echo "${whatami}: $file is unwritable. Skipping."
  else
    set tmpfile = $file.$whatami.$$
    $action $file > $tmpfile
    if (-z $tmpfile) then
      echo "${whatami}: '$action $file' did not create any output. Skipping."
      rm -f $tmpfile
    else
      mv $tmpfile $file
    endif
  endif
end
