#! /bin/csh
#
# Name: check_load
#
# Descriptions: Performs various checks of the .lod files in a CDBS delivery
#               by generating new comparison load files using the mkload tool,
#               and verifying that certain values in the comparison files match
#               those in the original .lod files (probably to ensure that
#               hand-editing of the .lod files didn't erroneously remove
#               key parameters).
#
# Called by:    deliver_cdbs.s
#
# Requires: env var CDBS_SERVER
#           env var CDBS_DB
#
# Returns: 0 - no errors found in .lod files
#          1 - error checking .lod files
#
#-----------------------------------------------------------------------------
echo "starting check_load"
echo "database ${CDBS_DB}"
echo "server ${CDBS_SERVER}"
date
#
set err = 0
#
# Look at each load file in the current directory
foreach file (*.lod)
  echo "load file: $file"
  #
  # Rename the load file temporarily, so a comparison version can be generated
  set ft = $file"_tmp"
  \mv -f $file $ft

  # grab the FILE_NAME from the .lod file to 
  # locate the source for the .lod file  #   content
  #
  set head_fn = `grep FILE_NAME $ft | awk '{print $3}' | tr A-Z a-z`
  echo "header file: $head_fn"
  if ( `echo $head_fn | wc -l` == 0) then
   echo "CDBS ERROR: No header file name found in $file"
   \mv -f $ft $file
   set err = 1
   goto jump
  endif

  if ( ! -e $head_fn) then
   echo "CDBS ERROR: No header file $head_fn found"
   \mv -f $ft $file
   set err = 1
   goto jump
  endif

  # Run mkload on the original source to generate a comparison .lod file
  mkload $head_fn 

  if ( ! -e $file) then
   echo "CDBS ERROR: New load file $file not found"
   \mv -f $ft $file
   set err = 1
   goto jump
  endif

  # Swap the new comparison and original .lod file names
  \mv -f $file $file"_tmp1"
  \mv -f $ft $file
  \mv -f $file"_tmp1" $ft

  set file_save = N
  #
  # generate a report of file differences between the comparison lod file
  # and the original (ignore case and whitespace)
  #
  diff -iw $file $ft > diff.tmp

  # make checks for specific items to see if they show up in the difference
  # report (not all differences are checked)
  #
  if ( `grep "FILE_NAME =" diff.tmp | wc -l` != 0) then
    echo "CDBS ERROR: Header file names do not match. See files $file and $ft"
    set err = 1
    set file_save = Y
  endif

  # Compare the instruments
  if ( `grep "INSTRUMENT =" diff.tmp | wc -l` != 0) then
    echo "CDBS ERROR: Instruments do not match. See files $file and $ft"
    set err = 1
    set file_save = Y
  endif

  # Compare the file types
  if ( `grep "REFERENCE_FILE_TYPE =" diff.tmp | wc -l` != 0) then
    echo "CDBS ERROR: reference_file_types do not match. See files $file and $ft"
    set err = 1
    set file_save = Y
  endif

  # Compare the useafter dates
  if ( `grep "USEAFTER_DATE =" diff.tmp | wc -l` != 0) then
    echo "CDBS ERROR: Useafter dates do not match. See files $file and $ft"
    set err = 1
    set file_save = Y
  endif
  
  # clean up the comparison .lod file
  \rm -f diff.tmp
  if ($file_save == "N") then
     \rm -f $ft
     echo "no differences for file $file"
  endif

jump:
end
exit $err
