#! /bin/sh

#    Sep 20/05 - F.Majaess

#id  crptnck - Checks for possible data corruption in a file.

#    AUTHOR  - F.Majaess

#hd  PURPOSE - "crptnck" script is used to check for possible
#hd             data corruption in a file by comparing the reported
#hd             expected file size by "ggstat", which is based on
#hd             the number of records read, vs. the actual file size.
#hd             Note: The check is not full proof since corruption
#hd                   may occur within the data record part without
#hd                   yielding any invalid data values.

#pr  PARAMETERS:
#pr
#pr    POSITIONAL
#pr
#pr      arg1   = [path/]filename to be checked.
#pr

#ex  EXAMPLE:
#ex
#ex          crptnck ma_xxx_999_m12_gs
#ex
#ex The above will run "ggstat" on "ma_xxx_999_m12_gs" and verify the
#ex reported vs. actual file size.
#ex

# set -x
if [ -s "$1" ] ; then
 Filn="$1"
 case $Filn in
  *psib) skipme='no' ;;
  *tar|*[aoifc]b|*.nc|*.gz|*.bz2|*.z|*.zip|access) skipme='yes' ;;
  *) skipme='no'
 esac
 #*ro|*os|*gz) skipme='yes' ;;
 if [ "$skipme" != 'yes' ] ; then
  CCCmarec=`od -o $Filn | head -1 | sed -e 's/^0000000 000000 000100 .*$/yes/g'`
  if [ "$CCCmarec" != 'yes' ] ; then
   case $Filn in
    *ro|*os|*gz) skipme='yes' ;;
    ukz_*_os_*) skipme='yes' ;;
    ubd_init_restart_192x96) skipme='yes' ;;
    # mc_sj_agcm_boundary_condition_fields_an) skipme='yes' ;;
    *) :
   esac
  fi
 fi
 if [ "$skipme" = 'yes' ] ; then
  echo "\ncrptnck: skipped checking -> $Filn <- file\n"
  exit 0
 else
  Msg=`ggstat $Filn 2>> /dev/null | egrep 'EXPECTED FILE LENGTH|EMPTY OR INVALID FORMAT INPUT FILE' `
  if [ -n "$Msg" ] ; then
   ExpctSz=`echo $Msg | egrep 'EXPECTED FILE LENGTH' | sed -e 's/^.*://g'`
   if [ -n "$ExpctSz" ] ; then
    ActlsZ=`ls -ldL $Filn | nawk '{print $5}'`
    if [ "$ActlsZ" -ne "$ExpctSz" ] ; then
     echo "\ncrptnck: >>>> Detected corruption in  -> $Filn <- file; ActlsZ=$ActlsZ, ExpctSz=$ExpctSz \n" 
     exit 1
    else
     echo "crptnck: No ggstat based detected corruption in -> $Filn <- file"
     exit 0
    fi
   else
    echo "\ncrptnck: Can not test for corruption in -> $Filn <- : $Msg \n" 
    exit 0
   fi
  else
   Msg=`ggstat $Filn 2>> /dev/null | head -1 | egrep 'ggstat' `
   if [ -n "$Msg" ] ; then
    echo "\ncrptnck: >>>> Bad data corruption in  -> $Filn  <- :" 
    ggstat $Filn | tail -3 
    echo " "
    exit 2
   else
    echo "\ncrptnck: Can not load ggstat to test for corruption in -> $Filn <- : $Msg \n"
    exit 0
   fi 
  fi
 fi

else
 
 echo "\ncrptnck: >>>> invalid -> $Filn <- file!\n"
 exit 3 

fi
