#! /bin/tcsh -f
# "@(#) $Id: ferosCleanByLog,v 1.3 2005/08/15 05:37:57 jpritcha Exp $"
#
set exstat=0

while ( $#argv )
  switch ( $1 )
  case -h:
    goto usage
  default:
    set logfile=$1; shift; breaksw
  endsw
end

if ( ! $?logfile ) then
  echo "Error\!\!\! Must at least supply <logfile>"
  set exstat=1
  goto usage
endif

ls fero????.mt >& /dev/null
if ( $status == 0 ) then
  foreach file ( fero????.mt )
    grep $file $logfile >& /dev/null
    if ( $status ) then
      echo "Removing ${file}..."
      rm -f $file
    endif
  end
endif

ls FEROS*.fits* >& /dev/null
if ( $status == 0 ) then
  foreach file ( FEROS*.fits* )
    grep ${file:s/.gz//} $logfile >& /dev/null
    if ( $status ) then
      echo "Removing ${file}..."
      rm -f $file
    endif
  end
endif

exit

usage:
echo "Usage: ferosCleanByLog [-h] <logfile>"
echo " "
echo "<logfile>"
echo "   Remove fero????.mt and FEROS*.fits* files NOT in <logfile>"
exit $exstat
