#! /bin/sh

#    May 26/10 - F.Majaess

#id  filszck - Checks file size conformance.

#    AUTHOR  - F.Majaess

#hd  PURPOSE - "filszck" script is used to check for possible
#hd             data corruption in a file by comparing the reported
#hd             expected file size by "invntry", 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          filszck ma_xxx_999_m12_gs
#ex
#ex The above will run "invntry" on "ma_xxx_999_m12_gs" and verify the
#ex reported vs. actual file size.
#ex

# set -x
if [ -s "$1" ] ; then
 Filn="$1"
 skipme='no'
 CCCma_format=''
 case $Filn in
  *_nmf) skipme='yes' ;;
 esac
 if [ -d "$Filn/." ] ; then
  skipme='yes'
 fi
 ## case $Filn in
 ## #*psib) skipme='no' ;;
 ##  *tar|*.nc|*.gz|*.bz2|*.z|*.zip) skipme='yes' ;;
 ##  *) skipme='no'
 ## esac
 ## skipme=${skipme='no'}
 ####*ro|*os|*gz) skipme='yes' ;;
 if [ "$skipme" != 'yes' ] ; then
  CCCma_format=`od -o -N 72 -w16 $Filn | sed -n -e '1p' -e '5p' | $AWK -F' ' 'BEGIN { linenum = 0 ; CCCMA = "no" } ; { linenum = ++linenum ; if ( linenum == 1 ) { head1=$2 ; head2=$3 ; } else if ( $4 == head1 && head1 == "000000" && $5 == head2 && ( head2 == "040000" || head2 == "000100")  ) { CCCMA = "yes" ; } ; } ; END { print CCCMA ; } '`
  CCCma_format=${CCCma_format='no'}
 #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
  if [ "$CCCma_format" != 'yes' ] ; then
   skipme='yes'
  fi
 fi
 if [ "$skipme" = 'yes' ] ; then
  if [ -d "$Filn/." ] ; then
   echo " " ; echo "filszck: skipped checking -> $Filn <- directory" ; echo " "
  else
   echo " " ; echo "filszck: skipped checking -> $Filn <- file" ; echo " "
  fi
  exit 0
 else
  Msg=`invntry $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 | $AWK '{print $5}'`
    if [ "$ActlsZ" -ne "$ExpctSz" ] ; then
     echo " " ; echo "filszck: >>>> Detected corruption in  -> $Filn <- file; ActlsZ=$ActlsZ, ExpctSz=$ExpctSz " ; echo " " 
     exit 1
    else
     echo "filszck: No invntry based detected corruption in -> $Filn <- file"
     exit 0
    fi
   else
    echo " " ; echo "filszck: Can not test for corruption in -> $Filn <- : $Msg " ; echo " " 
    exit 2
   fi
  else
   Msg=`invntry $Filn 2>> /dev/null | head -1 | egrep 'invntry' `
   if [ -n "$Msg" ] ; then
    echo " " ; echo "filszck: >>>> Bad data corruption in  -> $Filn  <- :" 
    invntry $Filn | tail -3 
    echo " "
    exit 3
   else
    echo " " ; echo "filszck: Can not load invntry to test for corruption in -> $Filn <- : $Msg " ; echo " "
    exit 4
   fi 
  fi
 fi

else
 
 echo " " ; echo "filszck: >>>> invalid/empty -> $1 <- file!" ; echo " "
 exit 5 

fi
