#!/usr/bin/ksh
#
# epo_jpg_check.ksh - check for transferred and combined 
# jpg files. Alert staff if data is missing.
#
# NB: Removed checks for spectral plots as these are no longer archived (lphilpott Oct-2011)

## Set the environment variables
#
  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
  
#. $THMSOC/src/config/thmsoc_epo.config
. /home/thmsoc/astraea2_test/thmsoc_epo_test.config

#. $HOME/software_dev/config/thmsoc_epo.config

## Function Definitions
#

## Move to the directory containg the EPO data
#
cd $EPO_HOME

## Check for lock file
#
  if [[ -a /tmp/epo_jpg_check_lock ]] ; then
    exit 0
  fi
  echo $$ > /tmp/epo_jpg_check_lock


## Set a counter (n) and then set a variable to the
## first school name in the SCHOOL array which was
## defined in the config file above
#
n=0
school=${SCHOOL[$n]}

# Now lets start looping through the schools
#
while [[ -n $school ]]
do
	school_dir=$(echo $school | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')
	chkStatus=""

  ##############################################
	## Search for x, y, and z time series plots ##
  ##############################################
	#
	find ./${school_dir} -name ${school}_TS1_\* -mtime ${CHECK_MTIME} > /tmp/ts1_file_check
	find ./${school_dir} -name ${school}_TS2_\* -mtime ${CHECK_MTIME} > /tmp/ts2_file_check
	find ./${school_dir} -name ${school}_TS3_\* -mtime ${CHECK_MTIME} > /tmp/ts3_file_check
	find ./${school_dir} -name ${school}_TS123_\* -mtime ${CHECK_MTIME} > /tmp/ts123_file_check
	xyzTSstatus="OK"
	if [[ ! -s /tmp/ts1_file_check || ! -s /tmp/ts2_file_check || ! -s /tmp/ts3_file_check || ! -s /tmp/ts123_file_check ]]
	then
		xyzTSstatus=Check
		chkStatus=Check
	fi
	rm -f /tmp/ts* 

  ##############################################
	## Search for B, D, and H time series plots ##
  ##############################################
	#
	find ./${school_dir} -name ${school}_TSB_\* -mtime ${CHECK_MTIME} > /tmp/tsb_file_check
	find ./${school_dir} -name ${school}_TSD_\* -mtime ${CHECK_MTIME} > /tmp/tsd_file_check
	find ./${school_dir} -name ${school}_TSH_\* -mtime ${CHECK_MTIME} > /tmp/tsh_file_check
	find ./${school_dir} -name ${school}_TSBDH_\* -mtime ${CHECK_MTIME} > /tmp/tsbdh_file_check
	bdhTSstatus="OK"
	if [[ ! -s /tmp/tsb_file_check || ! -s /tmp/tsd_file_check || ! -s /tmp/tsh_file_check || ! -s /tmp/tsbdh_file_check ]]
	then
		bdhTSstatus=Check
		chkStatus=Check
	fi
	rm -f /tmp/ts*

	#############################################
	## Search for x, y, and z day series plots ##
	#############################################
	#
	find ./${school_dir} -name ${school}_DS1_\* -mtime ${CHECK_MTIME} > /tmp/ds1_file_check
	find ./${school_dir} -name ${school}_DS2_\* -mtime ${CHECK_MTIME} > /tmp/ds2_file_check
	find ./${school_dir} -name ${school}_DS3_\* -mtime ${CHECK_MTIME} > /tmp/ds3_file_check
	find ./${school_dir} -name ${school}_DS123_\* -mtime ${CHECK_MTIME} > /tmp/ds123_file_check
	xyzDSstatus="OK"
	if [[ ! -s /tmp/ds1_file_check || ! -s /tmp/ds2_file_check || ! -s /tmp/ds3_file_check || ! -s /tmp/ds123_file_check ]]
	then
		xyzDSstatus=Check
		chkStatus=Check
	fi
	rm -f /tmp/ds*

	#############################################
	## Search for B, D, and H day series plots ##
	#############################################
	#
	find ./${school_dir} -name ${school}_DSB_\* -mtime ${CHECK_MTIME} > /tmp/dsb_file_check
	find ./${school_dir} -name ${school}_DSD_\* -mtime ${CHECK_MTIME} > /tmp/dsd_file_check
	find ./${school_dir} -name ${school}_DSH_\* -mtime ${CHECK_MTIME} > /tmp/dsh_file_check
	find ./${school_dir} -name ${school}_DSBDH_\* -mtime ${CHECK_MTIME} > /tmp/dsbdh_file_check
	bdhDSstatus="OK"
	if [[ ! -s /tmp/dsb_file_check || ! -s /tmp/dsd_file_check || ! -s /tmp/dsh_file_check || ! -s /tmp/dsbdh_file_check ]]
	then
		bdhDSstatus=Check
		chkStatus=Check
	fi

	rm -f /tmp/ds*

	####################################
	## Search for Time Series Spectra ##
	####################################
	#
	#find ./${school_dir} -name ${school}_TSpectra_\* -mtime ${CHECK_MTIME} > /tmp/tspec_file_check
	#specTSstatus="OK"
	#if [[ ! -s /tmp/tspec_file_check ]]
	#then
	#	specTSstatus=Check
	#	chkStatus=Check
	#fi
	#rm /tmp/tspec_file_check


	####################################
	## Search for Day Series Spectra  ##
	####################################
	#
	#find ./${school_dir} -name ${school}_DSpectra_\* -mtime ${CHECK_MTIME} > /tmp/dspec_file_check
	#specDSstatus="OK"
	#if [[ ! -s /tmp/dspec_file_check ]]
	#then
	#	specDSstatus=Check
	#	chkStatus=Check
	#fi
	#rm /tmp/dspec_file_check 

	##############################
	## Now report on file check ##
	##############################
	 
	if [[ -n $chkStatus ]]
	then
		printf "%s\n%s\n" "THEMIS EPO-GMAG JPG Processing Warning" \
		"Files not received for $CHECK_MTIME days. " \
		"Check the following:" \
		"School = $school" \
		"xyzTSstatus = $xyzTSstatus" \
		"bdhTSstatus = $bdhTSstatus" \
		"xyzDSstatus = $xyzDSstatus" \
		"bdhDSstatus = $bdhDSstatus" | mail -s "THEMIS EPO-GMAG JPG Processing Warning" $CONTACT_LIST
		#"specTSstatus = $specTSstatus" \
		#"specDSstatus = $specDSstatus" | mail -s "THEMIS EPO-GMAG JPG Processing Warning" $CONTACT_LIST
	fi			
	n=$(( n + 1 ))
	school=${SCHOOL[$n]}

done


	#
	## Cleanup
	#
  rm -f /tmp/epo_jpg_check_lock

exit 0
