#!/bin/sh
# PURPOSE:      dfoMonitor: creates GUI page as central DFO monitor
# AUTHOR:       Reinhard Hanuschik / ESO
# DATE:		2004-12-22
# VERSIONS:     1.0 -- rewritten from scratch, inspired by the Giraffe version 
#		<complete history under http://www.eso.org/~qc/dfos/history/history_dfoMonitor.html>
# ----------------------------------------------------------------------------------------------------------------------------
#		2.0 -- bug fixed with raw_Incomplete; links to DTS status, SOS Nagios/MUNIN; new key CLUSTER_WIDTH; safweb1->qcweb; option -N; checking for HCMON_SUPPORT (2010-03-01)
# ----------------------------------------------------------------------------------------------------------------------------
#		3.0 -- new workflow for QC XXLight; JOB_FILE_NAME replaced by JOBS_NIGHT; ING_FILE_NAME replaced by JOBS_INGEST (2011-10-04)
# ----------------------------------------------------------------------------------------------------------------------------
#		4.0 --  common support for DFOS and PHOENIX installations; new config key DATA_QUOTA (2014-08-13)
#		4.0.1-  bug fix in line :4315 (2014-08-21)
#		4.0.2-  some active buttons switched off for PHOENIX; added: ganglia mem-report (2014-08-28)
#		4.1_beta - check for qc1Parser removed; prepared for new calSelector (2014-10-31) [BWo]
#		4.1_beta2- minor change for phoenix v2 (2015-02-20)
#		4.1_beta3- ConnectTimeOut removed; bbcp-->DTS label (2015-03-23)
#		4.1_beta4- default mode for XXLight is RANDOM (2015-06-11)
#		4.1 --  workflow support for calSelector v2 (2015-06-22)
#		4.1.1-  new IMG for timezone (2015-07-13)
#		4.1.2-  cronjob checks added for cleanupRawdisp and autoDaily in watch mode (2015-07-22)
#		4.1.3-	bug fix for autoDaily cronjob check, and XDM alignment (2015-08-10)
#		4.1.4-  minor bug fixes (2015-09-01)
#		4.2 --  new button for rawdisp2reference; logo changed from XXLight to DFOS (2015-11-30)
#		4.2.1-	flag sci_Certif supported (for PHOENIX; lines 4029/4062) (2016-01-29)
#		4.2.2-  new link for RAWDISP_reference file (DFOS only) (2016-05-30)
#		4.2.3-  improved links for PHOENIX (2016-06-08)
#		4.2.4-	small changes for non-std dates (PHOENIX DEEP); added link to new REFERENCE page (DFOS) (2016-08-09)
#		4.2.5-  makes JOBS_NIGHT visible under phoenix (2016-11-07)
#		4.2.6-  bug fix for PHOENIX ($TMP_DIR/$PID was not managed properly) (2016-11-17)
#		4.2.7-  new link to dfoMonitor_contact.txt (2017-09-13)
#		4.2.8-  new links for DTS; simplified behaviour for PHOENIX (2017-09-18)
#		4.2.9- link for DYNAMIT added, dateandtime disabled (2017-12-07)
#		4.3 -- SCIENCE step offered if no CALIBs (step 3.14) (2017-12-18)
# 		4.3.1- updated DYNAMIT URL (2018-01-09)
#		4.3.2- no disk space check if 'refresh' button is called (for better performance) (2018-03-26)
#		4.3.3- improved refresh, improved removeNight (2018-04-03)
#		4.3.4- labels updated for PHOENIX DEEP mode (2018-04-16)
#		4.3.5- refresh_browser: usage of new variable DFO_BROWSER_PATH (2018-06-06, BWo)
#		4.3.6- sourcing .qcrc switched off (2018-06-20)
# ----------------------------------------------------------------------------------------------------------------------------
#		5.0 -- supporting DFOS, PHOENIX, OPSHUB environments (2018-08-02)
#		5.0.1- added MONITOR_TAG (2018-08-03)
#		5.0.2- added shortcuts for OPSHUB (2018-10-01)
#		5.0.3- OPSHUB: new link for DOCU; refresh_browser modified for firefox v52+; system monitors disabled (2018-10-22)
# 		5.0.4- OPSHUB: some format changes in upper table (nowrap); Q&A link for OPSHUB (2018-11-16)
#		5.0.5- PHOENIX: bug fix (2018-12-19)
# 		5.0.6- OPHSUB: support for streams available in $OPSHUB_TOOL_PROJECT_FILE; support for pipeline_overview page; du is suppressed on opshub (2019-03-19)
#		5.0.7- link to 'release notes' added (2019-03-26)
# 		5.0.8- dropped: system monitor for OPSHUB (2019-04-16)
#		5.0.9- added: account maintenance for OPSHUB, link to opshub wiki; SUPPRESS_CALL added ifor OPSHUB (2019-04-23)
#		5.1 -- no SCIENCE ABs called for DFOS_OPS (2020-10-22)
#		5.1.1- small bug fixes related to new runID format for DEEP mode (2021-03-12)
#
# PARAMETERS:   -a, -m, -q: for calls by autoDaily
# CONFIG:       config.dfoMonitor; config.createAB
# TOOLS CALLED: qcdate
# OUTPUT:       dfoMonitor.html under $DFO_MON_DIR
# COMMENTS:    	tool links to data reports, NR, calchecker and histoMonitor 
#		observations..data_products: the sara query is joined to observations which is correct here
#		(since it is the original information as seen by DTS, not the dbcm-edited one).
#		Tool supporting PHOENIX and OPSHUB.
# ===================================================================================
TOOL_NAME="dfoMonitor"
TOOL_VERSION="5.1.1"

WEB_DIR="/home/qc/qc/${DFO_INSTRUMENT}/reports"		  # on $DFO_WEB_SERVER, for ngasWatcher
WISQ_DIR="/home/qc/qc/WISQ/XDM"				  # on $DFO_WEB_SERVER; XDM stands for Xtended Diskspace Monitor

INSTR_DIR="/home/qc/public_html/${DFO_INSTRUMENT}"        # on $DFO_WEB_SERVER
QCBIN_DIR="/home/qc/bin"        		 	  # on $DFO_WEB_SERVER

ALL_URL="http://www.eso.org/qc/ALL"
STYLE_URL="http://www.eso.org/qc/ALL/common/qc_eso.style" # URL for qc style sheet
IMG_URL="http://www.eso.org/qc/ALL/img"			  # URL for images
REF_URL="http://www.eso.org/qc/${DFO_INSTRUMENT}/reports/RAWDISP/REFERENCE"
							  # URL for rawdisp
CONTACT_URL="http://www.eso.org/~qc/admin/qc-staff.html"  # QCG staff
ESO_PIPE="/usr/share/doc/esopipes"			  # link to pipeline info

WEB_SERVER_URL="http://qcweb.hq.eso.org/~qc"		  # URL on QC web server

DFOSEXP_LOGO="dfosExp.png"				  # logo for dfosExplorer
SCIOPS_LOGO="sciops_logo.png"				  # sciops logo
HISTO_LOGO="nautilus.gif"				  # nautilus logo for histoMonitor
PHOENIX_LOGO="<font size=4 color=\${PHOENIX_COL}>PHO<br>ENIX:</font>"
							  # PHOENIX logo (PHOENIX_COL determined later)

MUNIN_LOGO="http://munin-monitoring.org/site/munin.png"	  # munin logo
MUNIN_HOME="http://munin.hq.eso.org"			  # munin: trending system for system performance
MUNIN_INDEX="${MUNIN_HOME}/hq.eso.org/$HOSTNAME.hq.eso.org/index.html"

GANGLIA_HOME="http://mucmp.hq.eso.org/ganglia"	  	# ganglia: SOS performance monitoring system
GANGLIA_INDEX="${GANGLIA_HOME}/?c=mucXX&h=$HOSTNAME.hq.eso.org"  # ganglia: SOS performance monitoring system
GANGLIA_IMG="${GANGLIA_HOME}/templates/default/images/logo.jpg"
GANGLIA_REFRESH=60

MUNIN_WIKI_URL="http://en.wikipedia.org/wiki/Munin_(network_monitoring_application)"
CONDOR_WIKI_URL="http://en.wikipedia.org/wiki/Condor_High-Throughput_Computing_System"
GANGLIA_WIKI_URL="http://en.wikipedia.org/wiki/Ganglia_(software)"
EVALSO_URL="http://monkey.sc.eso.org/mrtg/mrtg2017"
DYNAMIT_URL="http://dynamit.pl.eso.org/dashboard_show.php"
OPSHUB_WIKI_URL="http://sciops.pl.eso.org/wiki/index.php/SCIPROJ-0112"

WISQ_URL="http://www.eso.org/qc/WISQ"	# URL for WISQ information system

NLT_URL="http://www.eso.org/gnlt/qcReport/instrumentReport"	# URL for NLT

MAXDELAY=1	# delay check
MAX_AGE=24	# maximum age for last created AB
MAX_AUTOD=6	# maximum age of last autoDaily execution

AB_NUMBER_MAX=2500	# maximum number of ABs in $DFO_AB_DIR

# for NGAS access check:
TEST_FILE="NACO.2008-11-07T13:34:19.595.fits"   # downloaded every time to test ngas connection; just 1.8 MB

SLEEP=1						# check time: every SLEEP sec we check again, up to $SLEEP_MAX
SLEEP_MAX=60					# max waiting time for test download

# check if this is a PHOENIX installation (environment variable THIS_IS_PHOENIX=YES is set by .dfosrc)
if [ Q$THIS_IS_PHOENIX != QYES ]
then
	THIS_IS_PHOENIX=NO
fi

# check if this is an OPSHUB installation (environment variable THIS_IS_OPSHUB=YES is set by .dfosrc)
if [ Q$THIS_IS_OPSHUB != QYES ]
then
	THIS_IS_OPSHUB=NO
fi

# just to be complete
if [ $THIS_IS_PHOENIX = NO ] && [ $THIS_IS_OPSHUB = NO ]
then
	THIS_IS_DFOS=YES
else
	THIS_IS_DFOS=NO
fi

# REPORT_DIR etc.: if on DFO_WEB_SERVER: no interactive functions
DEEP_MODE=NO
if [ $THIS_IS_DFOS = YES ]
then
	REPORT_DIR="file://$DFO_LST_DIR/REPORT"
	REPORT_DIR2=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/REPORT
	CALCHECK_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/CALCHECK
else
	REPORT_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/REPORT
	CALCHECK_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/CALCHECK
fi

if [ $THIS_IS_PHOENIX = YES ]
then
	MCAL_CONFIG=`grep "^MCAL_CONFIG" $DFO_CONFIG_DIR/config.phoenix | awk '{print $2}'`
	DEEP_CONFIG=`grep "^DEEP_CONFIG" $DFO_CONFIG_DIR/config.phoenix | awk '{print $2}'`
	if [ Q$MCAL_CONFIG != Q ]
	then
		MCAL_MODE=YES
	else
		MCAL_MODE=NO
	fi
	if [ Q$DEEP_CONFIG != Q ]
	then
		DEEP_MODE=YES
	else
		DEEP_MODE=NO
	fi
fi

NR_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/NIGHTLOG

HISTO_DIR="file://${DFO_MON_DIR}/FINISHED"
HISTO_DIR2=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/monitor/FINISHED

LOGS_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs

OPSHUB_TOOL="distillery"        # name of opshub workflow tool 

# ===================================================================================
# 0. Initialization
# 0.1 check for directory variables which should be set globally in .dfosrc
# ===================================================================================
CHECK=`printenv | grep DFO`

if [ "Q$CHECK" = "Q" ]
then
	echo "*** ERROR: DFO variables not defined. Check ~/.dfosrc and restart."
	exit -1
fi

if [ ! -s $DFO_CONFIG_DIR/config.dfoMonitor ]
then
        echo "*** ERROR: No configuration file $DFO_CONFIG_DIR/config.dfoMonitor found. Check and re-start."
        exit -1
fi

if [ ! -d $DFO_GUI_DIR ]
then
        echo "*** ERROR: No \$DFO_GUI_DIR defined. Check out .dfosrc ."
        exit -1
fi

if [ $THIS_IS_DFOS = YES ]
then
	if [ "Q$DFO_WEB_SERVER" = "Q" ]
	then
		echo "*** WARNING: \$DFO_WEB_SERVER not defined, update your .dfosrc."
	fi
fi

# =========================================================================
# 0.2 get options
# =========================================================================

AUTO_CHECK=YES
MESSAGE=NO
QUIET=NO
SCP_YN=YES
NGAS_TEST=YES
DISK_SPACE=YES
SUPPRESS_CALL=NO	# YES: no refresh_browser called

while getopts ahi:mnNqsSv OPTION
do
        case "$OPTION" in
         h ) cat $DFO_DOC_DIR/dfoMonitor.h | more
             exit 0 ;;
         v ) echo "$TOOL_VERSION"
             exit 0 ;;
	 a ) AUTO_CHECK=NO ;;
	 i ) PROJ_INSTRUMENT=$OPTARG ;;
	 m ) MESSAGE=YES ;;
	 n ) SCP_YN=NO ;;
	 s ) DISK_SPACE=NO ;;
	 N ) NGAS_TEST=NO ;;
	 S ) SUPPRESS_CALL=YES ;;
	 q ) QUIET=YES ;;
        esac
done

if [ $THIS_IS_OPSHUB = YES ]
then
	SCP_YN=NO
	if [ Q$PROJ_INSTRUMENT = Q ]
	then
		PROJ_INSTRUMENT=$DFO_INSTRUMENT
	fi

	if [ ! -s ~/.dfosrc_opshub_$PROJ_INSTRUMENT ]
	then
		echo "***ERROR: no \$HOME/.dfosrc_opshub_$PROJ_INSTRUMENT found. This account is not enabled for $PROJ_INSTRUMENT."
		echo "Check out $DFO_CONFIG_DIR/config.${OPSHUB_TOOL} for the list of supported instruments:"
		grep "^SUPPORTED" $DFO_CONFIG_DIR/config.${OPSHUB_TOOL} | awk '{print $2}'
		exit
	else
		HDR_DIR=$DFO_HDR_DIR
		RAW_DIR=$DFO_RAW_DIR
		SCI_DIR=$DFO_SCI_DIR
		CAL_DIR=$DFO_CAL_DIR
		CONFIG_DIR=$DFO_CONFIG_DIR

		source ~/.dfosrc_opshub_$PROJ_INSTRUMENT
		REPORT_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/REPORT
		CALCHECK_DIR=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/CALCHECK
	fi
else
	if [ Q$PROJ_INSTRUMENT != Q ]
	then
		echo "*** INFO: this is not an OPSHUB installation. We call the tool for $DFO_INSTRUMENT."
	fi
fi

# =========================================================================
# 0.3 Initialize some parameters
# =========================================================================

OTMP_DIR=$TMP_DIR
export TMP_DIR=$OTMP_DIR/$$
rm -rf $TMP_DIR
mkdir $TMP_DIR

OPSHUB_TOOL_PROJECT_FILE="$OTMP_DIR/projects.${OPSHUB_TOOL}"	# name of projects file for this tool
#OPSHUB_TOOL_PROJECT_FILE="$DFO_CONFIG_DIR/../projects.${OPSHUB_TOOL}"	# name of projects file for this tool

CONFIG_OCA=$DFO_CONFIG_DIR/OCA/config.createAB

if [ "Q$DFO_BROWSER" = "Q" ]
then
	if [ $THIS_IS_PHOENIX = NO ]
	then
		echo "***WARNING: No \$DFO_BROWSER defined in \$HOME/.dfosrc. We assume firefox."
	fi
	DFO_BROWSER=firefox
fi

if [ $THIS_IS_DFOS = YES ]
then
	N_DFO_HDR=`grep "^N_DFO_HDR" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
	if [ $N_DFO_HDR -gt 100 ]
	then
		echo "***WARNING: N_DFO_HDR is set to $N_DFO_HDR. Unless there is a good reason, you may want to set it to say 20. Larger values cost performance."
	fi
fi

TABLE_WIDTH="100%"
DISK_SCALE=120			#width in px corresponding to 100% disk  space

DATA_DISK=`grep	"^DATA_DISK"	$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
DATA_QUOTA=`grep "^DATA_QUOTA"	$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$DATA_QUOTA = Q ]
then
	DATA_QUOTA=100
fi

CREATEAB_VCAL=`grep "^CREATEAB_VCAL" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$CREATEAB_VCAL != QYES ]
then
	CREATEAB_VCAL=NO
fi

EMPTY_COLOR="#CCCCCC"		#grey: color for WAIT entries
OFFER_COLOR="#FFFF00"		#yellow: color for OFFER entries
DONE_COLOR="#33FF33"		#green:  color for DONE entries
CAL_COL="#FF6600"		#orange: color for raw file related links
DGREEN="#009933"		#dark-green (for browser_refresh on)
DRED="#CC0000"			#dark-red (for condor configured off)

NGAS_NOK_COLOR="#FF9999"	#light-red: color for NGAS access failure
INCOMPLT_COLOR="#FFFF00"	#yellow: color for incomplete dates
COMPLETE_COLOR="#33FF33"	#green: color for complete dates

VCALIB_COLOR="#006A9D"		#eso-blue: DATE is in VCALIB
MCALIB_COLOR="#006A9D"		#eso-blue: DATE is in MCALIB

TITLE_COLOR="#006A9D"		#eso-blue: font color
SM_COLOR="#006A9D"              #eso-blue:    color for SM nights
VM_COLOR="#666666"              #dark_grey:   color for VM nights

THIS_PAGE_COLOR="#006A9D"	#eso-blue
NOT_THISPAGE_COLOR="#CCCCCC"	 #grey

DFOS_URL=`grep "^DFOS_URL"		$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$DFOS_URL = Q ]
then
	DFOS_URL="http://www.eso.org/~qc/dfos"	# link to DFOS documentation
fi

MIDAS_CHECK=`grep "^MIDAS_CHECK"	$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$MIDAS_CHECK != QNO ]
then
	MIDAS_CHECK=YES
fi

if [ $THIS_IS_OPSHUB = YES ]
then
	MIDAS_CHECK=NO
fi

CONDOR_CONFIG=`grep "^CONDOR_CONFIG"	$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$CONDOR_CONFIG = Q ]
then
	CONDOR_CONFIG=/home/condor/condor_config
fi

GANGLIA_FREQ=`grep "^GANGLIA_FREQ"	$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
if [ Q$GANGLIA_FREQ != QHOUR ] && [ Q$GANGLIA_FREQ != QDAY ] && [ Q$GANGLIA_FREQ != QWEEK ] && [ Q$GANGLIA_FREQ != QMONTH ]
then
	GANGLIA_FREQ=HOUR
	echo "***WARNING: GANGLIA_FREQ should be HOUR|DAY|WEEK|MONTH. Set to HOUR."
fi

case $GANGLIA_FREQ in
 "HOUR" ) GANGLIA_FREQ=hour  ;;
 "DAY"  ) GANGLIA_FREQ=day  ;;
 "WEEK" ) GANGLIA_FREQ=week ;;
 "MONTH") GANGLIA_FREQ=month ;;
esac

cat > $OTMP_DIR/ganglia <<EOT
hour	HOUR	H	+1
day	DAY	D	3
week	WEEK	w	2
month	MONTH	m	1	
EOT

GANGLIA_CORE="${GANGLIA_HOME}/graph.php?"

GANGLIA_IMG1="${GANGLIA_CORE}g=load_report&r=${GANGLIA_FREQ}&s=descending&c=mucXX&h=${HOSTNAME}.hq.eso.org&sh=1&hc=4"
GANGLIA_HTML1="${GANGLIA_IMG1}&z=large"

GANGLIA_IMG2="${GANGLIA_CORE}g=cpu_report&r=${GANGLIA_FREQ}&s=descending&c=mucXX&h=${HOSTNAME}.hq.eso.org&sh=1&hc=4"
GANGLIA_HTML2="${GANGLIA_IMG2}&z=large"

GANGLIA_IMG3="${GANGLIA_CORE}g=network_report&r=${GANGLIA_FREQ}&s=descending&c=mucXX&h=${HOSTNAME}.hq.eso.org&sh=1&hc=4"
GANGLIA_HTML3="${GANGLIA_IMG3}&z=large"

GANGLIA_IMG4="${GANGLIA_CORE}g=mem_report&r=${GANGLIA_FREQ}&s=descending&c=mucXX&h=${HOSTNAME}.hq.eso.org&sh=1&hc=4"
GANGLIA_HTML4="${GANGLIA_IMG4}&z=large"

# XDM: extended disk monitor
XTERM_GEOM=`grep "^XTERM_GEOM"		$DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
XDM_INCLUDE=YES
XDM_SUPPRESS=NO

CFG_EDITOR=`eval "echo $DFO_EDITOR $DFO_CONFIG_DIR/config.dfoMonitor"`

TRANSFER_URL="http://achmp.hq.eso.org/datatransfermonitor/networktransfer.html?filenamePattern=${DFO_FILE_NAME}&status=all&fileType=fits&nrResults=all"
BANDWIDTH_URL1="${EVALSO_URL}/10.0.0.11_gigabitethernet0_0_1.html"	# DTS   PAR->VIT #1
BANDWIDTH_URL2="${EVALSO_URL}/10.0.0.12_gigabitethernet0_0_1.html"	# DTS   PAR->VIT #2
BANDWIDTH_URL3="${EVALSO_URL}/10.0.0.12_gigabitethernet0_0_5.html"	# REUNA VIT->EUR
BANDWIDTH_GRAPH="${EVALSO_URL}/10.0.0.11_gigabitethernet0_0_1-day.png" # closeup

# from config.createAB
N_VCAL_LIST=`grep ^N_VCAL_LIST	$CONFIG_OCA | awk '{print $2}'`
N_MCAL_LIST=`grep ^N_MCAL_LIST	$CONFIG_OCA | awk '{print $2}'`

JOB_FILE_NAME=JOBS_NIGHT
JOB_EDITOR=`eval "echo $DFO_EDITOR $DFO_JOB_DIR/$JOB_FILE_NAME"`

if [ ! -s $DFO_JOB_DIR/$JOB_FILE_NAME ]
then
	cat > $DFO_JOB_DIR/$JOB_FILE_NAME <<EOT
#!/bin/sh
# This is the job file for off-line processing.
# You may edit this file anytime. Outdated entries are removed by moveProducts.
EOT
	chmod u+wx $DFO_JOB_DIR/$JOB_FILE_NAME
fi

ING_FILE_NAME=JOBS_INGEST
ING_EDITOR=`eval "echo $DFO_EDITOR $DFO_JOB_DIR/$ING_FILE_NAME"`
CLEAN_EDITOR=`eval "echo $DFO_EDITOR $DFO_JOB_DIR/JOBS_CLEANUP"`

if [ ! -s $DFO_MON_DIR/DFO_POSTIT ]
then
	POSTIT=NONE
fi

POSTIT_EDITOR=`eval "echo $DFO_EDITOR $DFO_MON_DIR/DFO_POSTIT"`

# messages (from autoDaily)
MESSAGE=""

if [ $THIS_IS_DFOS = YES ]
then
	if [ Q$MESSAGE = QYES ] && [ -s $OTMP_DIR/message ]
	then
		MESSAGE=`cat $OTMP_DIR/message`
	elif [ Q$MESSAGE = QYES ] && [ ! -s $OTMP_DIR/message ]
	then
		MESSAGE="no message"
	fi
fi

# DFOS: check config.certifyProducts for CERTIF_MODE, should be NONZERO
if [ $THIS_IS_DFOS = YES ]
then
	CERTIF_MODE=`grep "^CERTIF_MODE"	$DFO_CONFIG_DIR/config.certifyProducts | awk '{print $2}'`

	if [ Q$CERTIF_MODE  = Q ]
	then
		CERTIF_MODE=ALL
	fi
	case $CERTIF_MODE in
 	"RANDOM" ) CERTIF_MODE="<font color=${DGREEN}><a title=\"CERTIF_MODE properly configured\">$CERTIF_MODE</a></font>" ;;
 	 *  ) 	     CERTIF_MODE="<font color=${DRED}><a title=\"CERTIF_MODE improperly configured\">$CERTIF_MODE</a></font>" ;;
	esac
fi

# PHOENIX support (meaning this is not a PHOENIX account but a DFOS account monitoring a PHOENIX account)
# disabled for QC1 transition
#if [ $THIS_IS_PHOENIX = NO ] && [ $THIS_IS_DFOS = YES ] 
#then
#	PHOENIX_ENABLED=`grep "^PHOENIX_ENABLED" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`
#	PHOENIX_ACCOUNT=`grep "^PHOENIX_ACCOUNT" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}'`

#	if [ Q$PHOENIX_ENABLED != QYES ]
#	then
#		PHOENIX_ENABLED=NO
#	fi
#else
#	PHOENIX_ENABLED=NO
#fi

PHOENIX_ENABLED=NO

# ===================================================================================
# 0.4 create helper scripts in $DFO_GUI_DIR
# 0.4.1 refresh_browser:  browser call
# $1 = file name to be called in existing browser window
# This helper tool is also used by other monitor tools!
# ===================================================================================

CHECK=`env | grep DFO_BROWSER_PATH`
# no DFO_BROWSER_PATH set: new firefox v52+ on opshub machine
if [ "Q$CHECK" = "Q" ] && [ $THIS_IS_OPSHUB = YES ]
then
	cat > $DFO_GUI_DIR/refresh_browser <<EOT
eval "$DFO_BROWSER '\$1'"
EOT
elif [ "Q$CHECK" = "Q" ]
then
	cat > $DFO_GUI_DIR/refresh_browser <<EOT
eval "$DFO_BROWSER -remote 'openFile(\$1)'"
EOT
else
	cat > $DFO_GUI_DIR/refresh_browser <<EOT
eval "$DFO_BROWSER_PATH/$DFO_BROWSER -remote 'openFile(\$1)'"
EOT
fi
chmod u+x $DFO_GUI_DIR/refresh_browser

# ===================================================================================
# 0.4.2 wrapper.esh and wrapper.ash
# ===================================================================================

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/wrapper.esh <<EOT
#!/bin/sh
# PURPOSE:      have a wrapper with xterm for shell scripts being called from the browser
#		maintained by $TOOL_NAME

xterm -T "${DFO_INSTRUMENT} script launcher" -geom $XTERM_GEOM -e $DFO_GUI_DIR/launcher.esh \$1
exit
EOT
	chmod u+x $DFO_GUI_DIR/wrapper.esh

	cat > $DFO_GUI_DIR/wrapper.ash <<EOT
#!/bin/sh
# PURPOSE:      have a wrapper for shell script calls in the background

$DFO_GUI_DIR/launcher.esh \$1

exit
EOT

	chmod u+x $DFO_GUI_DIR/wrapper.ash

# ===================================================================================
# 0.4.3 launcher.esh 
# ===================================================================================

	cat > $DFO_GUI_DIR/launcher.esh <<EOT
#!/bin/sh
# PURPOSE:      have a wrapper for shell scripts being called from the browser
#		maintained by $TOOL_NAME

cat \$1 > $OTMP_DIR/script.sh
chmod u+x $OTMP_DIR/script.sh
$OTMP_DIR/script.sh
rm -f $OTMP_DIR/script.sh
exit
EOT
	chmod u+x $DFO_GUI_DIR/launcher.esh

# ===================================================================================
# 0.4.4 edit_dfoMonitor.esh
# ===================================================================================

	cat > $DFO_GUI_DIR/edit_dfoMonitor.esh <<EOT
#!/bin/sh

$CFG_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_dfoMonitor.esh

# ===================================================================================
# 0.4.5 edit_gui_navbar.esh
# ===================================================================================

	GUI_EDITOR=`echo $CFG_EDITOR | sed "s/config.dfoMonitor/config.gui_navbar/"`
	cat > $DFO_GUI_DIR/edit_gui_navbar.esh <<EOT
#!/bin/sh

$GUI_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_gui_navbar.esh

# ===================================================================================
# 0.4.6 refresh_gui_navbar.esh
# ===================================================================================

	cat > $DFO_GUI_DIR/refresh_gui_navbar.esh <<EOT
#!/bin/sh
echo "- create navbars for all monitors ..."
for TN in \`grep "^NAV_BAR" $DFO_CONFIG_DIR/config.gui_navbar | awk '{print \$6}' | grep -v NO | sort -u | sed "/^\$/d"\`
do
	echo "\$TN ..."
	cat > $OTMP_DIR/navbar <<EOF
<!-- begin navbar-->                                     <!--FILTER-->
<script type="text/javascript"><!--                     <!--FILTER-->
 imageOn = new Image();  imageOff = new Image();        <!--FILTER-->
 imageOn.src =  "$IMG_URL/itemOn.gif";                 <!--FILTER-->
 imageOff.src = "$IMG_URL/itemOff.gif";                <!--FILTER-->
 function over(imgname) { imgname.src = imageOn.src }   <!--FILTER-->
 function out(imgname)  { imgname.src = imageOff.src }  <!--FILTER-->
//--></script>					<!--FILTER-->

<table BORDER=0 WIDTH="100%" CELLSPACING=1 CELLPADDING=1 BGCOLOR=#000000> <!--FILTER-->
EOF

	for ITEM in \`grep "^NAV_BAR" $DFO_CONFIG_DIR/config.gui_navbar | awk '{print \$2}'\`
	do
       		TEXT=\`grep              \$ITEM $DFO_CONFIG_DIR/config.gui_navbar | grep -v "^#" | grep NAV_BAR | awk '{print \$3}'\`
        	STATUS_TEXT=\`grep       \$ITEM $DFO_CONFIG_DIR/config.gui_navbar | grep -v "^#" | grep NAV_BAR | awk '{print \$5}'\`
        	TOOL=\`grep              \$ITEM $DFO_CONFIG_DIR/config.gui_navbar | grep -v "^#" | grep NAV_BAR | awk '{print \$6}'\`
        	URL=\`grep               \$ITEM $DFO_CONFIG_DIR/config.gui_navbar | grep -v "^#" | grep NAV_BAR | awk '{print \$4}'\`

        	if [ \$TEXT = "SPACE" ]
        	then
               		cat >> $OTMP_DIR/navbar <<EOF
  <tr><td BGCOLOR=$NOT_THISPAGE_COLOR ALIGN="RIGHT" nowrap>&nbsp;</td></tr> <!--FILTER-->
EOF
	                continue
       		fi

        	TEXT=\`echo \$TEXT | sed "s/_/ /g"\`
        	STATUS_TEXT=\`echo \$STATUS_TEXT | sed "s/_/ /g"\`

        	ITEM_IS_HEADER=\`echo \$ITEM | grep HEADER\`
        	if [ "Q\$ITEM_IS_HEADER" != "Q" ]
        	then
               		cat >> $OTMP_DIR/navbar <<EOF
  <tr><td BGCOLOR=$NOT_THISPAGE_COLOR ALIGN="LEFT" nowrap><font color=#666666 size=2><b>\$TEXT</td></tr> <!--FILTER-->
EOF
                	continue
        	fi

		URL_BASE=\`basename \$URL\`
		if [ \$URL_BASE = \$URL ]
		then
			URL=$DFO_MON_DIR/\$URL
		fi

        	if [ "\$TOOL" = "\$TN" ]
        	then
               		BGCOLOR=$THIS_PAGE_COLOR
        	else
                	BGCOLOR=$NOT_THISPAGE_COLOR
        	fi

        	cat >> $OTMP_DIR/navbar <<EOF
  <tr><td BGCOLOR=\$BGCOLOR align="right" nowrap><img name="\$ITEM" src="$IMG_URL/itemOff.gif" width="10" height="10" hspace="1" border="0"><a onMouseOver="over(\$ITEM);" onMouseOut="out(\$ITEM);" title="\$STATUS_TEXT" href="\$URL"  target=_top><font color=#FFFFFF size=2>\$TEXT</a></td></tr> <!--FILTER-->
EOF
	done

	cat >> $OTMP_DIR/navbar <<EOF
</table><!--FILTER-->
<!-- end navbar--> <!--FILTER-->

EOF
	mv $OTMP_DIR/navbar $DFO_MON_DIR/vnavbar_\${TN}.html
done
echo ""
echo "... done. The updated navigation bars will display after the tools have been refreshed. Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/refresh_gui_navbar.esh

# ===================================================================================
# 0.4.7 edit_JOB_FILE.esh 
# ===================================================================================

	cat > $DFO_GUI_DIR/edit_JOB_FILE.esh <<EOT
#!/bin/sh

$JOB_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_JOB_FILE.esh

# ===================================================================================
# 0.4.8 edit_ING_FILE.esh and edit_CLEAN_FILE.esh
# ===================================================================================

	cat > $DFO_GUI_DIR/edit_ING_FILE.esh <<EOT
#!/bin/sh

$ING_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_ING_FILE.esh

	cat > $DFO_GUI_DIR/edit_CLEAN_FILE.esh <<EOT
#!/bin/sh

$CLEAN_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_CLEAN_FILE.esh
fi

# ===================================================================================
# 0.4.9 createRefresh_no_disk
# ===================================================================================

createRefresh_no_disk(){
cat > $DFO_GUI_DIR/refresh_dfoMonitor_nodisk.esh <<EOT
#!/bin/sh
echo "refresh dfoMonitor ..."
dfoMonitor -s
$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
exit
EOT
}

# ===================================================================================
# 0.4.10 launch_refreshVCAL.esh
# ===================================================================================

if [ $THIS_IS_DFOS = YES ]
then
	cat > $DFO_GUI_DIR/launch_refreshVCAL.esh <<EOT
#!/bin/sh
cd $DFO_LOG_DIR
LAST_DATE=\`ls -d 20* | tail -1\`
echo "Enter a reference date (default = last available: \$LAST_DATE) :"
read REF_DATE
if [ "Q\$REF_DATE" = "Q" ]
then
	REF_DATE=\$LAST_DATE
fi
echo "Enter N_VCAL_LIST (default = configured value: $N_VCAL_LIST) :"
read N_VCAL_LIST1
if [ "Q\$N_VCAL_LIST1" = "Q" ]
then
	N_VCAL_LIST1=$N_VCAL_LIST
fi

$DFO_BIN_DIR/refreshVCAL -d \$REF_DATE -n \$N_VCAL_LIST1
echo "... done. Hit return:"
read input
exit
EOT
	chmod u+x $DFO_GUI_DIR/launch_refreshVCAL.esh
fi

# ===================================================================================
# 0.4.11 launch_productExplorer.esh
# ===================================================================================

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/launch_productExplorer.esh <<EOT
#!/bin/sh
echo "- Specify a date and launch productExplorer -d <date>. "
echo "- For the other options of productExplorer, launch the tool on the command-line."
echo ""
echo "Enter a date:"
read XPLORE_DATE
if [ "Q\$XPLORE_DATE" = "Q" ]
then
	echo "You have to enter a valid date. Hit return:"
	read input
	exit
fi

echo ""
$DFO_BIN_DIR/productExplorer -d \$XPLORE_DATE 
echo "... done. Hit return:"
read input
exit
EOT
	chmod u+x $DFO_GUI_DIR/launch_productExplorer.esh

# ===================================================================================
# 0.4.12 launch_extractStat.esh
# ===================================================================================

	cat > $DFO_GUI_DIR/launch_extractStat.esh <<EOT
#!/bin/sh
echo "We call extractStat -i ..."
$DFO_BIN_DIR/extractStat -i
echo "... done. Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/launch_extractStat.esh


# ===================================================================================
# 0.4.13 launch_JOB_FILE.esh 
# ===================================================================================

	cat > $DFO_GUI_DIR/launch_JOB_FILE.esh <<EOT
#!/bin/sh
echo "Do you really want to launch the job file (Y/N) ?"
read LAUNCH_YN

if [ "Q\$LAUNCH_YN" = "QY" ]
then
	$DFO_JOB_DIR/$JOB_FILE_NAME
	echo "refresh dfoMonitor ..."
	dfoMonitor
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
fi
exit
EOT
	chmod u+x $DFO_GUI_DIR/launch_JOB_FILE.esh

# ===================================================================================
# 0.4.14 launch_ING_FILE.esh 
# ===================================================================================

	cat > $DFO_GUI_DIR/launch_ING_FILE.esh <<EOT
#!/bin/sh
echo "Do you really want to launch the INGEST file (Y/N) ?"
read LAUNCH_YN

if [ "Q\$LAUNCH_YN" = "QY" ]
then
	$DFO_JOB_DIR/$ING_FILE_NAME
	echo "refresh dfoMonitor ..."
	dfoMonitor
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
fi
exit
EOT
	chmod u+x $DFO_GUI_DIR/launch_ING_FILE.esh

# ===================================================================================
# 0.4.15 launch_CLEAN_FILE.esh
# ===================================================================================

	cat > $DFO_GUI_DIR/launch_CLEAN_FILE.esh <<EOT
#!/bin/sh
echo "Do you really want to launch the CLEANUP file?
[Note: never run this in parallel with a 'createAB' session.]
(Y/N) (N)"
read LAUNCH_YN

if [ "Q\$LAUNCH_YN" = "QY" ]
then
	$DFO_JOB_DIR/JOBS_CLEANUP 
	echo "refresh dfoMonitor ..."
	dfoMonitor
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
	echo "... done. Hit return:"
	read input
fi
exit
EOT
	chmod u+x $DFO_GUI_DIR/launch_CLEAN_FILE.esh

# ===================================================================================
# 0.4.16 edit_DFO_POSTIT.esh 
# ===================================================================================

	cat > $DFO_GUI_DIR/edit_DFO_POSTIT.esh <<EOT
#!/bin/sh

$POSTIT_EDITOR
echo "Refresh dfoMonitor to make changes visible? (Y/N) (Y)"
read REFRESH_YN
if [ "Q\$REFRESH_YN" != "QN" ]
then
	dfoMonitor
fi
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_DFO_POSTIT.esh
fi

# ===================================================================================
# 0.4.17 done_phoenix.esh (only for PHOENIX_ENABLED = YES)
# ===================================================================================
if [ $THIS_IS_PHOENIX = NO ] && [ Q$PHOENIX_ENABLED = QYES ]
then
	cat > $DFO_GUI_DIR/done_phoenix.esh <<EOT
#!/bin/sh
# job to be launched from dfoMonitor.html

echo "If you confirm the execution of the listed phoenix jobs for $DFO_INSTRUMENT as $PHOENIX_ACCOUNT,
the reminder on the dfoMonitor will be removed. If there is more than one month to do,
you will be prompted for each month, starting with the oldest one. Hit return to continue:"

read input

if [ ! -s $OTMP_DIR/df_list_phoenix ]
then
	echo "ERROR: no list of open PHOENIX jobs found. Exit."
	exit
else
	cat $OTMP_DIR/df_list_phoenix
fi

for DP in \`cat $OTMP_DIR/df_list_phoenix\`
do
	echo "
Do you confirm that the phoenix job as $PHOENIX_ACCOUNT for \$DP is terminated (Y/N; default: N)?"
	read CONFIRM_YN
	if [ Q\$CONFIRM_YN != QY ]
	then
		echo "\$DP: termination not confirmed."
	else
		echo "\$DP: termination successfully confirmed, this month has been removed from \$DFO_MON_DIR/DFO_STATUS."
		sed -i -e "/^phoenix_Ready \$DP/d" $DFO_MON_DIR/DFO_STATUS
	fi
	echo ""
done

echo "Refreshing dfoMonitor ..."
dfoMonitor
$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html &
exit
EOT
	chmod u+x $DFO_GUI_DIR/done_phoenix.esh
fi

# ===================================================================================
# 0.4.18 refresh_disk.ash
# ===================================================================================
if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/refresh_disk.ash <<EOT
#!/bin/sh
DISK_USED=\`df $DATA_DISK | grep -v File | awk '{printf"%5.1f GB\n",\$3/1024/1024}'\`
DISK_FRAC=\`df $DATA_DISK | grep -v File | awk '{printf"%i\n",\$5/quota*100}' quota=$DATA_QUOTA\`
DISK8=\`echo \$DISK_FRAC | awk '{printf"%1i\n", \$1/100*8}'\`
DISK_SYMBOL=\`echo pie\${DISK8}.gif\`

sed -i -e "s/^.*<!--DISK_STAT1-->/             \$DISK_USED (\${DISK_FRAC}%)<!--DISK_STAT1-->/" $DFO_MON_DIR/dfoMonitor.html 
sed -i -e "s/disk.*<!--DISK_STAT2-->/\$DISK_SYMBOL height=18 width=17 border=0><!--DISK_STAT2-->/" $DFO_MON_DIR/dfoMonitor.html
$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
EOT
	chmod u+x $DFO_GUI_DIR/refresh_disk.ash
fi

# ===================================================================================
# 0.4.18a procedure changeGanglia
# $1: hour|day|week|month $2: HOUR|DAY|WEEK|MONTH
# ===================================================================================
changeGanglia(){

cat > $DFO_GUI_DIR/change_$1.esh <<EOT
#!/bin/sh
echo "Switching Ganglia to 'last $1' (wait a few seconds)..."
for FRQ in \`cat $OTMP_DIR/ganglia | awk '{print \$1}'\`
do
	if [ $1 = \$FRQ ]
	then	
		STYLE="class=\"nav-subitem\""
	else
		STYLE=""
	fi

	sed  "/GANGLIA_MON--/s/r=\${FRQ}/r=${1}/"g $DFO_MON_DIR/dfoMonitor.html |\
         sed "/GANGLIA displays last \$FRQ/s/<a.*href=/<a \$STYLE href=/" \
         > $OTMP_DIR/tmp_monitor
	mv $OTMP_DIR/tmp_monitor $DFO_MON_DIR/dfoMonitor.html
done

sed "/^GANGLIA_FREQ/s/^.*/GANGLIA_FREQ	$2	#DAY|WEEK|MONTH: time interval displayed (NOTE: this may be modified by the tool!)/" $DFO_CONFIG_DIR/config.dfoMonitor > $OTMP_DIR/tmp_config
if [ -s $OTMP_DIR/tmp_config ]
then
	mv $OTMP_DIR/tmp_config $DFO_CONFIG_DIR/config.dfoMonitor
else
	echo "Can't do. Hit return:"
	read input
fi

$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
EOT
chmod u+x $DFO_GUI_DIR/change_$1.esh
}

if [ $THIS_IS_OPSHUB = NO ]
then
	changeGanglia hour HOUR
	changeGanglia day  DAY
	changeGanglia week WEEK
	changeGanglia month MONTH
fi

# ===================================================================================
# 0.4.19 tail_autoDaily.ash: view the autoDaily log
# ===================================================================================

if [ $THIS_IS_DFOS = YES ]
then
	cat > $DFO_GUI_DIR/tail_autoDaily.ash <<EOT
#!/bin/sh
xterm -T "AUTODAILY log watcher ($OTMP_DIR/autolog); hit ctrl-C to terminate" -geom 75x15+1000+500 -e tail -f $OTMP_DIR/autolog
EOT
	chmod u+x $DFO_GUI_DIR/tail_autoDaily.ash
fi

# ===================================================================================
# 0.4.20 writeXDM: create the extended diskspace monitor XDM
# ===================================================================================

writeXDM(){
cat >> $TMP_DIR/disk_space_monitor.html <<EOT
        <tr align=right bgcolor=#FFFF00>
          <td><font size=1 color=${TITLE_COLOR}><b><a title="$5">${6}:</a></b></font></td>
          <td><font size=1 color=${TITLE_COLOR}><b><a title="$1 GB">$1</a></b></font></td>
          <td valign=middle><img src=${IMG_URL}/sq3.png width=$2 height=6 title="${4}%"><img src=${IMG_URL}/sqg.png width=$3 height=6></td>
        </tr>
EOT
}

# ===================================================================================
# 0.4.21 refreshXDM.esh: function to refresh LOG_DISK, PLT_DISK, PLT_DISK
# ===================================================================================

cat > $DFO_GUI_DIR/refreshXDM.esh <<EOT
#!/bin/sh
echo "Updating disk space for \$DFO_LOG_DIR ..."
UPDATE=\`date -u +%Y-%m-%d"T"%H:%M:%S\`
export LOG_DISK=\`du -s $DFO_LOG_DIR/2* 2>/dev/null | awk '{sum +=\$1} END {printf"%i\n", sum/1024/1024 }'\`
echo "Updating disk space for \$DFO_PLT_DIR ..."
export PLT_DISK=\`du -s $DFO_PLT_DIR/2* 2>/dev/null | awk '{sum +=\$1} END {printf"%i\n", sum/1024/1024 }'\`
echo "Updating disk space for \$DFO_HDR_DIR ..."
export HDR_DISK=\`du -s $DFO_HDR_DIR/2* 2>/dev/null | awk '{sum +=\$1} END {printf"%i\n", sum/1024/1024 }'\`

if [ $THIS_IS_OPSHUB = NO ]
then
	cp $DFO_MON_DIR/DFO_STATUS $DFO_MON_DIR/DFO_STATUS.save
fi
sed -i -e "/^DFO_PLT_DIR/d" \\
       -e "/^DFO_LOG_DIR/d" \\
       -e "/^DFO_HDR_DIR/d" $DFO_MON_DIR/DFO_STATUS

cat >> $DFO_MON_DIR/DFO_STATUS <<EOF
DFO_PLT_DIR \$PLT_DISK \$UPDATE
DFO_LOG_DIR \$LOG_DISK \$UPDATE
DFO_HDR_DIR \$HDR_DISK \$UPDATE
EOF

echo "... done. Now we refresh the dfoMonitor to make the changes visible ..."
dfoMonitor
exit
EOT
chmod u+x $DFO_GUI_DIR/refreshXDM.esh

# ===================================================================================
# 0.4.22 refreshTRQ.esh: query to get open transfer requests
# $1: -Q (optional) --> quiet mode; only files listed with delay <3 days
# ===================================================================================

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/refreshTRQ.esh <<EOF
#!/bin/sh
CURRENT=\`date -u +%b" "%d" "%Y" "%r\`
QUIET=NO

while getopts Q OPTION
do
	case "\$OPTION" in
	 Q ) QUIET=YES ;;
	esac
done

cat > $OTMP_DIR/sara_sql1 <<EOT
select
	trq.file_id ,
	convert (char(7),dp.dp_cat) ,
	transfer_method ,
	transfer_priority ,
	transfer_status ,
	convert (char(15), submission_time ,  102 ),
	convert (char(15), submission_time ,  108 ),
	datediff (mi, submission_time, "\$CURRENT" )
from
	sara..transfer_requests trq ,
	observations..data_products dp
where
	trq.file_id like "${DFO_FILE_NAME}%"
and
	transfer_catg = "rawfile"
and
	transfer_status != 6
and
	transfer_priority between 0 and 40
and
	trq.file_id = dp.dp_id
and
	datediff (hh, submission_time, "\$CURRENT" ) < 72
-- 3 days!
ORDER BY
	datediff (hh, submission_time, "\$CURRENT" )
go
EOT

if [ \$QUIET = NO ]
then
	echo "Data transfer: view delayed files (somewhere between OLAS and PA; check DataTransferMonitor for details)

isql query ...
"
fi

# sara_out1: only entries with delays >1hr
rm -f $OTMP_DIR/sara_out1 $OTMP_DIR/sara_out
isql -Uqc -P\`cat \$QC1_PWD\` -S${QC1_SERVER} -w999 -i $OTMP_DIR/sara_sql1 > $OTMP_DIR/sara_out
export CHECK_ERROR=\`grep "Login failed" $OTMP_DIR/sara_out | head -1\`

if [ "Q\$CHECK_ERROR" != Q ] &&  [ \$QUIET = NO ]
then
	echo "Error: Login to sara database failed."
fi

cat $OTMP_DIR/sara_out |\
 sed "1,2 d" |\
 grep -v affected |\
 sed "/^$/d" |\
 sed "s/\./Y/" | sed "s/\./Y/" | sed "s/\./-/"g | sed "s/Y/./"g |\
 awk '{if (\$8/60 > 1) {printf"%-35s %-7s %7s %-4i %-2i %10sT%8s %5.1f\\n",\$1".fits",\$2,\$3,\$4,\$5,\$6,\$7,\$8/60} else {printf"%-35s %-7s %7s %-4i %-2i %10sT%8s %5.1f\\n",\$1".fits",\$2,\$3,\$4,\$5,\$6,\$7,\$8/60} }'\
> $OTMP_DIR/sara_out1

# called only from dfoMonitor
if [ "Q\$CHECK_ERROR" != Q ]
then
	if [ \$QUIET = NO ]
	then
		echo "$DFO_INSTRUMENT: No connection to sara database. File delays unknown."
	fi
elif [ ! -s $OTMP_DIR/sara_out1 ]
then
	if [ \$QUIET = NO ]
	then
		echo "$DFO_INSTRUMENT: No files delayed by >1 hr."
	fi
else
	if [ \$QUIET = NO ]
	then
		echo "file_name                           DP_CATG method  Prio St submission           delay(hrs)"
		echo "-------------------------------------------------------------------------------------------"

		cat $OTMP_DIR/sara_out1

		echo "-------------------------------------------------------------------------------------------"
		echo "file_name                    DP_CATG method  Prio St submission      delay(hrs)"
		echo "
St:         file status (1-5: OLAS...staging area, but not in NGAS)
submission: timestamp of OLAS archiving
delay:      time in hrs between OLAS submission and NOW

This list is available as \$TMP_DIR/sara_out1 ."
	fi
fi

if [ \$QUIET = NO ]
then
	echo "Update browser ..."
fi

LONGEST_DELAY=0
LONGEST_DELAY_ANY=0

LONGEST_DELAY=\`cat $OTMP_DIR/sara_out1 | grep CALIB | sort -k7,7 | tail -1 | awk '{if (\$7 > maxdelay) {printf"<a title=\"delays with more than 72 hrs ignored\">longest CALIB delay</a>: %-35s %-7s (%5.1f hrs)<br> \n",\$1,\$2,\$7}}' maxdelay=${MAXDELAY}\`
LONGEST_DELAY_ANY=\`cat $OTMP_DIR/sara_out1 | sort -k7,7 | tail -1 | awk '{if (\$7 > maxdelay) {printf"<a title=\"delays with more than 72 hrs ignored\">longest delay (any dpr.catg)</a>: %-35s %-7s (%5.1f hrs)\n",\$1,\$2,\$7}}' maxdelay=${MAXDELAY}\`

if [ "Q\$CHECK_ERROR" != Q ]
then
	TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Data transfer system DTS: query to sara database failed, transfer status unknown\">transfer: unknown</a></font></td>"
	LONGEST_DELAY_ANY="<a title=\"delays unknown since sara database down\">longest delay unknown</a>"
elif [ "Q\$LONGEST_DELAY" = "Q" ]
then
	LONGEST_DELAY="no CALIB file delayed by >1 hr"
	TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"Data transfer system DTS: no CALIB file delayed by more than 1 hour\">transfer</a></font></td>"
else
	TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Data transfer system DTS: CALIB files delayed\">transfer: error</a></font></td>"
fi

sed "/<!--LONGEST_DELAY--/s|^.*|\${LONGEST_DELAY} <!--LONGEST_DELAY-->|" $DFO_MON_DIR/dfoMonitor.html |\
 sed "/<!--LONGEST_DELAY_ANY--/s|^.*|\${LONGEST_DELAY_ANY} <!--LONGEST_DELAY_ANY-->|" |\
 sed "/<!--TRANSFER_STATUS--/s|^.*|\${TRANSFER_STATUS} <!--FILTER--> <!--TRANSFER_STATUS-->|" > $OTMP_DIR/dfoMonitor.html$$
mv $OTMP_DIR/dfoMonitor.html$$ $DFO_MON_DIR/dfoMonitor.html

if [ $SCP_YN = YES ]
then
	echo "\${TRANSFER_STATUS}" | sed "s/999/CCCCCC/" > $DFO_MON_DIR/transferWatcher.html
	scp -o BatchMode=yes $DFO_MON_DIR/transferWatcher.html ${DFO_WEB_SERVER}:${WEB_DIR}/ 1>/dev/null
fi

$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html &

if [ \$QUIET = NO ]
then
	echo "Hit return:"
	read input
fi
EOF
	chmod u+x $DFO_GUI_DIR/refreshTRQ.esh
fi

# define procedure to call it either with or without parameter -Q
refreshTRQ(){
$DFO_GUI_DIR/refreshTRQ.esh $1
}

# since the database is sometimes slow, we evaluate results from the previous run ...

if [ $THIS_IS_DFOS = NO ]
then
	TRANSFER_STATUS="<td bgcolor=#CCCCCC></td><td bgcolor=#CCCCCC></td>"
else
	LONGEST_DELAY=0
	LONGEST_DELAY_ANY=0

	LONGEST_DELAY=`if [ -s $OTMP_DIR/sara_out1 ]; then cat $OTMP_DIR/sara_out1 | grep CALIB | sort -k7,7 | tail -1 | awk '{if ($7 > maxdelay) {printf"<a title=\"delays with more than 72 hrs ignored\">longest CALIB delay</a>: %-35s %-7s (%5.1f hrs)<br>\n",$1,$2,$7}}' maxdelay=$MAXDELAY; fi`
	LONGEST_DELAY_ANY=`if [ -s $OTMP_DIR/sara_out1 ]; then cat $OTMP_DIR/sara_out1 | sort -k7,7 | tail -1 | awk '{if ($7 > maxdelay) {printf"<a title=\"delays with more than 72 hrs ignored\">longest delay (any dpr.catg)</a>: %-35s %-7s (%5.1f hrs)\n",$1,$2,$7}}' maxdelay=$MAXDELAY; fi`
	CHECK_ERROR=`if [ -s $OTMP_DIR/sara_out1 ]; then cat $OTMP_DIR/sara_out1 | grep "Login failed"; fi`

	if [ "Q$CHECK_ERROR" != Q ]
	then
		TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Data transfer system DTS: query to sara database failed, transfer status unknown\">transfer: unknown</a></font></td>"
		LONGEST_DELAY_ANY="<a title=\"delays unknown since sara database down\">longest delay unknown</a>"
	elif [ "Q$LONGEST_DELAY" = "Q" ]
	then
		LONGEST_DELAY="<a title=\"delays with more than 72 hrs ignored\">no CALIB file delayed by >1 hr</a>"
		TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"Data transfer system DTS: no CALIB file delayed by more than one hour\">transfer</a></font></td>"
	else
		TRANSFER_STATUS="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Data transfer system DTS: CALIB files delayed\">transfer: error</a></font></td>"
	fi

# ... and now call it in the background, for displaying the current longest delay
	if [ $NGAS_TEST = YES ]
	then
		if [ $QUIET = NO ]
		then
			echo "  -checking DTS (in the background) ..."
		fi
	
		refreshTRQ -Q &
	else
		if [ $QUIET = NO ]
		then
			echo "  -DTS test skipped."
		fi
	fi

# transfer to ${DFO_WEB_SERVER} to be available for HC monitor
	if [ $SCP_YN = YES ]
	then
		echo "${TRANSFER_STATUS}" | sed "s/999/CCCCCC/" > $DFO_MON_DIR/transferWatcher.html
		scp -o BatchMode=yes $DFO_MON_DIR/transferWatcher.html ${DFO_WEB_SERVER}:${WEB_DIR}/ 1>/dev/null
	fi

# ===================================================================================
# 0.4.23 refreshPA.esh: query to get recent data transfer statistics
# ===================================================================================

	cat > $DFO_GUI_DIR/refreshPA.esh <<EOF
#!/bin/sh
CURRENT=\`date -u +%b" "%d" "%Y" "%r\`
cat > $OTMP_DIR/sara_sql2 <<EOT
select
	trq.file_id ,
	convert (char(7),dp.dp_cat) ,
	transfer_method ,
	transfer_priority ,
	transfer_status ,
	convert (char(15), last_update_time ,  102 ),
	convert (char(15), last_update_time ,  108 ),
	datediff (mi, submission_time, last_update_time )
from
	sara..transfer_requests trq ,
	--obs_metadata..data_products dp
	observations..data_products dp
where
	trq.file_id like "${DFO_FILE_NAME}%"
and
	transfer_catg = "rawfile"
and
	transfer_status = 6
and
	trq.file_id = dp.dp_id
and
	datediff (hh, last_update_time, "\$CURRENT" ) < 24
-- last 24 hours
ORDER BY
	trq.file_id
go
EOT

echo "Data transfer: delay statistics for NGAS files which arrived in last 24 hrs

isql query ...
"
rm -f $TMP_DIR/sara_out2 $OTMP_DIR/sara_out
isql -Uqc -P\`cat \$QC1_PWD\` -S${QC1_SERVER} -w999 -i $OTMP_DIR/sara_sql2 > $OTMP_DIR/sara_out
CHECK_ERROR=\`grep "Login failed" $OTMP_DIR/sara_out | head -1\`
if [ "Q\$CHECK_ERROR" != Q ]
then
	echo "Login to sara database failed. Hit return:"
	read input
	exit
fi

cat $OTMP_DIR/sara_out |\
 sed "1,2 d" |\
 grep -v affected |\
 sed "/^$/d" |\
 sed "s/\./Y/" | sed "s/\./Y/" | sed "s/\./-/"g | sed "s/Y/./"g |\
 awk '{printf"%-35s %-7s %7s %-4i %-2i %10sT%8s %5.1f\\n",\$1".fits",\$2,\$3,\$4,\$5,\$6,\$7,\$8/60}' \
> $OTMP_DIR/sara_out2

if [ ! -s $OTMP_DIR/sara_out2 ]
then
	echo "No files detected for $DFO_INSTRUMENT in last 24 hours."
else
	echo "file_name                           DP_CATG method  Prio St submission           delay(hrs)"
	echo "-------------------------------------------------------------------------------------------"

	cat $OTMP_DIR/sara_out2
	echo "-------------------------------------------------------------------------------------------"
	echo "file_name                           DP_CATG method  Prio St submission           delay(hrs)"
	echo "
St:         file status (6: archived in NGAS)
submission: timestamp of OLAS archiving
delay:      time in hrs between OLAS submission and arrival in Primary Archive

This list is available as \$TMP_DIR/sara_out2 ."
fi

echo "Hit return:"
read input
EOF
	chmod u+x $DFO_GUI_DIR/refreshPA.esh
fi

# ===================================================================================
# 0.4.24 createWrapper: create a wrapper around ngasClient, to avoid overly long timeouts
# ===================================================================================

createWrapper(){
cat > $TMP_DIR/wrappedNgasclient <<EOT
#!/bin/sh
cd \$HOME
ngasClient -f $TEST_FILE -F > $OTMP_DIR/ngas_report &
PID=\$!
ALREADY_SLEPT=0

while [ \$ALREADY_SLEPT -lt $SLEEP_MAX ]
do
        CHECK_PID=\`ps \$PID | grep -v PID  | awk '{print \$6}'\`
        if [ Q\${CHECK_PID} != Q ]
        then
                ALREADY_SLEPT=\`echo \$ALREADY_SLEPT $SLEEP | awk '{print \$1+\$2}'\`
                sleep $SLEEP
        else
                break
        fi
done

CHECK_PID=\`ps \$PID | grep -v PID | awk '{print \$6}'\`
if [ Q\${CHECK_PID} != Q ]
then
        kill -9 \$PID 2>&1 1>/dev/null
        echo "TIMEOUT: ngasClient killed after $SLEEP_MAX secs."
fi
EOT
chmod u+x $TMP_DIR/wrappedNgasclient
}

# ===================================================================================
# 0.4.25 ngasAccess: check access to NGAS, by attempting to download a file
#        (taken from ngasMonitor)
# ===================================================================================

ngasAccess(){
if [ $QUIET = NO ]
then
	echo "  -testing NGAS access (in the background) ..."
fi

rm -f $OTMP_DIR/ngas_report $HOME/${TEST_FILE} $HOME/${TEST_FILE}.*
createWrapper
$TMP_DIR/wrappedNgasclient

if [ -s $HOME/${TEST_FILE} ]
then
	echo 0 > $OTMP_DIR/ngasAccess_report
else
	echo -1 > $OTMP_DIR/ngasAccess_report
fi

if [ -s $OTMP_DIR/ngas_report ]
then
	sed -i -e "s/Exit.//" $OTMP_DIR/ngas_report
fi

rm -f $HOME/${TEST_FILE} $HOME/${TEST_FILE}.*
}

# ===================================================================================
# 0.4.26 create_proc_status: get processing status from DFO_STATUS
#	$1: date
# ===================================================================================
create_proc_status(){
echo "cal_AB
cal_Queued
cal_QC
cal_Certif
cal_Finished
cal_Renamed
cal_Updated
cal_Ingested
sci_AB
sci_Queued
sci_QC
sci_Certif
sci_Finished
sci_Renamed
sci_Updated
sci_Ingested
sci_Verified
finished
cal_Cleaned
" > $TMP_DIR/all_status_values_$$

echo "
Pre-processing:" > $DFO_MON_DIR/proc_status_$1.txt

EXIST=`grep "hdr_Exist $1 " $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{print $3}'`
if [ "Q$EXIST" != Q ]
then
        echo "hdr_Exist:	yes	$EXIST" >> $DFO_MON_DIR/proc_status_$1.txt
else
        echo "hdr_Exist:	no" >> $DFO_MON_DIR/proc_status_$1.txt
fi

COMPL=`grep "raw_Complete $1 " $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{print $3}'`
if [ "Q$COMPL" != Q ]
then
        echo "raw_Complete:	yes	$COMPL" >> $DFO_MON_DIR/proc_status_$1.txt
else
        echo "raw_Complete:	no" >> $DFO_MON_DIR/proc_status_$1.txt
fi

CHECK_CAL=`grep " $1 " $DFO_MON_DIR/DFO_STATUS | grep "^cal_" | grep -v Cleaned | tail -1`
if [ "Q$CHECK_CAL" = Q ]
then
	echo "
no CAL processing" >> $DFO_MON_DIR/proc_status_$1.txt
else	
	echo "
CAL processing: (last entries only)" >> $DFO_MON_DIR/proc_status_$1.txt

	for S in `cat $TMP_DIR/all_status_values_$$  | grep "^cal_"`
	do
       		grep "^$S $1 " $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{print $1,$3}' | tr " " "	" | sed "s/cal_AB/&	/" | sed "s/cal_QC/&	/" >> $DFO_MON_DIR/proc_status_$1.txt
	done
fi

CHECK_SCI=`grep " $1 " $DFO_MON_DIR/DFO_STATUS | grep "^sci_" | tail -1`
if [ "Q$CHECK_SCI" = Q ]
then
	echo "
no SCI processing" >> $DFO_MON_DIR/proc_status_$1.txt
else
	echo "
SCI processing: (last entries only)" >> $DFO_MON_DIR/proc_status_$1.txt

	for S in `cat $TMP_DIR/all_status_values_$$  | grep "^sci_" | grep -v sci_Finished`
	do
       		grep "^$S $1 " $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{print $1,$3}' | tr " " "	" | sed "s/sci_AB/&	/" | sed "s/sci_QC/&	/" >> $DFO_MON_DIR/proc_status_$1.txt
	done
fi

CHECK_POST=`grep " $1 " $DFO_MON_DIR/DFO_STATUS | egrep "^finished|Cleaned" | tail -1`
if [ "Q$CHECK_POST" = Q ]
then
	echo "
no post-processing" >> $DFO_MON_DIR/proc_status_$1.txt
else
	echo "
post-processing: (last entries only)" >> $DFO_MON_DIR/proc_status_$1.txt

	for S in `cat $TMP_DIR/all_status_values_$$  | egrep -v "^cal_|^sci_"; cat $TMP_DIR/all_status_values_$$  | grep "cal_Cleaned"`
	do
		grep "^$S $1 " $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{print $1,$3}' | tr " " "	" >> $DFO_MON_DIR/proc_status_$1.txt
	done
fi

rm -f $TMP_DIR/all_status_values_$$
}

# ===================================================================================
# 0.4.27 launch_dfoManager.esh
# ===================================================================================

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/launch_dfoManager.esh <<EOT
#!/bin/sh
echo "Now we launch the dfoManager. The configured URLs will display in a new window. Hit return to continue:"
read input
dfoManager
echo "... done. Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/launch_dfoManager.esh
fi

# ===================================================================================
# 0.4.28 procedure autoDWatcher
# $1 ISO TIMESTAMP to monitor
# ===================================================================================
autoDWatcher(){
YEAR_TS=`echo  $1 | cut -c1-4`
MONTH_TS=`echo $1 | cut -c6,7 | awk '{print $1-1}'`
DAY_TS=`echo   $1 | cut -c9,10`
HOUR_TS=`echo  $1 | cut -c12,13`
MIN_TS=`echo   $1 | cut -c15,16`
SEC_TS=`echo   $1 | cut -c18,19`

OK_TAG="<tr bgcolor=${DONE_COLOR}><td width=1% bgcolor=#CCCCCC><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td nowrap><font size=1><b>"
NOK_TAG="<tr bgcolor=${NGAS_NOK_COLOR}><td width=1% bgcolor=#CCCCCC><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td nowrap><font size=1><b>"

cat > $TMP_DIR/autoDWatcher.html <<EOT
<SCRIPT language="JavaScript"><!--
var today = new Date();
var Now = today.getTime() / 1000 ;
var tzdiff = today.getTimezoneOffset();
var lastTimeStamp = new Date($YEAR_TS, $MONTH_TS, $DAY_TS, $HOUR_TS, $MIN_TS, $SEC_TS);
var TimeStamp = lastTimeStamp.getTime() / 1000 - tzdiff*60;

var dayDiff = Math.floor((Now-TimeStamp) / 86400 );
var hourDiff = Math.floor(((Now-TimeStamp) - dayDiff*86400)/3600);
var hourDiff2 = ((hourDiff < 10) ? "d 0" : "d ");
var minDiff = Math.floor(((Now-TimeStamp) - dayDiff*86400 - hourDiff*3600)/60);
var minDiff2 = ((minDiff < 10) ? "h:0" : "h:");

if ( dayDiff < 1 && hourDiff < $MAX_AUTOD ) {
 qc_tag = "${OK_TAG}" ;
 }
else
 {
qc_tag = "${NOK_TAG}" ;
}

document.write(qc_tag,"<a title=\"green if last autoDaily execution within $MAX_AUTOD hours; last: $1\">", dayDiff, hourDiff2, hourDiff, minDiff2, minDiff,"m autoDaily</a></b></td></tr>");
--></SCRIPT>
EOT
mv $TMP_DIR/autoDWatcher.html $DFO_MON_DIR
}

# ===================================================================================
# 0.5 create action jobs (the ones behind the buttons)
# 0.5.1 createGuiJob: the standard job creator
#   $1: action (launch, refresh)
#   $2: name of dfos tool 
#   $3: mode (1: w/o asking; 2: asking for DATE only; 3: DATE is known; 
#             4: DATE and MODE are known; 5: asking for DATE and MODE)
#             mode 4 has some cases where multiple tasks are launched (e.g. certify and moveProducts)
#	      modes 3 and 4 always have dfoMonitor launched at the end
#   $4: CALIB or SCIENCE (for mode 4)
# ===================================================================================

createGuiJob(){
TODAY1=`qcdate $TODAY -1`

# case1: execute
if [ $3 = 1 ] && [ $2 = dfoMonitor ]
then
	cat > $DFO_GUI_DIR/${1}_${2}.esh <<EOT
#!/bin/sh
echo "$1 $2 ..."
$2
$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
exit
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}.esh

elif [ $3 = 1 ]
then
	cat > $DFO_GUI_DIR/${1}_${2}.esh <<EOT
#!/bin/sh
echo "$1 $2 ..."
$2
exit
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}.esh

# case 2: interactive: we ask for DATE
elif [ $3 = 2 ] && [ $2 != rawdisp2reference ]
then
	cat > $DFO_GUI_DIR/${1}_${2}.esh <<EOT
#!/bin/sh
echo "$1 $2 ..."
echo "Which DATE to use (e.g. $TODAY1):"
read GUI_DATE
$2 -d \$GUI_DATE
echo "Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}.esh

elif [ $3 = 2 ] && [ $2 = rawdisp2reference ]
then
	cat > $DFO_GUI_DIR/${1}_${2}.esh <<EOT
#!/bin/sh
echo "$1 $2 ..."
echo "With this dialogue, you can call '$2' only for those dates that are already certified 
and within the last 7 days.
The list of all current reference frames is available in $DFO_MON_DIR/RAWDISP/list_RAWDISP_references.
Which DATE to use (e.g. $TODAY1):"
read GUI_DATE
if [ "Q\$GUI_DATE" != "Q" ]
then
	$DFO_GUI_DIR/refresh_browser ${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs/\${GUI_DATE}/status_\${GUI_DATE}.html
	echo ""
	echo "Use the exported AB monitor as reference for the AB list."
	rawdisp2reference -d \$GUI_DATE	-t | sed "s/^.*/[$2]	&/"
	echo "... done."
	dfoMonitor
	echo "Hit return:"
	read input
else
	echo "Think about it. Hit return:"
	read input
fi
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}.esh
# case 3: DATE is known (as $D)
elif [ $3 = 3 ] 
then
	cat > $DFO_GUI_DIR/${1}_${2}_${D}.esh <<EOT
#!/bin/sh
echo "$1 $2 for date $D (Y/N)?"
read START_YN
if [ "Q\$START_YN" = "QY" ]
then
	$2 -d $D
	echo ""
	dfoMonitor
	echo "Hit return:"
	read input
else
	echo "Think about it. Hit return:"
	read input
fi
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}_${D}.esh

# case 4: DATE and MODE are known (as $D and parameter $4)
# some special cases first, with combined launches
elif [ $3 = 4 ] && [ $2 = "certifyProducts" ]
then
	if [ $THIS_IS_PHOENIX = NO ]
	then
		cat > $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
#!/bin/sh
echo "$1 $2 and moveProducts for date $D and mode $4 (Y/N)?"
read START_YN
if [ "Q\$START_YN" = "QY" ]
then
	certifyProducts -d $D -m $4
	echo ""
	echo "================================================================="
	echo "We now continue with moveProducts. Hit return:"
	read input
EOT
	else
		cat > $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
#!/bin/sh
echo "$1 and moveProducts for date $D and mode $4 (Y/N)?"
read START_YN
if [ "Q\$START_YN" = "QY" ]
then
	rm -rf $DFO_SCI_DIR/$D
EOT
	fi

	cat >> $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
	moveProducts    -d $D -m $4
EOT

	if [ $THIS_IS_PHOENIX = YES ]
	then
		cat >> $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT

	echo "Remove \$DFO_RAW_DIR/$D ..."
	rm -rf $DFO_RAW_DIR/$D
	echo "Cleanup CAL is left for the next automatic tool run."
	echo "================================================================="
	dfoMonitor -n
EOT
	else
		cat >> $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
	echo ""
	echo "================================================================="
	dfoMonitor
EOT
	fi

	cat >> $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
	echo "Hit return:"
	read input
else
	echo "Think about it. Hit return:"
	read input
fi
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh

elif [ $3 = 4 ] && [ $2 = "certifyPLight" ]
then
	cat > $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
#!/bin/sh
echo "$1 $2 (\"light\": no moveP, just certification workflow plus export of flags and comments) 
  for date $D (TODAY) and mode CALIB (Y/N)?"
read START_YN
if [ "Q\$START_YN" = "QY" ]
then
	certifyProducts -d $D -m $4 -L
	echo "
- 'moveProducts' has not been called. 
- Comments have been inserted. HC plots are being updated, if applicable.
- The AB monitor has been updated and displays
  your comments both on the local and the exported version. 
- To finish, you will need to use the regular certifyProducts workflow button
  (you can skip there already certified ABs).
================================================================="
	dfoMonitor
	echo "Hit return:"
	read input
else
	echo "Think about it. Hit return:"
	read input
fi
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh

# createAB for CALIB/SCIENCE
elif [ $3 = 4 ] && [ $2 = createAB ]
then
	cat > $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
#!/bin/sh
echo "$1 $2 for date $D and mode $4 (Y/N)?"
read START_YN
if [ "Q\$START_YN" != "QY" ]
then
	echo "Think about it. Hit return:"
	read input
	exit
fi

if [ $4 = SCIENCE ] 
then
# Check for mcalibs to be ingested
	CHECK_INGEST=\`grep "^cal_Ingested $D" $DFO_MON_DIR/DFO_STATUS | tail -1\`
	if [ "Q\$CHECK_INGEST" = Q ]
	then
		echo "*** WARNING: mcalibs have not yet been ingested for $D; verifyAB might give wrong results; continue (Y/N) (N)?"
		read CONTI_YN
		if [ "Q\$CONTI_YN" != QY ]
		then
			echo "Think about it. Hit return:"
			read input
			exit
		fi
	else
		echo "*** INFO: mcalibs are ingested. It takes a couple of minutes to make them available to calSelector. Ok to continue (Y/N) (Y)?"
		read CONTI_YN

		if [ "Q\$CONTI_YN" = QN ]
		then
			echo "OK, try later. Hit return:"
			read input
			exit
		fi
	fi

# check for calSelector lock file (WH 2018-07-30)
	if [ -f $DFO_AB_DIR/CALSELECTOR/.calselector.lock ]
	then
		echo "*** ERROR: $DFO_AB_DIR/CALSELECTOR/.calselector.lock detected, preventing the execution of verifyAB."
		echo "    Please investigate, delete .calselector.lock and then try again."
		echo "    Hit return:"
		read input
		exit -1
	fi

	echo "Execution plan:
- we create the SCIENCE ABs
- a completeness check is performed to check that all science files have created ABs
- we verify them using verifyAB
- we offer the ABs for inspection."

	echo "Hit return to continue:"
	read input
fi

if [ $CREATEAB_VCAL = YES ] && [ $4 = SCIENCE ]
then
	$2 -d $D -m $4 -N
else
	$2 -d $D -m $4
fi
echo "============================================================================="
echo ""
if [ $4 = SCIENCE ]
then
	echo "createJob: No createJob for $4."

	echo ""
	echo "============================================="
	echo "Call verifyAB for $D ..."
	verifyAB -d $D -P
	echo ""
	echo "getStatusAB: Refresh AB monitor to prepare for inspection ..."
	getStatusAB -d $D
# to get the provisional links right:
	sed -i -e "s|CALSELECTOR/$D|CALSELECTOR/WORK_DIR|"g $DFO_MON_DIR/status_$D.html 

else
	echo "Creating job (createJob -d $D -m $4)" | sed "s/^.*/	&/"
	createJob -d $D -m $4
	echo "============================================="
	echo ""
	echo "Refresh AB monitor (getStatusAB -d $D) ..."
	getStatusAB -d $D
fi

if [ $4 = SCIENCE ]
then
	D1="`qcdate $D +1`"
	N_AB=\`grep "DATE.*$D" $DFO_AB_DIR/*${D}*ab $DFO_AB_DIR/*\${D1}*ab 2>/dev/null | sed "s/:DATE.*//" | wc -l | awk '{print \$1}'\`
	sed -i -e "/<\!--AB_STATUS1--><\!--${D}/s|^.*|<td BGCOLOR=$INCOMPLT_COLOR nowrap><\!--${D}-->|" $DFO_MON_DIR/dfoMonitor.html
	sed -i -e "/<\!--AB_STATUS2--><\!--${D}/s|^.*|<a title=\"show AB status\" href=./status_${D}.html><img src=$IMG_URL/index.gif width=15 height=15 border=0></a> <font size=2>yes (\$N_AB)</font></td> <\!--${D}-->|" $DFO_MON_DIR/dfoMonitor.html
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/status_$D.html

	echo "Check out the SCIENCE ABs for $D. 
For verification they got calSelector XML files. If all OK (green), enter Y to continue, otherwise abort and fix:"

       	read CONTI_YN
	while [ "Q\$CONTI_YN" != "QY" ]
       	do
               	echo "      Enter Y to continue:"
          		read CONTI_YN
       	done
	
	if [ ! -d $DFO_LOG_DIR/CALSELECTOR/$D ]
	then
       		mkdir $DFO_LOG_DIR/CALSELECTOR/$D
	fi

	mv $DFO_LOG_DIR/CALSELECTOR/WORK_DIR/* $DFO_LOG_DIR/CALSELECTOR/$D/
	mv $DFO_LOG_DIR/CALSELECTOR/$D/calsel_overview.txt $DFO_LOG_DIR/CALSELECTOR/$D/verifyAB_$D.txt
	rm -f $DFO_LOG_DIR/CALSELECTOR/$D/*.tab

# also do the scp since moveProducts doesn't do that
	echo "Transfer of calSelector files to ${DFO_WEB_SERVER}:${INSTR_DIR}/logs/CALSELECTOR/$D ..."
	ssh -o BatchMode=yes $DFO_WEB_SERVER "bin/webDirCreate CALSELECT ${DFO_INSTRUMENT} $D"
	scp -o BatchMode=yes $DFO_LOG_DIR/CALSELECTOR/$D/*.xml ${DFO_WEB_SERVER}:${INSTR_DIR}/logs/CALSELECTOR/$D/
   	scp -o BatchMode=yes $DFO_LOG_DIR/CALSELECTOR/$D/*.txt ${DFO_WEB_SERVER}:${INSTR_DIR}/logs/CALSELECTOR/$D/ 1>/dev/null
	scp -o BatchMode=yes $DFO_LOG_DIR/CALSELECTOR/$D/*.html ${DFO_WEB_SERVER}:${INSTR_DIR}/logs/CALSELECTOR/$D/ 1>/dev/null

	echo "============================================="
	echo "Call moveProducts for $D and mode $4 ..."
	moveProducts -d $D -m $4

# set DFO_STATUS to sci_Verified
	UPDATE=\`date +%Y-%m-%d"T"%H:%M:%S\`
	DFO_STATUS="sci_Verified"
	CHECK_EXIST=\`grep "\$DFO_STATUS $D" $DFO_MON_DIR/DFO_STATUS | tail -1\`
	if [ "Q\$CHECK_EXIST" = Q ]
	then
		echo "\$DFO_STATUS $D \$UPDATE" >> $DFO_MON_DIR/DFO_STATUS
	fi
fi
echo "============================================="
echo ""
dfoMonitor
echo "Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh

# all other cases 4
elif [ $3 = 4 ]
then
	cat > $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh <<EOT
#!/bin/sh
echo "$1 $2 for date $D and mode $4 (Y/N)?"
read START_YN
if [ "Q\$START_YN" = "QY" ]
then
	$2 -d $D -m $4
	echo ""
	dfoMonitor
	echo "Hit return:"
	read input
else
	echo "Think about it. Hit return:"
	read input
fi
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}_${D}_${4}.esh

# case 5: interactive: we ask for DATE and MODE
elif [ $3 = 5 ]
then
	cat > $DFO_GUI_DIR/${1}_${2}.esh <<EOT
#!/bin/sh
echo "$1 $2 ..."
echo "Which DATE to use (e.g. $TODAY):"
read GUI_DATE
echo "Which MODE to use (CALIB or SCIENCE; default: CALIB):"
read MODE
if [ Q\$MODE = Q ]
then
	MODE=CALIB
fi
echo "We call $2 -d \$GUI_DATE -m \$MODE ..."
$2 -d \$GUI_DATE -m \$MODE
echo "Hit return:"
read input
EOT
	chmod u+x $DFO_GUI_DIR/${1}_${2}.esh
fi
}

# ===================================================================================
# 0.5.2 removeNight_${D}.esh
# ===================================================================================

removeNight(){
cat > $DFO_GUI_DIR/removeNight_${D}.esh <<EOT
#!/bin/sh
echo "remove DATE $D from dfoMonitor (Y/N) ?"
read REMOVE_YN
if [ "Q\$REMOVE_YN" != "QY" ]
then
	echo "Think about it!"
	exit
else
	if [ -s $DFO_JOB_DIR/$ING_FILE_NAME ]
	then
		CHECK_INGEST=\`grep "^ingestProducts" $DFO_JOB_DIR/$ING_FILE_NAME | grep $D\`
		if [ "Q\$CHECK_INGEST" != "Q" ]
		then
			echo "***WARNING: Entry found in \$DFO_JOB_DIR/$ING_FILE_NAME; don't forget to ingest these products."
			echo "            Exit for checking (E) or continue (C)? (E)"
               		read CONTI_YN
               		if [ "Q\$CONTI_YN" != "QC" ]
                	then
				echo "Exit. Date has not been removed from dfoMonitor. Hit return:"
				read input
                       		exit
                	fi
		fi
	fi

	echo ""
	echo "  Should we delete \$DFO_RAW_DIR/$D (Y/N; default: N) ?"
	read DELETE_YN

	if [ "Q\$DELETE_YN" = "QY" ]
	then 
		cd $DFO_RAW_DIR
        	rm -rf $D
        	echo "  \$DFO_RAW_DIR/$D and all data therein deleted. Hit return:"
        	read input
	else
        	echo "  \$DFO_RAW_DIR/$D not deleted."
		echo ""
	fi

# remove other entries w/o asking
	if [ $THIS_IS_PHOENIX = YES ]
	then
		rm -f $DFO_JOB_DIR/*_$D 
	fi

# update HTML table
	sed -i -e "/<\!--$D-->/d" $DFO_MON_DIR/dfoMonitor.html
	NEW_ROWSPAN=\`grep "ROWSPAN_" $DFO_MON_DIR/dfoMonitor.html | sort -u | sed "s/^.*ROWSPAN_//" | sed "s/-->//" | awk '{print \$1-1}'\`
	NEW_HEIGHT=\`echo \${NEW_ROWSPAN} | awk '{print \$1*22}'\`
	sed -i -e "s/<td ROWSPAN=.*><\!--ROWSPAN.*/<td ROWSPAN=\${NEW_ROWSPAN} width=1 bgcolor=#999><\!--ROWSPAN_\${NEW_ROWSPAN}-->/"g  $DFO_MON_DIR/dfoMonitor.html

	echo "  $D removed from dfoMonitor. Public version on $DFO_WEB_SERVER will be refreshed with the next tool refresh. Hit return:" 
	read input
	TIMESTAMP=\`date +%Y-%m-%d"T"%H:%M:%S\`
	echo "removed $D \$TIMESTAMP" >> $DFO_MON_DIR/DFO_STATUS

	BROWSER_ACTIVE=`ps -wfC $DFO_BROWSER | grep -v CMD| awk '{print $1}' | grep $USER | sort -u`
	if [ "Q\$BROWSER_ACTIVE" != "Q" ]
	then
		$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html
	fi
fi
rm -f $DFO_GUI_DIR/removeNight_${D}.esh $DFO_MON_DIR/proc_status_${D}.txt
exit
EOT
chmod u+x $DFO_GUI_DIR/removeNight_${D}.esh
}

# ===================================================================================
# 0.6 create standard HTML components
# 0.6.1 style sheets (external QC styles, plus internal one for this monitor)
# =========================================================================

includeStyles(){
cat >> $TMP_DIR/dfoMonitor.html <<EOT
<link rel="StyleSheet" href="${STYLE_URL}" type="text/css">
<style TYPE="text/css">
<!--
body.page {
        font-family: sans-serif;
	font-size: small;
        background-color: #FFFFFF;
}
table.main {
  	font-family: sans-serif;
        font-size: small;
}
tr.dfos {
	font-family: sans-serif;
	font-size: small;
	background-color: #006A9D;
	color: #FFFFFF;
	vertical-align: top;
	text-align: left ;
}
tr.qc {
	font-family: sans-serif;
	font-size: small;
	background-color: #FFFF00;
	color: #006A9D;
	vertical-align: top;
	text-align: left ;
}

a.qc_item:link {
	color: #006A9D;
}	

a.qc_item:visited {
	color: #006A9D;
}	
a.dfos_item:link {
	font-size: small;
	color: #FFFFFF;
}	

a.dfos_item:visited {
	font-size: small;
	color: #FFFFFF;
}	
-->
</style>
EOT
}

# ===================================================================================
# 0.7 call and create actions behind the buttons
# 0.7.1 callAction: call an action associated to an HTML entry
# ===================================================================================

callAction(){
if [ $THIS_IS_PHOENIX = NO ]
then
	PHOENIX_DIFF="certifyProducts and"
else
	PHOENIX_DIFF=""
fi

case $1 in
 "02" )	echo "<a title=\"create CALIB ABs for $D\" href=$DFO_GUI_DIR/launch_createAB_${D}_CALIB.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>" ; createAction createAB CALIB;;
 "03" )	echo "<a title=\"certifyProducts and moveProducts (CALIB) for $D\" href=$DFO_GUI_DIR/launch_certifyProducts_${D}_CALIB.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>" ; createAction certifyProducts CALIB;;
 "04" )	echo "<a title=\"create SCIENCE ABs for $D (CREATEAB_VCAL=${CREATEAB_VCAL})\" href=$DFO_GUI_DIR/launch_createAB_${D}_SCIENCE.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>" ; createAction createAB SCIENCE;;
 "05" )	echo "<a title=\"$PHOENIX_DIFF moveProducts (SCIENCE) for $D\" href=$DFO_GUI_DIR/launch_certifyProducts_${D}_SCIENCE.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>" ; createAction certifyProducts SCIENCE ;;
 "07" )	echo "<a title=\"finishNight for $D\" href=$DFO_GUI_DIR/launch_finishNight_${D}.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>" ; createAction finishNight ;;
 "08" )	echo "<a title=\"remove from dfoMonitor\" href=$DFO_GUI_DIR/removeNight_${D}.esh><img src=${IMG_URL}/redball.gif width=10 height=10 border=0></a>" ; createAction removeNight ;;
 "09" )	echo "<a title=\"certifyProducts and moveProducts (CALIB) for $D\" href=$DFO_GUI_DIR/launch_certifyProducts_${D}_CALIB.esh><img src=${IMG_URL}/greenball.gif width=10 height=10 border=0></a>&nbsp; &nbsp; <a title=\"certifyProducts light (CALIB) for $D\" href=$DFO_GUI_DIR/launch_certifyPLight_${D}_CALIB.esh><img src=${IMG_URL}/blueball.gif width=10 height=10 border=0></a>" ; createAction certifyProducts CALIB; createAction certifyPLight CALIB;;
esac
}

# ===================================================================================
# 0.7.2 createAction: create esh script to launch an ACTION
#     $1: action (name of tool to launch)
#     $2: mode
# ===================================================================================

createAction(){

case $1 in
 createAB )		createGuiJob launch createAB        4 ${2} ;;
 certifyProducts )	createGuiJob launch certifyProducts 4 ${2} ;;
 certifyPLight )	createGuiJob launch certifyPLight   4 ${2} ;;
 finishNight )		createGuiJob launch finishNight     3      ;;
 removeNight )		removeNight ;;
esac

}

# ===================================================================================
# 0.8 checkCompleteness: (taken from checkAvail@createReport)
#     This is about file availability in NGAS, not about completeness of headers!
#     we compare files in ngas with the header files; raw_Complete if both identical and > 0
#     raw_Incomplete if at least one file is not yet available for NGAS download
# ===================================================================================

checkCompleteness(){
DATE_MINUS1=`qcdate $D -1`
DATE_PLUS1=`qcdate  $D +1`
HOUR_MIN=`echo "12 $DFO_OFFSET" | awk '{print $1+$2}'`
HOUR_MAX=`echo "12 $DFO_OFFSET" | awk '{print $1+$2-1}'`

# hdr list may be different from 24 hour ngas query due to broken template handling; therefore we
# read 2 complete days from the database, to be on the safe side
DATE_MIN=`echo ${DATE_MINUS1}T${HOUR_MIN}:00:00`
DATE_MAX=`echo ${DATE_PLUS1}T${HOUR_MAX}:59:59.999`

cat > $TMP_DIR/dm_ngas <<EOT
SELECT DISTINCT
	convert (char(40), file_id )
FROM
	ngas..ngas_files f ,
	ngas..ngas_disks d ,
	ngas..ngas_hosts h
WHERE
	file_id between "${DFO_FILE_NAME}.${DATE_MIN}" and "${DFO_FILE_NAME}.${DATE_MAX}"
AND
	h.domain = "hq.eso.org"
AND
	d.disk_id = f.disk_id
AND
	h.host_id = d.host_id
--AND
--	d.host_id != "${FTP_HOSTID}"
GO
EOT

rm -f $TMP_DIR/dm_out_ngas
isql -Uqc -P`eval "cat ${ARCH_PWD}"` -S${ARCH_SERVER} -w 999 -i $TMP_DIR/dm_ngas | grep ${DFO_FILE_NAME} > $TMP_DIR/dm_out_ngas

FLAG=raw_Complete
if [ -d $DFO_HDR_DIR/$D ]
then
	rm -f $TMP_DIR/dm_list_hdr
	ls $DFO_HDR_DIR/$D | grep hdr | sed "s/.hdr//" > $TMP_DIR/dm_list_hdr
	if [ -s $TMP_DIR/dm_list_hdr ]
	then
		for H in `cat $TMP_DIR/dm_list_hdr`
		do
			H1=`basename $H`
			CHECK_HDR=`grep $H1 $TMP_DIR/dm_out_ngas | awk '{print $1}'`
			if [ Q${CHECK_HDR} = Q ]
			then
				FLAG=raw_Incomplete
				break
			fi
		done
	fi
fi
}

# ===================================================================================
# 1. Create top table 
# 1.1 Check for dates in $DFO_CAL_DIR/MCAl and VCAL
# ===================================================================================

UPDATE=`date -u +%Y-%m-%d" "%H:%M:%S`

TODAY=`date -u +%Y-%m-%d`
LAST_HOUR=`date -u +%H`
BOUNDARY=`echo 12 $DFO_OFFSET | awk '{print $1+$2}'`

if [ $LAST_HOUR -ge $BOUNDARY ]
then
	TODAY=$TODAY
else
	TODAY=`qcdate $TODAY -1`
fi

TODAYminus90=`qcdate $TODAY -90`

# dates in MCAL and VCAL
if [ $THIS_IS_DFOS = YES ]
then
	rm -f $TMP_DIR/mcalib_dates $TMP_DIR/vcalib_dates

# we list the N_MCAL_LIST latest $DFO_CAL_DIR directories (since MCAL is refreshed
# only after renewed createAB execution)
	if [ -s $DFO_CAL_DIR/MCAL ]
	then
		ls -d $DFO_CAL_DIR/20* | tail -${N_MCAL_LIST} | sed "s|$DFO_CAL_DIR|"'$DFO_CAL_DIR|g' | tr "/" " " | awk '{print $2}' > $TMP_DIR/mcalib_dates
	fi

	if [ -s $DFO_CAL_DIR/VCAL ]
	then
		CHECK_EXIST=`ls $DFO_CAL_DIR/VCAL | grep hdr | wc -c`
		if [ $CHECK_EXIST -gt 0 ] && [ $CHECK_EXIST -lt 122000 ]
		then
			ORIG_PWD=$PWD
			cd $DFO_CAL_DIR/VCAL
			dfits `ls | grep fits` | fitsort -d DATE | grep -v "no input data" | awk '{print $2}' | sort -u > $TMP_DIR/vcalib_dates
			cd $ORIG_PWD
		elif [ $CHECK_EXIST -ge 122000 ]
		then
# more stable for large number of files (WHu 2009-06-16)
			find $DFO_CAL_DIR/VCAL -name \*.hdr -exec dfits {} \; | fitsort -d DATE | grep -v "no input data" | awk '{print $2}' | sort -u > $TMP_DIR/vcalib_dates
		fi
	fi
fi

# ===================================================================================
# 1.2 Create top table 
# ===================================================================================

cat > $TMP_DIR/dfoMonitor.html <<EOT
<html>
<!-- created by ${TOOL_NAME} v${TOOL_VERSION} -->
<head>
EOT

includeStyles

BROWSER_REFRESH="on"
BROWSER_TAG="(every $GANGLIA_REFRESH sec)"
META_TAG="<META http-equiv=\"refresh\" content=\"${REFRESH}\"> <!--FILTER--> <!--REFRESH-->"

# check DRS_TYPE configuration
CONDOR_TAG=`grep "^DRS_TYPE" $DFO_CONFIG_DIR/OCA/config.createAB | awk '{print $2}'`
if [ Q$CONDOR_TAG = QCON ]
then
	CONDOR_TAG=on ; CONDOR_COL=$DGREEN; CONDOR_TITLE="DRS_TYPE in config.createAB is CON"
else
	CONDOR_TAG=off; CONDOR_COL=$DRED; CONDOR_TITLE="DRS_TYPE in config.createAB is not CON"
fi

# check $DFS_RELEASE variable
DFS_DEFAULT=`readlink -f ~flowmgr/dfs`
if [ Q$DFS_DEFAULT = Q ]
then
	DFS_DEFAULT=missing
else
	DFS_DEFAULT=`basename $DFS_DEFAULT`
fi

DFS_RELEASE=`readlink -f ~flowmgr/${DFS_RELEASE}`
if [ Q$DFS_RELEASE = Q ]
then
	echo "*** ERROR: no dfs installed!"
else
	DFS_RELEASE=`basename $DFS_RELEASE`
fi

if [ Q$DFS_RELEASE = Q$DFS_DEFAULT ]
then
	DFS_STD="<font color=$DGREEN><b>std</b></font>"
else
	DFS_STD="<font color=$DRED><b>non-std</b></font>"
	DFS_RELEASE="<font color=$DRED><b>missing</b></font>"
	DFS_DEFAULT="<font color=$DRED><b>missing</b></font>"
fi

# check MIDAS version
if [ $MIDAS_CHECK = YES ]
then
	MIDAS_DEFAULT=`grep " MIDVERS=" $DFS_ROOT/dfs/admin/dataflowUser.sh | awk '{print $2}' | sed "s/MIDVERS=//"`
	if [ Q$MIDAS_DEFAULT = Q$MIDVERS ]
	then
		MID_STD="<font color=$DGREEN><b>std</b></font>"
	else
		MID_STD="<font color=$DRED><b>non-std</b></font>"
	fi
fi

# DFOS, PHOENIX: check PIPE_VERSION (version of operational pipeline) because these installations are INS-specific
# OPSHUB: no such check
if [ $THIS_IS_OPSHUB = NO ]
then
# the following is from processAB; PIPE_VERSION: could be in ESOREX_PLUGIN_DIR ...
	PIPE_VERSION=$ESOREX_PLUGIN_DIR

	if [ Q$PIPE_VERSION = Q ]
	then
# ... or in esorex.rc ...
		PIPE_VERSION=`grep "^esorex.caller.recipe-dir" $HOME/.esorex/esorex.rc | sed "s/esorex.caller.recipe-dir=//"`
	fi

# ... or in ${PIPE_HOME}
	if [ Q$PIPE_VERSION = Q ]
	then
       		PIPE_NAME=`grep "^PIPE_NAME"    $DFO_CONFIG_DIR/config.processAB | awk '{print $2}'`
        	if [ -s ${PIPE_HOME}/$PIPE_NAME ]
        	then
                	PIPE_VERSION=`ls -latr ${PIPE_HOME}/$PIPE_NAME | awk '{print $11}'`
        	else
               		PIPE_VERSION="unknown"
        	fi
	fi

# exclude detmon
	PIPE_VERSION=`echo $PIPE_VERSION | tr ":" "\012" | grep -v detmon`
	PIPE_VERSION=`basename $PIPE_VERSION`
elif [ $THIS_IS_OPSHUB = YES ]
then
	PIPE_VERSION=`grep "^SUPPORTED" $DFO_CONFIG_DIR/../config.distillery | grep "$DFO_INSTRUMENT" | awk '{print $8}'`
fi

if [ $THIS_IS_PHOENIX = YES ]
then
	MONITOR_TAG="DFO monitor"
	INSTR_TAG="instrument: PHOENIX/$DFO_INSTRUMENT"
	ROWSPAN=4
elif [ $THIS_IS_DFOS = YES ]
then
	MONITOR_TAG="DFO monitor"
	INSTR_TAG="instrument: $DFO_INSTRUMENT"
	ROWSPAN=4
elif [ $THIS_IS_OPSHUB = YES ]
then
	MONITOR_TAG="OPSHUB project monitor"
	INSTR_TAG="instrument: $DFO_INSTRUMENT"
	ROWSPAN=2
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
<title>${MONITOR_TAG} ($INSTR_TAG)</title>
$META_TAG
<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=300,height=250,scrollbars=yes,menubar=no");
  }
function openWin2(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=600,height=600,scrollbars=yes,menubar=no");
  }
//-->
</script>

<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>


</head>

<body class=page>

<!-- first main table: header -->
<table BORDER=0 WIDTH="$TABLE_WIDTH" class=main>
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td width=5% ROWSPAN=$ROWSPAN>
<!--PLACEHOLDER-->
EOT

# ===================================================================================
# 1.3 Include the vnavbar
# ===================================================================================

if [ -s $DFO_MON_DIR/vnavbar_dfoMonitor.html ]
then
        cat $DFO_MON_DIR/vnavbar_dfoMonitor.html >> $TMP_DIR/dfoMonitor.html
elif [ $THIS_IS_OPSHUB = NO ]
then
        echo "***WARNING: no \$DFO_MON_DIR/vnavbar_dfoMonitor.html found. Create it using \"navigation bar: refresh\". "
elif [ $THIS_IS_OPSHUB = YES ]
then
# OPSHUB LOGO
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <table cellpadding="0" cellspacing="0" border=0>
        <tr align="left" valign="bottom">
         <td>

           <table style="border-collapse: collapse;" border="1" bordercolor="#fff" cellpadding="0" cellspacing="0">
             <tr>
               <td bgcolor="${TITLE_COLOR}" height="137" width="10" align="left" valign="bottom"><b><font color="#fff" size="6">
                 <a title="this is an OPSHUB installation">OPS<br>HUB</a></font></b>
               </td>
             </tr>
           </table>

         </td>
         <td valign="bottom" nowrap><b><font color=${REPORT_COLOR} size=+2>${TITLE}</font></b></td>
        </tr>
      </table>
EOT
fi

# ===================================================================================
# 1.4 get disk status
# ===================================================================================

if [ $QUIET = NO ] 
then
	if [ $DISK_SPACE = YES ]
	then
		echo "  -checking disk space ..."
	else
		echo "  -checking disk space (except for \$DFO_SCI_DIR) ..."
	fi
fi

if [ $THIS_IS_OPSHUB = YES ]
then
# gluster: ohgluster1:/oh1
	DISK_USED=`df $DATA_DISK | grep -v File | awk '{printf"%5.1f GB\n",$3/1024/1024}'`
	DISK_FRAC=`df $DATA_DISK | grep -v File | awk '{printf"%i\n",$5}'`
	DISK_SIZE=`df $DATA_DISK | grep -v File | awk '{printf"%i \n",($3+$4)/1024/1024}'`
else
	if [ $DATA_QUOTA = 100 ]
	then
		DISK_USED=`df $DATA_DISK | grep -v File | grep -v : | awk '{printf"%5.1f GB\n",$3/1024/1024}'`
		DISK_FRAC=`df $DATA_DISK | grep -v File | grep -v : | awk '{printf"%i\n",$5}'`
		DISK_SIZE=`df $DATA_DISK | grep -v File | grep -v : | awk '{printf"%i \n",($3+$4)/1024/1024}'`
	else
		FILE_SYSTEM=`df $DATA_DISK | grep -v File | grep -v : | awk '{print $1}'`
		DISK_USED=`quota -w | grep $FILE_SYSTEM | awk '{printf"%i\n",$2/1024/1024}'`
		DISK_SIZE=`quota -w | grep $FILE_SYSTEM | awk '{printf"%i \n",$4/1024/1024}'`
		DISK_FRAC=`echo $DISK_USED $DISK_SIZE | awk '{printf"%i \n",($1/$2)*100}'`
		DISK_USED=`echo ${DISK_USED} GB`
	fi
fi

# quotas: retrieve with 'quota -ws' if needed; limit=$4 is the quota, blocks=$2 is the currently used space

# send WARNING if DISK_FRAC larger than 80%
rm -f $TMP_DIR/warning

RED_SCORE="<a title=\"your data disk is ${DISK_FRAC}% full!\"><img src=${IMG_URL}/sq0.png width=10 height=10></a>"
GREEN_SCORE="<a title=\"data disk: ${DISK_FRAC}%\"><img src=${IMG_URL}/sq2.png width=10 height=10></a>"

if [ $DISK_FRAC -gt 80 ]
then
	DISK_SCORE=$RED_SCORE
	echo "***WARNING: Your data disk is ${DISK_FRAC}% full." 
	echo "***WARNING: Your data disk is ${DISK_FRAC}% full." > $TMP_DIR/warning
	mail -s "dfoMonitor@`whoami`@$HOSTNAME: Your data disk is ${DISK_FRAC}% full" $OP_ADDRESS <$TMP_DIR/warning
else
	DISK_SCORE=$GREEN_SCORE
fi
	
DISK8=`echo $DISK_FRAC | awk '{printf"%1i\n", $1/100*8}'`
DISK_SYMBOL=`echo pie${DISK8}.gif`

# monitor certain disk areas
if [ $THIS_IS_OPSHUB = NO ]
then
	RAW_DISK=`du -s $DFO_RAW_DIR | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024}'`
	RAW_FRAC=`echo $RAW_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	RAW_FRAC1=`echo $DISK_SCALE $RAW_FRAC | awk '{print $1-$2}'`
	RAW_FRAC2=`echo $RAW_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
		
	CAL_DISK=`du -s $DFO_CAL_DIR | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024 }'`
	CAL_FRAC=`echo $CAL_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	CAL_FRAC1=`echo $DISK_SCALE $CAL_FRAC | awk '{print $1-$2}'`
	CAL_FRAC2=`echo $CAL_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
	
	if [ $DISK_SPACE = NO ]
	then
		SCI_DISK=`grep "^DFO_SCI_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`
		if [ Q$SCI_DISK = Q ]
		then
			SCI_DISK=`du -s $DFO_SCI_DIR | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024 }'`
			UPDATE=`date -u +%Y-%m-%d"T"%H:%M:%S`
			echo "DFO_SCI_DIR $SCI_DISK $UPDATE" >> $DFO_MON_DIR/DFO_STATUS
		fi
	else
		SCI_DISK=`du -s $DFO_SCI_DIR | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024 }'`
	fi
	SCI_FRAC=`echo $SCI_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	SCI_FRAC1=`echo $DISK_SCALE $SCI_FRAC | awk '{print $1-$2}'`
	SCI_FRAC2=`echo $SCI_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
		
	DFS_DISK=`du -s $DFS_PRODUCT | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024 }'`
	DFS_FRAC=`echo $DFS_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	DFS_FRAC1=`echo $DISK_SCALE $DFS_FRAC | awk '{print $1-$2}'`
	DFS_FRAC2=`echo $DFS_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 

	LST_DISK=`du -s $DFO_LST_DIR | awk '{sum +=$1} END {printf"%i\n", sum/1024/1024 }'`
	LST_FRAC=`echo $LST_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	LST_FRAC1=`echo $DISK_SCALE $LST_FRAC | awk '{print $1-$2}'`
	LST_FRAC2=`echo $LST_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
	
# read from DFO_STATUS
	PLT_DISK=`grep "^DFO_PLT_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`
	LOG_DISK=`grep "^DFO_LOG_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`

	case $THIS_IS_PHOENIX in
 	"YES" ) HDR_DISK=0 ;;
 	"NO"  ) HDR_DISK=`grep "^DFO_HDR_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`;;
	esac

# check if existing, otherwise refresh
	if [ "Q$PLT_DISK" = "Q" ] || [ "Q$LOG_DISK" = "Q" ] || [ "Q$HDR_DISK" = "Q" ]
	then
		$DFO_GUI_DIR/refreshXDM.esh
		PLT_DISK=`grep "^DFO_PLT_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`
		LOG_DISK=`grep "^DFO_LOG_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`
		HDR_DISK=`grep "^DFO_HDR_DIR" $DFO_MON_DIR/DFO_STATUS | tail -1 | awk '{printf"%i\n", $2 }'`
	fi

	PLT_FRAC=`echo $PLT_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	PLT_FRAC1=`echo $DISK_SCALE $PLT_FRAC | awk '{print $1-$2}'`
	PLT_FRAC2=`echo $PLT_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
		
	LOG_FRAC=`echo $LOG_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	LOG_FRAC1=`echo $DISK_SCALE $LOG_FRAC | awk '{print $1-$2}'`
	LOG_FRAC2=`echo $LOG_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
		
	HDR_FRAC=`echo $HDR_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	HDR_FRAC1=`echo $DISK_SCALE $HDR_FRAC | awk '{print $1-$2}'`
	HDR_FRAC2=`echo $HDR_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'`
	
# SUM
	SUM_DISK=`echo $RAW_DISK $CAL_DISK $SCI_DISK $DFS_DISK $HDR_DISK $LST_DISK $PLT_DISK $LOG_DISK | awk '{print $1+$2+$3+$4+$5+$6+$7+$8}'`
	SUM_FRAC=`echo $SUM_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	SUM_FRAC1=`echo $DISK_SCALE $SUM_FRAC | awk '{print $1-$2}'`
	SUM_FRAC2=`echo $SUM_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
	
# OTH
	OTH_DISK=`echo $SUM_DISK $DISK_USED | awk '{printf"%i\n", $2-$1}'`
	OTH_FRAC=`echo $OTH_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	OTH_FRAC1=`echo $DISK_SCALE $OTH_FRAC | awk '{print $1-$2}'`
	OTH_FRAC2=`echo $OTH_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
	
# REST
	REST_DISK=`echo $DISK_SIZE $DISK_USED | awk '{printf"%i\n", $1-$2}'`
	REST_FRAC=`echo $REST_DISK ${DISK_SIZE} ${DISK_SCALE} | awk '{if ($1/$2*$3 > 1) { printf"%i\n", $1/$2*$3 } else {print 1}}'`
	REST_FRAC1=`echo $DISK_SCALE $REST_FRAC | awk '{print $1-$2}'`
	REST_FRAC2=`echo $REST_FRAC ${DISK_SCALE} | awk '{printf"%i\n", $1/$2*100}'` 
fi

# ================================================================================
# 1.5 ngas access, checks for autoDaily/HC monitor/calChecker
# ===================================================================================

# skip for PHOENIX and OPSHUB
if [ $THIS_IS_DFOS = NO ]
then
	NGAS_TEST=NO
	NGAS_ACCESS=0 
fi

if [ $NGAS_TEST = YES ]
then
	ngasAccess &

	if [ -s $OTMP_DIR/ngasAccess_report ]
	then
		NGAS_ACCESS=`cat $OTMP_DIR/ngasAccess_report`
	else
		NGAS_ACCESS=0
	fi
else
	NGAS_ACCESS=0
	if [ $QUIET = NO ]
	then
		echo "  -ngas test skipped."
	fi
fi

if [ $NGAS_TEST = NO ]
then
	NGAS_ACCESS="<td bgcolor=#CCCCCC></td><td bgcolor=#CCCCCC><font size=1><a title=\"NGAS access not tested\">&nbsp;</a></font></td>"
elif [ $NGAS_ACCESS != 0 ] && [ $NGAS_TEST = YES ]
then
	NGAS_ACCESS="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"NGAS access: problem found\">ngas: error</a></font></td>"
else
	NGAS_ACCESS="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"NGAS access: last test access ok\">ngas</a></font></td>"
fi

if [ $SCP_YN = YES ]
then
	echo "$NGAS_ACCESS" | sed "s/999/CCCCCC/" > $DFO_MON_DIR/ngasWatcher.html
	scp -o BatchMode=yes $DFO_MON_DIR/ngasWatcher.html ${DFO_WEB_SERVER}:${WEB_DIR}/ 1>/dev/null
fi

# autoDaily support
if [ $THIS_IS_DFOS = NO ]
then
	AUTOD_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><i>not operational</font></td>"
else
	AUTOD_SUPPORT1=`crontab -l | grep -v "^#" | grep autoDaily | grep -v dfosCron | awk '{print $1,$2,$3,$4,$5}'`
	if [ "Q$AUTOD_SUPPORT1" = "Q" ]
	then
		AUTOD_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>autoDaily disabled! Should be called once per hour</font></td>"
	else
		AUTOD_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"enabled as cronjob ($AUTOD_SUPPORT1)\">enabled as cronjob</a></font></td>"
	fi
fi

# v4.1.2: checking for cleanupRawdisp
if [ $THIS_IS_PHOENIX = YES ]
then
	AUTOD_SUPPORT2="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><i>not operational</font></td>"
else
	AUTOD_SUPPORT2=`crontab -l 2>/dev/null | grep -v "^#" | grep cleanupRawdisp | awk '{print $1,$2,$3,$4,$5}'`
	if [ "Q$AUTOD_SUPPORT2" = "Q" ]
	then
		AUTOD_SUPPORT2="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>cleanupRawdisp disabled! Should be called once per day</font></td>"
	else
		AUTOD_SUPPORT2="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"cleanupRawdisp enabled as cronjob ($AUTOD_SUPPORT2)\">cleanupRD enabled</a></font></td>"
	fi

	AUTOD_SUPPORT3=`crontab -l 2>/dev/null | grep -v "^#" | grep "dfosCron -t autoDaily" | awk '{print $1,$2,$3,$4,$5}' | grep "\* \* \* \* \*"`
	if [ "Q$AUTOD_SUPPORT3" = "Q" ]
	then
		AUTOD_SUPPORT3="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>'dfosCron -t autoDaily' disabled! Must be called once per minute</font></td>"
	else
		AUTOD_SUPPORT3="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"'dfosCron -t autoDaily' monitoring as cronjob for forced refresh ($AUTOD_SUPPORT3)\">dfosCron monitoring enabled</a></font></td>"
	fi	
fi	

CURRENT=`date -u +%Y-%m-%dT%H:%m:%S`
CURRENT=`date -u +%s -d $CURRENT`

# LAST_AB
if [ $THIS_IS_PHOENIX = YES ]
then
	LAST_AB="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${DONE_COLOR}><font size=1><i>Last created AB not checked</font></td>"
elif [ -s $DFO_MON_DIR/LAST_AB ]
then
	LAST_AB=`grep -v "^#" $DFO_MON_DIR/LAST_AB | grep created | awk '{print $1}'`
	LAST_TIMESTAMP=`echo $LAST_AB | sed "s/${DFO_FILE_NAME}.//" | sed "s/_.*//" | sed "s/.ab//"`
	LAST=`date -u +%s -d $LAST_TIMESTAMP`
	DIFF=`echo $CURRENT $LAST | awk '{printf"%5.1f\n",($1-$2)/3600}'`

	AGE_ALERT=`echo $DIFF $MAX_AGE | awk '{if ($1 > $2) {print "NOK"} else {print "OK"}}'`

	case $AGE_ALERT in
	 "OK"  ) LAST_AB="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${DONE_COLOR}><font size=1><a title=\"Last AB in \$DFO_AB_DIR created by autoDaily\">Last created AB: $LAST_AB</a><a title=\"maximum age: $MAX_AGE h\"> (age: $DIFF h)</a></font></td>" ;;
	 "NOK" ) LAST_AB="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Last AB in |$DFO_AB_DIR created by autoDaily\">Last created AB: $LAST_AB</a><a title=\"maximum age: $MAX_AGE h\"> (age: $DIFF h)</a></font></td>" ;;
	esac
else
	LAST_AB="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${NGAS_NOK_COLOR}><font size=1>Last created AB: unknown</font></td>"
fi

# LAST_PROCESSED
CHECK_EXIST=`ls $DFO_AB_DIR | grep "\.ab" | wc -l`
CHECK_EXIST1=`grep "processed" $DFO_MON_DIR/LAST_AB 2>/dev/null | grep -v "^#" | awk '{print $1}' | grep -v none`

if [ $CHECK_EXIST != 0 ]
then
	LAST_PROCESSED=`grep "^PROCESS_STATUS" $DFO_AB_DIR/*.ab  | egrep "FAILED|SUCCESS" | tail -1 | awk '{print $1}' | sed "s/:PROCESS_STATUS//"`
	if [ "Q$LAST_PROCESSED" != Q ]
	then 
		LAST_PROCESSED=`basename $LAST_PROCESSED`
		LAST_TIME=`echo $LAST_PROCESSED | sed "s/${DFO_FILE_NAME}.//" | sed "s/_.*//" | sed "s/.ab//" | sed "s/-//"g | sed "s/://g" | sed "s/T//" | sed "s/^20//"`
	else
		LAST_PROCESSED=$CHECK_EXIST1
		LAST_TIME=`grep "$CHECK_EXIST1" $DFO_MON_DIR/LAST_AB 2>/dev/null | grep "processed" | grep -v "^#" | awk '{print $2}' | sort -u`
	fi
elif [ Q$CHECK_EXIST1 != Q ]
then
	LAST_PROCESSED=$CHECK_EXIST1
	LAST_TIME=`grep "$CHECK_EXIST1" $DFO_MON_DIR/LAST_AB | grep "processed" | grep -v "^#" | awk '{print $2}' | sort -u`
else
	LAST_PROCESSED=none
	LAST_TIME=0
fi

if [ "$LAST_PROCESSED" != "none" ]
then
	LAST_PROCESSED1="<td bgcolor=#BBBBBB></td><td colspan=7 bgcolor=#BBBBBB><font size=1><a title=\"Last processed AB (successful or failed) in \$DFO_AB_DIR\">Last processed AB: $LAST_PROCESSED</a></font></td>"
else
	LAST_PROCESSED1="<td bgcolor=#BBBBBB></td><td colspan=7 bgcolor=#BBBBBB><font size=1><a title=\"Currently no processed AB in \$DFO_AB_DIR\">Last processed AB: $LAST_PROCESSED</a></font></td>"
fi

if [ $THIS_IS_PHOENIX = YES ]
then
	LAST_EXEC=""

# edit $DFO_MON_DIR/LAST_AB (same in createAB for last created)
elif [ -s $DFO_MON_DIR/LAST_AB ]
then
# condensed timestamp (date string shortened for easy comparison to earlier entry)
	PREVIOUS_TIME=`grep processed $DFO_MON_DIR/LAST_AB | grep -v ^# | tail -1 | awk '{print $2}'`
	if [ Q$PREVIOUS_TIME != Q ]
	then
		LARGER=`echo $LAST_TIME $PREVIOUS_TIME | awk '{ if ($1 > $2) print "YES"}'`
		if [ ! -s $DFO_MON_DIR/LAST_AB.locked ]
		then
			cp $DFO_MON_DIR/LAST_AB $DFO_MON_DIR/LAST_AB.locked
			if [ Q$LARGER = QYES ] && [ $LAST_PROCESSED != none ]
			then
				sed -i -e "/processed/s/^.*processed/${LAST_PROCESSED} ${LAST_TIME} processed/" $DFO_MON_DIR/LAST_AB
			fi
			rm $DFO_MON_DIR/LAST_AB.locked
# locked? yes (poor man's solution: wait a second and then try again; good enough)
		else
			echo "*** INFO: $DFO_MON_DIR/LAST_AB found locked. Wait for 1 sec and re-try..."
			sleep 1
			if [ ! -s $DFO_MON_DIR/LAST_AB.locked ]
			then
				echo "*** INFO: $DFO_MON_DIR/LAST_AB found unlocked now."
				cp $DFO_MON_DIR/LAST_AB $DFO_MON_DIR/LAST_AB.locked
				if [ Q$LARGER = QYES ]
				then
					sed -i -e "/processed/s/^.*processed/${LAST_PROCESSED} ${LAST_TIME} processed/" $DFO_MON_DIR/LAST_AB
				fi
				rm $DFO_MON_DIR/LAST_AB.locked
			else
# still locked? give up
				echo "*** WARNING: $DFO_MON_DIR/LAST_AB still locked. No update done."
			fi
		fi
	else
		cat >> $DFO_MON_DIR/LAST_AB <<EOT
${LAST_PROCESSED} ${LAST_TIME} processed
EOT
	fi
fi

# Last autoDaily execution
if [ $THIS_IS_DFOS = YES ]
then
	CURRENT_DATE=`date -u +%Y-%m-%dT%H:%M:%S`
	CURRENT=`date -u +%s -d $CURRENT_DATE`

	LAST_AUTOD=`ls $DFO_MON_DIR/AUTO_DAILY/AD* | tail -1`
	LAST_AUTOD=`grep "autoDaily finished" $LAST_AUTOD | tail -1 | awk '{print $1"T"$2}'`
	LAST=`date -u +%s -d $LAST_AUTOD`

	DIFF=`echo $CURRENT $LAST | awk '{printf"%5.1f\n",($1-$2)/3600}'`
	AGE_ALERT=`echo $DIFF $MAX_AUTOD | awk '{if ($1 > $2) {print "NOK"} else {print "OK"}}'`

	case $AGE_ALERT in
	 "OK"  ) LAST_EXEC="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${DONE_COLOR}><font size=1><a title=\"Last autoDaily execution\">Last autoDaily: $LAST_AUTOD</a><a title=\"maximum age: $MAX_AUTOD h\"> (age: $DIFF h)</a></font></td>" ;;
	 "NOK" ) LAST_EXEC="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td colspan=7 bgcolor=${NGAS_NOK_COLOR}><font size=1><a title=\"Last autoDaily execution\">Last autoDaily: $LAST_AUTOD</a><a title=\"maximum age: $MAX_AUTOD h\"> (age: $DIFF h)</a></font></td>" ;;
	esac

	autoDWatcher $LAST_AUTOD

	if [ $SCP_YN = YES ]
	then
		scp -o BatchMode=yes $DFO_MON_DIR/autoDWatcher.html ${DFO_WEB_SERVER}:${WEB_DIR}/ 1>/dev/null
	fi
fi

# HC monitor support
# phoenix: simple
if [ $THIS_IS_PHOENIX = YES ]
then
	cat > $TMP_DIR/HCMON_SUPPORT1 <<EOT
<td bgcolor=#999 width=10><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td>
<td bgcolor=${DONE_COLOR}><font size=1><i>not operational</i></font></td><!--HCMONITOR-->
EOT
	cat $TMP_DIR/HCMON_SUPPORT1 > $TMP_DIR/HCMON_SUPPORT2
	cat $TMP_DIR/HCMON_SUPPORT1 > $TMP_DIR/HCMON_SUPPORT3

# normal case
else
	HCMON_SUPPORT1=`crontab -l 2>/dev/null | grep -v "^#" | grep JOBS_TREND | awk '{print $1,$2,$3,$4,$5}'`
	if [ "Q$HCMON_SUPPORT1" = "Q" ]
	then
		cat > $TMP_DIR/HCMON_SUPPORT1 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td>
<td bgcolor=${NGAS_NOK_COLOR}><font size=1>JOBS_TREND: should be called at least once per day</font></td><!--HCMONITOR-->
EOT
	else
		cat > $TMP_DIR/HCMON_SUPPORT1 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td>
<td bgcolor=${DONE_COLOR}><font size=1>
<a href=$DFO_JOB_DIR/JOBS_TREND title="enabled as cronjob ($HCMON_SUPPORT1)"> <!--FILTER-->
JOBS_TREND
</a> <!--FILTER-->
<a title="enabled as cronjob ($HCMON_SUPPORT1) ">enabled as cronjob</a></font></td><!--HCMONITOR-->
EOT
	fi

	if [ ! -s $DFO_JOB_DIR/JOBS_HEALTH ]
	then
		cat > $TMP_DIR/HCMON_SUPPORT2 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>JOBS_HEALTH: should be defined to update all HC plots daily</font></td>
EOT
	else
		cat > $TMP_DIR/HCMON_SUPPORT2 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td>
<td bgcolor=${DONE_COLOR}><font size=1>
<a href=$DFO_JOB_DIR/JOBS_HEALTH title="existing"> <!--FILTER-->
JOBS_HEALTH
</a>
existing</font></td>
EOT
	fi

	HCMON_SUPPORT3=`crontab -l 2>/dev/null | grep -v "^#" | grep JOBS_NAVBAR | awk '{print $1,$2,$3,$4,$5}'`

	if [ "Q$HCMON_SUPPORT3" = "Q" ]
	then
		cat > $TMP_DIR/HCMON_SUPPORT3 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td>
<td bgcolor=${NGAS_NOK_COLOR}><font size=1>JOBS_NAVBAR: should be called once or twice per month</font></td>
EOT
	else
		cat > $TMP_DIR/HCMON_SUPPORT3 <<EOT
<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td>
<td bgcolor=${DONE_COLOR}><font size=1>
<a href=$DFO_JOB_DIR/JOBS_NAVBAR title="enabled as cronjob ($HCMON_SUPPORT3)"> <!--FILTER-->
JOBS_NAVBAR
</a> <!--FILTER-->
<a title="enabled as cronjob ($HCMON_SUPPORT3) ">enabled as cronjob</a></font></td>
EOT
	fi
fi

# calChecker support (SUPPORT1: incremental mode; SUPPORT2: FULL mode)
if [ $THIS_IS_DFOS = YES ]
then
	CALCHECK_SUPPORT1=`crontab -l | grep -v "^#" | grep calChecker | grep -v "\-F" | awk '{print $1,$2,$3,$4,$5}'`
	CALCHECK_SUPPORT2=`crontab -l | grep -v "^#" | grep calChecker | grep "\-F"    | awk '{print $1,$2,$3,$4,$5}'`
	if [ "Q$CALCHECK_SUPPORT1" = "Q" ]
	then
	        CALCHECK_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>increm.: disabled! Should be called twice per hour</font></td>"
	else
		CALCHECK_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"increm.: enabled as cronjob ($CALCHECK_SUPPORT1)\">increm.: enabled as cronjob</a></font></td>"
	fi

	if [ "Q$CALCHECK_SUPPORT2" = "Q" ]
	then
		CALCHECK_SUPPORT2="<td bgcolor=#999><img src=${IMG_URL}/nok.gif width=10 height=10 border=0></td><td bgcolor=${NGAS_NOK_COLOR}><font size=1>FULL: disabled! Should be called once per day</font></td>"
	else
		CALCHECK_SUPPORT2="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><a title=\"FULL: enabled as cronjob ($CALCHECK_SUPPORT2)\">FULL: enabled as cronjob</a></font></td>"
	fi
else
	CALCHECK_SUPPORT1="<td bgcolor=#999><img src=${IMG_URL}/ok.gif width=10 height=10 border=0></td><td bgcolor=${DONE_COLOR}><font size=1><i>not operational</font></td>"
	CALCHECK_SUPPORT2=$CALCHECK_SUPPORT1
fi

# ================================================================================
# 1.6 disk space monitor XDM
# disk_space_monitor.html: created as separate file for export
# ===================================================================================

case $DATA_QUOTA in
 "100" ) QUOTA_TEXT="" ;;
 *     ) QUOTA_TEXT=", ${DATA_QUOTA}% quota" ;;
esac

cat >> $TMP_DIR/disk_space_monitor.html <<EOT
	<tr align=left bgcolor=#FFFF00>
          <td align=center>$DISK_SCORE</td>
          <td COLSPAN=2><font size=1 color=${TITLE_COLOR}><b>${DFO_MACHINE}:$DATA_DISK</b><br>(total: ${DISK_SIZE}GB${QUOTA_TEXT})
          </td>
        </tr>
EOT

writeXDM ${RAW_DISK} ${RAW_FRAC} ${RAW_FRAC1} ${RAW_FRAC2} "\$DFO_RAW_DIR: $DFO_RAW_DIR" RAW
writeXDM ${CAL_DISK} ${CAL_FRAC} ${CAL_FRAC1} ${CAL_FRAC2} "\$DFO_CAL_DIR: $DFO_CAL_DIR" CAL
writeXDM ${SCI_DISK} ${SCI_FRAC} ${SCI_FRAC1} ${SCI_FRAC2} "\$DFO_SCI_DIR: $DFO_SCI_DIR" SCI
writeXDM ${DFS_DISK} ${DFS_FRAC} ${DFS_FRAC1} ${DFS_FRAC2} "\$DFS_PRODUCT: $DFS_PRODUCT" DFS

writeXDM ${LST_DISK} ${LST_FRAC} ${LST_FRAC1} ${LST_FRAC2} "\$DFO_LST_DIR: $DFO_LST_DIR" LST
writeXDM ${HDR_DISK} ${HDR_FRAC} ${HDR_FRAC1} ${HDR_FRAC2} "\$DFO_HDR_DIR: $DFO_HDR_DIR" \*HDR
writeXDM ${PLT_DISK} ${PLT_FRAC} ${PLT_FRAC1} ${PLT_FRAC2} "\$DFO_PLT_DIR: $DFO_PLT_DIR" \*PLT
writeXDM ${LOG_DISK} ${LOG_FRAC} ${LOG_FRAC1} ${LOG_FRAC2} "\$DFO_LOG_DIR: $DFO_LOG_DIR" \*LOG

cat >> $TMP_DIR/disk_space_monitor.html <<EOT
        <tr align=left bgcolor=#CCCCCC>
          <td COLSPAN=3 align=center><img src=${IMG_URL}/sqg.png width=1 height=1>
        </tr>
        <tr align=left bgcolor=#FFFF00>
          <td><font size=1 color=${TITLE_COLOR}><b><a title="sum of all above">SUM:</a></b></font></td>
          <td align=right><font size=1 color=${TITLE_COLOR}><b><a title="${SUM_DISK} GB">${SUM_DISK}</a></b></font></td>
          <td align=right valign=middle><img src=${IMG_URL}/sq3.png width=${SUM_FRAC} height=6 title="${SUM_FRAC2}%"><img src=${IMG_URL}/sqg.png width=${SUM_FRAC1} height=6></td>
        </tr>
        <tr align=left bgcolor=#FFFF00>
          <td><font size=1 color=${TITLE_COLOR}><b><a title="all other data on $DATA_DISK">OTH:</a></b></font></td>
          <td align=right><font size=1 color=${TITLE_COLOR}><b><a title="${OTH_DISK} GB">${OTH_DISK}</a></b></font></td>
          <td align=right valign=middle><img src=${IMG_URL}/sq3.png width=${OTH_FRAC} height=6 title="${OTH_FRAC2}%"><img src=${IMG_URL}/sqg.png width=${OTH_FRAC1} height=6></td>
        </tr>
        <tr align=left bgcolor=#FFFF00>
          <td><font size=1 color=${TITLE_COLOR}><b><a title="remaining on $DATA_DISK">FREE:</a></b></font></td>
          <td align=right><font size=1 color=${TITLE_COLOR}><b><a title="${REST_DISK} GB">${REST_DISK}</a></b></font></td>
          <td align=right valign=middle><img src=${IMG_URL}/sq3.png width=${REST_FRAC} height=6 title="${REST_FRAC2}%"><img src=${IMG_URL}/sqg.png width=${REST_FRAC1} height=6></td>
        </tr>
EOT

# ================================================================================
# 1.7 put info together
# 1.7.1 ROW 1
# ===================================================================================

if [ $THIS_IS_PHOENIX = NO ]
then
	case $PHOENIX_ENABLED in
	 "YES" ) COLSP=1 ;;
 	 "NO"  ) COLSP=2 ;;
	esac
fi

if [ $THIS_IS_OPSHUB = YES ]
then
	MONITOR_EXPLAIN="<i><font color=#666>This is the project monitor, listing all open (active) OPSHUB PROJECTs for this instrument.</font></i><p>"
else
	MONITOR_EXPLAIN=""
fi
	
cat >> $TMP_DIR/dfoMonitor.html <<EOT
    </td>

<!-- ROW1: title, info, forms -->
    <td width=90% COLSPAN=$COLSP>
      <b><font size=+2 color=$TITLE_COLOR> ${MONITOR_TAG} ($INSTR_TAG) </font></b><br>
      <font size=2>${MONITOR_EXPLAIN}
      last update: $UPDATE (UT); machine: <b>$HOSTNAME</b>; account: <b>`whoami`</b>;
      browser_refresh: <b><font color=$DGREEN>${BROWSER_REFRESH}</font></b> ${BROWSER_TAG} <!--BROWSER_TAG-->
      <br> condor: <a href=${CONDOR_WIKI_URL} title="what's this?" target=_blank><font size=1>[?]</font></a> 
                   <b><a title="$CONDOR_TITLE"><font color=$CONDOR_COL>${CONDOR_TAG}</font></a></b>
      &nbsp; dfs: ${DFS_STD} (<a title="set as \${DFS_RELEASE}">used: <b>$DFS_RELEASE</b></a>, <a title="default defined in $DFS_ROOT/dfs">default: <b>$DFS_DEFAULT</b>)</a>
EOT

if [ $MIDAS_CHECK = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT      
      &nbsp; MIDAS: ${MID_STD} (<a title="set as \${MIDVERS}">used: <b>$MIDVERS</b></a>, <a title="default defined in $DFS_ROOT/dfs/admin/dataflowUser.sh">default: <b>$MIDAS_DEFAULT</b>)</a>
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; pipeline: <b><font color=$DGREEN>$PIPE_VERSION</font></b>
      &nbsp; <font color=#999><a title="This is a DFOS installation" style="background-color:#CCCCFF;"><b>DFOS</b></a></font> certif_mode: <b>$CERTIF_MODE </b>
      <!--EXTERNAL-->
    </td>
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; pipeline: <b><font color=$DGREEN>$PIPE_VERSION</font></b>
      &nbsp; <font color=#999><a title="This is a PHOENIX installation" style="background-color:#CCCCFF;">This is a <b>PHOENIX</b> installation.</a></font>
      <!--EXTERNAL-->
    </td>
EOT
elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; pipeline: <b><font color=$DGREEN>$PIPE_VERSION</font></b>
      &nbsp; <font color=#999><a title="This is an OPSHUB installation" style="background-color:#CCCCFF;">This is an <b>OPSHUB</b> installation.</a></font>
      <!--EXTERNAL-->
    </td>
EOT
fi

# check for new PHOENIX job
if [ $PHOENIX_ENABLED = YES ] && [ $THIS_IS_DFOS = YES ]
then
	rm -f $TMP_DIR/df_list_phoenix
	grep "^phoenix_Ready " $DFO_MON_DIR/DFO_STATUS | awk '{print $2}' > $TMP_DIR/df_list_phoenix

	if [ -s $TMP_DIR/df_list_phoenix ]
	then
		PHOENIX_COL=white
		PHOENIX_LOGO=`eval "echo \"$PHOENIX_LOGO\""`
		echo "    <td bgcolor=$TITLE_COLOR><a title=\"There are phoenix jobs for $DFO_INSTRUMENT on ${PHOENIX_ACCOUNT}!\"><!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
		echo "${PHOENIX_LOGO}</a>        <!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
		cat $TMP_DIR/df_list_phoenix | sed "s/^.*/<font size=2 color=white>&<\/font>      <!--FILTER-->/" >> $TMP_DIR/dfoMonitor.html
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
	<br>&nbsp;<a href="$DFO_GUI_DIR/done_phoenix.esh" title="confirm finishing phoenix jobs"><img src="${DFOS_URL}/images/done.png" border="0"></a> <!--FILTER-->
EOT
		mv $TMP_DIR/df_list_phoenix $OTMP_DIR
	else
		PHOENIX_COL=$TITLE_COLOR
		PHOENIX_LOGO=`eval "echo \"$PHOENIX_LOGO\""`
		echo "    <td><a title=\"there are currently no phoenix jobs\"> <!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
		echo "${PHOENIX_LOGO}</a>        <!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
		echo "<font size=2 color=$TITLE_COLOR>none</font> <!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
	fi
	echo "    </td>        <!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
elif [ $THIS_IS_PHOENIX = YES ]
then
	echo "    <td>&nbsp;</td><!--FILTER-->" >> $TMP_DIR/dfoMonitor.html
fi

# ===================================================================================
# 1.7.2 Table with N_AB monitor and disk space monitor XDM
# ===================================================================================
# AB_NUMBER_ALL (same as in getStatusAB)
# monitoring of total number of ABs; if too big, getStatusAB is too slow and autoDaily becomes compromised
AB_NUMBER_ALL=`ls $DFO_AB_DIR/*.ab 2>/dev/null | wc -w`
AB_NUMBER_SCORE=`echo $AB_NUMBER_ALL $AB_NUMBER_MAX | awk '{if ($1 > $2) {print "NOK"} else {print "OK"}}'`

RED_SCORE="<a title=\"please check \$DFO_AB_DIR, there are more than $AB_NUMBER_MAX ABs\"><img src=${IMG_URL}/sq0.png width=15 height=15> <font size=1 color=white>$AB_NUMBER_ALL</font></a>"
GREEN_SCORE="<a title=\"number of ABs in \$DFO_AB_DIR: ok\"><img src=${IMG_URL}/sq2.png width=10 height=10> <font size=1 color=white>$AB_NUMBER_ALL</font></a>"

if [ $AB_NUMBER_SCORE = NOK ]
then
	echo "***WARNING: There are $AB_NUMBER_ALL ABs in your \$DFO_AB_DIR. This will slow down all processing. Please check!"
	AB_NUMBER=$RED_SCORE
else
	AB_NUMBER=$GREEN_SCORE
fi

echo "$AB_NUMBER" > $DFO_MON_DIR/NUMBER_AB

if [ $THIS_IS_OPSHUB = NO ]
then
	RDISK_BUTTON="<a title=\"refresh disk status, wait a few seconds ...\" href=$DFO_GUI_DIR/refresh_disk.ash> <img src=$IMG_URL/redball.gif width=10 height=10 border=0></a>"
	DATADISK_LABEL="data disk:"
else
	RDISK_BUTTON="&nbsp;"
	DATADISK_LABEL="data disk <br>($DATA_DISK):"
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT      
<!-- disk status, XDM, autoDaily -->
    <td ROWSPAN=$ROWSPAN>

      <table WIDTH=100% cellpadding=0 cellspacing=1 border=0>
        <tr align=left valign=bottom BGCOLOR=#999>
          <td COLSPAN=2 align=right><font color=#FFFFFF size=1><a title="N_ABs in \$DFO_AB_DIR">N_ABs:</a></font></td>
          <td COLSPAN=2><font size=1>$AB_NUMBER</font></td>
        </tr>

        <tr align=left valign=bottom BGCOLOR=#999>
          <td nowrap COLSPAN=2>
           $RDISK_BUTTON <!--FILTER-->
            <font color=#FFFFFF size=1><a title="data disk space used">${DATADISK_LABEL}</a></td>
          <td nowrap><font color=#FFFFFF size=1><a title="disk space used">
             $DISK_USED (${DISK_FRAC}%)<!--DISK_STAT1-->
             <img src=$IMG_URL/$DISK_SYMBOL height=18 width=17 border=0><!--DISK_STAT2-->
             </a></font></td>
        </tr> 
EOT

# put XDM into dfoMonitor.html (unless THIS_IS_OPSHUB=YES: issue with du)
if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
       <tr align=left BGCOLOR=#999> 
         <td COLSPAN=2>&nbsp;</td>    
         <td>
           <a href=$DFO_GUI_DIR/refreshXDM.esh title="complete refresh of disk space monitor XDM"><img src="${DFOS_URL}/images/refresh.png" border=0></a> </td> <!--FILTER-->
	  </td>	
       </tr>
EOT

cat $TMP_DIR/disk_space_monitor.html >> $TMP_DIR/dfoMonitor.html
fi
  
# autoDaily part
if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <tr><td COLSPAN=2><font size=1 color=#CCCCCC><td></tr>
        <tr align=left valign=bottom BGCOLOR=#CCCCCC>
          <td COLSPAN=2 width="5%" nowrap>
            <font color=${TITLE_COLOR} size=2><b>autoDaily:</b></td>  <!--FILTER-->
          <td width=95% nowrap bgcolor=#999><font size=2>
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <tr><td COLSPAN=2><font size=1 color=#CCCCCC><td></tr>
EOT
elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <tr><td COLSPAN=2><font size=1 color=#CCCCCC><td></tr>
EOT
fi

# find autoDaily session (and disregard any session merely editing files containing that string)
CHECK_AUTO=""
if [ $THIS_IS_DFOS = YES ]
then
	if [ "$AUTO_CHECK" = "YES" ]
	then
		CHECK_AUTO=`ps -aef | grep $USER | grep autoDaily | grep -v grep | grep -v defunct | egrep -v "vi |vim |emacs"`
	fi

	if [ -s $DFO_MON_DIR/list_data_dates ]
	then
		if [ "Q$CHECK_AUTO" = "Q" ]
		then
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <a class=dfos_item title="dates available for autoDaily execution" href=$DFO_MON_DIR/list_data_dates> list_data_dates</a> <!--FILTER-->
EOT
		else
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <a class=dfos_item title="dates being executed by autoDaily" href=$OTMP_DIR/list_data_dates> list_data_dates</a> <!--FILTER-->
EOT
		fi
	fi

	if [ ! -s $DFO_MON_DIR/list_data_dates ] && [ "Q$CHECK_AUTO" = "Q" ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <font color=#FFFFFF size=2> no dates in queue
EOT
	fi

	if [ ! -s $DFO_MON_DIR/list_data_dates ] && [ "Q$CHECK_AUTO" != "Q" ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
	    <font size=2>&nbsp;
EOT
	fi
fi	

cat >> $TMP_DIR/dfoMonitor.html <<EOT
             </font></td>
         </tr>
EOT

if [ "Q$CHECK_AUTO" != "Q" ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
        <tr align=left valign=bottom BGCOLOR=#999> <!--AUTO-->
          <td COLSPAN=2> <!--AUTO-->
            <img src=$IMG_URL/redball.gif width=10 height=10 border=0> <a class=dfos_item title="view autoDaily log" href=$DFO_GUI_DIR/tail_autoDaily.ash>log</a></td><!--AUTO--> <!--FILTER-->
          </td>  <!--AUTO-->
          <td><font color=#CC3300 size=2><blink><b>autoDaily running!</blink></td> <!--AUTO-->
        </tr>  <!--AUTO-->
        <tr align=left valign=bottom BGCOLOR=#999>   <!--AUTO-->
          <td COLSPAN=3><font color=#FFFFFF size=2>$MESSAGE</td> <!--AUTOMESSAGE-->
         </tr> <!--AUTO-->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
       </table>
    </td>
  </tr>
EOT

# ===================================================================================
# 1.7.3 ROW 2
# ===================================================================================

cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- ROW2: CAL information -->
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td width=80% COLSPAN=2>

      <table border="0" cellspacing="1" cellpadding="1">
EOT
if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP> 
          <td BGCOLOR=#CCC><font size=2 color=$TITLE_COLOR><b>CAL:</b></font></td>
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP > 
  <td BGCOLOR=#CCC ROWSPAN=2><b><font size=2 color=$TITLE_COLOR>Select: &nbsp;</font></td>
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <td><font size=2 color=#FFFFFF>Last N days in \$DFO_CAL_DIR:</td>
  <td>&nbsp;
    <a href="$DFO_GUI_DIR/edit_createAB.esh" title="edit config.createAB, look for N_MCAL_LIST">	<!--FILTER-->
    <font color=$TITLE_COLOR><b>$N_MCAL_LIST</b></font></a>&nbsp;</td> 
  <td><form> 
      <select>
EOT

# pseudo-form for mcalib
if [ -s $TMP_DIR/mcalib_dates ]
then
	cat $TMP_DIR/mcalib_dates | sort -r | sed "/^$/d" | sed "s/^.*/                <option value=\"&\">&<\/option>/"  >> $TMP_DIR/dfoMonitor.html
else
	echo "                <option value=NONE>NONE      <option>"  >> $TMP_DIR/dfoMonitor.html
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
      </select> 
    </form>
  </td> 
EOT

# THIS_IS_PHOENIX: not needed; OPSHUB: truncated
elif [ $THIS_IS_PHOENIX = YES ] 
then
cat >> $TMP_DIR/dfoMonitor.html <<EOT
<td colspan=3><font size=1 color=#CCC><i> not applicable</i></font>
</td>
EOT
fi

if [ $THIS_IS_OPSHUB = NO ]
then
cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <td>		<!--FILTER-->
    <a href="$DFO_GUI_DIR/launch_productExplorer.esh" title="launch productExplorer to explore NGAS-ingested products"><font size=2 color=#FFFFFF>[productExplorer]</font></a>		<!--FILTER-->
  </td>		<!--FILTER-->
EOT
fi

if [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<td ROWSPAN=2> 
  <form name="instruments">  
  <select name="qc" onChange="MM_jumpMenu('parent',this,1)">
    <option value="../${DFO_INSTRUMENT}/dfoMonitor.html">Other instrument:</option>
EOT
	ls ~/.dfosrc_opshub_* | sed "s/^.*opshub_//" > $TMP_DIR/ds_installations
	for I in `cat $TMP_DIR/ds_installations`
	do
		echo "    <option value=\"../${I}/dfoMonitor.html\">${I}</option>" >> $TMP_DIR/dfoMonitor.html
	done

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
  </select>
  </form>
</td>
EOT

# THIS_IS_OPSHUB=YES: config links and optional comment
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <td nowrap><font size=2 color=#FFF>OPSHUB configuration:</font></td>
  <td COLSPAN=3 nowrap><font size=2 color=#FFF>OPSHUB shortcuts (type on the command line):</td>
EOT

	if [ -s $DFO_CONFIG_DIR/info.${OPSHUB_TOOL}_$DFO_INSTRUMENT ]
	then
		CHECK_CONTENT=`cat $DFO_CONFIG_DIR/info.${OPSHUB_TOOL}_$DFO_INSTRUMENT | grep -v "^#" | grep -v "^$" | head -1`
		if [ "Q$CHECK_CONTENT" != Q ]
		then
			cat >>  $TMP_DIR/dfoMonitor.html <<EOT
<td width=500><font size=2 color=#FFF>
Remarks for $DFO_INSTRUMENT OPSHUB processing:</font>
</td>
EOT
		fi
	fi

	cat >>  $TMP_DIR/dfoMonitor.html <<EOT
</tr>

<tr bgcolor=#999 valign=top>
  <td><font size=2 color=#FFF>
  <a href=$DFO_CONFIG_DIR/../projects.${OPSHUB_TOOL} title="'$OPSHUB_TOOL' projects definition file"><font color=#FFF>projects config</font></a> |
  <a href=$DFO_CONFIG_DIR/../streams.${OPSHUB_TOOL} title="'$OPSHUB_TOOL' streams definition file"><font color=#FFF>streams config</font></a> <p>
  <a href=$DFO_CONFIG_DIR/../config.${OPSHUB_TOOL} title="'$OPSHUB_TOOL' config file"><font color=#FFF>tool config</font></a> <p>
  <a href=${DFO_LOG_DIR}/overview.html title="stored OPSHUB results for ${DFO_INSTRUMENT}"><font color=#FFF>stored PROJECTS</font></a>
  </font>
  </td>

<td width=10><font size=1 color=#fff>
<b>Type:</b><p>
  HDR  <br>
  RAW  <br>
  CAL  <br>
  PRO  <br>
  DATA  <br>
  CONFIG  
</td>

<td nowrap><font size=1 color=#fff>
<b>Alias:</b><p>
  cd $HDR_DIR <br>
  cd $RAW_DIR <br>
  cd $CAL_DIR <br>
  cd $SCI_DIR <br>
  cd $BQSROOT <br>
  cd $CONFIG_DIR
</td>

<td nowrap><font size=1 color=#fff>
<b>Or click:</b><p>
<a href=$HDR_DIR>HDR</a> <br>
<a href=$RAW_DIR>RAW</a> <br>
<a href=$CAL_DIR>CAL</a> <br>
<a href=$SCI_DIR>PRO</a> <br>
<a href=$BQSROOT>DATA</a> <br>
<a href=$CONFIG_DIR>CONFIG</a>
</td>
EOT
	if [ -s $DFO_CONFIG_DIR/info.${OPSHUB_TOOL}_$DFO_INSTRUMENT ]
	then
		CHECK_CONTENT=`cat $DFO_CONFIG_DIR/info.${OPSHUB_TOOL}_$DFO_INSTRUMENT | grep -v "^#" | grep -v "^$" | head -1`
		if [ "Q$CHECK_CONTENT" != Q ]
		then
			cat >>  $TMP_DIR/dfoMonitor.html <<EOT
<td>
  <font size=1 color=#fff>
EOT
			cat $DFO_CONFIG_DIR/info.${OPSHUB_TOOL}_$DFO_INSTRUMENT | grep -v "^#" >> $TMP_DIR/dfoMonitor.html
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
  </font>
</td>
EOT
		fi
	fi
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
        </tr> 
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP> 
          <td bgcolor=#CCCCCC><font color=#CCCCCC size=2><b>Transfer:</b></font></td>
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <td><font size=2 color=#FFFFFF>Number of days in VCAL:</td>
          <td>&nbsp;
            <a href="$DFO_GUI_DIR/edit_createAB.esh" title="edit config.createAB; look for N_VCAL_LIST">  <!--FILTER-->
            <font color=$TITLE_COLOR><b>$N_VCAL_LIST</b></font></a>
          &nbsp;</td> 
          <td><form> 
              <select> 
EOT

# pseudo-form for vcalib
	if [ -s $TMP_DIR/vcalib_dates ]
	then
		cat $TMP_DIR/vcalib_dates | sort -r | sed "/^$/d" | sed "s/^.*/                <option value=\"&\">&<\/option>/"  >> $TMP_DIR/dfoMonitor.html
	else
		echo "                <option value=NONE>NONE      <option>"  >> $TMP_DIR/dfoMonitor.html
	fi

# right box: statistics etc.
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
              </select> 
            </form>
          </td> 
EOT

# THIS_IS_PHOENIX/OPSHUB=YES: not needed
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<td colspan=3><font size=1 color=#CCC><i> not applicable</i></font>
</td>
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <td>&nbsp;		<!--FILTER-->
            <a href="$DFO_GUI_DIR/launch_refreshVCAL.esh" title="launch refreshVCAL to refresh the VCAL memory"><font size=2 color=#FFF>[refresh VCAL]</font></a>           <!--FILTER-->
          </td>		<!--FILTER-->
        </tr> 
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          <td>&nbsp;		<!--FILTER-->
          </td>		<!--FILTER-->
        </tr> 
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
     </table>

    </td>
  </tr>
EOT

# ===================================================================================
# 1.7.4 ROW 3: Data Transfer and autoDaily support (needed only for DFOS; 
#		entirely dropped for OPSHUB)
# ===================================================================================

if [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- ROW 3: Data Transfer and autoDaily support: skipped -->
  <tr BGCOLOR=#CCC>	
    <td COLSPAN=2 BGCOLOR=#CCC> <font size=2 color=$TITLE_COLOR><b>Data Transfer, cronjobs:</b></font> <font size=2 color=#666><i>not needed</i></td>
    </td>
  </tr>
EOT
elif [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- ROW 3: Data Transfer and autoDaily support -->
  <tr BGCOLOR=#CCC>	
    <td COLSPAN=2>		

<!-- data transfer table -->   
      <table border="0" cellspacing="1" cellpadding="1">
        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP> 	
          <td BGCOLOR=#CCCCCC ROWSPAN=7><font size=2 color=$TITLE_COLOR><b>Data<br>Transfer:</b></font></td>
          <td><font size=2>&nbsp;</td>		
          <td>						
            <font size=2 color=#FFFFFF>Monitor: <a class=dfos_item title="Data Transfer Monitor" href=${TRANSFER_URL} target=_blank><b>DataTransfer</b></a>&nbsp;   
            <a title="Data Transfer System: delay statistics for NGAS files arrived in last 24 hrs" href=$DFO_GUI_DIR/refreshPA.esh> <!--FILTER-->
            <img src=$IMG_URL/greenball.gif width=10 height=10 border=0></a> <!--FILTER-->
          </td>
          <td BGCOLOR=#CCCCCC ROWSPAN=99>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>
          <td BGCOLOR=#CCCCCC ROWSPAN=99>
            <font size=2 color=$TITLE_COLOR><b><a title="Operational cronjobs">Cronjobs:</a></b></font><br><br>
            <a href="$DFO_GUI_DIR/launch_dfoManager.esh" title="call dfoManager"><img src="${DFOS_URL}/images/dfoManager.png" border=0></a> 	<!--FILTER-->
       	  </td>   
          <td COLSPAN=2><font size=2  color=#FFFFFF><a title="support for incremental processing?">autoDaily?</a></td>
          <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
          <td COLSPAN=2><font size=2  color=#FFFFFF><a title="proper support for HC updates?">HC monitor updates?</a></td>
          <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
          <td COLSPAN=2><font size=2  color=#FFFFFF><a title="proper support for calChecker?">calChecker?</a></td>
        </tr>

        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP> 
 $NGAS_ACCESS	
 $AUTOD_SUPPORT1 
 <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
EOT
	cat $TMP_DIR/HCMON_SUPPORT1 >> $TMP_DIR/dfoMonitor.html

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
$CALCHECK_SUPPORT1
        </tr>   

        <tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP>         
 $TRANSFER_STATUS	<!--TRANSFER_STATUS-->
 $AUTOD_SUPPORT2
 <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
EOT

	cat $TMP_DIR/HCMON_SUPPORT2 >> $TMP_DIR/dfoMonitor.html

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
$CALCHECK_SUPPORT2
        </tr>    

<tr BGCOLOR=#999 ALIGN=LEFT VALIGN=TOP>  
 <td bgcolor=#999>
  <a title="Data transfer: check for delayed files (somewhere between OLAS and PA)" href=$DFO_GUI_DIR/refreshTRQ.esh> <!--FILTER-->
  <img src=$IMG_URL/redball.gif width=10 height=10 border=0></a> <!--FILTER-->
 </td>	
 <td bgcolor=#999 valign=top><font size=1>
 $LONGEST_DELAY	<!--LONGEST_DELAY-->
 $LONGEST_DELAY_ANY	<!--LONGEST_DELAY_ANY-->
 </td> 	

 $AUTOD_SUPPORT3
 <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
EOT
	cat $TMP_DIR/HCMON_SUPPORT3 >> $TMP_DIR/dfoMonitor.html

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
        </tr>
     	<tr><td colspan=8 height=5></td</tr> 

	<tr>
          <td colspan=4><font size=2>&nbsp;</td>
 $LAST_AB
        </tr>

	<tr>
          <td colspan=4><font size=2>&nbsp;</td>
 $LAST_PROCESSED1
        </tr>

	<tr>
          <td colspan=4><font size=2>&nbsp;</td>
 $LAST_EXEC
        </tr>
      </table>			

    </td>			
  </tr>
EOT
fi

# ===================================================================================
# 1.7.5 ROW 4: ToDo lists
# 1.7.5.1 Prepare
# ===================================================================================

NIGHTS_TODO=`cat $DFO_JOB_DIR/${JOB_FILE_NAME} | grep -v "^#" |\
 grep -v "^$" |\
 wc |\
 awk '{if ($1 > 0) {print "YES"} else {print "NO"}}'`

case $NIGHTS_TODO in
 "YES" ) NIGHTS_BGCOLOR=$OFFER_COLOR ; NIGHTS_FT_COLOR=#CC3300 ;;
 "NO"  ) NIGHTS_BGCOLOR=#999 ;      NIGHTS_FT_COLOR=#FFFFFF ;; 
esac

# product ingestion
if [ $THIS_IS_OPSHUB = NO ]
then
	if [ -s $DFO_JOB_DIR/${ING_FILE_NAME} ]
	then
		INGEST_TODO=`egrep "^ingestProducts" $DFO_JOB_DIR/${ING_FILE_NAME}`

		if [ "Q$INGEST_TODO" = "Q" ]
		then
			INGEST_TODO=NO
		else
			INGEST_TODO=YES
		fi
	fi
else
	INGEST_TODO=NO
fi

case $INGEST_TODO in
 "YES" ) INGEST_BGCOLOR=$OFFER_COLOR ; INGEST_FT_COLOR=#C30 ;;
 "NO"  ) INGEST_BGCOLOR=#999 ;         INGEST_FT_COLOR=#FFF ;; 
esac

# make invisible
if [ $THIS_IS_OPSHUB = YES ]
then
	INGEST_FT_COLOR1=$INGEST_BGCOLOR
else
	INGEST_FT_COLOR1=$INGEST_FT_COLOR
fi


# product cleanup
if [ -s $DFO_JOB_DIR/JOBS_CLEANUP ]
then
	CLEANUP_TODO=`grep "^cleanupProducts" $DFO_JOB_DIR/JOBS_CLEANUP`

	if [ "Q$CLEANUP_TODO" = "Q" ]
	then
		CLEANUP_TODO=NO
	else
		CLEANUP_TODO=YES
	fi
else
	CLEANUP_TODO=NO
fi

case $CLEANUP_TODO in
 "YES" ) CLEANUP_BGCOLOR=$OFFER_COLOR ; CLEANUP_FT_COLOR=#CC3300 ;;
 "NO"  ) CLEANUP_BGCOLOR=#999 ;      CLEANUP_FT_COLOR=#FFFFFF ;; 
esac

# ===================================================================================
# 1.7.5.2 Fill in
# ===================================================================================

if [ $THIS_IS_OPSHUB = YES ]
then
	ING_FILE_NAME1="<font color=#CCC size=1><i>not applicable</i></font>"
else
	ING_FILE_NAME1=$ING_FILE_NAME	
fi

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT

<!-- ROW#4: ToDo buttons -->
  <tr BGCOLOR=#CCCCCC> <!--FILTER-->
    <td ALIGN=LEFT VALIGN=TOP COLSPAN=2> <!--FILTER-->

      <table border="0" cellspacing="1" cellpadding="1">  <!--FILTER-->
        <tr BGCOLOR=#999> 		<!--FILTER-->

<!-- TO_DOs: --> <!--FILTER-->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=#CCCCCC> <!--FILTER-->
            <font color=$TITLE_COLOR size=2><b>ToDo:</b></font><!--FILTER-->
          </td> <!--FILTER-->

<!-- JOBS_NIGHT --> <!--FILTER-->
          <td ALIGN=LEFT VALIGN=BOTTOM BGCOLOR=${NIGHTS_BGCOLOR} COLSPAN=3> <!--FILTER-->
            <font color=$NIGHTS_FT_COLOR size=2>off-line processing:<br>${JOB_FILE_NAME}</font></a><!--FILTER-->
          </td> <!--FILTER-->

<!-- JOBS_INGEST --> <!--FILTER-->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${INGEST_BGCOLOR} COLSPAN=3> <!--FILTER-->
            <font color=$INGEST_FT_COLOR size=2>ingest products:<br>${ING_FILE_NAME1}</font></a><!--FILTER-->
          </td> <!--FILTER-->

<!-- JOBS_CLEANUP --> <!--FILTER-->
          <td ALIGN=LEFT VALIGN=BOTTOM BGCOLOR=${CLEANUP_BGCOLOR} COLSPAN=3> <!--FILTER-->
            <font color=$CLEANUP_FT_COLOR size=2>cleanup (fits->hdr):<br>JOBS_CLEANUP</font></a><!--FILTER-->
          </td> <!--FILTER-->

          <td BGCOLOR=#CCCCCC ROWSPAN=2>&nbsp; &nbsp;</td>   <!--FILTER-->

<!-- POSTIT --> <!--FILTER-->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=#999>         <!--FILTER-->
            <img src="${IMG_URL}/image1.gif"> <!--FILTER--> 
            <font color=#FFFFFF size=2><b>Notes:</b></font>  <!--FILTER-->
          </td> <!--FILTER-->

          <td ALIGN=RIGHT VALIGN=TOP BGCOLOR=#999> <!--FILTER-->
            <a class=dfos_item title="edit DFO_POSTIT" href=$DFO_GUI_DIR/edit_DFO_POSTIT.esh> <!--FILTER-->
            <font color=#FFFFFF>[edit]</a> <!--FILTER-->
          </td> <!--FILTER-->

        </tr>   <!--FILTER-->
  
        <tr>    <!--FILTER-->
          <td BGCOLOR=#CCCCCC> <!--FILTER-->
            <font color=#CCCCCC size=2><b>Transfer:</b></font><!--FILTER-->
          </td> <!--FILTER-->

<!-- view JOBS_NIGHT -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${NIGHTS_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="view ${JOB_FILE_NAME}" href=$DFO_JOB_DIR/$JOB_FILE_NAME> <!--FILTER-->
            <font color=${NIGHTS_FT_COLOR}>view</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- edit JOBS_NIGHT -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${NIGHTS_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="edit ${JOB_FILE_NAME}" href=$DFO_GUI_DIR/edit_JOB_FILE.esh> <!--FILTER-->
            <font color=${NIGHTS_FT_COLOR}>[edit]</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- launch JOBS_NIGHT -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${NIGHTS_BGCOLOR} nowrap> <!--FILTER-->
EOT

	if [ "$NIGHTS_TODO" = "YES" ] && [ $THIS_IS_PHOENIX = NO ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <a class=dfos_item title="launch ${JOB_FILE_NAME}" href=$DFO_GUI_DIR/launch_JOB_FILE.esh> <!--FILTER-->
            <img src=$IMG_URL/redball.gif width=10 height=10 border=0><font color=${NIGHTS_FT_COLOR}>[launch]</a> <!--FILTER-->
EOT
	elif [ "$NIGHTS_TODO" = "YES" ] && [ $THIS_IS_PHOENIX = YES ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <font size=1>(manual launch<br> only)</font> <!--FILTER-->
EOT
	else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <!--FILTER-->
EOT
	fi

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          </td> <!--FILTER-->
EOT

# ING_FILE_NAME
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- view JOBS_INGEST -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${INGEST_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="view ${ING_FILE_NAME}" href=$DFO_JOB_DIR/$ING_FILE_NAME> <!--FILTER-->
            <font color=${INGEST_FT_COLOR1}>view</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- edit JOBS_INGEST -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${INGEST_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="edit ${ING_FILE_NAME}" href=$DFO_GUI_DIR/edit_ING_FILE.esh> <!--FILTER-->
            <font color=${INGEST_FT_COLOR1}>[edit]</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- launch JOBS_INGEST -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${INGEST_BGCOLOR} nowrap> <!--FILTER-->
EOT

	if [ "$INGEST_TODO" = "YES" ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <a class=dfos_item title="launch ${ING_FILE_NAME}" href=$DFO_GUI_DIR/launch_ING_FILE.esh> <!--FILTER-->
            <img src=$IMG_URL/redball.gif width=10 height=10 border=0><font color=${INGEST_FT_COLOR}>[launch]</a> <!--FILTER-->
EOT
	else
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <!--FILTER-->
EOT
	fi

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          </td> <!--FILTER-->
EOT

# JOBS_CLEANUP
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- view JOBS_CLEANUP -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${CLEANUP_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="view JOBS_CLEANUP" href=$DFO_JOB_DIR/JOBS_CLEANUP> <!--FILTER-->
            <font color=${CLEANUP_FT_COLOR}>view</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- edit JOBS_CLEANUP -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${CLEANUP_BGCOLOR}> <!--FILTER-->
            <a class=dfos_item title="edit JOBS_CLEANUP" href=$DFO_GUI_DIR/edit_CLEAN_FILE.esh> <!--FILTER-->
            <font color=${CLEANUP_FT_COLOR}>[edit]</a> <!--FILTER-->
          </td> <!--FILTER-->

<!-- launch JOBS_CLEANUP -->
          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=${CLEANUP_BGCOLOR} nowrap> <!--FILTER-->
EOT

	if [ "$CLEANUP_TODO" = "YES" ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            <a class=dfos_item title="launch JOBS_CLEANUP" href=$DFO_GUI_DIR/launch_CLEAN_FILE.esh> <!--FILTER-->
            <img src=$IMG_URL/redball.gif width=10 height=10 border=0><font color=${CLEANUP_FT_COLOR}>[launch]</a> <!--FILTER-->
EOT
	else
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <!--FILTER-->
EOT
	fi

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          </td> <!--FILTER-->

<!-- display DFO_POSTIT --> <!--FILTER-->
EOT

# DFO_POSTIT
	if [ -s $DFO_MON_DIR/DFO_POSTIT ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
	          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=#FFFFFF COLSPAN=2 width="400"> <!--FILTER-->
	            <font size=2> <!--FILTER-->
EOT
		cat $DFO_MON_DIR/DFO_POSTIT  | sed "s/^.*/& <!--FILTER-->/" >> $TMP_DIR/dfoMonitor.html
	else
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
	          <td ALIGN=LEFT VALIGN=TOP BGCOLOR=#FFFFFF COLSPAN=2 width="400"> <!--FILTER-->
	           <font size=2>none <!--FILTER-->
EOT
	fi	

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
          </td> <!--FILTER-->
        </tr>   <!--FILTER-->
      </table>  <!--FILTER-->
    </td> 	<!--FILTER-->
  </tr> 	<!--FILTER-->
EOT
fi

# ===================================================================================
# 1.7.6 ROW5: refresh, help etc.
# ===================================================================================

cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- ROW5: refresh, help etc. -->
  <tr bgcolor=$TITLE_COLOR>			<!--FILTER-->
EOT

if [ $THIS_IS_OPSHUB = YES ]
then
# skip all interactive buttons
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td width=10% ALIGN=LEFT VALIGN=BOTTOM> 	<!--FILTER-->
  <font size=2 color=white>Refresh:<br><tt> $TOOL_NAME -i $DFO_INSTRUMENT</tt>
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td ALIGN=LEFT VALIGN=BOTTOM> 	<!--FILTER-->
      <img src=$IMG_URL/back.gif width=20 height=20 border=0> 		     <!--FILTER-->
      <a href=$DFO_GUI_DIR/refresh_dfoMonitor_nodisk.esh title="refresh this page"><img src="${DFOS_URL}/images/refresh.png" border=0></a> <!--FILTER-->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
    </td> 			<!--FILTER-->

    <td COLSPAN=2><font color=#FFFFFF> 	<!--FILTER-->
      <b>Logs:</b>		<!--FILTER-->
      <a class=dfos_item title="DFO_STATUS file" href=$DFO_MON_DIR/DFO_STATUS>DFO_STATUS</a> | 	   <!--FILTER-->
EOT

if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=dfos_item title="autoDaily logs"  href=$DFO_MON_DIR/AUTO_DAILY>autoDaily logs</a> | <!--FILTER-->
      <a class=dfos_item title="cronjob logs"    href=$DFO_MON_DIR/CRON_LOGS>cron logs</a>         <!--FILTER-->
      | <a class=dfos_item title="statistics"      href=$DFO_GUI_DIR/launch_extractStat.esh>[statistics]</a>  <!--FILTER-->
       &nbsp; &nbsp; &nbsp; <b>CalSelector:</b> <!--FILTER-->
      <a class=dfos_item title="calSelector comparison results" href=$DFO_AB_DIR/CALSELECTOR/SDIFF.html>SDIFF</a> <!--FILTER--> 
EOT
elif [ $THIS_IS_PHOENIX = YES ] && [ $MCAL_MODE = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>phoenix:</b> <!--FILTER-->
      <a class=dfos_item title="list of rejected files" href=$DFO_MON_DIR/list_REJECTED>list_REJ</a> | <!--FILTER--> 
      <a class=dfos_item title="phoenix logs"  href=$DFO_MON_DIR/AUTO_DAILY>phoenix logs</a> | <!--FILTER-->
      <a class=dfos_item title="cronjob logs"  href=$DFO_MON_DIR/CRON_LOGS>cron logs</a>         <!--FILTER-->
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>phoenix:</b> <!--FILTER-->
      <a class=dfos_item title="phoenix logs"  href=$DFO_MON_DIR/AUTO_DAILY>phoenix logs</a> | <!--FILTER-->
      <a class=dfos_item title="cronjob logs"  href=$DFO_MON_DIR/CRON_LOGS>cron logs</a>         <!--FILTER-->
EOT
elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=dfos_item title="${OPSHUB_TOOL} logs"  href=$DFO_MON_DIR/OPSHUB_LOGS/>OPSHUB logs</a>  <!--FILTER-->
       &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>CalSelector:</b> <!--FILTER-->
      <a class=dfos_item title="calSelector OCA rules" href="http://www.eso.org/observing/dfo/quality/ALL/OCA/oca_rule_sets.html">OCA</a> |
      <a class=dfos_item title="calSelector results" href="$DFO_AB_DIR/CALSELECTOR">DFO_AB_DIR/CALSELECTOR</a>
       &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>pipeline:</b> <!--FILTER-->
      <a class=dfos_item title="pipeline versions installed" href="$DFO_MON_DIR/../pipe_versions_`whoami`_`hostname`.html" >versions</a> | <!--FILTER-->
      $PIPE_VERSION: <a class=dfos_item title="info about this version" href="$ESO_PIPE/$PIPE_VERSION">release notes</a> <!--FILTER-->
EOT
fi

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; &nbsp; &nbsp; <b>navigation bar:</b> <!--FILTER-->
      <a class=dfos_item title="edit navigation config file" href=$DFO_GUI_DIR/edit_gui_navbar.esh>[config]</a> | <!--FILTER-->
      <a class=dfos_item title="refresh navigation config file" href=$DFO_GUI_DIR/refresh_gui_navbar.esh>[refresh]</a>  <!--FILTER-->
      &nbsp; &nbsp; <b>mucMonitor:</b> <a href="javascript:openWin2('${WEB_SERVER_URL}/ALL/mucMonitor_$HOSTNAME.html');" style="color:white" title="Click to pop up mucMonitor">open</a> <!--FILTER-->
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      &nbsp; &nbsp; <b>RAWDISP reference:</b> <a class=dfos_item title="option to call rawdisp2reference" href=$DFO_GUI_DIR/launch_rawdisp2reference.esh>[launch]</a> <!--FILTER-->
      | <a class=dfos_item title="list_RAWDISP_references" href=$DFO_MON_DIR/RAWDISP/list_RAWDISP_references>file</a>  <!--FILTER-->
      | <a class=dfos_item title="Reference frames for $DFO_INSTRUMENT" href=${REF_URL}/rawfiles_START_ALL.html>REF</a> <!--FILTER-->
       &nbsp; &nbsp; &nbsp; <b>pipeline:</b> <a class=dfos_item title="info about this version" href="$ESO_PIPE/$PIPE_VERSION">release notes</a> <!--FILTER-->
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
       &nbsp; &nbsp; &nbsp; <b>pipeline:</b> <a class=dfos_item title="info about this version" href="$ESO_PIPE/$PIPE_VERSION">release notes</a> <!--FILTER-->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
  </td> 	<!--FILTER-->

    <td align=left valign=top nowrap><font color=#FFF> <!--FILTER-->
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=dfos_item title="contact rules"               href=$DFO_DOC_DIR/dfoMonitor_contact.txt>contact</a> |  <!--FILTER-->
      <a class=dfos_item title="help for this page"          href=${ALL_URL}/dfoMonitor_help_QC.html>help</a> |  <!--FILTER-->
      <a class=dfos_item title="edit config file"            href=$DFO_GUI_DIR/edit_dfoMonitor.esh>[config]</a> | <!--FILTER-->
      <a class=dfos_item title="documentation for this tool" href=$DFOS_URL>DFOS</a>  <!--FILTER-->
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=dfos_item title="contact QC group"            href=${CONTACT_URL}>CONTACT</a> |  <!--FILTER-->
      <a class=dfos_item title="help for this page"          href=${ALL_URL}/dfoMonitor_help_OPSHUB.html>HELP</a> |  <!--FILTER-->
      <a class=dfos_item title="opshub documentation"        href=$DFOS_URL/opshub.html>DOCU</a> | <!--FILTER-->
      <a class=dfos_item title="opshub Q&A"    	             href=$DFOS_URL/opshub_quanda.html>Q&A</a>  <!--FILTER-->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
    </td> 	<!--FILTER-->
  </tr> 	<!--FILTER-->
</table>

EOT

# ===================================================================================
# 2.  Prepare DATE loop for the DATE table
# 2.1 check all dirs under $DFO_HDR_DIR (for THIS_IS_PHOENIX = NO)
#     for better performance, we use $N_DFO_HDR
# ===================================================================================

cat >> $TMP_DIR/dfoMonitor.html <<EOT
<!-- DATE table -->
<table BORDER=0 WIDTH="$TABLE_WIDTH" class=main>
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	createGuiJob refresh dfoMonitor 1
	createRefresh_no_disk
	createGuiJob launch rawdisp2reference 2
fi

# DFOS: we check $DFO_HDR_DIR 
if [ $THIS_IS_DFOS = YES ]
then
	cd $DFO_HDR_DIR
	rm -f $TMP_DIR/list_dates_hdr*
	ls -d 20* 2>/dev/null | tail -$N_DFO_HDR > $TMP_DIR/list_dates_hdr1

# find DATEs with hdr AND (w/o flag 'removed' && with raw data); always add $TODAY
# thereby we focus on incremental processing
	for D in `cat $TMP_DIR/list_dates_hdr1`
	do
		CHECK_ENTRY=`grep " $D " $DFO_MON_DIR/DFO_STATUS | tail -1`
		CHECK_REMOVED=`grep "^removed " $DFO_MON_DIR/DFO_STATUS | grep " $D "`

		if [ "Q$CHECK_ENTRY" = "Q" ]
		then
			continue
		fi

		if ( [ "Q$CHECK_REMOVED" = "Q" ] && [ -s $DFO_RAW_DIR/$D ] ) || [ $D = $TODAY ]
		then
			echo $D >> $TMP_DIR/list_dates_hdr
		fi
	done

# PHOENIX: check the JOBS_PHOENIX files
elif [ $THIS_IS_PHOENIX = YES ]
then
	cd $DFO_JOB_DIR
	rm -f $TMP_DIR/list_dates_hdr*
	ls JOBS_PHOENIX* 2>/dev/null | sed "s/JOBS_PHOENIX_//" > $TMP_DIR/list_dates_hdr

# OPSHUB: check the JOBS_PROJECT files
elif [ $THIS_IS_OPSHUB = YES ]
then
	cd $DFO_JOB_DIR
	rm -f $TMP_DIR/list_dates_hdr*
	ls execAB* 2>/dev/null | sed "s/_/ /g" | awk '{print $3}' | sort -u > $TMP_DIR/list_dates_hdr
fi

# ===================================================================================
# 2.2 create the head line for the DATE table
# ===================================================================================
if [ -s $TMP_DIR/list_dates_hdr ]
then
	N_DATES=`cat $TMP_DIR/list_dates_hdr | wc -l | awk '{print $1}'`
else
	N_DATES=0
fi

N_DATES1=`echo $N_DATES | awk '{print $1+4}'`
case $DEEP_MODE in
	"NO"  ) DATE_TITLE="DFO date" ; DATE_LABEL="DATE" ; OTHER_TITLE="" ; OTHER_TITLE2="" ;;
	"YES" ) DATE_TITLE="encoded RUN_ID"; DATE_LABEL="PSEUDO-DATE" ; OTHER_TITLE="(not applicable in DEEP mode)" ; OTHER_TITLE2=$OTHER_TITLE ;;
esac

if [ $THIS_IS_OPSHUB = YES ]
then
	DATE_TITLE="Date" ; DATE_LABEL="DATE" ; OTHER_TITLE="(not applicable here)"; OTHER_TITLE2="" 
    	TAG_CREATEAB_VCAL=""
else
    	TAG_CREATEAB_VCAL=", CREATEAB_VCAL=$CREATEAB_VCAL"
fi

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td WIDTH=5% ALIGN=RIGHT><a title="${DATE_TITLE}">	         	       <b>${DATE_LABEL}</b></td>
    <td WIDTH=1%><a title="entry in filter file? ${OTHER_TITLE}">      <b>filter</b></td> 
    <td WIDTH=1%><a title="calib products ingested? ${OTHER_TITLE}">   <b>cdb</b></td> 
EOT
	if [ $THIS_IS_DFOS = NO ]
	then
		echo "<td WIDTH=1%><a title="science ABs created and stored? ${OTHER_TITLE}">   <b>sci</b></td> " >> $TMP_DIR/dfoMonitor.html
	else
		echo "<td WIDTH=1%><a title="obsolete for DFOS ${OTHER_TITLE}">   <b>sci</b></td> " >> $TMP_DIR/dfoMonitor.html
	fi

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td WIDTH=3%><a title="SM or VM SCIENCE taken this night? ${OTHER_TITLE}"><b>SM_VM</b></td> 
    <td WIDTH=1%><a title="nightlog tool ${OTHER_TITLE}">      	       <b>NLT</b></td> 
    <td WIDTH=1%><a title="calChecker report ${OTHER_TITLE}">  	       <b>CAL</b></td>
    <td WIDTH=1%><a title="data report ${OTHER_TITLE2}">                <b>report</b></td> 
    <td WIDTH=1%><a title="data completely available in NGAS? ${OTHER_TITLE}"><b>complete?</b></td>
    <td WIDTH=1%><a title="processing status from DFO_STATUS ${OTHER_TITLE} "><b>Status</b></a></td>
    <td WIDTH=1%><a title="DATE in VCAL? ${OTHER_TITLE}">              <b>VCAL</b> </a></td>
    <td WIDTH=1%><a title="DATE in MCAL? ${OTHER_TITLE}">              <b>MCAL</b></a></td>

    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->
    <td><a title="createAB (mode CALIB) ${OTHER_TITLE2}">               <b>createAB</b></a></td>
    <td><a title="CALIB ABs in \$DFO_AB_DIR ${OTHER_TITLE}">           <b>CALIB ABs</b></a></td>
    <td><a title="certifyProducts and moveProducts (mode CALIB) ${OTHER_TITLE}"><b>certify + move</b></a></td>

    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->
EOT
	if [ $THIS_IS_DFOS = NO ]
	then
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td><a title="createAB (mode SCIENCE${TAG_CREATEAB_VCAL}) ${OTHER_TITLE}"><b>createAB</b></a></td>
    <td><a title="SCIENCE ABs in \$DFO_AB_DIR">                <b>SCIENCE ABs</b></a></td>
    <td><a title="moveProducts (mode SCIENCE has no certifyProducts) ${OTHER_TITLE}"><b>move</b></a></td>
EOT
	else
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td><a title="createAB for SCIENCE done in PHOENIX"><b>createAB</b></a></td>
    <td><a title="none">                <b>SCIENCE ABs</b></a></td>
    <td><a title="not offered in mode SCIENCE"><b>move</b></a></td>
EOT
	fi

	cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->
    <td><a title="finish daily workflow  ${OTHER_TITLE}">              <b>finish</b></a></td>
  </tr>
EOT

# OPSHUB is special
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td WIDTH=10% ALIGN=RIGHT><a title="${DATE_TITLE}">          <b>${DATE_LABEL}</b></td>
    <td WIDTH=1%><a title="nightlog tool ">      	       <b>NLT</b></td> 
    <td WIDTH=1%><a title="calChecker report ">  	       <b>CAL</b></td>
    <td WIDTH=1%><a title="data report ">                      <b>report</b></td> 
    <td WIDTH=150><a title="PROJECT name(s) for this DATE">     <b>PROJECTS</b> </a></td>
    <td WIDTH=150><a title="AB_METHOD">                         <b>AB_METHOD</b> </a></td>

    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->

    <td><a title="created/downloaded ABs (CALIB)">               <b>CALIB: process ABs</b></a></td>
    <td><a title="overview of CALIB ABs in \$DFO_AB_DIR">        <b>overview</b></a></td>

    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->
    <td><a title="created/downloaded ABs (SCIENCE)">            <b>SCIENCE: process ABs</b></a></td>
    <td><a title="overview of SCIENCE ABs in \$DFO_AB_DIR">     <b>overview</b></a></td>
    <td><a title="processing jobs">                             <b>Jobs</b></a></td>

    <td ROWSPAN=${N_DATES1} width=1 bgcolor=#999> </td><!--ROWSPAN_${N_DATES1}-->
    <td><a title="remove (X) or save (M)">                              <b>remove (X) or save (M)</b></a></td>
  </tr>
EOT
fi

# ===================================================================================
# 3.  DATE table
# 3.1 evaluate DFO_STATUS for DATE
# ===================================================================================

rm -f $TMP_DIR/list_flag_all
cd $DFO_AB_DIR

if [ -s $TMP_DIR/list_dates_hdr ]
then
	FIRST_DATE=`cat $TMP_DIR/list_dates_hdr | head -1`
	for D in `cat $TMP_DIR/list_dates_hdr`
	do
		rm -f $TMP_DIR/list_status
		if [ "$QUIET" = "NO" ]
		then
			echo "  $D ..."
		fi

		echo "" >> $TMP_DIR/dfoMonitor.html
		echo "  <tr VALIGN=TOP BGCOLOR=#CCCCCC><!--${D}-->" >> $TMP_DIR/dfoMonitor.html

# all entries for $D in DFO_STATUS
		grep " $D " $DFO_MON_DIR/DFO_STATUS | grep -v "^#" > $TMP_DIR/list_status

# check for CALIB and SCIENCE files existing
		if [ $THIS_IS_DFOS = YES ]
		then
			CAL_EXISTS=`grep fits $DFO_LST_DIR/list_${DFO_INSTRUMENT}_$D.txt 2>/dev/null | grep CALIB   | wc -l`
			SCI_EXISTS=`grep fits $DFO_LST_DIR/list_${DFO_INSTRUMENT}_$D.txt 2>/dev/null | grep SCIENCE | wc -l`
		else
			CAL_EXISTS=0
			SCI_EXISTS=0
		fi

# check if files are completely available in NGAS 
		if [ $THIS_IS_DFOS = YES ]
		then
			ENTRY_EXISTS=`grep "raw_Complete" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
			if [ Q$ENTRY_EXISTS = Q ] && [ $D != $TODAY ]
			then
				checkCompleteness

				UPDATE=`date -u +%Y-%m-%d"T"%H:%M:%S`
				if [ Q$FLAG = Qraw_Complete ]
				then
					echo "raw_Complete $D $UPDATE" >> $DFO_MON_DIR/DFO_STATUS
				else
					sed -i -e "/raw_Incomplete $D /d" $DFO_MON_DIR/DFO_STATUS
					echo "raw_Incomplete $D $UPDATE" >> $DFO_MON_DIR/DFO_STATUS
				fi
				grep " $D " $DFO_MON_DIR/DFO_STATUS > $TMP_DIR/list_status
			fi

# last entry in DFO_STATUS
			LAST_ENTRY=`grep -v "^#" $TMP_DIR/list_status | grep -v cal_Ingested | tail -1 | awk '{print $1}'`
		fi

# NLT
		DATE_SHORT=`echo $D | sed "s/-//g"`
		if [ $THIS_IS_OPSHUB = NO ]
		then
			NLT_APPLICABLE=`grep "^#NLT" $DFO_BIN_DIR/getObInfo | awk '{print $2,$3}' | grep "^$PANL_INSTRUMENT " | awk '{print $2}'`

			if [ Q$NLT_APPLICABLE = Q ]
			then
				NLT_FLAG=NO
			elif [ $DATE_SHORT -lt $NLT_APPLICABLE ]
			then
				NLT_FLAG=NO
			else
				NLT_FLAG=YES
			fi
		else
			NLT_FLAG=YES
		fi	

# check if entry in filter file (THIS_IS_DFOS = YES)
		CHECK_FILT="&nbsp;"
		TITLE_FILT=""
		COLOR_FILT=$EMPTY_COLOR

		if [ $THIS_IS_DFOS = YES ]
		then
			FILT_FILE=`echo "filt_${DFO_INSTRUMENT}_$D.txt"`
			CHECK_FILT=`ls $DFO_LST_DIR | grep $FILT_FILE`
			if [ "Q$CHECK_FILT" != "Q" ]
			then
				CHECK_ENTRY=`grep "^${DFO_FILE_NAME}" $DFO_LST_DIR/$FILT_FILE | grep "fits"`
				if [ "Q$CHECK_ENTRY" != "Q" ]
				then
					CHECK_FILT="! filter"
					TITLE_FILT="filter file"
					COLOR_FILT=$OFFER_COLOR
				else
					CHECK_FILT="&nbsp;"
				fi
			fi
		fi

# CDB flag: check if data under $DFO_CAL_DIR/$DATE have been ingested into the cdb (THIS_IS_DFOS = YES)
		CDB_FLAG1=""
		CDB_FLAG2="&nbsp;"
		CDB_COLOR="#CCCCCC"

		if [ $THIS_IS_DFOS = YES ]
		then
			if ( [ -s $DFO_LST_DIR/list_ingest_CALIB_$D.txt ] || [ -s $DFO_LST_DIR/list_ingest_$D.txt ] ) && [ -d $DFO_CAL_DIR/$D ]
			then
				CDB_FLAG1="<a href=$DFO_LST_DIR/list_ingest_CALIB_$D.txt>  <!--FILTER-->"
				CDB_FLAG2="<img src=${IMG_URL}/ok.gif   width=12 height=15 border=0 title=\"calib products ingested; click for the log\"></a>"
				CDB_COLOR="#FFFF00"
			elif ( [ ! -s $DFO_LST_DIR/list_ingest_CALIB_$D.txt ] && [ ! -s $DFO_LST_DIR/list_ingest_$D.txt ] ) && [ -d $DFO_CAL_DIR/$D ]
			then
				CDB_FLAG2="<img src=${IMG_URL}/open.gif width=12 height=15 title=\"no calib products ingested\">"
				CDB_COLOR="#FFFF00"
			fi
		fi

# SCI flag: PHOENIX -- check if data under $DFO_SCI_DIR/$DATE have been ingested into the cdb
#           DFOS    -- we scan for sci_AB/sci_Updated 
#	    OPSHUB  -- nothing

		if [ $THIS_IS_PHOENIX = YES ]
		then
			SCI_FLAG1=""
			SCI_FLAG2="&nbsp;"
			SCI_COLOR="#CCCCCC"
			CHECK_INGEST=`grep "^sci_Ingested" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
			CHECK_UPDATED=`grep "^sci_Updated" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
			if [ Q$CHECK_INGEST != Q ]
			then
				SCI_FLAG1="<a href=$DFO_LST_DIR/list_ingest_SCIENCE_$D.txt> <!--FILTER-->"
				SCI_FLAG2="<img src=${IMG_URL}/ok.gif width=12 height=15 border=0 title=\"science files ingested; click for the log\"></a>"
				SCI_COLOR="#66CCFF"
			elif [ Q$CHECK_INGEST = Q ] && ( [ -d $DFO_SCI_DIR/$D ] || [ Q$CHECK_UPDATED != Q ] )
			then
				SCI_FLAG2="<img src=${IMG_URL}/open.gif width=12 height=15 title=\"no science files ingested\">"
				SCI_COLOR="#66CCFF"
			fi

		elif [ $THIS_IS_DFOS = YES ]
		then
			SCI_FLAG1=""
			SCI_FLAG2=""
			CHECK_UPDATED=`grep "^sci_Updated" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
			if ( [ -d $DFO_SCI_DIR/$D ] || [ Q$CHECK_UPDATED != Q ] )
			then
				SCI_COLOR="#66CCFF"
			else
				SCI_COLOR="#CCCCCC"
			fi
# OPSHUB
		else
			SCI_FLAG1=""
			SCI_FLAG2=""
			SCI_COLOR="#CCCCCC"
		fi

# AB flag: check if SCIENCE ABs have been created and verified (OPSHUB: downloaded)
		CHECK_VERIFIED=`grep "^sci_Verified" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
		AB_FLAG1=""

	        if [ Q$CHECK_VERIFIED != Q ]
	        then
			if [ $THIS_IS_DFOS = YES ]
			then
				AB_FLAG1="<img src=${IMG_URL}/ok.gif width=12 height=15 border=0 title=\"science ABs created and verified\"></a>"
			fi
	        elif [ Q$CHECK_VERIFIED = Q ] && ( [ -d $DFO_SCI_DIR/$D ] || [ Q$CHECK_UPDATED != Q ] ) && [ $DEEP_MODE = NO ]
		then
	                AB_FLAG1="<img src=${IMG_URL}/open.gif width=12 height=15 title=\"no science ABs created\">"
		else
			AB_FLAG1="&nbsp;"
	        fi

# ===================================================================================
# 3.2 DATE; check if finished etc.
# ===================================================================================
# DFOS: mark TODAY

		TEXT="<b>$D</b>"
		ACTION="&nbsp;"

		if [ $THIS_IS_DFOS = YES ]
		then
			N_FILES=`ls $DFO_HDR_DIR/$D | grep .hdr | wc -l | awk '{print $1}'`
			if [ $D = $TODAY ]
			then
				TEXT="<b>$D<br></b><i>(today)</i>"
			elif [ $N_FILES = 0 ]
			then
				TEXT="<b><font color=#666666>$D</font></b>"
			fi
		fi
	
		CHECK_FINISHED=`grep "^finished" $TMP_DIR/list_status`
		if [ "Q$CHECK_FINISHED" != "Q" ] && [ $THIS_IS_OPSHUB = NO ]
		then
			ACTION=`callAction 08`
		fi
	
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td ALIGN=RIGHT nowrap> <!--${D}-->
        $ACTION      <!--${D}--> <!--FILTER-->
        $TEXT</td>   <!--${D}--> 
EOT
	
# ===================================================================================
# 3.3 filter check
# ===================================================================================

		if [ $THIS_IS_DFOS = YES ]
		then
			case $CHECK_FILT in
		 	 "! filter" )
				echo "    <td bgcolor=${COLOR_FILT} WIDTH=1% align=center nowrap> <!--${D}-->
<a href=\"${DFO_LST_DIR}/${FILT_FILE}\" title=\"${TITLE_FILT}\">$CHECK_FILT</a> <!--${D}--> <!--FILTER-->
</td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html ;;
			 *	 )
				echo "    <td bgcolor=${COLOR_FILT} WIDTH=1% align=center nowrap>${CHECK_FILT}</td> <!--${D}-->" >> $TMP_DIR/dfoMonitor.html ;;
			esac
		elif [ $THIS_IS_PHOENIX = YES ]
		then
			echo "    <td bgcolor=${COLOR_FILT} WIDTH=1% align=center nowrap>${CHECK_FILT}</td> <!--${D}-->" >> $TMP_DIR/dfoMonitor.html
		fi

# none for OPSHUB
	
# ===================================================================================
# 3.4 CDB and SCI check (none for OPSHUB
# ===================================================================================

		if [ $THIS_IS_OPSHUB = NO ]
		then	
	 		echo "    <td bgcolor=${CDB_COLOR} WIDTH=1% align=center> <!--${D}-->
$CDB_FLAG1 <!--${D}-->
$CDB_FLAG2 </td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html

	 		echo "    <td bgcolor=${SCI_COLOR} WIDTH=1% align=center nowrap> <!--${D}-->
$SCI_FLAG1 <!--${D}-->
$SCI_FLAG2 <!--${D}-->
$AB_FLAG1  <!--${D}-->
 </td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html
		fi
	
# ===================================================================================
# 3.5 SM_VM, SCI_NUM
# ===================================================================================
	
		rm -f $TMP_DIR/list_flag
		SCI_NUM=0

		if [ -s $DFO_LST_DIR/list_${DFO_INSTRUMENT}_${D}.txt ]
		then
			cat $DFO_LST_DIR/list_${DFO_INSTRUMENT}_${D}.txt |\
	                 awk '{
	                        if ( change == 1 && $1 != "Number" && $2 != "" )  { print date, $2 }
	                        if ( $1 == "OBS_PROG_ID" && $2 == "SM/VM" ) { change=1 }
	                        if ( $1 == "Number" ) { change=0 }
	                      }' change=0 date=$D | sort -u > $TMP_DIR/list_flag
			cat $TMP_DIR/list_flag | awk '{print $2}' | sort -u |\
 tr "\012" " " |\
 sed "s/s v/<font color=${SM_COLOR}><b>SM <font color=${VM_COLOR}>VM<\/b><\/font>/" |\
 sed "s/v s/<font color=${SM_COLOR}><b>SM <font color=${VM_COLOR}>VM<\/b><\/font>/" |\
 sed "s/s/<font color=${SM_COLOR}><b>SM<\/b><\/font>/" |\
 sed "s/v/<font color=${VM_COLOR}>VM<\/font>/" > $TMP_DIR/list_flag1
			SCI_NUM=`grep " SCIENCE " $DFO_LST_DIR/list_${DFO_INSTRUMENT}_${D}.txt | grep -v "60\.A" | grep -v "Programmes with " | wc -l`
		fi
	
		if [ -s $TMP_DIR/list_flag ]
	        then
	                SM_VM=`cat $TMP_DIR/list_flag1`
			cat $TMP_DIR/list_flag >> $TMP_DIR/list_flag_all
	        else
	                SM_VM="&nbsp;"
	        fi

		if [ $THIS_IS_OPSHUB = NO ]
		then
			if [ "$SM_VM" = "&nbsp;" ]
			then
	 			echo "    <td bgcolor=#CCCCCC WIDTH=1% align=center><font size=1>&nbsp;</font></td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html
			else
	 			echo "    <td bgcolor=#CCCCCC WIDTH=1% align=center nowrap><font size=1>$SM_VM <font color=${VM_COLOR}><i><a title=\"number of science files\">${SCI_NUM}</a></i></font></td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html
			fi
		fi
	
# ===================================================================================
# 3.6 NR check (obsolete for DFOS)
# ===================================================================================

		if [ $THIS_IS_PHOENIX = YES ] && [ $DEEP_MODE = YES ]
		then
	 		echo "    <td bgcolor=#CCCCCC WIDTH=1% align=center><font size=2>&nbsp;</font></td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html
		elif [ $THIS_IS_PHOENIX = YES ] && [ $NLT_FLAG = NO ]
		then
			echo "    <td bgcolor=#CCCCCC WIDTH=1% align=center><a href=\"${NR_DIR}/NR_$D\" title=\"instrument night log\" target=_blank><font color=$CAL_COL>NR</font></a></td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html
		else
			echo "    <td bgcolor=#CCCCCC WIDTH=1% align=center><a href=\"${NLT_URL}?date=${D}&instrument=$PANL_INSTRUMENT\" title=\"instrument night log\" target=_blank><font color=$CAL_COL>NLT</font></a></td> <!--${D}--> " >> $TMP_DIR/dfoMonitor.html 
		fi
	
# ===================================================================================
# 3.7 calChecker and data reports
# ===================================================================================
# calChecker report 
		CAL_LINK="&nbsp; <!--${D}-->"

		if [ $THIS_IS_DFOS = YES ]
		then
			CALCHECK_EXISTS=`ls $DFO_LST_DIR/CALCHECK 2>/dev/null | grep $D`
			if [ "Q$CALCHECK_EXISTS" = "Q" ]
			then
				CAL_LINK="&nbsp; <!--${D}-->"
			else
				CAL_LINK="<a title=\"view calChecker report\" href=$CALCHECK_DIR/calResults_${DFO_INSTRUMENT}_${D}.html target=_blank><font color=${CAL_COL}>CAL</font></a> <!--${D}-->"
			fi
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			CAL_LINK="<a title=\"view calChecker report\" href=$CALCHECK_DIR/calResults_${DFO_INSTRUMENT}_${D}.html target=_blank><font color=${CAL_COL}>CAL</font></a> <!--${D}-->"
			TEXT0="<a title=\"view data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
		fi
	
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
	    <td>$CAL_LINK </td>	
EOT
	
# data report 
		HTML_EXISTS=`ls $DFO_LST_DIR/REPORT 2>/dev/null | grep $D`
		if [ -d $DFO_HDR_DIR/$D ]
		then
			CHECK_HDR=`ls $DFO_HDR_DIR/$D | grep .hdr | head -1`
		fi
	
# ===================================================================================
# 3.8a NGAS availability
# ===================================================================================

		REPORT_COL=#F60
		REPORT_NAME=list_${DFO_INSTRUMENT}_${D}_data.html

		if [ $THIS_IS_PHOENIX = YES ]
		then
			BGCOLOR="#CCC"
			ACTION=""
			TEXT2=""
			if [ $DEEP_MODE = NO ]
			then
				TEXT0="<a title=\"view local data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
				TEXT1="<a title=\"not checked\"><font color=#666>yes</font></a>"
			elif [ $DEEP_MODE = YES ]
			then
				TEXT0=""
				TEXT1=""
			fi
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			BGCOLOR="#CCC"
			ACTION=""
			TEXT0="<a title=\"view data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
		else	
			if [ $D = $TODAY ]
			then
				BGCOLOR="$INCOMPLT_COLOR"
				ACTION=""
				if [ Q$CHECK_HDR != Q ]
				then
					TEXT0="<a title=\"view local data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
					TEXT1="<a title=\"data at least partially available\"><font color=#666666><i>not yet finished</i></font></a>"
					TEXT2=""
				else
					TEXT0="<a title=\"no data acquired\"><font color=#666666>no data</font></a>"
					TEXT1="<a title=\"no data acquired\"><font color=#666666><i>not yet finished</i></font></a>"
					TEXT2=""
				fi
			elif [ Q$LAST_ENTRY = "Qraw_Incomplete" ]
			then
				BGCOLOR="$INCOMPLT_COLOR"
				TEXT0="<a title=\"view local data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
				TEXT1=""
				TEXT2="<a title=\"check data report for missing files\">no</a>"
			elif [ Q$CHECK_HDR != Q ]
			then
				if [ -s $DFO_LST_DIR/list_${DFO_INSTRUMENT}_${D}.txt ]
				then
					N_FILES=`ls $DFO_HDR_DIR/$D | grep .hdr | wc -l | awk '{print $1}'`
				else
					N_FILES=?
				fi

				BGCOLOR="$COMPLETE_COLOR"
				ACTION=""
				TEXT0="<a title=\"view local data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
				TEXT1="yes <a title=\"number of raw files\">($N_FILES)</a>"
				TEXT2=""
			else
				BGCOLOR="#CCCCCC"
				ACTION=""
				TEXT0="<a title=\"view local data report\" href=${REPORT_DIR}/${REPORT_NAME} target=_blank><font color=${REPORT_COL}>report</font></a>"
				TEXT1="<a title=\"no data acquired\"><font color=#666666>no data</font></a>"
				TEXT2=""
			fi
		fi

		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td nowrap>$TEXT0<!--${D}-->
    </td><!--${D}-->
EOT
		if [ $THIS_IS_OPSHUB = NO ]
		then	
			 cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR ALIGN=RIGHT nowrap><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT1<!--${D}-->
      $TEXT2<!--${D}--><!--FILTER-->
    </td><!--${D}-->
EOT
		fi
	
# ===================================================================================
# 3.8b Status in DFO_STATUS
# ===================================================================================

		STATUS_COL=#333333
		STATUS_NAME="proc_status_${D}.txt"
		create_proc_status ${D}
	
		if [ $THIS_IS_OPSHUB = NO ]
		then
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      <a title="processing status from DFO_STATUS" href=${DFO_MON_DIR}/${STATUS_NAME} target=_blank><font color=${STATUS_COL}>status</font></a></td><!--${D}--> <!--FILTER-->
    </td><!--${D}-->
EOT
		fi

# ===================================================================================
# 3.9 VCAL (OPSHUB: PROJECTS)
# ===================================================================================
	
		BGCOLOR=$EMPTY_COLOR
		TXT="&nbsp;"

		if [ $THIS_IS_DFOS = YES ] && [ -s $TMP_DIR/vcalib_dates ]
		then
			CHECK_VCALIB=`grep $D $TMP_DIR/vcalib_dates`
			if [ "Q$CHECK_VCALIB" != "Q" ]
			then
				BGCOLOR=$VCALIB_COLOR
			fi

# OPSHUB: project overview
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			rm -f $TMP_DIR/dm_ds_list_projects
			grep -v "^#" $OPSHUB_TOOL_PROJECT_FILE | grep "[[:space:]]${D}[[:space:]]" | grep "[[:space:]]${DFO_INSTRUMENT}[[:space:]]" | awk '{print $2}' | sort -u > $TMP_DIR/dm_ds_list_projects
# monthly mode?
			if [ ! -s  $TMP_DIR/dm_ds_list_projects ]
			then
				YYMM=`echo $D | cut -c1-7`
				grep -v "^#" $OPSHUB_TOOL_PROJECT_FILE | grep "[[:space:]]${YYMM}[[:space:]]" | grep "[[:space:]]${DFO_INSTRUMENT}[[:space:]]" | awk '{print $2}' | sort -u > $TMP_DIR/dm_ds_list_projects
				MONTHLY=YES
			fi

			if [ -s $TMP_DIR/dm_ds_list_projects ]
			then
				TXT=`cat $TMP_DIR/dm_ds_list_projects 2>/dev/null | tr "\012" "|" | sed "s/|$//" | sed "s/|/ & /"`
			fi
		fi

		echo "    <td BGCOLOR=$BGCOLOR><font size=1>$TXT</font></td><!--${D}-->" >> $TMP_DIR/dfoMonitor.html
		
# ===================================================================================
# 3.10 MCAL (OPSHUB: AB_METHOD)
# ===================================================================================
	
		BGCOLOR=$EMPTY_COLOR
		TXT="&nbsp;"

		if [ $THIS_IS_DFOS = YES ] && [ -s $TMP_DIR/mcalib_dates ]
		then
			CHECK_MCALIB=`grep $D $TMP_DIR/mcalib_dates`
			if [ "Q$CHECK_MCALIB" != "Q" ]
			then
				BGCOLOR=$MCALIB_COLOR
			fi

# OPSHUB: AB_METHOD overview
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			if [ Q$MONTHLY = QYES ]
			then
				TXT=`grep -v "^#" $OPSHUB_TOOL_PROJECT_FILE | grep "[[:space:]]${YYMM}[[:space:]]" | grep "[[:space:]]${DFO_INSTRUMENT}[[:space:]]" | awk '{print $7}' | sort -u | tr "\012" "|" | sed "s/|$//" | sed "s/|/ & /"`
			else
				TXT=`grep -v "^#" $OPSHUB_TOOL_PROJECT_FILE | grep "[[:space:]]${D}[[:space:]]" | grep "[[:space:]]${DFO_INSTRUMENT}[[:space:]]" | awk '{print $7}' | sort -u | tr "\012" "|" | sed "s/|$//" | sed "s/|/ & /"`
			fi
		fi

		echo "    <td BGCOLOR=$BGCOLOR><font size=1>$TXT</font></td><!--${D}-->" >> $TMP_DIR/dfoMonitor.html
	
# ===================================================================================
# 3.11 AB creation (CALIB)
# ===================================================================================

		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT="&nbsp;"
		ENTRY_EXISTS=`grep "cal_AB" $TMP_DIR/list_status`

		LAST_ENTRY=`grep -v "^#" $TMP_DIR/list_status | grep -v raw_Complete | grep -v raw_Incomplete | grep -v cal_Ingested | tail -1 | awk '{print $1}'`
		if [ Q$LAST_ENTRY = Q ]
		then
			LAST_ENTRY=UNKNOWN
		fi

# needed for OPSHUB (we could have valid CAL and SCI flags together)
		if [ $THIS_IS_OPSHUB = YES ]
		then
			LAST_CALENTRY=`grep -v "^#" $TMP_DIR/list_status | grep -v raw_Complete | grep -v raw_Incomplete | grep -v cal_Ingested | grep cal_ | tail -1 | awk '{print $1}'`
			if [ Q$LAST_CALENTRY = Q ]
			then
				LAST_CALENTRY=UNKNOWN
			fi
		fi

		if [ $THIS_IS_DFOS = YES ]
		then

			if ( [ $LAST_ENTRY = "cal_AB" ] || [ $LAST_ENTRY = "cal_Queued" ] || [ $LAST_ENTRY = "cal_QC" ] )
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 02`
				TEXT="&nbsp;"

			elif [ "Q$ENTRY_EXISTS" != "Q" ]
			then
				BGCOLOR="$DONE_COLOR"
				ACTION=""
				TEXT="done"
			else
				BGCOLOR="$EMPTY_COLOR"
				ACTION=""
				TEXT="&nbsp;"
			fi
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			if [ $LAST_CALENTRY = "cal_Queued" ] 
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=""
				TEXT="waiting for processing"

# OPSHUB: cal_Processed set by opshub tool
			elif [ $LAST_CALENTRY = "cal_Processed" ]
			then
				BGCOLOR="$DONE_COLOR"
				ACTION=""
				TEXT="done"
			else
				BGCOLOR="$EMPTY_COLOR"
				ACTION=""
				TEXT="&nbsp;"
			fi
		fi
	
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT</td><!--${D}-->
EOT
	
# ===================================================================================
# 3.12 existing ABs (CALIB)
# ===================================================================================

		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT1=""
		TEXT2="&nbsp;"

		if [ $THIS_IS_DFOS = YES ] || [ $THIS_IS_PHOENIX = YES ]
		then
			if [ Q$LAST_ENTRY = "Qcal_AB" ] || [ Q$LAST_ENTRY = "Qcal_Queued" ] || [ Q$LAST_ENTRY = "Qcal_QC" ]
			then
				CHECK_AB=`ls | grep ab`
				if [ "Q$CHECK_AB" != "Q" ]
				then
					if [ $THIS_IS_PHOENIX = YES ]
					then
# for CALIB processing by phoenix, we need BATCH_ID
						grep "BATCH_ID.CALIB_${D}" `ls | grep ab` | wc -l | awk '{print $1}' > $TMP_DIR/result
					else
						grep "DATE..$D" `ls | grep ab` | wc -l | awk '{print $1}' > $TMP_DIR/result
					fi
	
					N_AB=`cat $TMP_DIR/result`
					BGCOLOR="$OFFER_COLOR"
					ACTION=""
					TEXT1="<a title=\"show AB status\" href=./status_$D.html><img src=$IMG_URL/index.gif width=15 height=15 border=0></a>"
					TEXT2="yes ($N_AB)"
				else
					BGCOLOR="$EMPTY_COLOR"
					ACTION=""
					TEXT1=""
					TEXT2="&nbsp;"
				fi
			fi
# OPSHUB: link to ABs no matter if created or processed
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			CHECK_AB=`ls | grep ab | head -1`
			if [ Q$CHECK_AB != Q ]
			then
				grep "BATCH_ID.*CALIB_$D" `ls | grep .ab` | wc -l | awk '{print $1}' > $TMP_DIR/result
				N_AB=`cat $TMP_DIR/result`
				if [ $N_AB -gt 0 ]
				then
					if [ $LAST_CALENTRY = "cal_Queued" ]
					then
						BGCOLOR="$OFFER_COLOR"
					elif [ $LAST_CALENTRY = "cal_Processed" ]
					then
						BGCOLOR="$DONE_COLOR"
					fi
					TEXT1="<a title=\"show AB status\" href=./status_$D.html><img src=$IMG_URL/index.gif width=15 height=15 border=0></a>"
					TEXT2="yes ($N_AB)"
				fi
			fi
		fi

		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR nowrap>$ACTION <!--${D}-->
        $TEXT1 <!--${D}-->
        $TEXT2</td><!--${D}-->
EOT

# ===================================================================================
# 3.13 certifyProducts and moveProducts (CALIB)
# ===================================================================================
	
		CERTIF_ENTRY_EXISTS=`grep "cal_Certif" $TMP_DIR/list_status`
		QC_ENTRY_EXISTS=`grep "cal_QC" $TMP_DIR/list_status`

		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT="&nbsp;"

# either: cal_QC last entry (for bulk proc.);
# or:     cal_QC exists and cal_Certif not yet done (for incremental proc.)
		if [ $THIS_IS_PHOENIX = NO ]
		then
			if  [ Q$LAST_ENTRY = "cal_QC" ] || ( [ "Q$QC_ENTRY_EXISTS" != "Q" ] && [ "Q$CERTIF_ENTRY_EXISTS" = Q ] )
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 03`
				TEXT="&nbsp;"
				if [ $D = $TODAY ]
				then
					ACTION=`callAction 09`
				fi
			elif [ "Q$CERTIF_ENTRY_EXISTS" != "Q" ]
			then
				BGCOLOR="$DONE_COLOR"
				ACTION=""
				TEXT="done"
			fi
		fi

		if [ $THIS_IS_OPSHUB = NO ]
		then
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT</td><!--${D}-->
EOT
		fi

# ===================================================================================
# 3.14 createAB (SCIENCE): disabled for QC1 (DFOS)
# ===================================================================================
	
		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT="&nbsp;"

		ENTRY_EXISTS=`grep "sci_AB" $TMP_DIR/list_status`
		if [ $THIS_IS_PHOENIX = YES ]
		then
			LAST_ENTRY=`grep   "sci_AB" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
		fi

# needed for OPSHUB (we could have valid CAL and SCI flags together)
		if [ $THIS_IS_OPSHUB = YES ]
		then
			LAST_SCIENTRY=`grep -v "^#" $TMP_DIR/list_status | grep -v raw_Complete | grep -v raw_Incomplete | grep sci_ | tail -1 | awk '{print $1}'`
			if [ Q$LAST_SCIENTRY = Q ]
			then
				LAST_SCIENTRY=UNKNOWN
			fi
		fi

# DFOS: nothing offered
		if [ $THIS_IS_DFOS = YES ]
		then
			if [ $SCI_EXISTS != 0 ] && ([ Q$LAST_ENTRY = "Qcal_Updated" ] || [ Q$LAST_ENTRY = "Qcal_Ingested" ])
			then
				TEXT="no creation"
			else
				TEXT="&nbsp;"
			fi
# PHOENIX:
		elif [ $THIS_IS_PHOENIX = YES ]
		then
			if ( [ Q$LAST_ENTRY = "Qcal_Updated" ]  \
			 ||  [ Q$LAST_ENTRY = "Qcal_Ingested" ] \
			 ||  [ Q$LAST_ENTRY = "Qsci_AB" ]       \
			 ||  [ Q$LAST_ENTRY = "Qsci_Queued" ]   \
			 ||  [ Q$LAST_ENTRY = "Qsci_QC" ] )
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 04`
				TEXT="&nbsp;"
# no CALIBs but SCIENCE
			elif [ $CAL_EXISTS = 0 ] && [ $SCI_EXISTS != 0 ]
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 04`
				TEXT="&nbsp;"
			elif [ "Q$ENTRY_EXISTS" != "Q" ]
			then
				BGCOLOR="$DONE_COLOR"
				ACTION=""
				TEXT="done"
			fi

		elif [ $THIS_IS_OPSHUB = YES ]
		then
			rm -f $TMP_DIR/result
			CHECK_AB=`ls | grep ab | head -1`
			if [ Q$CHECK_AB != Q ]
			then
				grep "BATCH_ID.*SCIENCE_$D" `ls | grep .ab` | wc -l | awk '{print $1}' > $TMP_DIR/result	
				N_AB=`cat $TMP_DIR/result`
				if [ $N_AB -gt 0 ]
				then
					if [ $LAST_SCIENTRY = "sci_Queued" ] 
					then
						BGCOLOR="$OFFER_COLOR"
						ACTION=""
						TEXT="waiting for processing"

# OPSHUB: sci_Processed set by opshub tool
					elif [ $LAST_SCIENTRY = "sci_Processed" ]
					then
						BGCOLOR="$DONE_COLOR"
						ACTION=""
						TEXT="done"
					fi
				fi
			fi
		fi

		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT</td><!--${D}-->
EOT
	
# ===================================================================================
# 3.15 existing ABs (SCIENCE)
# ===================================================================================
	
		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT1=""
		TEXT2="&nbsp;"

		if [ $THIS_IS_PHOENIX = YES ]
		then
			N_AB=`grep "DATE.*$D" $DFO_AB_DIR/*ab | sed "s/:DATE.*//" | wc -l | awk '{print $1}'`
			LAST_ENTRY=`grep "sci_" $TMP_DIR/list_status | awk '{print $1}' | tail -1`
		else
			D1="`qcdate $D +1`"
			N_AB=`grep "DATE.*$D" $DFO_AB_DIR/*${D}*ab $DFO_AB_DIR/*${D1}*ab 2>/dev/null | sed "s/:DATE.*//" | wc -l | awk '{print $1}'`
		fi

		if [ $THIS_IS_DFOS = YES ] || [ $THIS_IS_PHOENIX = YES ]
		then
			if [ Q$LAST_ENTRY = "Qsci_AB" ] || [ Q$LAST_ENTRY = "Qsci_Queued" ] || [ Q$LAST_ENTRY = "Qsci_QC" ] ||  [ Q$LAST_ENTRY = "Qsci_Certif" ]
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=""
				TEXT1="<a title=\"show AB status\" href=./status_$D.html><img src=$IMG_URL/index.gif width=15 height=15 border=0></a>"
				TEXT2="yes ($N_AB) &nbsp; &nbsp;"
			fi

# OPSHUB: link to ABs no matter if created or processed
		elif [ $THIS_IS_OPSHUB = YES ]
		then
			CHECK_AB=`ls | grep ab | head -1`
			if [ "Q$CHECK_AB" != "Q" ]
			then
				rm -f $TMP_DIR/result
				grep "BATCH_ID.*SCIENCE_$D" `ls | grep .ab` | wc -l | awk '{print $1}' > $TMP_DIR/result	
				N_AB=`cat $TMP_DIR/result`
				if [ $N_AB -gt 0 ]
				then
					if [ $LAST_SCIENTRY = "sci_Queued" ]
					then
						BGCOLOR="$OFFER_COLOR"
					elif [ $LAST_SCIENTRY = "sci_Processed" ]
					then
						BGCOLOR="$DONE_COLOR"
					fi
					TEXT1="<a title=\"show AB status\" href=./status_$D.html><img src=$IMG_URL/index.gif width=15 height=15 border=0></a>"
					TEXT2="yes ($N_AB)"
				fi
			fi
		fi

		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR nowrap>$ACTION <!--AB_STATUS1--><!--${D}-->
        $TEXT1 <!--${D}-->
        $TEXT2 </td><!--AB_STATUS2--><!--${D}-->
EOT
	
# ===================================================================================
# 3.16 moveProducts (SCIENCE) (OPSHUB: JOBS)
# ===================================================================================

		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT="&nbsp;"

		ENTRY_EXISTS=`egrep "sci_QC | sci_Certif|sci_Updated|sci_Finished" $TMP_DIR/list_status`
		if [ Q$LAST_ENTRY = "Qsci_QC" ]
		then
			if [ $THIS_IS_DFOS = YES ]
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 05`
			elif [ $THIS_IS_PHOENIX = YES ]
			then
				BGCOLOR="$EMPTY_COLOR"
			fi
		fi

		if [ $THIS_IS_OPSHUB = YES ]
		then
			if [ Q$LAST_ENTRY = "Qsci_Processed" ] || [ Q$LAST_ENTRY = "Qcal_Processed" ]
			then
				BGCOLOR="$DONE_COLOR"
				TEXT=done
			else
				BGCOLOR="$OFFER_COLOR"
				if [ -s $TMP_DIR/dm_ds_list_projects ]
				then
					TEXT1="&nbsp;"
					for PROJ in `cat $TMP_DIR/dm_ds_list_projects`
					do
						if [ -s $DFO_JOB_DIR/JOBS_$PROJ ]
						then
							TEXT1="$TEXT1 <a href=file://$DFO_JOB_DIR/JOBS_$PROJ>$PROJ</a> |"
						fi
					done
					TEXT=`echo "$TEXT1" | sed "s/|$//"`
				fi
			fi
		fi

		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT</td><!--${D}-->
EOT
	
# ===================================================================================
# 3.17 finishNight
# ===================================================================================

		ENTRY_EXISTS=`grep "finished" $TMP_DIR/list_status`
		HDR_EXISTS=`grep "hdr_Exist" $TMP_DIR/list_status | awk '{print $1}' | tail -1`

		BGCOLOR="$EMPTY_COLOR"
		ACTION=""
		TEXT="&nbsp;"

# DFOS: no SCIENCE, and CALIB updated or ingested: offer
		if [ $THIS_IS_DFOS = YES ]
		then
			#was:if [ $SCI_EXISTS = 0 ] && ( [ Q$LAST_ENTRY = Q"cal_Updated" ] || [ Q$LAST_ENTRY = Q"cal_Ingested" ] ) && [ "Q$ENTRY_EXISTS" = "Q" ]
			if ( [ Q$LAST_ENTRY = Q"cal_Updated" ] || [ Q$LAST_ENTRY = Q"cal_Ingested" ] ) && [ "Q$ENTRY_EXISTS" = "Q" ]
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 07`
				TEXT="&nbsp;"
	# SCIENCE, updated/ingested/verified: not offered anymore with v5.1
			#elif [ $SCI_EXISTS != 0 ] && \
	                # ( [ Q$LAST_ENTRY = Q"sci_Updated" ] || [ Q$LAST_ENTRY = Q"sci_Ingested" ] || [ Q$LAST_ENTRY = Q"sci_Verified" ] ) || \
			# ( [ Q$LAST_ENTRY = Q"sci_Finished" ] && [ $N_AB = 0 ] ) && [ "Q$ENTRY_EXISTS" = "Q" ]
			#then
			#	BGCOLOR="$OFFER_COLOR"
			#	ACTION=`callAction 07`
			#	TEXT="&nbsp;"
	# no hdr at all but not $TODAY: offer
			elif [ Q$HDR_EXISTS = Q ] && [ $D != $TODAY ]
			then
				BGCOLOR="$OFFER_COLOR"
				ACTION=`callAction 07`
				TEXT="&nbsp;"
	# already finished: don't offer (done)
			elif [ "Q$ENTRY_EXISTS" != "Q" ]
			then
				BGCOLOR="$DONE_COLOR"
				ACTION=""
				TEXT="finished"
			else
	# other cases: don't offer (too early)
				BGCOLOR="$EMPTY_COLOR"
				ACTION=""
				TEXT="&nbsp;"
			fi
		fi

# PHOENIX: never offer
		if [ $THIS_IS_PHOENIX = YES ]
		then
			BGCOLOR="$EMPTY_COLOR"
			ACTION=""
			TEXT="&nbsp;"
		fi

# OPSHUB: always offer
		if [ $THIS_IS_OPSHUB = YES ]
		then
			BGCOLOR="$OFFER_COLOR"
			ACTION=""
			TEXT="${OPSHUB_TOOL} -X|-M"
		fi

# construct
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
    <td BGCOLOR=$BGCOLOR><!--${D}-->
      $ACTION<!--${D}--><!--FILTER-->
      $TEXT</td><!--${D}-->
EOT
	
		echo "  </tr><!--${D}-->" >> $TMP_DIR/dfoMonitor.html
	done
fi
	
# ===================================================================================
# 4. Finish
# 4.1 add the dfos tool bar (internal version only)
# ===================================================================================
	
PROMPT1="you will be prompted for DATE"
PROMPT2="you will be prompted for DATE and MODE"

if [ $THIS_IS_DFOS = YES ]
then	
	cat >> $TMP_DIR/dfoMonitor.html<<EOT
  <tr>
  </tr>
	
  <tr class=dfos> 
    <td ROWSPAN=2 align=right><b>dfos</b></td> 
    <td ROWSPAN=2 COLSPAN=11 width=50><i><a title="provided by autoDaily in incremental mode">(autoDaily)</a></i></td>

    <td COLSPAN=2 nowrap> 
      <a class=dfos_item title="launch createAB ($PROMPT2)"  href=$DFO_GUI_DIR/launch_createAB.esh><!--FILTER-->
        createAB
    </a> <!--FILTER-->
        | 
      <a class=dfos_item title="launch createJob ($PROMPT2)" href=$DFO_GUI_DIR/launch_createJob.esh><!--FILTER-->
        createJob
    </a>  <!--FILTER-->
    </td> 

    <td>  
      <a class=dfos_item title="launch certifyProducts ($PROMPT2)" href=$DFO_GUI_DIR/launch_certifyProducts.esh> <!--FILTER-->
         certifyP
     </a> <!--FILTER-->
      | 
      <a class=dfos_item title="launch moveProducts ($PROMPT2)"    href=$DFO_GUI_DIR/launch_moveProducts.esh><!--FILTER-->
         moveP
      </a> <!--FILTER-->
    </td>  

    <td COLSPAN=2 nowrap> 
        <a class=dfos_item title="disabled in QC1">disabled in QC1</a> <!--FILTER-->
        <font size=2 color=${TITLE_COLOR}>createJob</font> <!--FILTER-->
    </td> 
    <td> 
        <font size=2 color=${TITLE_COLOR}>moveP</font> <!--FILTER-->
    </td> 
    <td>
      <a class=dfos_item title="launch finishNight ($PROMPT1)" href=$DFO_GUI_DIR/launch_finishNight.esh> <!--FILTER-->
         finishNight
       </a> <!--FILTER-->
     </td> 
  </tr> 
	
  <tr class=dfos> 
    <td COLSPAN=2 BGCOLOR=$EMPTY_COLOR><font size=2><i>(autoDaily)</i></font></td> 
    <td COLSPAN=2 BGCOLOR=$EMPTY_COLOR><font size=2><i>(interactive)</i></font></td>
    <td COLSPAN=3 BGCOLOR=$EMPTY_COLOR><font size=2>&nbsp;&nbsp;&nbsp;&nbsp;<font color=#999><a title="This is a DFOS installation" style="background-color:#CCCCFF;"><b>DFOS</b></a></font></font></td>
    <td BGCOLOR=$EMPTY_COLOR><font size=2><i>(interactive)</i></font></td> 
  </tr> 
EOT

	createGuiJob launch createAB 5
	createGuiJob launch createJob 5
	createGuiJob launch certifyProducts 5
	createGuiJob launch moveProducts 5
	createGuiJob launch finishNight 2

elif [ $THIS_IS_PHOENIX = YES ] 
then
	cat >> $TMP_DIR/dfoMonitor.html<<EOT
  <tr>
  </tr>
	
  <tr class=dfos> 
    <td><font size=2 color=${TITLE_COLOR}>dfos</font></td> 
    <td COLSPAN=11 width=50></td>
    <td COLSPAN=3 nowrap> </td> 
    <td COLSPAN=3 nowrap> </td> 
    <td> </td> 
  </tr> 
EOT

elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html<<EOT
  <tr>
  </tr>
	
  <tr class=dfos> 
    <td align=right ROWSPAN=2><b>OPSHUB</b></td> 
    <td ROWSPAN=2 COLSPAN=5 width=50>&nbsp;</td>
    <td ROWSPAN=2 COLSPAN=2 width=300 nowrap> <a class=dfos_item title="launch with '${OPSHUB_TOOL} -p <project>'">AB_METHOD: DOWNLOAD/CREATE_CASCADE/CALSELECTOR </a> </td> 
    <td COLSPAN=2 nowrap> <a class=dfos_item title="launch with '${OPSHUB_TOOL} -p <project>'">${OPSHUB_TOOL} -p &lt;project&gt; ...</a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class=dfos_item title="-C: create or download ABs">-C</a> </td> 
    <td> <a class=dfos_item title="-P: process">-P</a> </td>
    <td ROWSPAN=2> <a class=dfos_item title="-X: remove |-M: move&save">-X|-M</a> </td> 
  </tr> 
  <tr class=dfos><td COLSPAN=3><a class=dfos_item title="no param: -C&-P in one go">${OPSHUB_TOOL} -p &lt;project&gt; ...</a> </td>
  </tr>
EOT
fi

# ===================================================================================
# 4.2 add QC and other links
# ===================================================================================

LAST_ROWSPAN=3
if [ $THIS_IS_OPSHUB = YES ]
then
	LAST_ROWSPAN=2
fi

cat >> $TMP_DIR/dfoMonitor.html<<EOT
  <tr>
  </tr>

<!-- links -->
  <tr>
    <td ROWSPAN=$LAST_ROWSPAN align=right valign=top bgcolor=#EEEEEE><b><font color=#333333>Links:</font></b></td>
    <td COLSPAN=21 bgcolor=#EEEEEE> 
EOT

# QC and trending bars
rm -f $TMP_DIR/list_qcitems
grep "^QC1_URL" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}' > $TMP_DIR/list_qcitems

if [ -s $TMP_DIR/list_qcitems ]
then
	for ITEM in `cat $TMP_DIR/list_qcitems`
	do
		LABEL=`grep "^QC1_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $3}' | sed "s/_/ /g"`

		if [ "$LABEL" = "SINGLE VBAR" ]
		then
			echo "| " >> $TMP_DIR/dfoMonitor.html 
			continue
		fi

		if [ "$LABEL" = "DOUBLE VBAR" ]
		then
			echo "|| " >> $TMP_DIR/dfoMonitor.html 
			continue
		fi

		if [ "$LABEL" = "BREAK" ]
		then
			echo "<br>" >> $TMP_DIR/dfoMonitor.html 
			continue
		fi

		if [ "$LABEL" = "SPACE" ]
		then
			echo "&nbsp; " >> $TMP_DIR/dfoMonitor.html 
			continue
		fi

		TEXT_DISPLAY=`grep "^QC1_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $4}' | sed "s/_/ /g"`
		URL=`grep "^QC1_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $5}'`
	
		cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=qc_item title="$TEXT_DISPLAY" href=$URL>$LABEL</a> 
EOT
	done
else
	echo "      &nbsp; " >> $TMP_DIR/dfoMonitor.html
fi

echo "    </td>" >> $TMP_DIR/dfoMonitor.html
echo "  </tr>" >> $TMP_DIR/dfoMonitor.html

# second line
if [ $THIS_IS_OPSHUB = NO ]
then
	echo "  <tr>" >> $TMP_DIR/dfoMonitor.html
	echo "    <td COLSPAN=21 bgcolor=#EEEEEE> " >> $TMP_DIR/dfoMonitor.html
	rm -f $TMP_DIR/list_qcitems
	grep "^QC2_URL" $DFO_CONFIG_DIR/config.dfoMonitor | awk '{print $2}' > $TMP_DIR/list_qcitems
	if [ -s $TMP_DIR/list_qcitems ]
	then
		LAST_QCITEM=`cat $TMP_DIR/list_qcitems | tail -1`
		for ITEM in `cat $TMP_DIR/list_qcitems`
		do
			LABEL=`grep "^QC2_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $3}' | sed "s/_/ /g"`
			TEXT_DISPLAY=`grep "^QC2_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $4}' | sed "s/_/ /g"`
			URL=`grep "^QC2_URL" $DFO_CONFIG_DIR/config.dfoMonitor | grep $ITEM | awk '{print $5}'`
	
# this works only with $TODAY and ${TODAYminus90} defined above!
			cat > $TMP_DIR/expand <<EOT
TODAY=$TODAY
TODAYminus90=${TODAYminus90}
echo "$URL"
EOT
			chmod u+x $TMP_DIR/expand
			URL=`$TMP_DIR/expand`
	
			if [ $ITEM != $LAST_QCITEM ]
			then
				SEPARATION=" |"
			else
				SEPARATION=""
			fi
	
			cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <a class=qc_item title="$TEXT_DISPLAY" href=$URL>$LABEL</a> $SEPARATION 
EOT
		done
	else
		echo "      &nbsp; " >> $TMP_DIR/dfoMonitor.html
	fi
	
	echo "    </td>" >> $TMP_DIR/dfoMonitor.html
	echo "  </tr>" >> $TMP_DIR/dfoMonitor.html
fi
	
# ===================================================================================
# 4.3 MONITORS: links to histoMonitor, MUNIN etc.
# ===================================================================================

if [ -s $TMP_DIR/list_dates_hdr ] && [ $THIS_IS_DFOS = YES ]
then
	FIRST_DATE=`cat $TMP_DIR/list_dates_hdr | head -1`
	HISTO_DATE=`qcdate $FIRST_DATE -1`
	YYYY=`echo $HISTO_DATE | cut -c1-4`
	YYMM=`echo $HISTO_DATE | cut -c1-7`
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
  <tr VALIGN=TOP>
    <td COLSPAN=99 BGCOLOR=#EEEEEE align=left>

<!-- begin of link table -->
    <table style="border-collapse: collapse;" border="1" bordercolor="#FFFFFF">
     <tr bgcolor=#EEEEEE>
EOT

if [ -d $DFO_MON_DIR/FINISHED/$YYYY/$YYMM ] && [ $THIS_IS_DFOS = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href=${HISTO_DIR}/$YYYY/$YYMM/$YYMM.html title="histoMonitor: finished dates"><img src="${IMG_URL}/$HISTO_LOGO" border=0 width=24 height=30></a></td>
      <td width=50 valign=top><a href="${DFO_TREND_DIR}/reports/scoreManager/scoreManager.html" title="scoreManager" target=_blank><img src="${IMG_URL}/ok.gif" width=13 height=14 border=0><img src="${IMG_URL}/open.gif" width=13 height=17 border=0><img src="${IMG_URL}/arr1red-up.gif" width=13 height=14 border=0></a></td> <!--FILTER-->
EOT
elif [ $THIS_IS_DFOS = YES  ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href=${DFO_MON_DIR}/FINISHED/histoMonitor.html title="histoMonitor"><img src="${IMG_URL}/$HISTO_LOGO" border=0 width=24 height=30></a></td>
      <td width=50 valign=top><a href="${DFO_TREND_DIR}/reports/scoreManager/scoreManager.html" title="scoreManager" target=_blank><img src="${IMG_URL}/ok.gif" width=13 height=14 border=0><img src="${IMG_URL}/open.gif" width=13 height=17 border=0><img src="${IMG_URL}/arr1red-up.gif" width=13 height=14 border=0></a></td> <!--FILTER-->
EOT
elif [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href=${DFO_MON_DIR}/FINISHED/histoMonitor.html title="phoenixMonitor"><img src="${IMG_URL}/$HISTO_LOGO" border=0 width=24 height=30></a></td>
EOT
elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top>
       <table border=0 cellspacing=1 cellpadding=1>
         <tr><td ROWSPAN=2 valign=top>
           <table style="border-collapse: collapse;" border="0" bordercolor="#fff" cellpadding="0" cellspacing="0">
             <tr>
               <td bgcolor="${CAL_COL}" height="27" width="24" align="left" valign="bottom"><b><font color="#fff" size="1">
                 OCA</font></b>
               </td>
             </tr>
           </table>
         </td><td><font color="#eee" size=1>nix</font></td></tr>
	<tr><td><font color="#fff" size="1"><a href=${ALL_URL}/OCA/oca_rule_sets.html>OCA</a></font>
        </td></tr>
       </table>
      </td>
      <td width=50 valign=top>
       <table border=0 cellspacing=1 cellpadding=1>
         <tr><td ROWSPAN=2 valign=top>
           <table style="border-collapse: collapse;" border="0" bordercolor="#fff" cellpadding="0" cellspacing="0">
             <tr>
               <td bgcolor="${TITLE_COLOR}" height="27" width="24" align="left" valign="bottom"><b><font color="#fff" size="1">
                 OPS<br>HUB</font></b>
               </td>
             </tr>
           </table>
          </td>
           <td><font color="#eee" size=1>nix</font></td></tr>
           <tr><td><font size=1><a href=${DFO_LOG_DIR}/overview.html title="stored OPSHUB results for ${DFO_INSTRUMENT}">stored</a></font></td></tr>
       </table>
       </td>
      <td width=50 valign=top><a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/monitor/FINISHED/histoMonitor.html title="QC processing history for ${DFO_INSTRUMENT}"><img src="${IMG_URL}/$HISTO_LOGO" border=0 width=24 height=30></a></td>
EOT
fi

if [ $THIS_IS_DFOS = YES ]
then
	HISTO_LABEL="histoMonitor" ; SCORE_CELL="<th valign=top><b><font size=1>scoreManager</font></th> <!--FILTER-->" ; DFOS_LABEL="dfosExplorer"
elif [ $THIS_IS_PHOENIX = YES ]
then
	HISTO_LABEL="phoenixMonitor" ; SCORE_CELL="" ; DFOS_LABEL="dfosExplorer"
elif [ $THIS_IS_OPSHUB = YES ]
then
	HISTO_LABEL="QC<br> histo<br>Monitor" ; SCORE_CELL="" ; DFOS_LABEL="tool mainte<br>nance"
fi

if [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href="../dfosExplorer.html" title="complete and up-to-date installation: dfosExplorer" target=_blank><img src="${IMG_URL}/${DFOSEXP_LOGO}" width=38 height=30 border=0></a></td> <!--FILTER-->
      <td width=50 valign=top>
       <table border=0 cellspacing=1 cellpadding=1>
         <tr><td ROWSPAN=2 valign=top>
           <table style="border-collapse: collapse;" border="0" bordercolor="#fff" cellpadding="0" cellspacing="0">
             <tr>
               <td bgcolor="${TITLE_COLOR}" height="27" width="24" align="left" valign="bottom"><b><font color="#fff" size="1">
                 OPS<br>HUB</font></b>
               </td>
             </tr>
           </table>
          </td>
           <td><font color="#eee" size=1>nix</font></td></tr>
           <tr><td><font size=1><a href="../../README_INSTALL" title="account installation" target=_blank>$USER</a></font></td></tr></td> <!--FILTER--> 
       </table>
       </td>
      <td width=50 valign=top align=center>
       <table border=0 cellspacing=1 cellpadding=1>
         <tr><td ROWSPAN=2 valign=top>
           <table style="border-collapse: collapse;" border="0" bordercolor="#fff" cellpadding="0" cellspacing="0">
             <tr>
               <td bgcolor="${TITLE_COLOR}" height="27" width="24" align="left" valign="bottom"><b><font color="#fff" size="1">
                 OPS<br>HUB</font></b>
               </td>
             </tr>
           </table>
          </td>
           <td><font color="#eee" size=1>nix</font></td></tr>
           <tr><td><font size=1><a href="${OPSHUB_WIKI_URL}" title="OPSHUB wiki [ESO internal]" target=_blank><font size=1>wiki</font></a></td> <!--FILTER-->
       </table>
       </td>
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href="dfosExplorer.html" title="tool maintenance: dfosExplorer" target=_blank><img src="${IMG_URL}/${DFOSEXP_LOGO}" width=38 height=30 border=0></a></td> <!--FILTER-->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=50 valign=top><a href="${DYNAMIT_URL}" title="SciOps checklists" target=_blank><img src="${IMG_URL}/${SCIOPS_LOGO}" width=38 height=30 border=0></a></td> <!--FILTER-->
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=100 valign=top>
       <table border=0 cellspacing=1 cellpadding=1>
        <tr><td ROWSPAN=2><a href="${WISQ_URL}/HEALTH/trend_report_dataflow_HC.html" title="WISQ monitor " target=_blank><img src="${IMG_URL}/wisq_icon.png" width=38 height=25 border=0></a></td>
          <td><a href="${WISQ_URL}/FULL/trend_report_raw_${DFO_INSTRUMENT}_HC.html" target=_blank title="WISQ ${DFO_INSTRUMENT}: processed raw data volume"><font size=1>raw</font></a></td>
          <td><a href="${WISQ_URL}/FULL/trend_report_AB_${DFO_INSTRUMENT}_HC.html" target=_blank title="WISQ ${DFO_INSTRUMENT}: number of ABs and execution times"><font size=1>AB</font></a></td>
        </tr>
        <tr>
          <td><a href="${WISQ_URL}/FULL/trend_report_products_${DFO_INSTRUMENT}_HC.html" target=_blank title="WISQ ${DFO_INSTRUMENT}: product volume"><font size=1>prod</font></a></td>
          <td><a href="${WISQ_URL}/HEALTH/trend_report_exectime_${DFO_INSTRUMENT}_HC.html" target=_blank title="WISQ ${DFO_INSTRUMENT}: execution times"><font size=1>exec</font></a></td>
        </tr>
      </table>
      </td>

      <td bgcolor=#FFFFFF width=10><font size=1>&nbsp;</td> 
      <td width=100 valign=top nowrap><a href="${GANGLIA_INDEX}" title="SOS performance monitor $HOSTNAME" target=_blank><img src="${GANGLIA_IMG}" height=30 border=0 align=top target=_blank></a> <font size=1><a href="$GANGLIA_WIKI_URL" title="what's this?" target=_blank>[?]</a></font></td> 
      <td width=100 valign=top nowrap><a href="${MUNIN_INDEX}" title="SOS performance trending $HOSTNAME" target=_blank><img src="${MUNIN_LOGO}" height=20 border=0 align=top target=_blank></a> <font size=1><a href="$MUNIN_WIKI_URL" title="what's this?" target=_blank>[?]</a></font></td> <!--FILTER-->
      <td width=70 valign=top nowrap><a href="${CONDOR_CONFIG}" title="CONDOR config file" target=_blank><img src="${IMG_URL}/CondorTitle.gif" width=40 border=0 align=top target=_blank></a> <font size=1><a href="$CONDOR_WIKI_URL" title="what's this?" target=_blank>[?]</a></font></td>
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
      <td width=60 valign=top nowrap><a href="${CONDOR_CONFIG}" title="CONDOR config file" target=_blank><img src="${IMG_URL}/CondorTitle.gif" width=40 border=0 align=top target=_blank></a> <font size=1><a href="$CONDOR_WIKI_URL" title="what's this?" target=_blank>[?]</a></font></td>
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
     </tr>

     <tr>
EOT

if [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
<th valign=top><b><font size=1><a title="all available association rules">OCA rules</a></font></th>
<th valign=top><b><font size=1><a title="stored files for OPSHUB projects">stored results</a></font></th>
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
       <th valign=top><b><font size=1>$HISTO_LABEL</font></th>
$SCORE_CELL
       <th valign=top><b><font size=1>$DFOS_LABEL</font></th> <!--FILTER-->
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
       <th valign=top><b><font size=1>Dynamit</font></th> <!--FILTER-->
       <th valign=top><b><font size=1>WISQ</font></th>
       <th valign=top bgcolor=#FFFFFF><b><font size=1>&nbsp;</font></th> 
       <th valign=top><b><font size=1>system monitor 1</font></th> 
       <th valign=top><b><font size=1>system monitor 2</font></th> <!--FILTER-->
       <th valign=top><b><font size=1>condor</font></th>
     </tr>
   </table>
<!-- end of link table -->
EOT
else
	cat >> $TMP_DIR/dfoMonitor.html <<EOT
       <th valign=top><b><font size=1>account mainte<br>nance</font></th> <!--FILTER-->
       <th valign=top><b><font size=1>opshub wiki</font></th> <!--FILTER-->
       <th valign=top><b><font size=1>Dynamit</font></th> <!--FILTER-->
       <th valign=top><b><font size=1>condor</font></th>
     </tr>
   </table>
<!-- end of link table -->
EOT
fi

cat >> $TMP_DIR/dfoMonitor.html <<EOT
  </td>
 </tr>
EOT

# ===================================================================================
# 4.4 GANGLIA monitors: not for OPSHUB
# ===================================================================================

HTML=dfoMonitor.html

if [ $THIS_IS_OPSHUB != YES ]
then
	cat >> $TMP_DIR/$HTML <<EOT

<!-- "System" row-->
 <tr>
   <td bgcolor=#EEEEEE  valign=top align=right><b>System:</b></td>
   <td COLSPAN=99 bgcolor=#EEEEEE>

<!-- begin of GANGLIA table-->
<table cellpadding=0 cellspacing=1>
  <tr valign=top>
    <td><a href="${GANGLIA_HTML1}" target=_blank><img src="${GANGLIA_IMG1}" title="complete graph set under ${GANGLIA_HOME}" border=0 width=300></a></td><!--GANGLIA_MON--> 
    <td><a href="${GANGLIA_HTML2}" target=_blank><img src="${GANGLIA_IMG2}" title="complete graph set under ${GANGLIA_HOME}" border=0 width=300></a></td><!--GANGLIA_MON--> 
    <td><a href="${GANGLIA_HTML4}" target=_blank><img src="${GANGLIA_IMG4}" title="complete graph set under ${GANGLIA_HOME}" border=0 width=278></a></td><!--GANGLIA_MON-->
    <td><a href="${GANGLIA_HTML3}" target=_blank><img src="${GANGLIA_IMG3}" title="complete graph set under ${GANGLIA_HOME}" border=0 width=300></a></td><!--GANGLIA_MON-->
EOT

	if [ $THIS_IS_PHOENIX = NO ]
	then
		cat >> $TMP_DIR/$HTML <<EOT
    <td valign=middle align=middle><a href="${BANDWIDTH_URL1}"><img src="${BANDWIDTH_GRAPH}" title="click to enlarge" border=0 width=300></a><br><font size="1">EVALSO PAR-&gt;VIT  <a href="${BANDWIDTH_URL1}" title="first link">#1</a> | <a href="${BANDWIDTH_URL2}" title="second link">#2</a> | <a href="${BANDWIDTH_URL3}" title="Evalso VIT-&gt;GAR">Evalso</a></font></td><!--GANGLIA_MON-->
EOT
	fi

	cat >> $TMP_DIR/$HTML <<EOT
  </tr>
</table>
<!-- end of GANGLIA table-->

   </td>
 </tr>
EOT
fi

# ===================================================================================
# 4.5 add last line
# ===================================================================================

SYMB="<img src=${IMG_URL}/pie0.gif height=11 width=10 title=\"configured default: cluster monitor excluded\">" 
 
cat >> $TMP_DIR/$HTML <<EOT

<!-- last line-->
  <tr BGCOLOR=#CCCCCC>
    <td nowrap></td>
    <td ALIGN=left COLSPAN=3 nowrap>
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	for FRQ in `cat $OTMP_DIR/ganglia | awk '{print $1}'`
	do
		if [ $GANGLIA_FREQ = $FRQ ]
		then	
			STYLE="class=\"nav-subitem\""
		else
			STYLE=""
		fi

		TAG=`grep   $FRQ $OTMP_DIR/ganglia | awk '{print $3}'`
		SIZE=`grep  $FRQ $OTMP_DIR/ganglia | awk '{print $4}'`

		cat >> $TMP_DIR/$HTML <<EOT
   <a $STYLE href=$DFO_GUI_DIR/change_${FRQ}.esh title="GANGLIA displays last $FRQ"><font size=$SIZE color=#666666>$TAG</a> <!--FILTER--> 
EOT
	done
fi

if [ $THIS_IS_PHOENIX = YES ]
then
	SUPPORTING="PHOENIX"
elif [ $THIS_IS_DFOS = YES ]
then
	SUPPORTING="DFOS"
elif [ $THIS_IS_OPSHUB = YES ]
then
	SUPPORTING="OPSHUB"
fi

cat >> $TMP_DIR/$HTML <<EOT
    </td>

    <td ALIGN=RIGHT COLSPAN=18> 
      <font COLOR=#FFF> created by dfoMonitor v${TOOL_VERSION}, a <b>dfos</b> tool; supporting <font color=#999><a title="This is a $SUPPORTING installation" style="background-color:#CCCCFF;"><b>$SUPPORTING</b></font>
    </td>
  </tr>

<!-- end of table -->
</table>
</body>
</html>
EOT

mv $TMP_DIR/$HTML $DFO_MON_DIR/

# ===================================================================================
# 5. scp output (only in DFOS mode)
# 5.1 scp disk_space_monitor to WISQ web site
# ===================================================================================

case $THIS_IS_PHOENIX in
 "NO"  ) XDM_NAME=disk_space_monitor_${DFO_INSTRUMENT}.html ;;
 "YES" ) XDM_NAME=disk_space_monitor_${DFO_INSTRUMENT}_PHOENIX.html ;;
esac

if [ ${DFO_INSTRUMENT} = MUSE ] && [ $THIS_IS_PHOENIX = YES ]
then
	XDM_NAME=disk_space_monitor_${DFO_INSTRUMENT}_PHOENIX_`whoami`.html
fi

echo "      <table cellpadding=0 cellspacing=1 border=0>"     > $TMP_DIR/$XDM_NAME
grep -v "<!\-\-FILTER\-\->" $TMP_DIR/disk_space_monitor.html >> $TMP_DIR/$XDM_NAME
echo "</table>" 				   	     >> $TMP_DIR/$XDM_NAME

if [ $SCP_YN = YES ]
then
	scp -o BatchMode=yes $TMP_DIR/$XDM_NAME ${DFO_WEB_SERVER}:${WISQ_DIR}    1>/dev/null
fi

mv $TMP_DIR/$XDM_NAME $DFO_MON_DIR/disk_space_monitor.html

# no export in PHOENIX or OPSHUB modes
if [ $THIS_IS_DFOS = NO ] 
then
	if [ $OTMP_DIR != $TMP_DIR ]
	then
		rm -rf $TMP_DIR
	fi

	BROWSER_ACTIVE=`ps -wfC $DFO_BROWSER | grep -v CMD| awk '{print $1}' | grep $USER | sort -u`
	if [ "Q$BROWSER_ACTIVE" != "Q" ] && [ $SUPPRESS_CALL = NO ]
	then
		$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html &
	fi
	exit
fi

# ===================================================================================
# 5.2 scp output to $DFO_WEB_SERVER
# ===================================================================================

if [ $QUIET = NO ] && [ $SCP_YN = YES ]
then
	echo "  -scp public version to $DFO_WEB_SERVER ..."
fi

# clean versions for export and scp	
if [ Q$DFO_WEB_SERVER != Q ] && [ $SCP_YN = YES ]
then
	rm -f $TMP_DIR/dfoMonitor.html

	ssh -o BatchMode=yes $DFO_WEB_SERVER "$QCBIN_DIR/webDirCreate MONITOR ${DFO_INSTRUMENT}" 1>/dev/null

	sed -e "s|${REPORT_DIR}|${REPORT_DIR2}|g" $DFO_MON_DIR/dfoMonitor.html |\
	 sed -e "s|${HISTO_DIR}|${HISTO_DIR2}|g" |\
	 sed -e "/show AB status/s|./status.*html|${LOGS_DIR}/&&|g" | sed "/show AB status/s|status_||" | sed "/show AB status/s|.html.||" |\
	 sed "s/local histoMonitor/external histoMonitor/g" |\
	 sed "s/view local data report/view external data report/" |\
      	 sed "s/<!--EXTERNAL-->/<p><b>This is the external version, with restricted functionality.<\/b>/" |\
	 sed "s/; click for the log//"g |\
	 grep -v "<!\-\-FILTER\-\->" > $TMP_DIR/dfoMonitor.html
	scp -o BatchMode=yes $TMP_DIR/dfoMonitor.html  ${DFO_WEB_SERVER}:${INSTR_DIR}/monitor/ 1>/dev/null 
fi

# ===================================================================================
# 5.3 Clean up $DFO_GUI_DIR
# remove all files with date tags outside of current date list
# ===================================================================================

if [ -s $TMP_DIR/list_dates_hdr ]
then
	if [ $QUIET = NO ]
	then
		echo "  -cleaning up \$DFO_GUI_DIR ..."
	fi
	rm -f $TMP_DIR/df_all_datefiles
	ls $DFO_GUI_DIR/*20* 2>/dev/null > $TMP_DIR/df_all_datefiles
	if [ -s $TMP_DIR/df_all_datefiles ]
	then
		for D in `cat $TMP_DIR/list_dates_hdr`
		do
			sed -i -e "/$D/d" $TMP_DIR/df_all_datefiles
		done
	fi

	if [ -s $TMP_DIR/df_all_datefiles ]
	then
		for F in `cat $TMP_DIR/df_all_datefiles`
		do
			rm $F
		done
	fi
fi

# ===================================================================================
# 5.4 Call browser and refresh
# ===================================================================================

BROWSER_ACTIVE=`ps -wfC $DFO_BROWSER | grep -v CMD| awk '{print $1}' | grep $USER | sort -u`
if [ "Q$BROWSER_ACTIVE" != "Q" ] && [ $SUPPRESS_CALL = NO ]
then
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfoMonitor.html &
fi

# ===================================================================================
# 5.5 End
# ===================================================================================
if [ $OTMP_DIR != $TMP_DIR ]
then
	rm -rf $TMP_DIR
fi

exit
