#!/bin/sh
#
# Name: fitsverify_file_lenient
#
# Description: Runs several FITS-format verifiers against a file
#              to ensure that no serious FITS errors
#              are present.  The opus_fitsverify tool is used
#              for this purpose, run in a mode to only flag SEVERE errors
#              (the "lenient" version distinguishes this version from the more
#               stringent fitsverify mode) and
#              also the pyFITS "verify" function is used.
#
# Called by:   fitsverify_delivery
#
# Returns: 0 = if no errors were found in the FITS files
#          1 = if errors were found
#
#--------------------------------------------------------------------------
echo "starting fitsverify_file_lenient for ${1}"
date
#
# Have to use OPUS FITS verifier on Linux (Farris version does not exist)
# (the OPUS version is a slight-modified version of the CFITSIO verifier).
# The -s option only flags SEVERE errors, which makes it more compatible
# with the results from the Farris version.
#
# NOTE: APPENDS to output file so that a multi-file run has all the outputs
#       in one place.
#
opus_fitsverify -s ${1} >> fitsverify.out
if [ `grep -i 'ERRORS were encountered' fitsverify.out | wc -l` -gt 0 ]
then
   echo "errors in fitsverify"
   echo "see output file fitsverify.out"
   # stop right away
   exit 1
else
 if [ `grep -i 'WARNINGS were encountered' fitsverify.out | wc -l` -gt 0 ]
 then
   echo "warnings in fitsverify"
   echo "see output file fitsverify.out"
   # continue on
   #exit 0
 else
   echo "no errors or warnings reported by fitsverify"
   echo "created output file fitsverify.out"
   # continue on
   #exit 0
 fi
fi
#
# and now pyFITS verification, which has been proven to catch problems that
# opus_fitsverify does not
pyfits_verify.py ${1}
if test $? -ne 0
then
   echo "pyFITS verification FAILED."
   exit 1
else
   echo "pyFITS verification OK."
   exit 0
fi
