#!/bin/sh
# PURPOSE:	scoreHC: score an HC report
# AUTHOR:	Reinhard Hanuschik / ESO
# VERSIONS:	1.0 -- created (April 2008)
#		1.0.1- select distinct in line 291; SCORE_COND evaluated for SEF and MEF case (2008-10-22)
#		1.1 -- improved handling of several error case; "last N days up to ..."; red triangles; handling of MULTIPLE datasets improved; ssh/scp with BatchMode (2008-12-02)
# 		1.1.1- local scoreManager improved: stat_grp files linked (2009-01-08)
#		1.2 -- handling of MULTIPLE data sets improved (2009-01-15)
#		1.3 -- enabled for parallel trendPlotter execution (2009-02-17)
#		1.3.1- bug fixed with launch_remove files (2009-03-23)
#		1.3.2- SCORE_DEPTH now 7 (to align with calChecker) (2009-06-18)
#		1.3.3- create stat_grp files from scratch if non-existing [BWo] (2009-06-18)
#		1.3.4- minor changes in tutorial text (2009-10-12)
#		1.3.5- for DATA_TRANSFER=DISK: SCORE_DEPTH configurable; l1251: bug fixed with TMODE (2009-10-26)
#		1.3.6- bug fixed with exported score overview [BWo] (2009-12-10)
#		1.3.7- direct transfer to w1/w2; qcWebcp not used anymore (2009-12-15)
#		1.4 -- product comments visible; dates visible on mouse over, symbols linked to score report (2010-02-02)
#		1.4.1- bug fix for openWin (line 985) (2010-02-04)
#		1.4.2- CONFIG_URL modified (2010-03-18)
#		1.5 -- edit_tp renamed into edit_trendP; consistency checks on scoreManager; link to config files; safweb1 replaced by qcweb (2010-04-26)
#		1.6 -- score sniplets for tooltips; stabilized wget (timeout) (2010-10-06)
#		1.7 -- scoreManager: override function, plus improved workflow for correction; consistency checks between trendPlotter thresholds and config.scoreQC [BWo]
#		1.7.1- small changes to go with trendPlotter 2.7.3+ (2011-07-07)
#		1.7.2- bug fixes with PARSET definition after CHECK_NUMBER [BWo] (2011-08-29)
#		1.7.3- bug fix in line 947 (2011-11-30)
#		1.8  - include the comments in score_overview.html; support <ins>_POSTIT (2011-12-05)
#		2.0  - creates new PHP interface (2012-10-08)
#		2.1 -  makes HC editor comments link visible; createInsScoreOverview (score_overview pages), local navbar for scoreManager (2012-12-04)
#		2.1.1- attempt to stabilize .lock mechanism (2013-01-14)
#		2.2 -- stargate1 replaced by $DFO_WEB_SERVER; stabilization against all red scores; lines 1998ff removed (2013-10-30)
#		2.2.1- bug fixes (2013-12-02)
#		2.2.2- modified TEL/INS for score_overview_7.html and score_overview_30.html (2014-01-02)
#		2.2.3- replace 'switch' by 'change' in awk commands (for OS upgrade) (2014-02-19)
#		2.2.4- fixed bug with missing closing 'title' tag (2014-03-10)
#		2.2.5- fixed bug in line 1401 (2014-04-22)
#		2.2.6- DATA_TRANSFER removed; SCORE_DEPTH hard-coded; modified TEL/INS for score_overview_7.html and score_overview_30.html (2014-09-25)
#		2.2.7- IMG_SRC1 replaced by IMG_URL (2015-02-24)
#		2.2.8- ConnectTimeOut removed (2015-03-23)
#		2.2.9- SPHERE added to configuration (2015-03-25)
#		2.3 -- consistency checks for HC_PLOT configuration of scoreQC on scoreManager (2015-04-13)
#		2.4 -- fixes issue with multiple config.scoreQC files (2015-07-01)
#		2.4.1- PIONIER added to configuration (2015-10-13)
#		2.4.2- external config file for TEL+INS, used for overview -O (2015-10-27)
#		2.4.3- bug fix for exported $TMP_DIR/score_overview_core.html (2015-11-12)
#		2.4.4- added comment "(certified nights only)" to HTML output (2016-01-10)
#		2.5 -- added completeness check for JOBS_TREND (2018-02-16)
#		2.5.1- improved handling of 'Quality comments' table (2020-02-10)
#		2.5.2- bug fix in SCORE_OVERVIEW (2020-10-27)
#		2.5.3- change of CREATE_OVERVIEW_USER (2020-11-25)
#		
# PARAMETERS:	-r <report_name>
# CONFIG:	reads $DFO_CONFIG_DIR/webNavBar/config.navbar_HC_${DFO_INSTRUMENT}
#		      $DFO_CONFIG_DIR/trendPlotter/config.trendPlotter
#		      $DFO_CONFIG_DIR/config.scoreQC or other config files for scoreQC (if $MULTI_CONFIG=YES)
#		      $DFO_CONFIG_DIR/config.calChecker (for $PSO_EMAIL)
#		      optional $DFO_CONFIG_DIR/config.scoreHC (only for account $CREATE_OVERVIEW_USER that runs 'scoreHC -O'!)
# TOOLS CALLED:	none
# OUTPUT: 	HTML file with score results, to be embedded in QUICK-LOOK report;
#		scoreManager.html (local)
# NOTES:	If multi scoreQC config files are used, and a HC report has plots for different RAW_TYPEs, it is highly
#		recommended to specify the section 4.1A MULTI_PLOT in config.scoreHC, otherwise there is the risk of 
#		inconsistencies in the displayed results on the QUICK_LOOK page.
#		Shiftleader's page: the last-7 and last-30 day files are created by a cronjob 'scoreHC -O' run by CREATE_OVERVIEW_USER.
#		By evolution, column #3 of config.scoreQC (HC_TAG) has become obsolete but there is no easy way to remove it.
# ==========================================================================================================================================
TOOL_VERSION="2.5.3"
TREND_TOOL=trendPlotter	
CONFIG_URL="http://qcweb/~qc/tqs/config_tp/"
CREATE_OVERVIEW_USER="kmos"	# hard-coded, can be changed to the QC user running the daily cronjob 'scoreHC -O'
				# producing the score overview pages score_overview_7.html and score_overview_30.html
				# under http://www.eso.org/observing/dfo/quality/ALL/common/
				# this is also the user who maintains the config.scoreHC file

# ==========================================================================
# 0. Initialize
# 0.1 define hard-coded configuration
# ==========================================================================

CELLWIDTH=120	#width of report score cells
WIDTH=500
REFRESH=60	#score_overview.html: browser to refresh every 60 sec

CONFIG_VNAVBAR="config.navbar_HC_${DFO_INSTRUMENT}"
if [ ! -s $DFO_CONFIG_DIR/webNavBar/$CONFIG_VNAVBAR ]
then
	echo "***ERROR: no $DFO_CONFIG_DIR/webNavBar/$CONFIG_VNAVBAR found. A file with this name is required for this tool to work properly."
	exit
fi

REPORT_DIR=$DFO_TREND_DIR/reports
CONFIG_DIR=$DFO_CONFIG_DIR/trendPlotter
TP_CONFIG="$CONFIG_DIR/config.trendPlotter"

IMG_SRC="/observing/dfo/quality/images"		#source for general images (ball.gif)
IMG_SRC1="http://www.eso.org/qc/ALL/img" 	#source for score squares
 
IMG_URL="/observing/dfo/quality/ALL/img"	#source for QC images (scores)

ALL_URL="http://www.eso.org/qc/ALL/" 		#source URL for QC pages
STYLE_URL="${ALL_URL}/common/qc_eso.style" 	#URL for qc style sheet

HELP_URL=`grep "^HELP_URL"	$TP_CONFIG | awk '{print $2}'`
QC_URL=`grep "^QC_URL"		$TP_CONFIG | awk '{print $2}'`
VNAVBAR_URL=`echo $QC_URL | sed "s/reports/common/"`
WEB_SERVER_URL="http://qcweb.hq.eso.org/~qc"	# QC web server
DFOS_URL="http://www.eso.org/~qc/dfos/images"	# for refresh.png etc.

QC_DIR=`grep "^QC_DIR"		$TP_CONFIG | awk '{print $2}'`

NAVBAR_DIR=`echo $QC_DIR   | sed "s/reports/common/"`
SCORE_TT_DIR=`echo $QC_DIR | sed "s/reports/scores/"`

ALERT_IMG=arr1red-up.gif	#to have different shape for alerts and OKs
OK_IMG=sq2.png
NULL_IMG=sqg.png

SCORE_DEPTH=7

# colors
SCORE_TAB_COL=#9BDEFF	# color for result table
NEWS_COL=#FFFF99  	# light-yellow
HC_COLOR=#006A9D	# eso color

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

OTMP_DIR=$TMP_DIR	#original TMP_DIR (for launch score jobs)
CREATE_PHP=NO
INTERFACE_ONLY=NO
CREATE_OVERVIEW=NO	#hidden option

while getopts r:T:IOPhv OPTION
do
	case "$OPTION" in
	 v ) echo $TOOL_VERSION
	     exit 0 ;;
	 h ) cat $DFO_DOC_DIR/scoreHC.h
	     exit 0 ;;
	 I ) INTERFACE_ONLY=YES ;;
	 O ) CREATE_OVERVIEW=YES ;;
	 P ) CREATE_PHP=YES;;
	 r ) REPORT_NAME=$OPTARG ;;
	 T ) JTMP_DIR=$OPTARG ;;
	 * ) cat $DFO_DOC_DIR/scoreHC.h
	     exit 0 ;;
	esac
done

if [ $# != 2 ] &&  [ $# != 4 ] && [ $CREATE_PHP = NO ] && [ $INTERFACE_ONLY = NO ] && [ $CREATE_OVERVIEW = NO ]
then
	cat $DFO_DOC_DIR/scoreHC.h
	exit
fi

MSG=${REPORT_NAME}.msg

CFG=config.tp_${REPORT_NAME}
if [ ! -s $CONFIG_DIR/$CFG ] && [ $CREATE_PHP = NO ] && [ $INTERFACE_ONLY = NO ] && [ $CREATE_OVERVIEW = NO ]
then
	echo "***ERROR: no file $CONFIG_DIR/$CFG found. Exit!"
	exit -1
fi

if [ $CREATE_PHP = NO ] && [ $INTERFACE_ONLY = NO ] && [ $CREATE_OVERVIEW = NO ]
then
	REPORT_TYPE=`grep "^REPORT_NAME"	$CONFIG_DIR/$CFG | awk '{print $3}'`
	TMODE=`grep "^REPORT_NAME"		$CONFIG_DIR/$CFG | awk '{print $4}'`
	if [ $TMODE != FULL ]
	then
		TMODE=HEALTH
	fi

	REPORT_GROUP=`grep "^REPORT_GROUP" 	$CONFIG_DIR/$CFG | awk '{print $2}'`
	ADD_TEXT=`grep "^ADD_TEXT"		$CONFIG_DIR/$CFG | sed "s/&&$//" | sed "s/^.*&&//"`
	if [ "Q$REPORT_GROUP" = "Q" ]
	then
		REPORT_GROUP=none
	fi

# check $TMP_DIR, could be exported/non-std one from trendPlotter
	if [ Q$JTMP_DIR = Q ]
	then
		export TMP_DIR=$TMP_DIR/$$
		mkdir $TMP_DIR
		EXTERNAL=NO
	else
		export TMP_DIR=$JTMP_DIR
		EXTERNAL=YES
	fi

	if [ ! -d $TMP_DIR ]
	then
		mkdir $TMP_DIR
	fi

	if [ ! -d $TMP_DIR/${REPORT_NAME} ]
	then
		mkdir $TMP_DIR/${REPORT_NAME}
	fi

	if [ ! -d $OTMP_DIR/${REPORT_NAME} ]
	then
		mkdir $OTMP_DIR/${REPORT_NAME}
	fi
fi

# ==========================================================================
# 0.4 procedure fillReport: evaluates the scores, prepares score HTML output
# ==========================================================================

rm -f $TMP_DIR/sc_HC_table
rm -f $TMP_DIR/scoreManager.html

fillReport(){
if [ "${REPORT_TYPE}" != "CUSTOM" ]
then
        FDF="$DFO_BIN_DIR/$TREND_TOOL"
else
        FDF="$CONFIG_DIR/${REPORT_NAME}.fdf"
        if [ ! -s $FDF ]
        then
                echo "***ERROR: no user-provided format definition file $FDF found. Exit."
		if [ $EXTERNAL = NO ]
		then
			rm -r $TMP_DIR
		fi
                exit -1
        fi
fi

LAST_ROW=`grep "^#HTML_REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print $4}' | sort -u | tail -1`
if [ Q$LAST_ROW = Q ]
then
	echo "***WARNING: no section '#HTML_REPORT_TYPE' found in $FDF. Display might be incomplete."
fi

# prod_comments
if [ -s $TMP_DIR/sc_score_query ]
then
	echo "  Query prod_comments ..."

	cat > $TMP_DIR/prod_comments_query <<EOT
SELECT 
	ab_name ,
	comment
FROM
	qc1..prod_comments
WHERE
	ab_name in (
EOT
	cat $TMP_DIR/sc_score_results | awk '{print "\""$13".ab\" ,"}' | sed "/^\".ab/d" | sort -u | sed "$,$ s/,//" >> $TMP_DIR/prod_comments_query
	cat >> $TMP_DIR/prod_comments_query <<EOT
)
AND
	visible = "Y"
GO
EOT

	rm -f $TMP_DIR/prod_comments $TMP_DIR/score_comments

	isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i$TMP_DIR/prod_comments_query |\
  grep -v "ab_name" |\
  grep -v "\-\-\-" |\
  grep -v affected > $TMP_DIR/prod_comments
else
	echo "  No prod_comments query."
fi

if [ -s $TMP_DIR/prod_comments ]
then
	for AB in `cat $TMP_DIR/sc_score_results | awk '{print $13}' | sort -u`
	do
		PROD_COMM=`grep $AB $TMP_DIR/prod_comments | sed "s/$AB.ab *//" | sed "s/ /_/"g | sed "s/__*/_/g" | sed "s/^_//" | sed "s/_$//" | sed "s/^.*/(&)/"`
		if [ "Q$PROD_COMM" != "Q()" ] && [ "Q$PROD_COMM" != "Q" ]
		then
			grep $AB $TMP_DIR/sc_score_results | awk '{print $2,$6,$13".ab",$7,comm}' comm="$PROD_COMM" >> $TMP_DIR/score_comments
		fi
	done
fi

# short labels and tool tips
COMPUTED="<a title=\\\"plot values have been modified by computation, cannot be scored\\\">computed</a>"
MULTIPLE="<a title=\\\"multiple data set, cannot be scored\\\">multiple dataset</a>"
EMPTY="<a title=\\\"no current data available\\\">no current data</a>"
NOT="<a title=\\\"scoring not implemented (e.g. multiple dataset, or too complicated)\\\">not implemented</a>"

for ROW in `grep "^#HTML_REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print $4}' | sort -u`
do
	for PN in `grep "^#HTML_REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | grep $ROW | awk '{print $3}'`
	do
		TOOLTIP_INDEX="<!-- INDEX_${ROW}_${PN} -->"
		PSEUDO_ID="`qcdate ${TODAY}`.$$"
		CHECK_EXIST=`grep "^Plot $PN " $TMP_DIR/sc_score_results`
		if [ "Q$CHECK_EXIST" = Q ]
		then
			echo "<td width=$CELLWIDTH> </td>" >> $TMP_DIR/sc_HC_table
			continue
		fi

		rm -f $TMP_DIR/sc_make_scorebox
		cat > $TMP_DIR/sc_make_scorebox <<EOT
CHECK_EMPTY=\`grep "^Plot $PN " $TMP_DIR/sc_score_results | egrep "EMPTY|IMPLEMENTED"\`
if [ "Q\$CHECK_EMPTY" = "Q" ]
then
	N_VALUE=\`grep "^Plot $PN " $TMP_DIR/sc_score_results | awk '{print NR}' | tail -1\`
fi
	
echo "        <td width=$CELLWIDTH nowrap valign=top>
                <font size=2><b>" >> $TMP_DIR/sc_HC_table
# only last score evaluated for plot score
grep "^Plot $PN " $TMP_DIR/sc_score_results | tail -1 | awk '{{print \$2":</b>",\$12,"<br>"}}' >> $TMP_DIR/sc_HC_table
grep "^Plot $PN " $TMP_DIR/sc_score_results | tail -1 |\
 awk '{if (\$3 == "MULTIPLE") {print "<font color=#666666 size=1><b>${MULTIPLE}"} 
  else if (\$3 == "COMPUTED") {print "<font color=#666666 size=1><b>${COMPUTED}"} 
  else if (\$3 == "EMPTY")    {print "<font color=#666666 size=1><b>${EMPTY}"} 
  else if (\$3 == "NOT")      {print "<font color=#666666 size=1><b>${NOT}"} 
  else if (\$7 > 0)           {print "<a title=\"plot score: last one NOK\"><img src=${IMG_SRC1}/${ALERT_IMG}?${PSEUDO_ID} width=10 height=10 border=0></a> <font size=1><a title=\"number of scores\">"nvalue"</a>|"} 
  else                        {print "<a title=\"plot score: last one OK\"><img src=${IMG_SRC1}/${OK_IMG}?${PSEUDO_ID} width=10 height=10 border=0></a> <font size=1><a title=\"number of scores\">"nvalue"</a>|"}}' nvalue=\${N_VALUE} |\
 awk '{ print \$0,index_tt}' index_tt="$TOOLTIP_INDEX" \
 >> $TMP_DIR/sc_HC_table

# details
grep "^Plot $PN " $TMP_DIR/sc_score_results |\
 awk '{if (\$7 == "DUMMY") {print ""} 
  else if (\$7 == 0) {print "<a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs/"\$6"/"\$13".html target=_blank>"; print "<img src=${IMG_SRC1}/${OK_IMG}?${PSEUDO_ID}    width=4 height=4 border=0 title=\""\$6, \$14"\"></a>"index_tt} 
  else               {print "<a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs/"\$6"/"\$13".html target=_blank>"; print "<img src=${IMG_SRC1}/${ALERT_IMG}?${PSEUDO_ID} width=6 height=7 border=0 title=\""\$6, \$14"\"></a>"index_tt} }' index_tt="$TOOLTIP_INDEX" \
 >> $TMP_DIR/sc_HC_table

echo "
        </font>
        </td>" >> $TMP_DIR/sc_HC_table
EOT
		chmod u+x $TMP_DIR/sc_make_scorebox
		$TMP_DIR/sc_make_scorebox

# extract score_${REPORT_NAME}_$ROW_$PN
		grep "$TOOLTIP_INDEX" $TMP_DIR/sc_HC_table |\
 sed "s/computed/QC1 parameter &, cannot be scored/" |\
 sed "s/multiple dataset/&, cannot be scored/" \
> $TMP_DIR/score_${REPORT_NAME}_$ROW_$PN
		scp -o BatchMode=yes $TMP_DIR/score_${REPORT_NAME}_$ROW_$PN ${DFO_WEB_SERVER}:${SCORE_TT_DIR}  1>/dev/null
	done

	if [ $ROW != $LAST_ROW ]
	then
		echo "  </tr>
  <tr bgcolor=#CCCCCC>" >> $TMP_DIR/sc_HC_table
	fi
done

cat >> $TMP_DIR/sc_HC_table <<EOT
</tr></table>
EOT
}

# ==========================================================================
# 0.5 procedure singleAccess
# $1: file to be locked (maximum for 10 sec)
# ==========================================================================

singleAccess(){
# if found locked by another scoreHC, wait until unlocked
WAIT=10
while [ -s $REPORT_DIR/statFiles/${1}.lock ] && [ $WAIT -gt 0 ]
do
	if [ $WAIT = 10 ]
	then
		echo "    $1 found locked, waiting for $WAIT more secs ..." 
	elif [ $WAIT = 9 ]
	then
		echo "    ." | tr "\012" " " | sed "s/ //"
	elif [ $WAIT = 1 ]
	then
		echo "    ."  
	else
		echo "." | tr "\012" " " | sed "s/ //"
	fi
	sleep 1
	WAIT=`echo $WAIT | awk '{print $1-1}'`
done
# WAIT = 0: timeout, unlock (otherwise we may end up in endless loop)
if [ -s $REPORT_DIR/statFiles/${1}.lock ] && [ $WAIT = 0 ]
then
	echo "    $1.lock rolled back, to avoid endless loop."
	mv $REPORT_DIR/statFiles/${1}.lock $REPORT_DIR/statFiles/${1}
fi

# now lock this file to protect against overwrites by another scoreHC
if [ -s $REPORT_DIR/statFiles/${1} ]
then
	mv $REPORT_DIR/statFiles/${1} $REPORT_DIR/statFiles/${1}.lock
else
	touch $REPORT_DIR/statFiles/${1}.lock
fi
}

# ==========================================================================
# 0.6 	edit_trendP procedures
# 0.6.1 procedure refresh_sc${REPORT_NAME}.esh
# $1 = ${REPORT_NAME}
# ==========================================================================

refreshReport(){
cat > $DFO_GUI_DIR/refresh_sc$1.esh <<EOT
#!/bin/sh
echo "If you have edited the report news, they are visible on the Web but not locally.
We now refresh the local page ... 
"

# we replace comment by include, then scp to web and expand, then download again
# this is a bit tricky with multi-lined comments ...
FIRST_LINE=\`grep -n "<\!--COMMENT-->" $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html | sed "s/:.*//" | head -1\`
if [ Q\$FIRST_LINE != Q ]
then
	FIRST1=\`echo \$FIRST_LINE | awk '{print \$1+1}'\`
	sed -i -e "\$FIRST_LINE,\$FIRST_LINE s|^.*|<\!--SEP--><\!--COMMENT--><\!--#include virtual=\"/observing/dfo/quality/${DFO_INSTRUMENT}/reports/HEALTH/${1}.msg\" -->|" $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html 
	sed -i -e "\$FIRST1,\$ s|^.*<\!--COMMENT-->.*||" $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html
fi

chmod u+x $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html
ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/scManager"
scp -p -o BatchMode=yes $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html ${DFO_WEB_SERVER}:${QC_DIR}/scManager
cd $DFO_TREND_DIR/reports/scoreManager
rm -f scManager_${1}.html
wget -q -T 10 -t 1 "http://www.eso.org${QC_URL}/scManager/scManager_${1}.html"
eval "$DFO_BROWSER -remote 'openFile(\$DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html)'"

if [ -s $DFO_TREND_DIR/reports/scoreManager/scManager_${1}.html ]
then
	echo "
Page successfully updated. Hit return:"
	read input
else
	echo "
An error occurred. Hit return:"
	read input
fi
EOT

chmod u+x $DFO_GUI_DIR/refresh_sc${1}.esh
}

# ==========================================================================
# 0.6.2 edit_trendP_${DFO_INSTRUMENT}_POSTIT.esh 
#       edit the ${DFO_INSTRUMENT} news file
# ==========================================================================

cat > $DFO_GUI_DIR/edit_trendP_${DFO_INSTRUMENT}_POSTIT.esh <<EOT
#!/bin/sh

echo "Edit a general comment for the ${DFO_INSTRUMENT} HC monitor, to appear in the \"${DFO_INSTRUMENT} news\" section."

if [ ! -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ]
then
	echo "That file resides in $CONFIG_DIR as ${DFO_INSTRUMENT}_POSTIT. "
	echo ""
	echo "${DFO_INSTRUMENT}_POSTIT does not (yet) exist. We create it and offer it for edits.
 "
else
	echo "That file resides in $CONFIG_DIR as ${DFO_INSTRUMENT}_POSTIT. Now you can edit it.
 "
fi
echo "Please start the comment with a civil date. Hit return:" 
read input

xterm -T "Editing $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT" -font "fixed" -geom 100x25+400+200 -e `eval "echo $DFO_EDITOR $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT"`

echo "
Now we scp $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT to the HC monitor site. 
It will be included in all ${DFO_INSTRUMENT} HC monitor pages. Updates on this scoreManager will display after 
scoreHC calls for the respective report.
Hit return:"
read input
scp -o BatchMode=yes $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ${DFO_WEB_SERVER}:${QC_DIR}
echo "... done. Hit return:"
read input
exit
EOT
chmod u+x $DFO_GUI_DIR/edit_trendP_${DFO_INSTRUMENT}_POSTIT.esh

# ==========================================================================
# 0.7 procedures related to scManager
# 0.7.1 launch_score: call correction of scores
# ==========================================================================

launch_score(){
AB_JOB="$OTMP_DIR/${REPORT_NAME}/launch_score_${AB}.esh"
AB_DATE=`grep $AB $TMP_DIR/sc_score_results2 | awk '{print $6}' | sort -u | sed "s/\./-/g"`

cat > ${AB_JOB} <<EOT
#!/bin/sh
echo "Launch the workflow for score correction...

Step 1a: modify configuration for the selected score; this could be either config.scoreQC (or the appropriate config file if multiple ones exist; static configuration) or config.tp_${REPORT_NAME} (dynamic). This step is not provided by this workflow, do it in a separate window.

Hit return once you are done:"
read input

echo "Step 1b: If you changed the dynamic configuration (config.tp_${REPORT_NAME}), you now need to update the HC plot.
         If you only changed config.scoreQC (or the appropriate config file), you can skip this step.       
         Call '$TREND_TOOL -r ${REPORT_NAME} -f' now (Y/N) (Y)?"

read CALL_TREND
if [ Q\$CALL_TREND != QN ]
then
	$TREND_TOOL -r ${REPORT_NAME} -f
fi

echo "Step 1c: call scoreQC -a $AB (Y/N) (Y)?"
read CALL_YN
if [ Q\$CALL_YN != QN ]
then
	AB_COPIED=NO
	if [ ! -s $DFO_AB_DIR/$AB ]
	then
		cp $DFO_LOG_DIR/${AB_DATE}/$AB $DFO_AB_DIR/
		AB_COPIED=YES
	fi
	scoreQC -a $AB
	if [ \$AB_COPIED = YES ]
	then
		rm $DFO_AB_DIR/$AB
	fi
	echo "
Step 2: Now refresh the HC scoring for $REPORT_NAME (Y/N) (Y)?"
	read CALL_YN
	if [ Q\$CALL_YN != QN ]
	then
		scoreHC -r $REPORT_NAME
		echo "... done."
	else
		echo "Think about it!"
	fi
else
	echo "Think about it!"
fi
echo "Hit return:"
read input
exit
EOT
chmod u+x ${AB_JOB}
}

# ==========================================================================
# 0.7.2 launch_remove: call removal of AB score
# ==========================================================================

launch_remove(){
REMOVE_JOB="$OTMP_DIR/${REPORT_NAME}/launch_remove_$AB.esh"

cat > $REMOVE_JOB <<EOT
#!/bin/sh
echo "Launch the workflow for score removal...

Step 1: Remove database entries for $AB (Y/N) (default: N)?"
read CALL_YN
if [ Q\$CALL_YN != QY ]
then
	echo "Think about it!"
else
	echo "delete from qc1..qc1_score where ab_name = \"$AB\"
go" > $OTMP_DIR/sc_delete
	isql -Uqc -P\`cat $QC1_PWD\` -S${QC1_SERVER} -i $OTMP_DIR/sc_delete | grep -v affected
	echo "... scores for $AB removed."
	echo "
Step 2: Now refresh the HC scoring for report $REPORT_NAME (Y/N) (Y)?"
	read CALL_YN
	if [ Q\$CALL_YN != QN ]
	then
		scoreHC -r $REPORT_NAME
		echo "... done."
	else
		echo "Think about it!"
	fi
fi
echo "Hit return:"
read input
exit
EOT
chmod u+x $REMOVE_JOB
}

# ==========================================================================
# 0.7.3 launch_override: force red score to become green
# ==========================================================================

launch_override(){
OVERRIDE_JOB="$OTMP_DIR/${REPORT_NAME}/launch_override_$AB.esh"

cat > $OVERRIDE_JOB <<EOT
#!/bin/sh
echo "Launch the workflow for red score overriding...

Step 1: Override database entries for $AB (Y/N) (default: N)?"
read CALL_YN
if [ Q\$CALL_YN != QY ]
then
	echo "Think about it!"
else
	echo "select
	qc1_table, qc1_param , det, thresh1, qc1_val, thresh2, qc1_score
from 
	qc1..qc1_score 
where 
	ab_name = \"$AB\"
and
	qc1_score = 1
go" > $OTMP_DIR/sc_override
	echo "The following red scores exist for $AB (may take a while ...):"
	isql -Uqc -P\`cat $QC1_PWD\` -S${QC1_SERVER} -w 999 -i $OTMP_DIR/sc_override |\
 grep -v affected |\
 sed "/^\$/d" |\
 sed "2,2 s/^.*/-----------------------------------------------------------------------------------------/" |\
 awk '{print \$1,\$2,\$3,\$4,\$5,\$6,\$7}' |\
 sed "s/ /	/g" \
  > $OTMP_DIR/sc_override_out
	cat $OTMP_DIR/sc_override_out
	
	CHECK_LENGTH=\`cat $OTMP_DIR/sc_override_out | wc -l\`
	if [ \$CHECK_LENGTH -le 2 ]
	then
		echo "No red score found, hit return to exit."
		read input
		exit
	fi

	echo "
Do you really want to set them ALL to 0/green/OK (Y/N) (N)?"
	read SET_YN
	if [ Q\$SET_YN != QY ]
	then
		echo "Think about it!"
		read input
		exit
	fi

# force all red scores to green
	rm -f $OTMP_DIR/sc_update
	cat > $OTMP_DIR/sc_update <<END
UPDATE
        qc1..qc1_score
SET
        qc1_score = 0
WHERE
        ab_name = "$AB"
AND
	qc1_score = 1
GO
END

	isql -Uqc -P\`cat $QC1_PWD\` -S${QC1_SERVER} -w 999 -i $OTMP_DIR/sc_update

	echo "... done. Note that this score/these scores remain green only until the next refresh of scoreQC."
	echo ""
	echo "
Step 2: Now refresh the HC scoring for $REPORT_NAME (Y/N) (Y)?"
	read CALL_YN
	if [ Q\$CALL_YN != QN ]
	then
		scoreHC -r $REPORT_NAME
		echo "... done."
	else
		echo "Think about it!"
	fi
fi
echo "Hit return:"
read input
exit
EOT
chmod u+x $OVERRIDE_JOB
}

# ==========================================================================
# 0.8 php script generation  
# 0.8.1 setup directories and permissions
# ==========================================================================

# list of all items for the scoreManager, to be used here, and also in normal mode
# (we mark in the last column whether an item is SPACE (skip); noscoreitem ("no scoring"); or normal)
rm -f $TMP_DIR/sc_list_items
grep "^NAVBAR" $DFO_CONFIG_DIR/webNavBar/$CONFIG_VNAVBAR |\
 egrep "nav-subitem|nav-noscoreitem" |\
 awk '{if ($2 == "SPACE") {print $2,$2,$3} 
  else if ($3 == "nav-noscoreitem") {print $2,$2,$3} 
  else {print $2,$6,$3}}' |\
 sed "/^$/d" |\
 sed "s/vnavbar_HC_//" |\
 sed "s/.html//" > $TMP_DIR/sc_list_items

if [ $CREATE_PHP = YES ]
then
	echo "We create the php scripts and directories and exit. Hit return to continue:"
	read input

	PSO_EMAIL=`grep "^PSO_EMAIL"	$DFO_CONFIG_DIR/CALCHECK/config.calChecker | awk '{print $2}'`

# permissions: directory in which php script creates files (HEALTH) must have a+w!
	ssh -o BatchMode=yes ${DFO_WEB_SERVER} "chmod a+w $QC_DIR/HEALTH"
	ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/php"

# security: index.html and .esoacc (to protect the php directory)
# .esoacc: provides password request

	echo "Access not permitted" > $OTMP_DIR/index.html
	cat > $OTMP_DIR/.esoacc <<EOT
AuthName "HC comment editor:"
AuthType Basic

AuthUserFile /home/web/eso/docs/observing/dfo/quality/.webauth/passwordfile
require valid-user

EOT
	chmod o+r $OTMP_DIR/.esoacc

	echo "Transfer of administration files ..."

	cat > $OTMP_DIR/README <<EOT
This directory contains the php scripts needed for the HC comment editor.
The scripts are created by 'scoreHC -P'. The directory is created by the same
command.
EOT
	scp $OTMP_DIR/.esoacc $OTMP_DIR/index.html $OTMP_DIR/README ${DFO_WEB_SERVER}:$QC_DIR/php

# ==========================================================================
# 0.8.2 make sure that all reports have at least an empty .msg file,
#       otherwise virtual includes would raise an error
# ==========================================================================

	echo "
We now check that each report has at least a dummy msg file in $QC_URL/HEALTH ..."
	for G in `cat $TMP_DIR/sc_list_items | grep -v SPACE | grep -v "nav-noscoreitem" | awk '{print $2}'`
	do
		echo "$G"
		GRP=`echo $G | sed "s/vnavbar_HC_//" | sed "s/.html//"`
		rm -f $TMP_DIR/sc_list_reports
		grep "^GROUP_MEMBERS" $DFO_CONFIG_DIR/trendPlotter/$GRP.grp | grep -v MARKER | awk '{print $3}' > $TMP_DIR/sc_list_reports
		if [ -s $TMP_DIR/sc_list_reports ]
		then
			for R in `cat $TMP_DIR/sc_list_reports`
			do
				echo "   $R ..."
				ssh -o BatchMode=yes $DFO_WEB_SERVER "if [ ! -s $QC_DIR/HEALTH/$R.msg ]; then touch $QC_DIR/HEALTH/$R.msg 2>/dev/null ; fi"
			done
		fi
	done

# ==========================================================================
# 0.8.3 php script hcComments.php
# ==========================================================================

# cleanup $DFO_GUI_DIR
	rm -f $DFO_GUI_DIR/edit_trendP*esh	

# hcComments.php
	cat > $OTMP_DIR/hcComments.php <<EOT
<?php
/*
This php script has been created by scoreHC v$TOOL_VERSION .
It creates the cgi interface to create, edit, or remove a comment.
This information is read from, and written into, \$messageSelect.
==================================================================
*/

//==============================================================
// 0. select the report
\$reportMode    = \$_GET[reportMode];     // mode: delete|update
\$reportSelect  = \$_GET[reportSelect];   // name of selected HC report (logical name)
\$messageSelect = \$reportSelect.".msg";  // same, with extension .msg (filename)
\$deleteConfirm = \$_GET[deleteConfirm];  // confirmed: yes, otherwise undefined
\$origin        = \$_GET[origin];         // 'local' | 'web'  | 'report', for jump 'back to main'
					  // local: from scoreManager; web: score overview; report: from HC report

\$origURL = "/observing/dfo/quality/$DFO_INSTRUMENT/common/score_overview_core_all.html" ;

// no report selected:  error
if (\$reportSelect=='') {
  die ('<img src=${IMG_URL}/php-warning.png height=30 width=30> <font size=2>Please select a <b>report</b>. <br><a href="javascript:back();">back</a></font>');
}

//==============================================================
// 1. DELETION
// 1.1 deletion: ask for confirmation
if (\$reportMode=='delete' && \$deleteConfirm=='') {

  if (\$origin=='local' || \$origin=='report') {
  \$back="<a href=\"javascript:self.close();\">close window</a>";
  \$back2="click 'close window'";
  }
  else {
  \$back="<a href=\"\$origURL\">back to main</a>";
  \$back2="click 'back to main'";
  }

 echo <<<EOT
<form method="get" action="./hcComments.php?reportMode=\$reportMode">
<input type="hidden" name="reportMode" value="\$reportMode">
<input type="hidden" name="reportSelect" value="\$reportSelect">
<input type="hidden" name="deleteConfirm" value="yes">
<input type="hidden" name="origin" value="\$origin">

<table style="border-collapse: collapse;" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
  <tr align="left" valign="bottom">
    <td bgcolor="$HC_COLOR" height="40" width="40"><b><font color="#ffffff" size="3">HC</font></b></td>
    <td valign="bottom"><font size=+2>CONFIRM</font></td>
   </tr>
</table><p>
<font size=2>Please confirm deletion of <b>\$messageSelect</b>:<p>
<input type="submit" value="Delete">
<p>
or \$back2 to cancel</font>
<p>
</form>

<font size="2">
\$back
</font>
</html>
EOT;
exit;
}

//==============================================================
// 1.2 deletion: do it
\$back2="";
\$back3="";
if (\$reportMode=='delete' && \$deleteConfirm=='yes') {
  if (\$origin=='local' || \$origin=='report') {
  \$back="<a href=\"javascript:self.close();\">close window</a>";
  if (\$origin=='local') {
    \$back3="<p><blink>Don't forget to <b>refresh</b> the local report.</blink>";
    }
  }
  else {
  \$back="<a href=\"\$origURL\">back to main</a> ";
  \$back2="Click 'back to main' for a new comment.";
  }

  chdir("../HEALTH");
  if (!unlink(\$messageSelect))
    die ("error: deleting \$messageSelect failed ...\n");

// to always have a zero-size msg file to be included
  fopen(\$messageSelect,"a");

  echo <<<EOT
<table style="border-collapse: collapse;" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
  <tr align="left" valign="bottom">
    <td bgcolor="$HC_COLOR" height="40" width="40"><b><font color="#ffffff" size="3">HC</font></b></td>
    <td valign="bottom"><font size=+2>Deleted</font></td>
   </tr>
</table><p>
<font size=2>Entry deleted for <b>\$messageSelect</b>.
\$back3
\$back2 <p>

<font size="2">
\$back
 </font>
</html>
EOT;
exit;
}

//==============================================================
// 2. Comment exists
echo <<<EOT
<html>
<head>
<title>$DFO_INSTRUMENT HC monitor: comment editor for '\$reportSelect' report</title>
</head>
<body>
<table style="border-collapse: collapse;" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
  <tr align="left" valign="bottom">
    <td bgcolor="$HC_COLOR" height="40" width="40"><b><font color="#ffffff" size="3">HC</font></b></td>
    <td valign="bottom"><font size=+2>$DFO_INSTRUMENT HC monitor: comment editor for '\$reportSelect' report</font></td>
   </tr>
</table><p>

EOT;

chdir("../HEALTH");

\$file = fopen("\$messageSelect","r");
\$line = fgets(\$file,4096);              // check if first line not empty

//==============================================================
// 2.1 Display choice for deletion or update

if (ereg ("^.*", \$line)&&(\$line!="")&&(\$line!=" *")&&(\$reportMode!="update")) {
// YES
  echo <<<EOT
<font size=2><b>Comment existing:</b><br>
<p>
<form method="get" action="./hcComments.php?reportSelect=\$reportSelect&origin=\$origin">
<table>
<tr style="font-size:small; color:$HC_COLOR;" bgcolor=#cccccc>
  <th COLSPAN=2>action</td>
  <th>DATE</td><th>comment</td><th>author</td>
</tr>
<tr style="font-size:small;" bgcolor=#ffff99>
  <td bgcolor=white>delete:</td>
  <td bgcolor=white>
    <input type="radio"  name="reportMode" value="delete">
  </td>
  <td>
EOT;
  \$messageText=file_get_contents(\$messageSelect);
  \$array = explode("<!--SEP--> ",\$messageText);

  if (\$origin=='local' || \$origin=='report')
    \$back="<a href=\"javascript:self.close();\">close window</a>";
  else
    \$back="<a href=\"\$origURL\">back to main</a> ";

  echo "\$array[0]"."</td><td>"."\$array[1]"."</td><td>"."\$array[2]";

  echo <<<EOT
    </td>
  </tr>

  <tr style="font-size:small;" bgcolor=white>
  <td>update:</td>
  <td>
    <input type="hidden" name="reportSelect" value="\$reportSelect">
    <input type="radio"  name="reportMode" value="update">
    <input name="origin"  value="\$origin" type="hidden">
  </td>
  <td colspan=3>&nbsp;</td>
  </tr>
</table>
<br>
<input type="submit" value="Submit">
<input type="reset"  value="Reset">
</b>
<p>
\$back
</form>
<p>

</html>
EOT;
fclose(\$file);
exit;
}

//==============================================================
// 3. NEW comment
elseif (\$reportMode!="update") {
  echo <<<EOT
<table width=700><tr><td>
<font size=2>
<b>No comment existing.</b>  <p>

Enter here a comment about the HC report \${reportSelect}.<br>
It will appear as "Report news".<p>

 Always enter a date, it will be used to monitor the comment age: </font>
</td></tr></table><p>
\n
EOT;
  fclose(\$file);
  \$date = gmdate("Y-m-d");

// Enter new comment
  echo <<<EOT
<form method="post" action="../php/hcCmtManager.php">
<input name="reportSelect" value="\$reportSelect" type="hidden">
<input name="origin"  value="\$origin" type="hidden">

<table cellpadding="2" cellspacing="2" border="0" width="700">
  <tr style="font-size:small; color:$HC_COLOR;" bgcolor=#cccccc valign=top>
    <td nowrap><font size="2"><b>Date of your comment:<br></b>(format YYYY-MM-DD)</font></td>
    <td><font size="2"><b>Enter comment:</b><br>(multiple lines supported)</font></td>
  </tr>

  <tr bgcolor="#ffff99" valign=top>
    <td width="100">
      <input name="commentDate" type="text" value="\$date" size="15" maxlength="12">
    </td>
    <td width="100">
      <textarea name="commentText" cols="50" rows="2">&lt;your comment&gt;</textarea>
    </td>
  </tr>

  <tr bgcolor="white">
    <td colspan="2"><font size="2">&nbsp;</font></td>
  </tr>

  <tr style="font-size:small; color:$HC_COLOR;" bgcolor=#cccccc>
    <td colspan="2"><font size="2"><b>Your email address <i></b><font size=1>(e.g. $PSO_EMAIL | $QC_ADDRESS | $OP_ADDRESS):</font></i></td>
  </tr>
  <tr bgcolor="#ffff99">
    <td colspan="2">
      <input name="commentAuthor" type="text" value="&lt;your_email&gt;@eso.org" size="30" maxlength="30">
    </td>
  </tr>
</table>

    <br>
    <input type="submit" value="Submit">
    <input type="reset"  value="Reset">
</p>
</form>
EOT;
}

//==============================================================
// 4. UPDATE
else {
  echo <<<EOT
<table width=700><tr><td>
<font size=2>
<b>Comment exists already.</b>  <p>

Update here a comment (or replace it entirely) about the HC report \${reportSelect}.<br>
It will appear as "Report news".<p>

 Always enter a date, it will be used to monitor the comment age.<p>
 The fields are filled with the current content. You can replace any of those.
 </font>
</td></tr></table><p>
\n
EOT;
  \$messageText=file_get_contents(\$messageSelect);
  \$array = explode("<!--SEP--> ",\$messageText);
  \$oldDate    = \$array[0];
  \$oldComment = \$array[1];
  \$oldComment = str_replace("<br />","",\$oldComment);
  \$oldComment = str_replace("<!--COMMENT-->","",\$oldComment);
  \$oldAuthor  = str_replace("[","",\$array[2]);
  \$oldAuthor  = str_replace("]","",\$oldAuthor);

// Update comment
  echo <<<EOT
<form method="post" action="../php/hcCmtManager.php">
<input name="reportSelect" value="\$reportSelect" type="hidden">
<input name="origin" value="\$origin" type="hidden">

<table cellpadding="2" cellspacing="2" border="0" width="700">
  <tr style="font-size:small; color:$HC_COLOR;" bgcolor=#cccccc valign=top>
    <td nowrap><font size="2"><b>Old date of comment:</b></font></td>
    <td><font size="2"><b>Enter comment:</b><br>(multiple lines supported)</font></td>
  </tr>

  <tr bgcolor="#ffff99" valign=top>
    <td width="100">
      <input name="commentDate" type="text" value="\$oldDate" size="15" maxlength="10">
    </td>
    <td width="100">
      <textarea name="commentText" cols="50" rows="2">\$oldComment</textarea>
    </td>
 </tr>

  <tr bgcolor="white">
    <td colspan="2"><font size="2">&nbsp;</font></td>
  </tr>

  <tr style="font-size:small; color:$HC_COLOR;" bgcolor=#cccccc>
    <td colspan="2"><font size="2"><b>Your email address <i></b><font size=1>(e.g. $PSO_EMAIL | $QC_ADDRESS | $OP_ADDRESS):</font></i></td>
  </tr>
  <tr bgcolor="#ffff99">
    <td colspan="2">
      <input name="commentAuthor" type="text" value="\$oldAuthor" size="30" maxlength="30">
    </td>
  </tr>
</table>
    <br>
    <input type="submit" value="Submit">
    <input type="reset"  value="Reset">
</p>
</form>
EOT;
  fclose(\$file);
}

if (\$origin=='local' || \$origin=='report')
  \$back="<a href=\"javascript:self.close();\">close window</a>";
else
  \$back="<a href=\"\$origURL\">back to main</a> ";

echo <<<EOT
</b><font size="2">
\$back
</font>

</body>
</html>
EOT;
?>
EOT

# ==========================================================================
# 0.8.4 hcCmtManager.php
# ==========================================================================

	cat > $OTMP_DIR/hcCmtManager.php <<EOT
<html>
<?php
/*
This php script is created by scoreHC v$TOOL_VERSION.
It analyzes the input from hcComments.php and updates the specified input file.
The analyzed line is replaced.
==============================================================
*/

//==============================================================
// 1. check if form elements are properly filled:
// 1.1 catch reportSelect (hidden)
\$reportSelect  = \$_POST[reportSelect];
\$messageSelect = \$reportSelect.".msg";
\$origin        = \$_POST[origin];

\$origURL = "/observing/dfo/quality/$DFO_INSTRUMENT/common/score_overview_core_all.html" ;

// 1.2 commentDate
\$commentDate = str_replace("<","&lt;",\$_POST[commentDate]);
if (\$commentDate=='&lt;YYYY-MM-DD>'||\$commentDate=='') {
  die ('<img src=${IMG_URL}/php-warning.png height=30 width=30> <font size=2>Please enter a valid <b>date</b>. <br><a href="javascript:back();">back</a></font>');
}

// 1.3 commentText
// nl2br: replace newline by <br>
\$commentText = nl2br(str_replace("<","&lt;",\$_POST[commentText]));
\$commentText = str_replace("<br />","<br /><!--COMMENT-->",\$commentText)."<!--COMMENT-->";
if (\$commentText=='&lt;your comment>'||\$commentText=='') {
  die ('<img src=${IMG_URL}/php-warning.png height=30 width=30> <font size=2>Please enter a <b>comment</b>. <br><a href="javascript:back();">back</a></font>');
}

// 1.4 valid email
\$commentAuthor = str_replace("<","&lt;",\$_POST[commentAuthor]);
if (\$commentAuthor=='&lt;your_email>@eso.org'||\$commentAuthor=='') {
  die ('<img src=${IMG_URL}/php-warning.png height=30 width=30> <font size=2>Please enter a valid <b>email address</b>. <br><a href="javascript:back();">back</a></font>');
}
else {
  \$commentAuthor = "[".\$commentAuthor."]";
}

//==============================================================
// 2. if all fields are valid, we continue
\$time = gmdate("Y-m-d H:i:s");
\$data = \$commentDate."<!--SEP--> ".\$commentText."<!--SEP--> ".\$commentAuthor."\n";

// we write new line into HC_COMMENT_new
chdir("../HEALTH");
\$new_file = "HC_COMMENT_new";
if (!\$file_new = fopen(\$new_file,"a"))
 echo "error: can't open \$new_file ...\n";

\$file_new = fopen(\$new_file,"a");
fwrite(\$file_new, \$data);

fclose(\$file_new);

if (!rename("HC_COMMENT_new", "../HEALTH/\$messageSelect"))
 echo "error: rename failed ...\n";             // have edited HC_COMMENT file

\$commentAuthor1 = str_replace("[","",\$commentAuthor);
\$commentAuthor1 = str_replace("]","",\$commentAuthor1);

\$back3="";
if (\$origin=='local' || \$origin=='report') {
  \$back="<a href=\"javascript:self.close();\">close window</a>";
  if (\$origin=='local') {
    \$back3="<blink>Don't forget to <b>refresh</b> the local report.</blink><br>";
    }
 }
else {
  \$back="<a href=\"\$origURL\">back to main</a> ";
  }

echo <<<EOT
<table style="border-collapse: collapse;" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
  <tr align="left" valign="bottom">
    <td bgcolor="#006a9d" height="40" width="40"><b><font color="#ffffff" size="3">HC</font></b></td>
    <td valign="bottom"><font size=+2>UPDATED</font></td>
   </tr>
</table><p>

<font size=2>Entry in <b>\$messageSelect</b> updated by <b>\$commentAuthor1</b>.<p>
It is now visible on the HC report page \${reportSelect}:<p>

<table style="font-size:small;">
  <tr style="font-size:small; color:#006a9d;" bgcolor=#cccccc>
    <th> DATE</td><th>comment</td><th>author</td>
  </tr>
  <tr bgcolor=#ffff99>
    <td>\$commentDate</td><td>\$commentText</td><td>\$commentAuthor
  </td>
  </tr></table>
<br>
<br>
<font size="2">
\$back3
\$back
</font>
EOT;
?>
</html>

EOT

# ==========================================================================
# 0.8.5 transfer
# ==========================================================================
	echo " 
Transfer of hcComments.php and hcCmtManager.php (old versions under hcComments.php_back and hcCmtManager.php_back) ..."
	ssh -o BatchMode=yes $DFO_WEB_SERVER "if [ -s $QC_DIR/php/hcComments.php ]; then cd $QC_DIR/php; cp hcComments.php hcComments.php_back; cp hcCmtManager.php hcCmtManager.php_back; fi"
	scp $OTMP_DIR/hcComments.php $OTMP_DIR/hcCmtManager.php ${DFO_WEB_SERVER}:$QC_DIR/php

	echo "
php setup finished: 
- HEALTH directory has permissions a+w 
- directory ${DFO_INSTRUMENT}/reports/php created 
- php scripts hcCmtManager.php and hcComments.php created/updated and uploaded."
	exit
fi

# ==========================================================================
# 0.9 procedure createInsScoreOverview: create "Shiftleader's page"
# score_overview.html/score_overview_7.html/score_overview_30.html
# ==========================================================================
createInsScoreOverview(){
if [ ! -s $DFO_CONFIG_DIR/config.scoreHC ]
then
	echo "***ERROR: no $DFO_CONFIG_DIR/config.scoreHC found. Can't do. Exit."
	exit -1
fi

# ==========================================================================
# 0.9.1 configuration
# The CURRENT observatory configuration by TEL and INS
# (for score_overview_7.html and score_overview_30.html)
# ==========================================================================
# TELescopes

grep "^TEL"	$DFO_CONFIG_DIR/config.scoreHC | awk '{print $2}' > $TMP_DIR/so_tel

# INStruments
grep "^INSTR"	$DFO_CONFIG_DIR/config.scoreHC | awk '{print $2,$3}' > $TMP_DIR/so_ins

# ==========================================================================
# 0.9.2 Catch current ins score and write into SCORE_OVERVIEW
# ==========================================================================

ALL_SCORE=all_score_overview.html
cd $TMP_DIR
rm -f $ALL_SCORE all_score_overview*.html

TODAY=`date -u +%Y-%m-%d`
TIMESTAMP=`date -u +%Y-%m-%d"T"%H:%M:%S`

# update SCORE_OVERVIEW (this is the memory of the ins scores)
wget -q ${ALL_URL}/common/$ALL_SCORE

# add TODAY only once
if [ -s $DFO_MON_DIR/SCORE_OVERVIEW ]
then
	CHECK_TODAY=`grep $TODAY $DFO_MON_DIR/SCORE_OVERVIEW | tail -1 | awk '{print $1}'`
fi

if [ Q$CHECK_TODAY = Q ]
then
	for INS in `cat $TMP_DIR/so_ins | awk '{print $1}'`
	do
		SCORE=`grep "$INS" $ALL_SCORE | grep -v "trending system" | grep -v "nav-div" | grep -v "calibration and instrument" | grep -v calChecker | grep -v onMouseOut | grep -v "<td nowrap" | sed "/all groups scored/s/^.*/OK/" | sed "/at least one/s/^.*/NOK/" | grep -v onMouseOut | grep -v "<td nowrap" | grep -v details | sort -u |  egrep "OK|NOK"`
		echo $INS $TODAY $SCORE >> $DFO_MON_DIR/SCORE_OVERVIEW
	done

# keep file to less than 1000 entries
	cat $DFO_MON_DIR/SCORE_OVERVIEW | tail -1000 > $TMP_DIR/SCORE_OVERVIEW
	mv $TMP_DIR/SCORE_OVERVIEW $DFO_MON_DIR/SCORE_OVERVIEW
fi

# ==========================================================================
# 0.9.3 Create history
# Last week and last month
# ==========================================================================

SCORE_OK="<img src=\"${IMG_URL}/sq2.png\" width=6 height=6 border=0>"
SCORE_NOK="<img src=\"${IMG_URL}/arr1red-up.gif\" width=6 height=7 border=0>"
SCORE_OK_LAST="<img src=\"${IMG_URL}/sq2.png\" width=11 height=11 border=0>"
SCORE_NOK_LAST="<img src=\"${IMG_URL}/arr1red-up.gif\" width=11 height=12 border=0>"

SCORE_GRY1="<img src=\"${IMG_URL}/sqg.png\" width=6 height=12 border=0>"
SCORE_GRY="<img src=\"${IMG_URL}/sqg.png\" width=6 height=6 border=0>"
SCORE_INV="<img src=\"${IMG_URL}/itemOff.gif\" width=6 height=6 border=0>"

# create score_overview_7.html
cat > score_overview_7.html <<EOT
<html>

<head>
<!-- created by 'scoreHC -O', cronjob on ${USER}@$HOST -->
<META HTTP-EQUIV="Refresh" CONTENT="60">
<META HTTP-EQUIV="Cache-Control" CONTENT="NO-CACHE">

  <title>Quality Control: Daily health check monitor</title>
  <link rel="StyleSheet" href="/qc/ALL/common/qc_eso.style" type="text/css">
</head>

<body bgcolor="#FFFFFF">

<!-- backbone table -->
<table width="950" border="0" cellspacing="1" cellpadding="1" bgcolor=#FFFFFF>

<!-- ROW 1: caption part -->
  <tr>
   <td colspan=99>

<!--begin include header -->
<!--#include virtual="/observing/dfo/quality/ALL/common/header_HC.html" -->
<!--end include header -->

    </td>
  </tr>
<!--end caption -->

<!-- ROW 2: vnavbar and overview table -->
  <tr align="left" valign="top" bgcolor="#999999">
<!-- COL 1: vnavbar -->
    <td align="center" valign="top" bgcolor="#006A9D" width="10">

<!--begin include vnavbar -->
<!--#include virtual="/observing/dfo/quality/ALL/common/navbar_HC.html" -->
<!--end include vnavbar -->
    </td>

<!-- COL 2: proper distance -->
    <td width="10" bgcolor="#FFFFFF"><img src="/images/itemOff.gif" width="10" height="10" border="0"></td>

<!-- COL 3: hosting main table -->
    <td width="782" bgcolor="#FFFFFF">

<h1>Overview of instrument scores</h1>
<p><font size="2"><img src="/qc/ALL/img/sl.png" width="22" height="20" alt="sl icon"> 
Find here the history of all instrument scores for the last 7 days. Each historical score, with the exception
of the current score,  is a snapshot for a 
24 hour period, all snapshots have been taken at the same time. The last score is the current value [click to connect to details].
</font>
</p>

<i><font color="#999999" size="1">&nbsp;&nbsp;&nbsp;[press Ctrl+R to enforce refresh of scores]</font></i>
&nbsp; &nbsp; <font color=#999999 size="2"><a href=./all_score_overview.html title="instrument scores today"><font color=#999999 size="2">current scores</font></a> | 
EOT
cat score_overview_7.html | sed "s/last 7 days\./last 30 days\./" > score_overview_30.html

cat >> score_overview_7.html <<EOT
 <a style="background-color:#c8d7f4;" href=./score_overview_7.html title="last 7 days"><font color=#999999 size="2">last 7 days</font></a> |
<a href=./score_overview_30.html title="last 30 days"><font color=#999999 size="2">30</font></a>

</font>

<table border="0" cellspacing="1" cellpadding="1">
EOT

cat >> score_overview_30.html <<EOT
 <a href=./score_overview_7.html title="last 7 days"><font color=#999999 size="2">last 7 days</font></a> |
<a style="background-color:#c8d7f4;" href=./score_overview_30.html title="last 30 days"><font color=#999999 size="2">30</font></a>

</font>

<table border="0" cellspacing="1" cellpadding="1">
EOT

# fill in the N last scores (N=7,30)
for TEL in `cat $TMP_DIR/so_tel`
do
	TEL1=`echo $TEL | sed "s/_/ /g"`
	echo "<tr><td colspan="2" nowrap bgcolor=#CCCCCC><font size="2"><b>$TEL1</b></font></td>" >> score_overview_7.html
	echo "<tr><td colspan="2" nowrap bgcolor=#CCCCCC><font size="2"><b>$TEL1</b></font></td>" >> score_overview_30.html
	if [ $TEL = UT1 ]
	then
        	echo "<td nowrap bgcolor=#CCCCCC colspan=2><font size="2"><b>score overview</b></font></td>" >> score_overview_7.html
        	echo "<td nowrap bgcolor=#CCCCCC colspan=2><font size="2"><b>score overview</b></font></td>" >> score_overview_30.html
	else
		echo "<td bgcolor=#CCCCCC colspan=2><font size="2">&nbsp;</font></td>" >> score_overview_7.html
  		echo "<td bgcolor=#CCCCCC align=right><font size=2 color=#666666><a title=\"30 days ago\"><font size=1>30</font> ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV} <a title=\"20 days ago\">${SCORE_INV}<font size=1>20</font> ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} <a title=\"10 days ago\">${SCORE_INV}${SCORE_INV}<font size=1>10</font>  ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV}</td>
  <td bgcolor=#CCCCCC><font size=1>&nbsp;</td>" >> score_overview_30.html
	fi

# clean SCORE_OVERVIEW (could contain comments etc.) to have 3 entries only: INS DATE OKorNOK
	cat $DFO_MON_DIR/SCORE_OVERVIEW | sed "/NOK$/s/^.*/NOK &N/" | sed "/OK$/s/^.*/OK &N/" | awk '{print $2,$3,$1}' > $DFO_MON_DIR/SCORE_OVERVIEW1
	mv  $DFO_MON_DIR/SCORE_OVERVIEW1 $DFO_MON_DIR/SCORE_OVERVIEW

	for INS in `grep $TEL $TMP_DIR/so_ins | awk '{print $1}'`
	do
		INS1=`echo $INS | sed "s|GIRAFFE|FLAMES/&|" | sed "s|UVES|&+FLAMES/&|"`
		echo "<tr><td bgcolor=#CCCCCC><font size=2>&nbsp;&nbsp;&nbsp;</font></td>" >> score_overview_7.html
		echo "<tr><td bgcolor=#CCCCCC><font size=2>&nbsp;&nbsp;&nbsp;</font></td>" >> score_overview_30.html
		echo "    <td bgcolor=#CCCCCC><font size=2><b>$INS1</b>&nbsp;</font></td>" >> score_overview_7.html
		echo "    <td bgcolor=#CCCCCC><font size=2><b>$INS1</b>&nbsp;</font></td>" >> score_overview_30.html

# all but last (last is embedded dynamically
		SCORE7=`grep "^$INS"  $DFO_MON_DIR/SCORE_OVERVIEW | grep -v "^#" | tail -7 | head -6 | awk '{print $3}' | sed "s|NOK|${SCORE_NOK}|" | sed "s|OK|${SCORE_OK}|" | tr "\012" " " | sed "s/^.*/&\n/"`
		SCORE30=`grep "^$INS" $DFO_MON_DIR/SCORE_OVERVIEW | grep -v "^#" | grep -v "no scoring implemented" | tail -30 | head -29 | awk '{print $3}' | sed "s|NOK|${SCORE_NOK}|" | sed "s|OK|${SCORE_OK}|" | tr "\012" " " | sed "s/^.*/&\n/"`

		echo "    <td bgcolor=#EEEEEE align=right><font size=2>$SCORE7</font></td>" >> score_overview_7.html
		echo "    <td bgcolor=#EEEEEE align=right><font size=2>$SCORE30</font></td>" >> score_overview_30.html

		echo "    <td bgcolor=#EEEEEE><a href=../../$INS/common/score_overview.html><!--#include virtual=\"/observing/dfo/quality/$INS/common/score_ins.html\"--></a></td>"   >> score_overview_7.html
		echo "    <td bgcolor=#EEEEEE><a href=../../$INS/common/score_overview.html><!--#include virtual=\"/observing/dfo/quality/$INS/common/score_ins.html\"--></a></td>"   >> score_overview_30.html

		echo "</tr>" >> score_overview_7.html
		echo "</tr>" >> score_overview_30.html
	done
done

echo "<tr>
  <td bgcolor=#CCCCCC><font size=2>&nbsp;</font></td>
  <td align=right bgcolor=#CCCCCC><font size=2>last UT date:</td>
  <td bgcolor=#EEEEEE align=right colspan=2><font size=2>$TODAY</td>
</tr>
<tr><td colspan=4 align=right><font size=1 color=#666666>updated: $TIMESTAMP UT by `whoami`</font></td></tr>
</table>" >> score_overview_7.html
echo "<tr>
  <td bgcolor=#CCCCCC><font size=1>&nbsp;</td>
  <td bgcolor=#CCCCCC><font size=1>&nbsp;</td>
  <td bgcolor=#CCCCCC align=right><font size=2 color=#666666><a title=\"30 days ago\"><font size=1>30</font> ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV} <a title=\"20 days ago\">${SCORE_INV}<font size=1>20</font> ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} <a title=\"10 days ago\">${SCORE_INV}${SCORE_INV}<font size=1>10</font>  ${SCORE_INV} ${SCORE_INV}</a> ${SCORE_INV} ${SCORE_INV} ${SCORE_GRY} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV} ${SCORE_INV}</td>
  <td bgcolor=#CCCCCC><font size=1>&nbsp;</td>
</tr>

<tr>
<tr>
  <td bgcolor=#CCCCCC><font size=2>&nbsp;</td>
  <td align=right bgcolor=#CCCCCC><font size=2>last UT date:</td>
  <td bgcolor=#EEEEEE align=right colspan=2><font size=2>$TODAY</td>
</tr>
<tr><td colspan=4 align=right><font size=1 color=#666666>updated: $TIMESTAMP UT by `whoami`</font></td></tr>
</table>" >> score_overview_30.html

chmod a+x score_overview_7.html score_overview_30.html
scp -o BatchMode=yes -p score_overview_7.html score_overview_30.html ${DFO_WEB_SERVER}:/home/qc/qc/ALL/common/ 1>/dev/null
}

# ==========================================================================
# 1.0 start collecting scores
# ==========================================================================

TODAY=`date -u +%Y-%m-%d`

# CREATE_OVERVIEW: create the common score overview pages and exit
if [ $CREATE_OVERVIEW = YES ]
then
	if [ Q$USER != Q$CREATE_OVERVIEW_USER ]
	then
		echo "***WARNING: this option provides the creation of a COMMON page and not of $DFO_INSTRUMENT specific output.
This mode is reserved for the user $CREATE_OVERVIEW_USER who also maintains the config file config.scoreHC with the current
instruments. Please continue only if you know what you do.
Do you want to continue (yEs/N) (N)?"
		read CONFIRM
		if [ Q$CONFIRM != QyEs ]
		then
			echo "Exit."
			exit
		fi
	fi
	createInsScoreOverview
	exit
fi

# INTERFACE_ONLY: section 4
if [ $INTERFACE_ONLY = NO ]
then
# get list of QC params
	rm -f $TMP_DIR/sc_plotnames
	grep "^PLOT_NAME" $CONFIG_DIR/$CFG | awk '{print $3,$2}' > $TMP_DIR/sc_plotnames
	
# check if multiple config.scoreQC exist
	MULTI_CONFIG=`grep "^MULTI_CONFIG"	$DFO_CONFIG_DIR/config.scoreQC | awk '{print $2}'`

# ==========================================================================
# 1.1 Plot loop
# ==========================================================================
# we collect queries for each plot, and then launch them in one go

	rm -f $TMP_DIR/sc_score_query $TMP_DIR/score_check_mail
	
	echo "  Analyze reports ..."
	for P in `cat $TMP_DIR/sc_plotnames  | awk '{print $1}'`
	do
		P_INDEX=`grep "^PLOT_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | awk '{print $2}'`
		ENABLED=`grep "^PLOT_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | awk '{print $11}'`
# not implemented: skip
		if [ Q$ENABLED = QNO ]
		then
			echo "   Plot $P_INDEX, $P: 	scoring not enabled."
			continue
		fi

# for MULTI_CONFIG: reports could be fed from different scoreQC config files
		if [ Q$MULTI_CONFIG = QYES ]
		then
# we need to find RAW_TYPE as a matchkey to find the proper CFG_FILE
			CHECK_RT=`grep "^HC_PLOT" $DFO_CONFIG_DIR/config.scoreQC | egrep "trend_report_${REPORT_NAME}_HC|trend_report_${REPORT_NAME}.html" | awk '{print $2}' | sort -u | wc -l`

			if [ $CHECK_RT != 1 ]
			then
# look for section 4.1A in config.scoreQC
				CHECK_CFG=`grep "^MULTI_PLOT" $DFO_CONFIG_DIR/config.scoreQC | awk '{print $2,$3,$4,$5}' | grep "^${REPORT_NAME} " | grep " $P_INDEX " | awk '{print $4}' | sort -u | wc -l`
				if [ $CHECK_CFG != 1 ] 
				then
					echo "*** WARNING: you need to enter specific configuration in config.scoreQC for $REPORT_NAME, section 4.1A -- MULTI_PLOT." > $TMP_DIR/score_check_mail
					echo "config.scoreQC is used as default but this might give wrong configuration." >> $TMP_DIR/score_check_mail
					cat $TMP_DIR/score_check_mail
				else
					CFG_FILE=`grep "^MULTI_PLOT" $DFO_CONFIG_DIR/config.scoreQC | awk '{print $2,$3,$4,$5}' | grep "^${REPORT_NAME} " | grep " $P_INDEX " | awk '{print $4}' | sort -u`
					RAW_TYPE=`grep "^MULTI_PLOT" $DFO_CONFIG_DIR/config.scoreQC | awk '{print $2,$3,$4,$5}' | grep "^${REPORT_NAME} " | grep " $P_INDEX " | awk '{print $3}' | sort -u`
				fi
			else
				RAW_TYPE=`grep "^HC_PLOT" $DFO_CONFIG_DIR/config.scoreQC | egrep "trend_report_${REPORT_NAME}_HC|trend_report_${REPORT_NAME}.html" | awk '{print $2}' | sort -u`
				CONFIG_STRING=`grep "^CONFIG_STRING"	$DFO_CONFIG_DIR/config.scoreQC | sed "s/&&$//" | sed "s/^.*&&//"`
				if [ "Q$CONFIG_STRING" = Q ]
				then
					CONFIG_VALUE=$RAW_TYPE
				else
					CONFIG_VALUE=`eval "echo $RAW_TYPE | $CONFIG_STRING"`
				fi
		
				CFG_FILE=`grep "^CONFIG_CASE"	$DFO_CONFIG_DIR/config.scoreQC | grep "[[:space:]]${CONFIG_VALUE}[[:space:]]" | awk '{print $3}'`
				if [ Q$CFG_FILE = Q ]
				then
					CONFIG_VALUE=other
					CFG_FILE=`grep "^CONFIG_CASE"	$DFO_CONFIG_DIR/config.scoreQC | grep "[[:space:]]${CONFIG_VALUE}[[:space:]]" | awk '{print $3}'`
				fi
			fi

			if [ Q$CFG_FILE = Q ]
			then
				echo "*** ERROR: no config file configured for CONFIG_VALUE '$CONFIG_VALUE'. Fix config error. We try config.scoreQC!"
				CFG_FILE=config.scoreQC
			fi

			if [ ! -s $DFO_CONFIG_DIR/$CFG_FILE ]
			then
				echo "*** ERROR: config file '$CFG_FILE' not found. Fix config error. We try config.scoreQC!"
				CFG_FILE=config.scoreQC
			fi
		else
			CFG_FILE=config.scoreQC
		fi

# Now there should be one value for CFG_FILE
		if [ -s $TMP_DIR/score_check_mail ] && [ Q$ALREADY_SENT = Q ]
		then
			mail -s "scoreHC: possible error in config.scoreQC for $REPORT_NAME" $OP_ADDRESS < $TMP_DIR/score_check_mail
			ALREADY_SENT=YES
		fi

# get information about MEF from $CFG_FILE 
		MEF_NCOL=`grep "^MEF_NCOL"      $DFO_CONFIG_DIR/$CFG_FILE | awk '{print $2}'`
		MEF_NROW=`grep "^MEF_NROW"      $DFO_CONFIG_DIR/$CFG_FILE | awk '{print $2}'`
		MEF_PSEUDO=`grep "^MEF_PSEUDO"  $DFO_CONFIG_DIR/$CFG_FILE | awk '{print $2}'`
		if [ "Q$MEF_PSEUDO" != "QYES" ]
		then
		        MEF_PSEUDO=NO
		fi
	
		if [ $MEF_NCOL -gt 1 ] || [ $MEF_NROW -gt 1 ] || [ $MEF_PSEUDO = YES ]
		then
		        MEF_MODE=YES
		else
		        MEF_MODE=NO
		fi

# name of detector/extension parameter, as used in qc1 database (possible values: chip_IDs, AGV, RMS)
		if [ $MEF_MODE = YES ]
		then
			DET_ID=`grep "^DET_ID"	$DFO_CONFIG_DIR/$CFG_FILE | awk '{print $2}'`
		fi
# QC1DB
		PLABEL="`grep "^PLOT_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | awk '{print $10}'` (no_title)"
		PLABEL=`echo $PLABEL | awk '{print $1}'`
	
# CHECK_NUMBER: checks if more than one threshold or more than one QC1 table configured, then MULTIPLE
# otherwise considered as single configuration, scoring possible
		CHECK_NUMBER=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | wc -l`
		if [ $CHECK_NUMBER -gt 1 ]
		then
			rm -f $TMP_DIR/sc_list_check_entries
			for PSET in `grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $3}'`
			do
				grep "^SYMBOLS"  $CONFIG_DIR/$CFG | grep "[[:space:]]${PSET}[[:space:]]" | awk '{print $2,$8}' | grep -v NONE  | awk '{print $1}' >> $TMP_DIR/sc_list_check_entries
			done
	
			if [ -s $TMP_DIR/sc_list_check_entries ]
			then
				CHECK_ENTRIES=`cat $TMP_DIR/sc_list_check_entries | sort -u | wc -l`
			else
				CHECK_ENTRIES=0
			fi
	
			if [ $CHECK_ENTRIES -gt 1 ]
			then
				echo "   Plot $P_INDEX, $P:	multiple parameter set, can't do scoring."
				echo "SELECT \"Plot $P_INDEX MULTIPLE DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
				echo "GO" >> $TMP_DIR/sc_score_query
				continue
			elif [ $CHECK_ENTRIES = 0 ]
			then
				echo "   Plot $P_INDEX, $P:	no thresholds configured, no scoring."
				echo "SELECT \"Plot $P_INDEX NOT IMPLEMENTED DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
				echo "GO" >> $TMP_DIR/sc_score_query
				continue
			else
				PARSET=`cat $TMP_DIR/sc_list_check_entries | awk '{print $1}'`
			fi
		else
			PARSET=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $3}'`
		fi
	
		CHECK_NTABLES=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $5}' | sort -u | wc -w`
		if [ $CHECK_NTABLES -gt 1 ]
		then
			echo "   Plot $P_INDEX, $P:	multiple parameter set, can't do scoring."
			echo "SELECT \"Plot $P_INDEX MULTIPLE DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
			echo "GO" >> $TMP_DIR/sc_score_query
			continue
		fi
		QC1_TABLE=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $5}' | sort -u`
		CHECK_NQCPARAMS=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $7}' | sort -u | wc -w`
		if [ $CHECK_NQCPARAMS -gt 1 ]
		then
			echo "   Plot $P_INDEX, $P:	multiple parameter set, can't do scoring."
			echo "SELECT \"Plot $P_INDEX MULTIPLE DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
			echo "GO" >> $TMP_DIR/sc_score_query
			continue
		fi
	
		QC1_PARAM=`grep "^PARSET_NAME"   $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $7}' | sort -u`
		COND=`grep    	"^PARSET_NAME"   $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $9}' | grep -v NONE | sort -u`
		CHECK_NCOND=`grep "^PARSET_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$P[[:space:]]" | grep QC1DB | awk '{print $9}' | sort -u | wc -l`
	
# BWo v1.7: check that configuration for scoreQC includes link to HC
		rm -f $TMP_DIR/hc_raw_types

		grep "^QC1_TABLE" ${DFO_CONFIG_DIR}/$CFG_FILE | grep "[[:space:]]${QC1_TABLE}[[:space:]]" | awk '{print $2}' > $TMP_DIR/hc_raw_types
		CHECK_INDEX=""
		for RT in `cat $TMP_DIR/hc_raw_types`
		do
			CHECK_PGI=`grep "^QC_PARAM" ${DFO_CONFIG_DIR}/$CFG_FILE | grep "[[:space:]]${RT}[[:space:]]" | grep "[[:space:]]${QC1_PARAM}[[:space:]]" | awk '{print $6}' | grep -v HC | cut -c1-1 | egrep -v "[0-9+]|\-" | wc -l`
			if [ $CHECK_PGI -gt 0 ]
			then
# threshold configuration involves pgi script; check cannot be performed
				CHECK_INDEX=$P_INDEX
				break
			fi
	
# otherwise we check that scoreQC takes thresholds for scoring from trendPlotter config file
			CHECK_THRESH=`grep " QC1DB " $REPORT_DIR/statFiles/stat_text_$REPORT_NAME | awk '{print $6,$10}' | grep -v none | grep " ${QC1_PARAM}" | sort -u | awk '{print $1}' | head -1`
			if [ Q$CHECK_THRESH = Q ]
			then
# no threshold configured for ${QC1_PARAM} in config.tp_$REPORT_NAME: nothing to check
				CHECK_INDEX=$P_INDEX
				break	
			fi
	
# threshold configured: check that ${QC1_PARAM} in $CFG_FILE has dynamic thresholds ('HC')
			CHECK_INDEX=`grep "^QC_PARAM" ${DFO_CONFIG_DIR}/$CFG_FILE | grep "[[:space:]]${RT}[[:space:]]" | grep "[[:space:]]${QC1_PARAM}[[:space:]]" | grep "[[:space:]]HC[[:space:]]" | grep "[[:space:]]$REPORT_NAME[[:space:]]" | awk '{print $9}' | grep $P_INDEX | sort -u`
			if [ "Q$CHECK_INDEX" = "Q$P_INDEX" ]
			then
# correct configuration for scoreQC found
				break
			fi
	
# if not correct: check if marked as #STATIC ("yes I know what I'm doing")
			CHECK_INDEX=`grep "^QC_PARAM" ${DFO_CONFIG_DIR}/$CFG_FILE | grep "[[:space:]]${RT}[[:space:]]" | grep "[[:space:]]${QC1_PARAM}[[:space:]]" | grep -v "#STATIC" | awk '{print $1}' | head -1`
			if [ Q$CHECK_INDEX = Q ]
			then
				CHECK_INDEX=$P_INDEX
				break
			fi	
		done

		if [ "Q$CHECK_INDEX" != "Q$P_INDEX" ]
		then
			rm -f $TMP_DIR/config_string $TMP_DIR/config_proposal
			grep "^QC_PARAM" ${DFO_CONFIG_DIR}/$CFG_FILE | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | grep "[[:space:]]${QC1_PARAM}[[:space:]]" | grep -v "[[:space:]]HC[[:space:]]" | grep -v "#STATIC" > $TMP_DIR/config_string
			CONFIG_STRING=`cat $TMP_DIR/config_string | head -1`
	
			if [ -s $TMP_DIR/score_check_mail ]
			then
				CHECK_ALREADY_THERE=`grep "$CONFIG_STRING" $TMP_DIR/score_check_mail`
			fi
	
			if [ -s $TMP_DIR/config_string ] && [ "Q$CHECK_ALREADY_THERE" = Q ]
			then
				cat $TMP_DIR/config_string | awk '{print $1,$2,$3,$4,$5,"HC	HC	"report"	<pindex>"}' report=${REPORT_NAME} | sed "s/ /	/g" | sed "s/pindex/& as appropriate/" > $TMP_DIR/config_proposal
				cat >> $TMP_DIR/score_check_mail <<EOT
*** WARNING: $CFG_FILE for RAW_TYPE ${RAW_TYPE}, QC1 parameter ${QC1_PARAM} uses fixed thresholds
             while HC report ${REPORT_NAME} exists for the same QC1 parameter with configured thresholds.
	     This might indicate an inconsistency and should be checked.
		     
	     This is the line under investigation in $CFG_FILE:
	
	Suspicious configuration:
EOT
				cat $TMP_DIR/config_string >> $TMP_DIR/score_check_mail
				cat >> $TMP_DIR/score_check_mail <<EOT
	
	Suggested configuration:
EOT
				cat $TMP_DIR/config_proposal >> $TMP_DIR/score_check_mail
	
				cat >> $TMP_DIR/score_check_mail <<EOT
	
	     It might also be true that this is a complex case where you may want to keep things unchanged.
	     You could then override this check in the future, by marking this configuration line by '#STATIC' at the end.
EOT
			fi
		fi
	
		rm -f $TMP_DIR/hc_all_score_cond
		if [ "Q$COND" != "Q" ]
	        then
			echo "   Plot $P_INDEX, $P:	collecting scores ..."
# get SCORE_COND
			for CND in `echo "$COND"`
			do
				grep "^SCORE_COND" $CONFIG_DIR/$CFG  | grep "[[:space:]]$CND[[:space:]]" | sort -u | sed "s/^.*[[:space:]]\&\&//" | sed "s/..$//" >> $TMP_DIR/hc_all_score_cond
			done
	
			CONDITION3=`grep "^SCORE_COND" $CONFIG_DIR/$CFG  | grep "[[:space:]]$COND[[:space:]]" | sort -u | sed "s/^.*[[:space:]]\&\&//" | sed "s/..$//"`
			CHECK_NCOND=`cat $TMP_DIR/hc_all_score_cond | wc -l`
			if [ $CHECK_NCOND -gt 1 ] 
			then
				echo "***WARNING: more than one SCORE_CONDition found, can't do scoring:"
				echo "$CONDITION3"
				echo "    Check your configuration: Define only one SCORE_COND."
				echo ""
				echo "SELECT \"Plot $P_INDEX MULTIPLE DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
				echo "GO" >> $TMP_DIR/sc_score_query
				continue
			fi
	
			CONDITION33=`echo $CONDITION3 | awk '{print $1}'`
	
			if [ "Q${CONDITION33}" = "Q" ]
			then
				echo "***WARNING: no SCORE_COND defined yet in $CFG for condition $COND. Can't do scoring."
				echo "   Plot $P_INDEX, $P:	no SCORE_COND defined yet, can't do scoring."
				echo ""
	
				echo "SELECT \"Plot $P_INDEX NOT IMPLEMENTED DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\" " >> $TMP_DIR/sc_score_query
				echo "GO" >> $TMP_DIR/sc_score_query
				continue
			fi
	
			if [ $MEF_MODE = YES ]
			then
				CONDITION31="AND SC.det = QC.${DET_ID} AND $CONDITION3"
				CONDITION3="where $CONDITION3"
			else
				CONDITION31="AND $CONDITION3"
				CONDITION3="where $CONDITION3"
			fi
	
		else
			echo "   Plot $P_INDEX, $P:	collecting scores ..."
		fi
	
# check for compute rule, check for HC_FACTOR in $CFG_FILE 
# reject from scoring if not found (can't compare to thresholds) 
		CHECK_COMP=`grep  "^SYMBOLS" $CONFIG_DIR/$CFG | grep "[[:space:]]$PARSET[[:space:]]" | sed "s/#.*//" | awk '{print $11}' | egrep -v "NO|NONE"`
		CHECK_COMP2=`grep "^SYMBOLS" $CONFIG_DIR/$CFG | grep "[[:space:]]$PARSET[[:space:]]" | sed "s/#.*//" | awk '{print $13}'`
		if [ "Q$CHECK_COMP" != Q ] && [ "Q$CHECK_COMP2" != QYES ]
		then
			echo "   Plot $P_INDEX, parameter $P:	uncompensated compute rule found, can't do scoring."
			echo "SELECT \"Plot $P_INDEX COMPUTED DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL\"" >> $TMP_DIR/sc_score_query
			echo "GO" >> $TMP_DIR/sc_score_query
			continue
		fi
	
# LAST_MJD: per plot (could be different data sets)
#	LAST_MJD = (select max(mjd_obs) from qc1..$QC1_TABLE $CONDITION ) 
# we need also arcfile in qc1_score, to join to qc1_DB tables!
	
# ==========================================================================
# 1.2 Query database (qc1_score)
# ==========================================================================
	
		cat >> $TMP_DIR/sc_score_query <<EOT
SELECT DISTINCT
	"Plot $P_INDEX" ,
	qc1_table , qc1_param, SC.mjd_obs, 
     	convert(varchar(10),SC.civil_date,102),
	qc1_score ,
	qc1_val, thresh1, thresh2,
	score_type,
	"$PLABEL",
	ab_name
FROM
	qc1..qc1_score SC, qc1..$QC1_TABLE QC
WHERE
	SC.arcfile = QC.arcfile
AND
	qc1_param = "$QC1_PARAM"
AND
	qc1_table = "$QC1_TABLE"
AND
	SC.visible = "Y"
	$CONDITION31
AND	
	SC.mjd_obs > (select max(QC.mjd_obs) from qc1..$QC1_TABLE QC $CONDITION3) - $SCORE_DEPTH 
GO
	
select
	"MJD_MAX" ,
	max(QC.mjd_obs)
from 
	qc1..$QC1_TABLE QC 
$CONDITION3
GO
EOT
	done
# end of loop

# send email about inconsistent scoreQC configuration
	if [ -s $TMP_DIR/score_check_mail ]
	then
		mail -s "scoreHC: inconsistency in $CFG_FILE between HC plot $REPORT_NAME and scoring configuration found" $OP_ADDRESS < $TMP_DIR/score_check_mail
	fi
	
	rm -f $TMP_DIR/sc_score_results* 

	if [ -s $TMP_DIR/sc_score_query ]
	then
		echo " "
		echo "  Query qc1_score ..."
		isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i$TMP_DIR/sc_score_query |\
  grep -v "qc1_score" |\
  grep -v "\-\-\-" |\
  grep -v affected |\
  grep -v arcfile |\
  sed "/^$/d" |\
  awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' | sort -nk2,5 |\
  sed "/^ /d" |\
  sed "s/\./-/" |\
  sed "s/\./-/" |\
  sed "s/\./-/" |\
  sed "s/-/\./" \
> $TMP_DIR/sc_score_results2

# if the query fails, the result is a grey score (meaning unknown)

# the last 4 sed's are for correcting the date format
		if [ ! -s $TMP_DIR/sc_score_results2 ]
		then
			CHECK_ERROR=""
		else
			CHECK_ERROR=`egrep "Incorrect syntax near|Server " $TMP_DIR/sc_score_results2 | wc -l`
		fi

		if [ Q$CHECK_ERROR != Q0 ]
		then
			echo "***WARNING: scoreHC -r $REPORT_NAME
		$CHECK_ERROR errors found in query results. This may cause an empty result list (\"no current data\").
		Re-run as 'scoreHC -r $REPORT_NAME -T \$TMP_DIR' and 
		check out \$TMP_DIR/sc_score_results2." > $TMP_DIR/mail
			cat $TMP_DIR/mail
		        mail -s "scoreHC -r $REPORT_NAME" $OP_ADDRESS <$TMP_DIR/mail
		else
			cat $TMP_DIR/sc_score_results2 | grep -v "Incorrect syntax near" | grep -v "^Server" | grep -v "^Msg" | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' | sed "s/.ab$//" > $TMP_DIR/sc_score_results
		fi
	else
		echo "  No qc1_score query."
	fi
	
# ==========================================================================
# 1.3 analyze results
# 1.3.1 start HTML output
# ==========================================================================
	
	echo "  Prepare output ..."
	rm -f $TMP_DIR/score_monitor.html
	cat > $TMP_DIR/score_monitor.html <<EOT
<style type="text/css">
table.details {
        text-align:     left;
        font-family:    arial;
        font-size:      small;
}
</style>
	
<table class="details">
  <tr><td COLSPAN=99><br><hr noshade><br>
  <b><font size=3>2. Extraction from AB monitors</font></b><br>
(list of all ABs which have entered the current score result table, sorted by DATE, extracted from AB monitors either in \$DFO_AB_DIR or in histoMonitor)
    </td>
  </tr>
  <tr bgcolor=#CCCCCC>
    <th valign=top>DATE</th>
    <th valign=top>AB NAME ([local]: in \$DFO_AB_DIR)</th>
    <th valign=top>COMPL.</th>
    <th valign=top>AB LOG</th>
    <th valign=top>RECIPE</th>
    <th valign=top>RAW_TYPE</th>
    <th valign=top>SETUP</th>
    <th valign=top>STATUS</th>
    <th valign=top>P LOG</th>
    <th valign=top>T_EXEC<br>[min]</th>
    <th valign=top>QC_REPORT</th>
    <th valign=top COLSPAN=2>SCORE</th>
    <th valign=top>CERTIF</th>
  </tr>
EOT

	if [ -s $TMP_DIR/sc_score_results2 ]
	then
		for AB in `cat $TMP_DIR/sc_score_results2 | awk '{print $13}' | sort -u`
		do
# Entry for ABs may exist in $DFO_MON_DIR/status_$DATE.html or in $DFO_MON_DIR/FINISHED (histoMOnitor)
# we give preference to the first source, since we assume this is more up-to-date.
		DATE=`grep $AB $TMP_DIR/sc_score_results2 | awk '{print $6}' | head -1 | sed "s/\./-/g"`
		CHECK_EXIST=""
		if [ -s $DFO_MON_DIR/status_$DATE.html ]
		then
			CHECK_EXIST=`grep $AB $DFO_MON_DIR/status_$DATE.html | head -1 | awk '{print $1}'`
			if [ Q$CHECK_EXIST != Q ]
			then
# we filter out the "^LOOKUP" information in the status page, as well as the columns with dependency information
				grep $AB $DFO_MON_DIR/status_$DATE.html |\
 grep -v "^LOOKUP" |\
 sed "s|<td><font size=1>CAL.*CAL[0-9]*?</font></td>||" |\
 sed "s|<td><font size=1>CAL.*SCI[0-9]*?</font></td>||" |\
 sed "s/BGCOLOR=.......><font size=1>\&nbsp;/BGCOLOR=#CCCCCC><font size=2><b>[local]/" \
 >> $TMP_DIR/score_monitor.html
			fi
		fi
	
		if [ ! -s $DFO_MON_DIR/status_$DATE.html ] || [ Q$CHECK_EXIST = Q ]
		then
			YEAR=`echo $DATE | cut -c1-4`
			YM=`echo $DATE | cut -c1-7`
			if [ -s $DFO_MON_DIR/FINISHED/$YEAR/$YM/status_$DATE.html ]
			then
# filter out as above
				grep $AB $DFO_MON_DIR/FINISHED/$YEAR/$YM/status_$DATE.html |\
 grep -v "^LOOKUP" |\
 sed "s|<td><font size=1>CAL.*CAL[0-9]*?</font></td>||" |\
 sed "s|<td><font size=1>CAL.*SCI[0-9]*?</font></td>||" |\
 sed "s/BGCOLOR=.......><font size=1>\&nbsp;/BGCOLOR=#CCCCCC><font size=2>$DATE/" \
>> $TMP_DIR/score_monitor.html
			fi
		fi
		done
	fi

	echo "</table>" >> $TMP_DIR/score_monitor.html
	
# fill in gaps for reports without current data  
	rm -f $TMP_DIR/sc_score_results1
	
	for PN in `cat $TMP_DIR/sc_plotnames | awk '{print $2}' | sort -n`
	do
		if [ -s $TMP_DIR/sc_score_results ]
		then
			CHECK_ENTRY=`grep "^Plot $PN " $TMP_DIR/sc_score_results | head -1`
		fi
		PLABEL="`grep "^PLOT_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$PN[[:space:]]" | awk '{print $10}'` (no_title)"
		PLABEL=`echo $PLABEL | awk '{print $1}'`
		ENABLED=`grep "^PLOT_NAME" $CONFIG_DIR/$CFG | grep "[[:space:]]$PN[[:space:]]" | awk '{print $11}'`
# not implemented: skip
		if [ Q$ENABLED = QNO ]
		then
			echo "Plot $PN NOT IMPLEMENTED DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL" >> $TMP_DIR/sc_score_results1
			echo "Plot $PN NOT IMPLEMENTED DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL" >> $TMP_DIR/sc_score_results2
# empty dataset: no values 
		elif [ "Q$CHECK_ENTRY" = "Q" ]
		then
			echo "Plot $PN EMPTY DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL" >> $TMP_DIR/sc_score_results1
# to have complete scoreManager.html file
			echo "Plot $PN EMPTY DATASET DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY DUMMY $PLABEL" >> $TMP_DIR/sc_score_results2
# results
		else
			grep "^Plot $PN " $TMP_DIR/sc_score_results >> $TMP_DIR/sc_score_results1
		fi
	done
	mv $TMP_DIR/sc_score_results1 $TMP_DIR/sc_score_results
	
# ==========================================================================
# 1.3.2 total score: sum up all *last* entries
# ==========================================================================
	
	rm -f $TMP_DIR/sc_total_score
	for PN in `cat $TMP_DIR/sc_score_results | awk '{print $2}' | sort -u`
	do
		grep "^Plot $PN " $TMP_DIR/sc_score_results | egrep -v "MULTIPLE|COMPUTED|EMPTY|IMPLEMENTED" | tail -1 >> $TMP_DIR/sc_total_score
	done
	
	TOTAL_SCORE=`cat $TMP_DIR/sc_total_score | awk '{sum+=$7} END {print sum}'`
	if [ Q$TOTAL_SCORE = Q ]
	then
		TOTAL_SCORE=-99
	fi
	
# ==========================================================================
# 1.3.3 update entry in stat_grp_$REPORT_GROUP (TMODE = HEALTH or FULL only)
# ==========================================================================
	
	rm -f $TMP_DIR/stat_text_ALL
	GRP_STAT=`echo stat_grp_${REPORT_GROUP} | sed "s/\.grp//"`	#${REPORT_GROUP}: e.g. DETECTOR.grp
	
	if [ $TMODE = HEALTH ] || [ $TMODE = FULL ]
	then
# lock
		#singleAccess ${GRP_STAT}
	
		#if [ -f $REPORT_DIR/statFiles/${GRP_STAT}.lock ]
		if [ -f $REPORT_DIR/statFiles/${GRP_STAT} ]
		then
			grep -v "^${REPORT_NAME}[[:space:]]" $REPORT_DIR/statFiles/${GRP_STAT} > $TMP_DIR/${GRP_STAT}
			echo "${REPORT_NAME} $TOTAL_SCORE" >> $TMP_DIR/${GRP_STAT}
			mv $TMP_DIR/${GRP_STAT} $REPORT_DIR/statFiles/
		else
			echo "${REPORT_NAME} $TOTAL_SCORE" >> $REPORT_DIR/statFiles/${GRP_STAT}
		fi
	fi
	
	if [ $REPORT_GROUP != none ]
	then
		grep "^GROUP_MEMBERS" $CONFIG_DIR/$REPORT_GROUP | grep -v SPACE | awk '{print $2,$3}' > $TMP_DIR/tp_list_groupmembers
	else
		echo "&nbsp;" > $TMP_DIR/tp_list_groupmembers
	fi
	
# ==========================================================================
# 1.4 update entries in stat_all_GRP
# 1.4.1 find all group files 
# ==========================================================================
	
# take $CONFIG_VNAVBAR as reference: all items listed as nav-subitem should have a $GRP file
	grep "^NAVBAR" $DFO_CONFIG_DIR/webNavBar/$CONFIG_VNAVBAR |\
 grep nav-subitem |\
 egrep -v "^NAVBAR[[:space:]]*SPACE" |\
 awk '{print $6}' |\
 sed "s/vnavbar_HC_/stat_grp_/" |\
 sed "s/.html//" > $TMP_DIR/sc_list_grp
	
	rm -f $TMP_DIR/stat_all_GRP

	for GRP in `cat $TMP_DIR/sc_list_grp`
	do
		if [ -e $REPORT_DIR/statFiles/$GRP ]
		then
			GRP_SCVAL=`cat $REPORT_DIR/statFiles/$GRP | grep -v "\-99" | awk '{sum+=$2} END {print sum}'`
# cosmetics	
			LENGTH=`echo $GRP | wc -c`
			if [ $LENGTH -lt 16 ]
			then
				echo "$GRP	$GRP_SCVAL" >> $TMP_DIR/stat_all_GRP
			else
				echo "$GRP	$GRP_SCVAL" >> $TMP_DIR/stat_all_GRP
			fi
		else
			echo "***WARNING: $GRP file expected but not found in $REPORT_DIR/statFiles."
		fi
	done

# check for lock by another scoreHC
	singleAccess stat_all_GRP
	mv $TMP_DIR/stat_all_GRP $REPORT_DIR/statFiles/stat_all_GRP

# ==========================================================================
# 1.4.2 update instrument score and transfer to web server
# ==========================================================================
	
	echo "  Update scores, transfer to $DFO_WEB_SERVER ..."
	if [ -s $REPORT_DIR/statFiles/stat_all_GRP ]
	then
		INSSCORE=`cat $REPORT_DIR/statFiles/stat_all_GRP | grep -v none | awk '{sum+=$2} END {print sum}'`
	fi

# default: -99
	if [ Q$INSSCORE = Q ]
	then
       		INSSCORE=-99
	fi

	case $INSSCORE in
	   0   ) SCORE_IMG="${IMG_SRC1}/$OK_IMG"    ; SCORE_TITLE="${DFO_INSTRUMENT}: all groups scored ok" ; 		SC_WIDTH=11; SC_HEIGHT=11 ;;
	   -99 ) SCORE_IMG="${IMG_SRC1}/$NULL_IMG"  ; SCORE_TITLE="${DFO_INSTRUMENT}: no scoring implemented" ; 	SC_WIDTH=5;  SC_HEIGHT=5 ;;
	   *   ) SCORE_IMG="${IMG_SRC1}/$ALERT_IMG" ; SCORE_TITLE="${DFO_INSTRUMENT}: at least one group scored NOK" ;  SC_WIDTH=11; SC_HEIGHT=12 ;;
	esac
	
# unlock stat_all_GRP
	rm -f $REPORT_DIR/statFiles/stat_all_GRP.lock
	
# prevent score result from being cached
	TODAY=`date +%Y-%m-%d`
	PSEUDO_ID="`qcdate ${TODAY}`.$$"
	
	echo "<img src=\"${SCORE_IMG}?${PSEUDO_ID}\" width=${SC_WIDTH} height=${SC_HEIGHT} border=0 title=\"${SCORE_TITLE}\">" > $TMP_DIR/score_ins.html

	scp -o BatchMode=yes $TMP_DIR/score_ins.html ${DFO_WEB_SERVER}:${NAVBAR_DIR}  1>/dev/null
	
# ==========================================================================
# 1.4.3 update group scores and transfer to web server
# ==========================================================================
	
	rm -f $TMP_DIR/sc_list_navbars
	
	cat $TMP_DIR/sc_list_items | grep -v SPACE | awk '{print $2}' > $TMP_DIR/sc_list_navbars

	PSEUDO_ID="`qcdate ${TODAY}`.$$"

	RG=`echo $REPORT_GROUP | sed "s/.grp//"`
	SCORE_GRP_NAME="score_grp_$RG.html"
	rm -f $TMP_DIR/$SCORE_GRP_NAME
	
# lock again
# default: -99
	singleAccess stat_all_GRP	#lock again
	if [ -s $REPORT_DIR/statFiles/stat_all_GRP.lock ]
	then
		GRPSCORE=`grep "^stat_grp_${RG}[[:space:]]" $REPORT_DIR/statFiles/stat_all_GRP.lock | awk '{print $2}'`
	fi

	if [ "Q$GRPSCORE" = "Q" ]
	then	
		GRPSCORE=-99
	fi

	case $GRPSCORE in
  	  0   ) SCORE_IMG="${IMG_SRC1}/$OK_IMG"    ; SCORE_TITLE="$RG: all group reports scored ok"    ; SC_WIDTH=8; SC_HEIGHT=8 ;;
  	  -99 ) SCORE_IMG="${IMG_SRC1}/$NULL_IMG"  ; SCORE_TITLE="$RG: no scoring implemented" 	       ; SC_WIDTH=5; SC_HEIGHT=5 ;;
  	  *   ) SCORE_IMG="${IMG_SRC1}/$ALERT_IMG" ; SCORE_TITLE="$RG: at least one report scored NOK" ; SC_WIDTH=10; SC_HEIGHT=10 ;;
	esac

# unlock stat_all_GRP
	if [ -s $REPORT_DIR/statFiles/stat_all_GRP.lock ]
	then
		mv $REPORT_DIR/statFiles/stat_all_GRP.lock  $REPORT_DIR/statFiles/stat_all_GRP
	fi
	
	echo "<img src=\"${SCORE_IMG}?${PSEUDO_ID}\" width=${SC_WIDTH} height=${SC_HEIGHT} border=0 title=\"${SCORE_TITLE}\">" > $TMP_DIR/$SCORE_GRP_NAME
	scp -o BatchMode=yes $TMP_DIR/$SCORE_GRP_NAME ${DFO_WEB_SERVER}:${NAVBAR_DIR}  1>/dev/null
	
# ==========================================================================
# 1.4.4 update report scores for this group and transfer to web server
# ==========================================================================
	
	for RN in `cat $TMP_DIR/tp_list_groupmembers | awk '{print $2}'`
	do
# default: -99
		#singleAccess $GRP_STAT
# unlock; singleAccess doesn't work 100% safe
		#if [ -s $REPORT_DIR/statFiles/$GRP_STAT.lock ]
		if [ -s $REPORT_DIR/statFiles/$GRP_STAT ]
		then
			TOTSCORE=`grep "^$RN[[:space:]]" $REPORT_DIR/statFiles/$GRP_STAT | awk '{print $2}'`
		fi

		if [ Q$TOTSCORE = Q ]
		then
			TOTSCORE=-99
		fi

		case $TOTSCORE in
	 	 0   ) SCORE_IMG="${IMG_SRC1}/$OK_IMG"    ; SCORE_TITLE="all values scored ok" ;	SC_WIDTH=8; SC_HEIGHT=8;;
	 	 -99 ) SCORE_IMG="${IMG_SRC1}/$NULL_IMG"  ; SCORE_TITLE="no scoring implemented" ;	SC_WIDTH=4; SC_HEIGHT=4;;
	 	 *   ) SCORE_IMG="${IMG_SRC1}/$ALERT_IMG" ; SCORE_TITLE="at least one value scored NOK"; SC_WIDTH=10; SC_HEIGHT=10;;
	
		esac
		SCORE_HTML="score_$RN.html" 
		PSEUDO_ID="`qcdate ${TODAY}`.$$"
		rm -f $TMP_DIR/$SCORE_HTML
	
		echo "<img src=\"${SCORE_IMG}?${PSEUDO_ID}\" width=$SC_WIDTH height=$SC_HEIGHT border=0 title=\"${SCORE_TITLE}\">" > $TMP_DIR/$SCORE_HTML
	
		scp -o BatchMode=yes $TMP_DIR/${SCORE_HTML} ${DFO_WEB_SERVER}:${NAVBAR_DIR}  1>/dev/null
	done
	
	case $TOTAL_SCORE in
	 0   ) SCORE_IMG="${IMG_SRC1}/$OK_IMG"    ; SCORE_TITLE="total report score: all values scored ok" ; SC_WIDTH=16; SC_HEIGHT=16;;
	 -99 ) SCORE_IMG="${IMG_SRC1}/$NULL_IMG"  ; SCORE_TITLE="no scoring implemented for this report" ;SC_WIDTH=8; SC_HEIGHT=8;;
	 *   ) SCORE_IMG="${IMG_SRC1}/$ALERT_IMG" ; SCORE_TITLE="total report score: at least one value scored NOK"; SC_WIDTH=13; SC_HEIGHT=14;;
	esac
	
# ==========================================================================
# 1.4.5 continue HTML output
# ==========================================================================
	
	rm -f $TMP_DIR/LAST_SCORE
	PSEUDO_ID="`qcdate ${TODAY}`.$$"
	LAST_SCORE=`grep "^MJD_MAX " $TMP_DIR/sc_score_results2 | awk '{print $2}' | sort -u | tail -1`
	qcdate `echo $LAST_SCORE $DFO_OFFSET | awk '{printf"%10f5\n", $1-$2/24-0.5}'` > $TMP_DIR/LAST_SCORE
	LAST_SCORE=`cat $TMP_DIR/LAST_SCORE`
	
	if [ Q$LAST_SCORE = Q ] || [ Q$LAST_SCORE = QERROR ]
	then
		LAST_SCORE="(scores)"
	else	
		LAST_SCORE="(scores, last $SCORE_DEPTH days up to ${LAST_SCORE}*)"
	fi
	
	cat > $TMP_DIR/sc_HC_table <<EOT
<!-- javascript -->
<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=1000,height=700,scrollbars=yes,menubar=yes");
  }
//-->
</script>
	
<!-- ROW8: score results -->
  <tr align="left" valign="top">
    <td bgcolor="#CCCCCC">

    <table border="0" cellspacing="1" cellpadding="0" bgcolor="${SCORE_TAB_COL}">
      <tr>
        <td bgcolor="#EEEEEE">

    <table border="0" cellspacing="1" cellpadding="1">
      <tr bgcolor=#CCCCCC>
        <td COLSPAN=99 WIDTH=$WIDTH nowrap><font size=2><b>$ADD_TEXT ${LAST_SCORE}</b></font></td>
      </tr>
      <tr bgcolor=#CCCCCC>
        <td ROWSPAN=10 width=10 align=center valign=top>
<img src="$SCORE_IMG?${PSEUDO_ID}" width=${SC_WIDTH} height=${SC_HEIGHT} border=0 title="$SCORE_TITLE">
</td>
EOT
	
	if [ ! -s $TMP_DIR/sc_score_results ]
	then
		CHECK_RESULTS=""
	else
		CHECK_RESULTS=`cat $TMP_DIR/sc_score_results | head -1`
	fi
	
	if [ "Q$CHECK_RESULTS" = "Q" ]
	then
		cat >> $TMP_DIR/sc_HC_table <<EOT
   <td COLSPAN=99 nowrap><font size=2>No scoring available</font></td>
  </tr>
</table>

</table>
</body>
</html>
EOT
		echo "    No scoring results found!"
# transfer to web server 
		scp -o BatchMode=yes $TMP_DIR/sc_HC_table ${DFO_WEB_SERVER}:${QC_DIR}/$TMODE/score_${REPORT_NAME}_QUICK.html  1>/dev/null
		if [ $EXTERNAL = NO ]
		then
			rm -r $TMP_DIR
		fi
		exit
	fi
	
# ==========================================================================
# 2. Score boxes and news (message file)
# 2.1 Create score boxes
# ==========================================================================
	
	echo "  Finish QUICK-LOOK report, transfer to $DFO_WEB_SERVER ..."
	ssh -o BatchMode=yes $DFO_WEB_SERVER "bin/qcDircheck ${SCORE_TT_DIR}" 1>/dev/null
	fillReport 
	
# ==========================================================================
# 2.2 Add message file
# ==========================================================================
	
	cat >> $TMP_DIR/sc_HC_table <<EOT
	  </td></tr>
	  <tr><td bgcolor=#EEEEEE>
	
	<table width=100% cellspacing=1 cellpadding=1>
EOT
	
# to prepare local version
	cat $TMP_DIR/sc_HC_table | sed "1,12d" | sed "s/$SCORE_TAB_COL/#FFFFFF/" > $TMP_DIR/sc_HC_table_loc1
	
# ==========================================================================
# 2.3 Quality comments 
# ==========================================================================
	
	if [ -s $TMP_DIR/score_comments ]
	then
		cat >> $TMP_DIR/sc_HC_table_loc2 <<EOT
  <tr><td bgcolor=#EEEEEE>
	
<table width=100% cellspacing=1 cellpadding=1>
  <tr>
    <td bgcolor=#999999><font size=2 color=#FFFFFF><b><a title="these are comments entered by the QC scientist during certification">Quality comments (certified nights only):</a></b></font></td>
  </tr>
  <tr><td bgcolor=#eeeeee>
<table cellspacing=1 cellpadding=1><tr bgcolor=#CCCCCC>
  <td><font size=1><b>Plot</b></td>
  <td><font size=1><b>Date*</b></td>
  <td><font size=1><b>AB name (linked to score report)</b></td>
  <td><font size=1><b>score</b></td>
  <td><font size=1><b>product quality comment</b></td>
</tr>
EOT

# DXTY and SCFAS chosen as delimiters with little risk to be confused
		cat $TMP_DIR/score_comments |\
sort -n -k1,3 |\
awk '{ if ( change == 1 ) { change=0; print $1,"DXTY"$2"DXTY","SCORE"$2"SCFAS"$3"SCORE","AA"$4"BB",$5"BGCOLOR#DDDDDD"}
       else               { change=1; print $1,"DXTY"$2"DXTY","SCORE"$2"SCFAS"$3"SCORE","AA"$4"BB",$5"BGCOLOR#CCCCCC"} }' |\
sed "s| |</td><td><font size=1>|"g  |\
sed "s|^.*|<tr bgcolor=REPLACE><td nowrap><font size=1><b>Plot &</td></tr>|" |\
sed "s|(||" |\
sed "s|)||" |\
sed "s|_| |g" |\
sed "s| tpl|_tpl|g" |\
sed "s|AA0BB|<center><img src=\"${IMG_SRC1}/${OK_IMG}?${PSEUDO_ID}\" width=4 height=4 border=0>|g" |\
sed "s|AA1BB|<center><img src=\"${IMG_SRC1}/${ALERT_IMG}?${PSEUDO_ID}\" width=6 height=7 border=0>|g" |\
sed "/#DDDDDD/s/bgcolor=REPLACE/bgcolor=#DDDDDD/" |\
sed "/#CCCCCC/s/bgcolor=REPLACE/bgcolor=#CCCCCC/" |\
sed "s/BGCOLOR#DDDDDD//" | sed "s/BGCOLOR#CCCCCC//" |\
sed "s|DXTY.*DXTY|<a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs/&/status_&.html target=_blank title=\"AB product page\">&</a>|" |\
sed "s|SCORE.*SCFAS|<a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/logs/&/SCFAS|" |\
sed "s|SCFAS.*SCORE|&.html target=_blank>&</a>|" |\
sed "s/DXTY//"g |\
sed "s/SCORE//"g | sed "s/SCFAS//"g | sed "s/.ab.html/.html/" | sed "s|target=_blank>/|target=_blank title=\"score report\">|" \
 >> $TMP_DIR/sc_HC_table_loc2
	
		cat >> $TMP_DIR/sc_HC_table_loc2 <<EOT
<tr bgcolor=#DDDDDD><td COLSPAN=5 align=right><font size=1 color=#333333><i>"Date" links to complete AB product page; "AB" links to score report</i></font></td></tr>
</table>
   </td></tr>
  </table>
  </td></tr>
EOT
	fi
	
	cat >> $TMP_DIR/sc_HC_table_loc2 <<EOT
	  <tr><td>
EOT
	
	cat $TMP_DIR/sc_HC_table_loc2 >> $TMP_DIR/sc_HC_table
	
# ==========================================================================
# 3. scoreManager: tool to assist with scoring management (edit/delete scores)
# 3.1 scManager_<report>.html page
# ==========================================================================
# scoreManager: has one page per report (scManager_<report>.html), plus the main page scoreManager.html, 
# all under $DFO_TREND_DIR/reports/scoreManager
	
	TIMESTAMP=`date -u +%Y-%m-%d"T"%H:%M:%S`

# this page will become scManager_<report>.html
	cat > $TMP_DIR/sc_HC_table1 <<EOT
<html>
<head>
<title>HC ScoreManager (instrument: $DFO_INSTRUMENT; report: ${REPORT_NAME})</title>
<!-- javascript -->
<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=1200,height=700,scrollbars=yes,menubar=yes");
  }
//-->
</script>
<link rel="StyleSheet" href="http://www.eso.org/qc/ALL/common/qc_eso.style" type="text/css">
</head>

<body class=page>
<table border=0>
  <tr>
    <td width=5% valign=top bgcolor=#CCCCCC>
EOT

# this navbar is created by $DFO_GUI_DIR/refresh_gui_navbar.esh@dfoMonitor
	if [ -s $DFO_MON_DIR/vnavbar_scoreHC.html ]
	then
		cat $DFO_MON_DIR/vnavbar_scoreHC.html >> $TMP_DIR/sc_HC_table1
	else
		echo "$DFO_MON_DIR/vnavbar_scoreHC.html TBD!" >> $TMP_DIR/sc_HC_table1
	fi

	cat >> $TMP_DIR/sc_HC_table1 <<EOT
</td>
<td>
<h2> <font color=${HC_COLOR}> $DFO_INSTRUMENT HC ScoreManager (report: ${REPORT_NAME})</font></h2>
<font size=2>back to: <a href=scoreManager.html title="main page scoreManager">main</a><br>
Last update: $TIMESTAMP (UT)<p>
	
<table>
  <tr>
    <td bgcolor=#EEEEEE align=right><font size=2><b><font color="$HC_COLOR">${DFO_INSTRUMENT} news:</font></b></td>
    <td align="left" bgcolor="#ffff99" valin=middle colspan=2>
EOT

# add general and instrument news from HC monitor
	if [ ! -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ]
	then
		touch $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT
	fi
	
	echo "<font color=$HC_COLOR size=2>" >> $TMP_DIR/sc_HC_table1
	cat $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT >> $TMP_DIR/sc_HC_table1

# add edit and refresh buttons; edit POSTIT and refresh: local under $DFO_GUI_DIR; edit comment: php interface
	cat >> $TMP_DIR/sc_HC_table1 <<EOT
    </font></td>
   <td align=left bgcolor=#eeeeee>
   <a href=$DFO_GUI_DIR/edit_trendP_${DFO_INSTRUMENT}_POSTIT.esh title="edit ${DFO_INSTRUMENT}_POSTIT.esh"><img src="${DFOS_URL}/edit.png" border=0></a> 
  </td>
  </tr>
	
<form method="post" action="http://www.eso.org${QC_URL}/php/hcComments.php">
  <tr>
    <td bgcolor=#EEEEEE align=right valign=top>
	<font size=2><b><font color="$HC_COLOR">Report news:</font></b>
    </td>
    <td align="left" bgcolor="#ffff99" valign=top colspan=2>
      <font color=$HC_COLOR size=2>
EOT
	
# make sure that $MSG always exists on $DFO_WEB_SERVER; TMODE = HEALTH or FULL
	echo "  Transfer msg file ..."
	rm -f $CONFIG_DIR/$MSG
	ssh -o BatchMode=yes $DFO_WEB_SERVER "if [ ! -s $QC_DIR/$TMODE/$MSG ]; then touch $QC_DIR/$TMODE/$MSG 2>/dev/null; fi"
	scp -o BatchMode=yes ${DFO_WEB_SERVER}:${QC_DIR}/$TMODE/$MSG $CONFIG_DIR/ 1>/dev/null
	if [ ! -s $CONFIG_DIR/$MSG ]
	then
		echo "<!--SEP--><!--COMMENT-->" > $CONFIG_DIR/$MSG
	fi
		
	cat $CONFIG_DIR/$MSG >> $TMP_DIR/sc_HC_table1
	
# end of form for comment editor
	cat >> $TMP_DIR/sc_HC_table1 <<EOT
    </font></td>
  <td align="left" bgcolor=#eeeeee>
   <a href="javascript:openWin('http://www.eso.org${QC_URL}/php/hcComments.php?reportSelect=${REPORT_NAME}&origin=local');" title="edit $MSG"><img src="${DFOS_URL}/edit.png" border=0></a> 
   <a href="$DFO_GUI_DIR/refresh_sc${REPORT_NAME}.esh" title="The php form updates the msg file on the web but cannot do this for the local version, so you do it here"><img src="${DFOS_URL}/refresh.png" border=0></a><br>
<font size=1>If you have edited the report news, they are visible on the Web<br> but not locally. Then you need to refresh the page.</font>
    </td>
  </tr>
</form>
	
  <tr>
    <td bgcolor=#EEEEEE align=right>
  <font size=2><a href=http://www.eso.org/${QC_URL}/${TMODE}/trend_report_${REPORT_NAME}_HC.html target=_blank title="(first) associated HC report trend_report_${REPORT_NAME}_HC.html (there might be more)">HC report</a>
    </td>
    <td bgcolor=#EEEEEE>
  <font size=2><a href=$DFO_GUI_DIR/edit_tp_${REPORT_NAME}.esh title="edit $TREND_TOOL configuration file config.tp_${REPORT_NAME}">
  <img src=${IMG_SRC1}/redball.gif width=12 height=12 border=0>edit config.tp file</a>
    </td>
    <td bgcolor=#EEEEEE align=left>
  <font size=2><a href=${CONFIG_DIR}/config.tp_${REPORT_NAME} title="view $TREND_TOOL configuration config.tp_${REPORT_NAME}">
  view $TREND_TOOL config.tp file</a>
    </td>
    <td bgcolor=#eeeeee><font size=2>&nbsp;</font></td>
  </tr>
</table> <p>
	
<table width=90% bgcolor=#CCCCCC cellspacing=1>
 <tr align="left" valign="top">
    <td bgcolor="#FFFFFF">

<b>1. Score results</b><br>
<font size=2>This is the score table which displays on the QUICK-LOOK report version; click on score symbols to connect to score report.
EOT

# section 1	
	cat $TMP_DIR/sc_HC_table_loc1 $TMP_DIR/sc_HC_table_loc2 >> $TMP_DIR/sc_HC_table1
	echo "
    </td>
  </tr>
</table>
</body>
</html>" >> $TMP_DIR/sc_HC_table1

# fill some tutorial
	cat >> $TMP_DIR/sc_HC_table <<EOT
  <tr><td>
<p>
<font size=2><br>
<b>Scores</b> <p>
This is the quick-look version of the HealthCheck (HC) Monitor. 
It displays <i><a href="${HELP_URL}#quick-look" target=_blank>scores</a></i> for each HC report. Scores are flags assessing the compliance of data points with configured thresholds. A green score indicates that the corresponding instrument component is performing as expected (OK) in the monitored time range. 
A red score is the result of a parameter exceeding these thresholds, and may indicate a problem.

<p> Scoring has both an automatic and an interactive component. 
The <i>automatic</i> part consists of monitoring, pipeline-processing, and flagging new data once per hour, 24/7. 
The <i>interactive</i> component is the certification step which is performed by the QC scientist. It involves the analysis of red scores:
A confirmation of their validity, an assessment of the severity of the red score and its cause, and often an analysis comment in the AB monitor.  In cases of serious instrument issues,  feedback to Paranal operations and/or USD is given.
However, this analysis step cannot be guaranteed to be available at the same time as the automatic scoring. <i>Do not expect new analysis results during weekends, holidays or other off-duty times.</i> 

<p>The last score, per plot, will determine the current plot score, which is displayed as a larger symbol. The other, older plot scores display only the <i>recent history</i> of instrument scores.
As part of the <i>automatic</i> scoring, a red score will be automatically overwritten by any new data that produce a green score. 
The date of any score can be seen by mousing over its score symbol. 
As part of the <i>interactive</i> analysis, there is also the possibility for the QC scientist to manually adjust thresholds and turn a red score into a green one, or to delete a false red score. Often this occurs after interaction with Paranal staff, after discussing the significance of the alert score. 
Use the 'contact' link to get in touch.
	
<p>If available, a table with quality comments is displayed on the <i>scores&comments</i> page,  usually offering additional information about data with red scores. 
A comment may refer to the quality of the pipeline products and not necessarily to the exact parameter scored in this report.
Quality comments are entered by the QC scientist as part of the analysis workflow.

<p>Scores are based on the QC data for the last ${SCORE_DEPTH} days before the indicated date. This date is always the last date with data for this report.

<p> The 'Quality comments' are collected from the QC scientist certification review and are available for certified nights.
	 
<p><i>Information on demand:</i><br>
- scroll over the small symbols and see the individual dates of the underlying data.<br>
- click on them and see the detailed score report for that particular data set.
<p>
 <a href="javascript:openWin('${HELP_URL}#quick-look');" title="more about quick-look and scores">More ...</a><br>
</td>
EOT

	cat >> $TMP_DIR/sc_HC_table <<EOT
    </tr>
  </table>

      </td>
    </tr>
  </table>

<!-- end main table -->
    </td>
  </tr>
</table>
	
<!-- end backbone table -->
    </td>
  </tr>
</table>
	
</body>
</html>
EOT
	
# transfer to web server 
	echo "  Transfer score files ..."
	ssh -o BatchMode=yes $DFO_WEB_SERVER "rm -f ${QC_DIR}/$TMODE/score_${REPORT_NAME}_QUICK.html"
	chmod uo+rwx,o+rx,o-w $TMP_DIR/sc_HC_table
	scp -o BatchMode=yes $TMP_DIR/sc_HC_table ${DFO_WEB_SERVER}:${QC_DIR}/$TMODE/score_${REPORT_NAME}_QUICK.html  1>/dev/null
	scp -o BatchMode=yes $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ${DFO_WEB_SERVER}:${QC_DIR} 1>/dev/null
	
	echo "Plot    table        parameter     mjd_obs      DATE       S P_value     thresh1    thresh2    meth Plabel" > $TMP_DIR/sc_score_results1
	echo "==========================================================================================================" >> $TMP_DIR/sc_score_results1
	cat $TMP_DIR/sc_score_results >> $TMP_DIR/sc_score_results1
	mv $TMP_DIR/sc_score_results1 $TMP_DIR/sc_score_results
	
	cat $TMP_DIR/sc_HC_table1 > $TMP_DIR/scoreManager.html
	cat $TMP_DIR/score_monitor.html >> $TMP_DIR/scoreManager.html
	
	if [ Q$MEF_MODE = QYES ]
	then
		ADDED_COMMENT="<b>MEF</b>: note that all buttons work per AB!<br>"
	else
		ADDED_COMMENT=""
	fi 
	
	cat >> $TMP_DIR/scoreManager.html <<EOT
	
<table bgcolor=#FFFFFF>
  <tr><td COLSPAN=99><br><hr noshade><br>
  <b>3. Details per HC plot and QC1 parameter</b><br>
<font size=2>This is a list of all QC1 parameters and scores for the $REPORT_NAME report on the HC monitor. It is not complete in terms of ABs.<br>
 This list can be used to correct, remove or override scores interactively.<br>
$ADDED_COMMENT
	
<table class="details"> 
  <tr bgcolor=#EEEEEE>
    <td valign=top width=20% bgcolor=#EEEEEE><img src=${IMG_SRC1}/redball.gif width=12 height=12 border=0> Workflow for correction: </td>
    <td>1a. modify configuration </td>
    <td>1b. call $TREND_TOOL for report --&gt; update stat_text file and plots</td>
    <td>1c. call scoreQC for AB --&gt; write new scores into db</td>
    <td width=30% valign=top>2. call scoreHC --&gt; update HC monitor</td>
  </tr>
  <tr bgcolor=#eeeeee>
    <td valign=top><img src=${IMG_SRC1}/blueball.gif width=12 height=12 border=0> Workflow for deletion: </td>
    <td colspan=3>1. delete all scores for selected AB in db </td>
    <td>2. call scoreHC --&gt; update HC monitor</td>
  </tr>
  <tr bgcolor=#eeeeee>
   <td valign=top><img src=${IMG_SRC1}/greenball.gif width=12 height=12 border=0> Workflow for NOK override: </td>
   <td colspan=3>1. force all scores for selected AB to green/OK </td>
   <td>2. call scoreHC --&gt; update HC monitor</td></tr>
</table>
	
</td></tr>
  <tr bgcolor=#CCCCCC>
    <td><font size=2><b>Action</td>
    <td><font size=2><b>Plot</td>
    <td><font size=2><b>QC1 table</td>
    <td><font size=2><b>QC1 param</td>
    <td><font size=2><b>DATE</td>
    <td><font size=2><b>MJD_OBS</td>
    <td><font size=2><b>score</td>
    <td><font size=2><b>AB</td>
  </tr>
EOT
	
	rm -f $OTMP_DIR/${REPORT_NAME}/launch_score_*esh $OTMP_DIR/${REPORT_NAME}/launch_remove_*esh $OTMP_DIR/${REPORT_NAME}/launch_override_*esh

	echo "  Create scoreManager ..."	

	for PN in `cat $TMP_DIR/sc_score_results2 | grep -v "^MJD_MAX" | awk '{print $2}' | sort -nu`
	do
		cat $TMP_DIR/sc_score_results2 |\
 sed "s/\./YY/" | sed "s/\./-/" | sed "s/\./-/" | sed "s/YY/./" |\
 grep "Plot $PN " |\
 awk '{if ($3 == "MULTIPLE" || $3 == "COMPUTED" || $3 == "EMPTY" || $3 == "NOT") {print TR3 TD "&nbsp;" TD2 BC $1,$2 ":" BC2 TD2 $3 TD2 $4 "</td><td colspan=99>" REND } 
  else if ($7 == 0) {print TR1 "<td bgcolor=#CCCCCC><a href="tmp"/launch_score_"$13".esh><img src="img_src"/redball.gif width=12 height=12 border=0 title=\"call scoreQC -a "$13"\"></a></td>" TD BC $1,$2":" BC2 TD2 $3 TD2 $4 TD2 $6 TD2 $5 TD2 BC $7 BC2 TD2 $13 REND } 
  else {print TR2 "<td bgcolor=#CCCCCC><a href="tmp"/launch_score_"$13".esh><img src="img_src"/redball.gif width=12 height=12 border=0 title=\"call scoreQC -a "$13"\"></a>&nbsp;<a href="tmp"/launch_remove_"$13".esh><img src="img_src"/blueball.gif width=12 height=12 border=0 title=\"remove this score from database\"></a>&nbsp;<a href="tmp"/launch_override_"$13".esh><img src="img_src"/greenball.gif width=12 height=12 border=0 title=\"override this score in database\"></a></td>" TD BC $1,$2":" BC2 TD2 $3 TD2 $4 TD2 $6 TD2 $5 TD2 BC $7 BC2 TD2 $13 REND }}' \
  TR1="<tr bgcolor=#33FF33>" \
  TR2="<tr bgcolor=#FF6666>" \
  TR3="<tr bgcolor=#CCCCCC>" \
  TD="<td><font size=2>"     \
  TD2="</td><td><font size=2>" \
  REND="</td></tr>" \
  BC="<b><center>" \
  BC2="</center></b>" \
  tmp=$OTMP_DIR/${REPORT_NAME} \
  img_src=$IMG_SRC1 \
  >> $TMP_DIR/scoreManager.html
	
		echo "<tr><td COLSPAN=99 bgcolor=#CCCCCC></td></tr>" >> $TMP_DIR/scoreManager.html
		for AB in `grep "Plot $PN " $TMP_DIR/sc_score_results2 | awk '{print $13}' | sort -u`
		do
			launch_score
	
			for TAB_PAR in `grep $AB $TMP_DIR/sc_score_results2 | awk '{print $3"YY"$4}'`
			do
				TABLE=`echo $TAB_PAR | sed "s/YY/ /" | awk '{print $1}'`
				PARAM=`echo $TAB_PAR | sed "s/YY/ /" | awk '{print $2}'`
				SCORE=`grep $AB $TMP_DIR/sc_score_results2 | grep "^Plot $PN " | grep " $TABLE " | grep " $PARAM " | awk '{print $7}'`
	
# for monitoring multiple $SCORE parameters which is due to invalid configuration
				CHECK_MULTI=`echo "$SCORE" | wc -w`
				if [ $CHECK_MULTI -gt 1 ]
				then
				        echo "***WARNING for AB $AB: the following command has produced multiple output which should not happen and indicates an issue with $CFG_FILE:
grep $AB $TMP_DIR/sc_score_results2 | grep \"^Plot $PN \" | grep \" $TABLE \" | grep \" $PARAM \" | awk '{print \$7}'
Result: 
$SCORE
We sum the scores up but it's not sure that this is the right thing to do." > $TMP_DIR/mail
				        mail -s "scoreHC -r $REPORT_NAME" $OP_ADDRESS <$TMP_DIR/mail
	        			SCORE=`echo $SCORE | tr " " "\012" | awk '{sum+=$1} END {print sum}'`
				fi
	
				if [ "Q$SCORE" = Q ]
				then
					continue
				fi
	
				if [ $SCORE != 0 ]
				then
					QC1_PARAM3=`grep $AB $TMP_DIR/sc_score_results2 | awk '{print $4}' | sort -u`
					QC1_TABLE3=`grep $AB $TMP_DIR/sc_score_results2 | awk '{print $3}' | sort -u`
					launch_remove
					launch_override
				fi
			done
		done
	done
	
	echo "</table>" >> $TMP_DIR/scoreManager.html
	
	if [ ! -d $REPORT_DIR/scoreManager ]
	then
		mkdir $REPORT_DIR/scoreManager
	fi
	
# create gui job refresh_sc$REPORT_NAME}.esh
	refreshReport ${REPORT_NAME}
	
	mv $TMP_DIR/scoreManager.html $REPORT_DIR/scoreManager/scManager_${REPORT_NAME}.html
fi

# ==========================================================================
# 4. Main scoreManager.html and score_overview.html
# 4.1 begin HTML
# ==========================================================================
# INTERFACE_ONLY=YES: does only this part
if [ $INTERFACE_ONLY = YES ]
then
	EXTERNAL=YES
fi

echo "  Create score_overview file, transfer to $DFO_WEB_SERVER ..."
VNAVBAR_SCORE="vnavbar_HC_scores.html"
INS_SCORE="<!--#include virtual=\"score_ins.html\"-->"

rm -f $TMP_DIR/score_overview.html
TIMESTAMP=`date -u +%Y-%m-%d"T"%H:%M:%S`

cat > $TMP_DIR/score_overview.html <<EOT
<html>
<!-- created by scoreHC -v${TOOL_VERSION} -->
<head>
<META HTTP-EQUIV="Refresh" CONTENT="$REFRESH">
<META HTTP-EQUIV="Cache-Control" CONTENT="NO-CACHE">

  <title> ${DFO_INSTRUMENT} trending system: overview of scores</title>
  <link rel="StyleSheet" href="http://www.eso.org/qc/ALL/common/qc_eso.style" type="text/css">
</head>

<body bgcolor="#FFFFFF">

<!-- backbone table -->
<table width="950" border="0" cellspacing="1" cellpadding="1" bgcolor=#FFFFFF>

<!-- ROW 1: caption part -->
  <tr>		<!--FILTER-->
   <td colspan=99>	<!--FILTER-->

<!--begin include header -->
<!--#include virtual="/observing/dfo/quality/ALL/common/header_HC.html" --> <!--FILTER-->
<!--end include header -->

    </td> 	<!--FILTER-->
  </tr>		<!--FILTER-->
<!--end caption -->

<!-- ROW 2: vnavbar and overview table -->
  <tr align="left" valign="top" bgcolor="#999999">
<!-- COL 1: vnavbar -->
    <td align="center" valign="top" bgcolor="$HC_COLOR" width="10">	<!--FILTER-->

<!--begin include vnavbar -->
<!--#include virtual="/observing/dfo/quality/${DFO_INSTRUMENT}/common/$VNAVBAR_SCORE" -->	<!--FILTER-->
<!--end include vnavbar -->

    </td> 	<!--FILTER-->

<!-- COL 2: proper distance -->
    <td width="10" bgcolor="#FFFFFF"><img src="/images/itemOff.gif" width="10" height="10" border="0"></td> <!--FILTER-->

<!-- COL 3: hosting main table -->
    <td width="782" bgcolor="#FFFFFF">

<h1>$DFO_INSTRUMENT trending system: overview of scores</h1>
<font size=2>Last update: $TIMESTAMP (UT)<font><p>

EOT

# ==========================================================================
# 4.2 we create two tables:
# score_overview: instrument score table; 
# score_overview_core: to become the scoreManager
# score_overview_core_all: to become the exported comment interface
# ==========================================================================

TIMESTAMP=`date -u +%Y-%m-%d"T"%H:%M:%S`
cat > $TMP_DIR/score_overview_core.html <<EOT
<html>
<head>
<title>$DFO_INSTRUMENT HC ScoreManager</title>
<!-- javascript -->
<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=1000,height=800,scrollbars=yes,menubar=yes");
  }
//-->
</script>
<link rel="StyleSheet" href="${STYLE_URL}" type="text/css">
</head>
EOT

sed "s/HC ScoreManager/HC monitor: comment editor/" $TMP_DIR/score_overview_core.html > $TMP_DIR/score_overview_core_all.html

# text for score_overview_core.html
cat >> $TMP_DIR/score_overview_core.html <<EOT
<body class=page>
<table border=0>
  <tr>
    <td width=5% valign=top bgcolor=#CCCCCC>
EOT

# this navbar is created by $DFO_GUI_DIR/refresh_gui_navbar.esh@dfoMonitor
if [ -s $DFO_MON_DIR/vnavbar_scoreHC.html ]
then
	cat $DFO_MON_DIR/vnavbar_scoreHC.html >> $TMP_DIR/score_overview_core.html
else
	echo "***WARNING: no \$DFO_MON_DIR/vnavbar_scoreHC.html found. Update the navbar configuration of dfoMonitor, then call option \"navigation bar: refresh\". "
	echo "\$DFO_MON_DIR/vnavbar_scoreHC.html TBD!" >> $TMP_DIR/score_overview_core.html
fi

cat >> $TMP_DIR/score_overview_core.html <<EOT
    </td>

<td bgcolor=#eeeeee>
<h2><font color=${HC_COLOR}>HC ScoreManager (instrument: $DFO_INSTRUMENT)</font></h2>
<font size=2>This interface manages scores displaying on the HC site.<br>
- Click on report links to inspect and manage the contributing scores<br>
- Click on stat_grp links to see all total report scores contributing to the HC group score<br>
- Check for inconsistencies in the report scores or the group scores (see below)<p>

You can manage comments for the HC reports (they appear under "News"): click on 'edit COMMENTS' and select the comment to manage.<p>

There is also some management of $TREND_TOOL files:<br>
- Find $TREND_TOOL config files <a href=$CONFIG_URL target=_blank>here</a> (sorted per instrument)<p>

Last update: $TIMESTAMP (UT)
<p>
EOT

# text for score_overview_core_all.html
cat >> $TMP_DIR/score_overview_core_all.html <<EOT
<table style="border-collapse: collapse;" border="1" bordercolor="#ffffff" cellpadding="0" cellspacing="0">
  <tr align="left" valign="bottom">
    <td bgcolor="$HC_COLOR" height="40" width="40"><b><font color="#ffffff" size="3">HC</font></b></td>
    <td valign="bottom"><font size=+2>$DFO_INSTRUMENT HC monitor: comment editor</font></td>
  </tr>
</table><p>
<font size=2>
<a href="javascript:self.close();"><font color=$HC_COLOR>close window</font></a><p>

Use this interface to manage the HC report comments for $DFO_INSTRUMENT.<br>
Select a comment for editing and then submit.<p>

Last update: $TIMESTAMP (UT)
<p>
EOT

cat > $TMP_DIR/sc_part <<EOT
<table width="782" border=0 cellpadding="1" cellspacing="1">
  <tr bgcolor=#eeeeee>
    <td align=right valign=top width=5% nowrap><b><font size=2 color=${HC_COLOR}>General news:<font></b></td>
    <td align=left  valign=top width=95% bgcolor=${NEWS_COL}><font size=2 color=${HC_COLOR}><b>
<!-- begin include HC_POSTIT -->
<!--#include virtual="/observing/dfo/quality/ALL/HC_POSTIT" -->
<!-- end include HC_POSTIT -->
    </b></font></td>
  </tr>

  <tr bgcolor=#eeeeee>
    <td align=right valign=top width=5% nowrap><b><font size=2 color=${HC_COLOR}>${DFO_INSTRUMENT} news:<font></b></td>
    <td align=left  valign=top width=95% bgcolor=${NEWS_COL}><font size=2 color=${HC_COLOR}><b>

<!-- begin include ${DFO_INSTRUMENT}_POSTIT -->
<!--#include virtual="${QC_URL}/${DFO_INSTRUMENT}_POSTIT" -->
<!-- end include ${DFO_INSTRUMENT}_POSTIT -->

    </b></font></td>
  </tr>
</table>

<p>
<!--LOCAL <font size=1><i>[check to have only stat_grp entries that are also listed below]</i></font> LOCAL!-->
EOT

cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview.html
cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core.html
cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html

if [ ! -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ]
then
	touch $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT
fi

cat >> $TMP_DIR/score_overview_core_all.html <<EOT
<form method="get" action="http://www.eso.org${QC_URL}/php/hcComments.php">
<input type="hidden" name="origin" value="web">
EOT

# begin ins score table (<!--LOCAL1!--> is a marker to later edit that cell for the local scoreManager.html; stupid but efficient)
cat > $TMP_DIR/sc_part <<EOT
<table border="0" cellspacing="1" cellpadding="1">
  <tr> 									<!--FILTER-->
    <td bgcolor=#CCCCCC nowrap><font size=+1><b>$DFO_INSTRUMENT instrument score:</b></td> <!--FILTER-->
     <!--LOCAL </td><td bgcolor=#CCCCCC><a href=../statFiles/stat_all_GRP><font size=2>all_GRP</font></a></td>  LOCAL!-->
    <td bgcolor=#CCFFFF align=center valign=middle>${INS_SCORE}</td>	<!--FILTER-->
    <td bgcolor=#FFFFFF>&nbsp;</td> <!--LOCAL1!-->
    <td bgcolor=#FFFFFF colspan=99>
      <img src="${IMG_SRC1}/right.gif" border="0" height="11" hspace="1" width="10"><a href=./score_overview_comment.html title="display scores and comments for red scores"><font color=${HC_COLOR} size=1>red score comments ...</font></a><font color=${HC_COLOR} size=1> | </font>
      <a href=./score_overview_comment_all.html title="display scores and all comments"><font color=${HC_COLOR} size=1>all comments ...</font></a> 
      <img src="${IMG_SRC1}/right.gif" border="0" height="11" hspace="1" width="10"><a class="nav-subitem" href="javascript:openWin('http://www.eso.org${VNAVBAR_URL}/score_overview_core_all.html');" title="enter/update/delete comments about red scores"><font size=1>edit COMMENTS ...</font></a> <!--LOCAL!-->
    </td>
  </tr> <!--FILTER-->

  <tr><td> </td></tr>
  <tr>
    <td bgcolor=#CCCCCC><font size=2><b>HC navigation group</td>
     <!--LOCAL </td><td bgcolor=#CCCCCC><font size=2><b>stat_grp*</td>  LOCAL!-->
    <td bgcolor=#CCCCCC><font size=2><b>group<br>score</td>			<!--MARK-->
    <td bgcolor=#FFFFFF>&nbsp;</td>
    <td bgcolor=#CCCCCC COLSPAN=99><font size=2><b>report scores</td>
  </tr>
EOT

cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview.html
cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core.html
cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html

# edit the navigation pane 
sed -i -e "/score_overview_comment.html/s|^.*|<a href=\"javascript:self.close();\"><font color=$HC_COLOR size=1>close window</font></a>|" $TMP_DIR/score_overview_core_all.html
sed -i -e "/edit COMMENTS/d" $TMP_DIR/score_overview_core_all.html
sed -i -e "/<a href=.\/score_overview_comment_all/d" $TMP_DIR/score_overview_core_all.html

sed "s/score_overview_comment.html.*red score comments/score_overview.html title=\"display scores only\"><font color=${HC_COLOR} size=1> scores only/" $TMP_DIR/score_overview.html > $TMP_DIR/score_overview_comment.html
sed -i -e "s/<b>report scores</<b>report scores and comments (for red scores only)</" $TMP_DIR/score_overview_comment.html
sed -i -e "/scores only/s/right.gif/left.gif/" $TMP_DIR/score_overview_comment.html

sed "s/score_overview_comment.html.*red score comments.*/score_overview.html title=\"display scores only\"><font size=1 color=${HC_COLOR}> scores only ...<\/a> |/" $TMP_DIR/score_overview.html > $TMP_DIR/score_overview_comment_all.html
sed -i -e "/score_overview_comment_all/d" $TMP_DIR/score_overview_comment_all.html
sed -i -e "s/<b>report scores</<b>report scores and comments (all reports)</" $TMP_DIR/score_overview_comment_all.html
sed -i -e "/scores only/s/right.gif/left.gif/" $TMP_DIR/score_overview_comment_all.html

# ==========================================================================
# 4.3 create core table
# 4.3.1 HC navigation groups (sc_list_items)
# ==========================================================================
# score_list_${DFO_INSTRUMENT}.txt: useful for HC review but not used otherwise

rm -f $OTMP_DIR/score_list_${DFO_INSTRUMENT}.txt
for G in `cat $TMP_DIR/sc_list_items | grep -v SPACE | awk '{print $2}'`
do
	LABEL=`grep " ${G} " $TMP_DIR/sc_list_items | awk '{print $1}'`
	GRP=`echo $G | sed "s/vnavbar_HC_//" | sed "s/.html//"`
	GRP_SCORE="score_grp_$GRP.html"

	CHECK_NOSCORE=`grep " ${G} " $TMP_DIR/sc_list_items | awk '{print $3}'`
	if [ Q$CHECK_NOSCORE = Qnav-noscoreitem ]
	then
		LABEL=`grep " ${G} " $TMP_DIR/sc_list_items | awk '{print $1}'`
		cat > $TMP_DIR/sc_part <<EOT
<tr bgcolor=#CCCCCC>
  <td align=left><font size=2>$LABEL</td>
EOT
	else
		cat > $TMP_DIR/sc_part <<EOT
<tr bgcolor=#CCCCCC>
  <td><font size=2>$LABEL 
     <!--LOCAL </td><td><font size=1><a href=../statFiles/stat_grp_$G>$G</a> LOCAL!-->
  </td>
EOT
	fi

	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment_all.html

	echo " " >> $OTMP_DIR/score_list_${DFO_INSTRUMENT}.txt

# ==========================================================================
# 4.3.2 noscoreitem
# ==========================================================================
	if [ Q$CHECK_NOSCORE = Qnav-noscoreitem ]
	then
		cat > $TMP_DIR/sc_part <<EOT
  <td bgcolor=#CCCCCC><font size=1>&nbsp; </td>
  <td bgcolor=#FFFFFF><font size=1>&nbsp; </td>
  <td bgcolor=#FFFFFF colspan=99><font size=1><i>no scoring</i></td>
EOT
		cat > $TMP_DIR/sc_part1 <<EOT
  <td bgcolor=#CCCCCC><font size=1>&nbsp; </td>
  <td bgcolor=#FFFFFF><font size=1>&nbsp; </td>
  <td bgcolor=#FFFFFF><font size=1>&nbsp; </td>
  <td bgcolor=#EEEEEE colspan=99><font size=1><i>no scoring</i></td>
EOT

		cat $TMP_DIR/sc_part1 >> $TMP_DIR/score_overview_core.html
		cat $TMP_DIR/sc_part  >> $TMP_DIR/score_overview_core_all.html
                cat $TMP_DIR/sc_part  >> $TMP_DIR/score_overview.html
		cat $TMP_DIR/sc_part  >> $TMP_DIR/score_overview_comment.html
		cat $TMP_DIR/sc_part  >> $TMP_DIR/score_overview_comment_all.html

# ==========================================================================
# 4.3.3 no group file (zombie)?
# ==========================================================================
	elif [ ! -s $DFO_CONFIG_DIR/trendPlotter/$GRP.grp ]
	then
		echo "***INFO: no $GRP.grp file found in \$DFO_CONFIG_DIR/trendPlotter." 
		cat > $TMP_DIR/sc_part <<EOT
  <td bgcolor=#CCFFFF align=center valign=middle><!--#include virtual="${GRP_SCORE}"-->
  </td>
  <td bgcolor=#FFFFFF>&nbsp; </td>
  <td align=right><font size=2>${GRP}</td><td colspan=99><font size=2>no scoring</td>
EOT
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment_all.html

# ==========================================================================
# 4.3.4 check grp files for HC report names
# ==========================================================================
# not all reports in grp files are under HEALTH, others may be under FULL!
	else
		cat > $TMP_DIR/sc_part <<EOT
  <td bgcolor=#CCFFFF align=center valign=middle><!--#include virtual="${GRP_SCORE}"-->
  </td>
  <td bgcolor=#FFFFFF>&nbsp; </td>
EOT
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment.html
		cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment_all.html

		rm -f $TMP_DIR/sc_list_reports
		grep "^GROUP_MEMBERS" $DFO_CONFIG_DIR/trendPlotter/$GRP.grp | grep -v MARKER | awk '{print $3}' > $TMP_DIR/sc_list_reports
		if [ -s $TMP_DIR/sc_list_reports ]
		then
			for R in `cat $TMP_DIR/sc_list_reports`
			do
				SC_MANAGER="scManager_$R.html"
				ITEM_NAME=`grep "[[:space:]]$R[[:space:]]" $CONFIG_DIR/$GRP.grp | grep "^GROUP_MEMBERS" | grep -v MARKER | awk '{print $2}'`
				RNAME=`grep     "[[:space:]]$R[[:space:]]" $CONFIG_DIR/$GRP.grp | grep "^GROUP_MEMBERS" | grep -v MARKER | awk '{print $3}'`
				RTYPE=`grep "^REPORT_NAME" $CONFIG_DIR/config.tp_$R | awk '{print $4}'`
				if [ $RTYPE != FULL ]
				then
					RTYPE=HEALTH
				fi

				if [ Q$SC_MANAGER != Q ] && [ -s $DFO_TREND_DIR/reports/scoreManager/$SC_MANAGER ]
				then
					echo "<td bgcolor=#99CCFF align=right><font size=2><a href=$SC_MANAGER title=\"report $R\">$ITEM_NAME</a></td><td bgcolor=#99CCFF align=center valign=middle><!--#include virtual=\"score_"${RNAME}".html\"--></td>" >> $TMP_DIR/score_overview_core.html
# constrain the width of comment cells a bit (50p)
					echo "<td bgcolor=#99CCFF align=right width=50><font size=2><a href=../reports/${RTYPE}/trend_report_"${RNAME}"_HC.html title=\"report $R\">${ITEM_NAME}</a></td><td width=5 bgcolor=#99CCFF align=center valign=middle><!--#include virtual=\"score_"${RNAME}".html\"--></td>" >> $TMP_DIR/score_overview_core_all.html
					echo "<td bgcolor=#99CCFF align=right><font size=2><a href=../reports/${RTYPE}/trend_report_"${RNAME}"_QUICK.html title=\"report $R\">${ITEM_NAME}</a></td><td bgcolor=#99CCFF align=center valign=middle><!--#include virtual=\"score_"${RNAME}".html\"--></td>" >> $TMP_DIR/score_overview.html
					echo "<td bgcolor=#99CCFF align=right><font size=2><a href=../reports/${RTYPE}/trend_report_"${RNAME}"_QUICK.html title=\"report $R\">${ITEM_NAME}</a></td><td bgcolor=#99CCFF align=center valign=middle><!--#include virtual=\"score_"${RNAME}".html\"--></td>" >> $TMP_DIR/score_overview_comment.html
					echo "<td bgcolor=#99CCFF align=right><font size=2><a href=../reports/${RTYPE}/trend_report_"${RNAME}"_QUICK.html title=\"report $R\">${ITEM_NAME}</a></td><td bgcolor=#99CCFF align=center valign=middle><!--#include virtual=\"score_"${RNAME}".html\"--></td>" >> $TMP_DIR/score_overview_comment_all.html
				else
					echo "<td bgcolor=#99CCFF align=right><font size=2>$ITEM_NAME</td><td bgcolor=#99CCFF> </td>" >> $TMP_DIR/score_overview_core.html
					echo "<td bgcolor=#99CCFF align=right><font size=2>${ITEM_NAME}</td><td bgcolor=#99CCFF> </td>" >> $TMP_DIR/score_overview_core_all.html
					echo "<td bgcolor=#99CCFF align=right><font size=2>${ITEM_NAME}</td><td bgcolor=#99CCFF align=center valign=middle>&nbsp;</td>" >> $TMP_DIR/score_overview.html
					echo "<td bgcolor=#99CCFF align=right><font size=2>${ITEM_NAME}</td><td bgcolor=#99CCFF align=center valign=middle>&nbsp;</td>" >> $TMP_DIR/score_overview_comment.html
					echo "<td bgcolor=#99CCFF align=right><font size=2>${ITEM_NAME}</td><td bgcolor=#99CCFF align=center valign=middle>&nbsp;</td>" >> $TMP_DIR/score_overview_comment_all.html
				fi
# temporary score_list file, for HC review
				echo "$G $RNAME $ITEM_NAME " >> $OTMP_DIR/score_list_${DFO_INSTRUMENT}.txt
			done
		fi
	fi
	echo "</tr>" >> $TMP_DIR/score_overview_core.html
	echo "</tr>" >> $TMP_DIR/score_overview_core_all.html
	echo "</tr>" >> $TMP_DIR/score_overview.html
	echo "</tr>" >> $TMP_DIR/score_overview_comment.html
	echo "</tr>" >> $TMP_DIR/score_overview_comment_all.html

# ==========================================================================
# 4.3.5 additional row for comments
# ==========================================================================
	cat > $TMP_DIR/sc_part <<EOT
<tr bgcolor=#EEEEEE>
  <td align=right>
EOT
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment_all.html

# BUTTON for comment editor
	if [ Q$CHECK_NOSCORE = Qnav-noscoreitem ]
	then
		BUTTON=""
	else
		BUTTON="<button type=\"submit\" value=\"Submit\" title=\"select a comment for editing and submit the edit here\"><font size=1 color=${HC_COLOR}>go</button>"
	fi
	echo "$BUTTON" >> $TMP_DIR/score_overview_core_all.html

	cat > $TMP_DIR/sc_part <<EOT
  </td> 
  <td><font size=1>&nbsp; </td>
  <td bgcolor=#FFFFFF><font size=1>&nbsp; </td>
EOT
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_core_all.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment.html
	cat $TMP_DIR/sc_part >> $TMP_DIR/score_overview_comment_all.html

	if [ -s $DFO_CONFIG_DIR/trendPlotter/$GRP.grp ]
	then
		grep "^GROUP_MEMBERS" $DFO_CONFIG_DIR/trendPlotter/$GRP.grp | grep -v MARKER | awk '{print $3}' > $TMP_DIR/sc_list_reports
		if [ -s $TMP_DIR/sc_list_reports ]
		then
			for R in `cat $TMP_DIR/sc_list_reports`
			do
				if [ -s $REPORT_DIR/statFiles/stat_grp_$GRP ]
				then
					RSCORE=`grep "^$R[[:space:]]" $REPORT_DIR/statFiles/stat_grp_$GRP | awk '{print $2}' | tail -1`
				else
					RSCORE=0
				fi
				NEWS_FILE=$R.msg

# $R not in $GRP?
				if [ Q$RSCORE = Q ]
				then
					echo "***WARNING: no valid entry for REPORT_GROUP in config.tp_${R}? Check your configuration."
				fi

				if [ $RSCORE -gt 0 ]
				then
					BGCOLOR=#FFFF99
				else
					BGCOLOR=#EEEEEE
				fi

# INPUT for comment editor
				VIRTUAL="<font size=1><!--#include virtual=\"${QC_URL}/HEALTH/${NEWS_FILE}\"--></font>"
				INPUT="<input type=\"radio\" name=\"reportSelect\" value=\"$R\"></input>"

				if [ $RSCORE -gt 0 ]
				then
					NEWS_FILE="<td bgcolor=$BGCOLOR align=left valign=top colspan=2>$VIRTUAL</td>"
					NEWS_FILE1="<td bgcolor=$BGCOLOR align=left valign=top>$VIRTUAL</td><td valign=top>$INPUT</td>"
					NEWS_FILE2=$NEWS_FILE
				else
					NEWS_FILE="<td bgcolor=$BGCOLOR colspan=2><font size=1>&nbsp;</font></td>"
					NEWS_FILE1="<td bgcolor=$BGCOLOR align=left valign=top>$VIRTUAL</td><td valign=top>$INPUT</td>"
					NEWS_FILE2="<td bgcolor=$BGCOLOR align=left valign=top colspan=2>$VIRTUAL</td>"
				fi

				echo "$NEWS_FILE"  >> $TMP_DIR/score_overview_comment.html
				echo "$NEWS_FILE1" >> $TMP_DIR/score_overview_core_all.html
				echo "$NEWS_FILE2" >> $TMP_DIR/score_overview_comment_all.html
			done
		fi
	else
  		echo "<td bgcolor=#FFFFFF><font size=1>&nbsp;</td>" >> $TMP_DIR/score_overview_comment.html
  		echo "<td bgcolor=#FFFFFF><font size=1>&nbsp;</td>" >> $TMP_DIR/score_overview_core_all.html
  		echo "<td bgcolor=#FFFFFF><font size=1>&nbsp;</td>" >> $TMP_DIR/score_overview_comment_all.html
	fi
	echo "</tr>" >> $TMP_DIR/score_overview_comment.html
	echo "</tr>" >> $TMP_DIR/score_overview_core_all.html
	echo "</tr>" >> $TMP_DIR/score_overview_comment_all.html
done

echo "  </table>" >> $TMP_DIR/score_overview.html
echo "  </table>" >> $TMP_DIR/score_overview_core.html

cat >> $TMP_DIR/score_overview_core_all.html <<EOT
  </table>
</form>
EOT

echo "  </table>" >> $TMP_DIR/score_overview_comment.html
echo "  </table>" >> $TMP_DIR/score_overview_comment_all.html

chmod u+x $TMP_DIR/score_overview_comment.html $TMP_DIR/score_overview_core_all.html $TMP_DIR/score_overview_core.html $TMP_DIR/score_overview.html $TMP_DIR/score_overview_comment_all.html
# end score table

# ==========================================================================
# 4.3.6 put together 
# ==========================================================================
cat >> $TMP_DIR/score_overview.html <<EOT
<!-- end backbone table -->
      </td>
    </tr>
  </table>

</body>
</html>
EOT

# ==========================================================================
# 4.3.7 expand virtual includes for the local version: transfer to web server, then wget --> all includes expanded
# ==========================================================================
scp -o BatchMode=yes -p $TMP_DIR/score_overview_core.html $TMP_DIR/score_overview_core_all.html ${DFO_WEB_SERVER}:${NAVBAR_DIR}/ 1>/dev/null

if [ ! -s $REPORT_DIR/scoreManager ]
then
	mkdir $REPORT_DIR/scoreManager
fi

# we remove the comment tag LOCAL and thereby enable the local link to the stat_grp files
rm -f $TMP_DIR/score_overview_core.html $TMP_DIR/score_overview_core_all.html

cd $TMP_DIR
eval "wget -q http://www.eso.org${VNAVBAR_URL}/score_overview_core.html -T 10 -t 1"

# ==========================================================================
# 4.3.8 Consistency check for stat_grp files
# ==========================================================================

if [ -s score_overview_core.html ]
then
	sed "s/<!--LOCAL //g" score_overview_core.html | sed "s/ LOCAL!-->//" > score_overview_core.html1
	cat >> score_overview_core.html1 <<EOT
<font size=2><i>*stat_grp: these files contain names and report scores of all reports contributing to this HC group score. They might be useful for analyzing problems with scores.</i><p>
EOT

# we check for "zombie" group files
	singleAccess stat_all_GRP
	if [ -s $REPORT_DIR/statFiles/stat_all_GRP.lock ]
	then
		cp $REPORT_DIR/statFiles/stat_all_GRP.lock $TMP_DIR/stat_all_GRP
		cp $TMP_DIR/stat_all_GRP $TMP_DIR/stat_all_GRP_orig
		cp $TMP_DIR/stat_all_GRP $REPORT_DIR/statFiles/stat_all_GRP
	fi
	rm -f $REPORT_DIR/statFiles/stat_all_GRP.lock 

	for G in `cat $TMP_DIR/sc_list_items | grep -v SPACE | awk '{print $2}'`
	do
		LABEL=`grep " ${G} " $TMP_DIR/sc_list_items | awk '{print $1}'`
		GRP=`echo $G | sed "s/vnavbar_HC_//" | sed "s/.html//"`
		CHECK_NOSCORE=`grep " ${G} " $TMP_DIR/sc_list_items | awk '{print $3}'`
		if [ Q$CHECK_NOSCORE = Qnav-noscoreitem ]
		then
			continue
		fi

		grep -v "stat_grp_${G}[[:space:]]" $TMP_DIR/stat_all_GRP > $TMP_DIR/stat_all_GRP1
		if [ -s $TMP_DIR/stat_all_GRP1 ]
		then
			mv $TMP_DIR/stat_all_GRP1 $TMP_DIR/stat_all_GRP
		else
			rm -f $TMP_DIR/stat_all_GRP1 $TMP_DIR/stat_all_GRP
			break
		fi
	done

	echo "<b>1. Consistency check  for stat_grp files</b><br>
They define the report groups on the HC monitor and the group scores. They should correspond one by one to existing reports. We check for \"zombies\" here.<p>" >> score_overview_core.html1

	if [ -s $TMP_DIR/stat_all_GRP ]
	then
		echo "<font size=2><img src=${IMG_SRC1}/nok.gif width=14 height=15 border=0> <span style=\"background-color: #FF9999\">The following stat_grp file(s) have been found in \$REPORT_DIR/statFiles/stat_all_GRP that are not linked to the HC monitor (since e.g. you used them at some point for testing).</span><br>
<img src=${IMG_SRC1}/itemOff.gif width=14 height=15 border=0> <span style=\"background-color: #FF9999\">They should be deleted, in order to avoid inconsistencies with group scores:</span><p>" >> score_overview_core.html1
		cat $TMP_DIR/stat_all_GRP | sed "s|^.*|<img src=${IMG_SRC1}/itemOff.gif width=14 height=15 border=0> <span style=\"background-color: #FF9999\"><a href=../statFiles/&>&</a></span><br>|" >> score_overview_core.html1
	else
		echo "<font size=2><img src=${IMG_SRC1}/ok.gif width=14 height=15 border=0> <span style=\"background-color: #99FF99\">All group files are consistent with \$TREND_DIR/reports/statFiles/stat_all_GRP.</span><br>" >> score_overview_core.html1
	fi

# ==========================================================================
# 4.3.9 Consistency check for stat_grp content
# ==========================================================================

	echo "<br><b>2. Consistency check for stat_grp content</b><br>
The stat_grp files should contain scores only for those reports which are visible on the HC monitor. We check for \"zombies\" here.<br>" >> score_overview_core.html1

	NO_ENTRY=TRUE
	for G in `cat $REPORT_DIR/statFiles/stat_all_GRP | awk '{print $1}' | sed "s/stat_grp_//"`
	do
		FIRST=YES
		for REPORT in `cat $REPORT_DIR/statFiles/stat_grp_$G | awk '{print $1}'`
		do
			CHECK=`grep "^GROUP_MEMBERS" $DFO_CONFIG_DIR/trendPlotter/*grp | awk '{print $3}' | grep $REPORT | head -n 1`
			if [ Q$CHECK = Q ]
			then
				if [ $NO_ENTRY = TRUE ]
				then
					echo "<br>
<img src=${IMG_SRC1}/nok.gif width=14 height=15 border=0><span style=\"background-color: #FF9999\"> The following group file(s) have outdated entries that are not  linked to the HC monitor. These entries should be removed from the respective group file:</span><br>" >> score_overview_core.html1
					NO_ENTRY=FALSE
				fi

				if [ $FIRST = YES ]
				then
					echo "<br>" >> score_overview_core.html1
					FIRST=NO
				fi
				echo "<img src=${IMG_SRC1}/itemOff.gif width=14 height=15 border=0><span style=\"background-color: #FF9999\"> <a href=../statFiles/stat_grp_$G>stat_grp_$G:</a>&nbsp; $REPORT</span><br>" >> score_overview_core.html1
			fi
		done
	done

	if [ $NO_ENTRY = TRUE ]
	then
		echo "<br><img src=${IMG_SRC1}/ok.gif width=14 height=15 border=0><span style=\"background-color: #99FF99\"> All group files checked to be filled consistently.</span></font>" >> score_overview_core.html1
	fi

	mv score_overview_core.html1 $REPORT_DIR/scoreManager/scoreManager.html
fi

rm -f score_overview_core.html*

# ==========================================================================
# 4.3.10 Consistency check for HC plot configuration
# ==========================================================================

echo "  Check HC configuration ..."

# list of all configured HC plots
rm -f $TMP_DIR/sc_list_hc_plots
grep "^HC_PLOT" $DFO_CONFIG_DIR/config.scoreQC | awk '{print $4}' | sed "s/^.*trend_report_//" | sed "s/_HC.html//" | sed "s/.html//" | sort -u > $TMP_DIR/sc_list_hc_plots
if [ ! -s $TMP_DIR/sc_list_hc_plots ]
then
	echo "*** ERROR: no HC plots found as configured in HC_PLOT section of config.scoreQC. Check your configuration!"
else
	rm -f $TMP_DIR/sc_list_config.tp
	ls $DFO_CONFIG_DIR/trendPlotter/config.tp_* | sed "s/^.*config.tp_//" | sort -u > $TMP_DIR/sc_list_config.tp

# a) any HC plot not configured in config/trendPlotter? (obsolescence: HC plot configured in scoreQC but not existing anymore)
	rm -f $TMP_DIR/sc_list_inconsist2
	for HC in `cat $TMP_DIR/sc_list_hc_plots`
	do
		CHECK_EXIST=`grep "^${HC}$" $TMP_DIR/sc_list_config.tp`
		if [ Q$CHECK_EXIST = Q ]
		then
			echo $HC >> $TMP_DIR/sc_list_inconsist2
		fi
	done
	
	cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><br><b>3. Consistency check for HC_PLOT section of config.scoreQC</b><br>
All scoreQC-configured HC reports should also be configured in \$DFO_CONFIG_DIR/trendPlotter.<br>
EOT
	if [ -s $TMP_DIR/sc_list_inconsist2 ]
	then
		cat > $TMP_DIR/hc_mail <<EOT
HC report(s) defined in config.scoreQC but not found in config/trendPlotter. Check your configuration:
EOT
		cat $TMP_DIR/sc_list_inconsist2 >> $TMP_DIR/hc_mail
		cat >> $TMP_DIR/hc_mail <<EOT
This mail is sent only once a day.
Find this list on the local HC ScoreManager, \$DFO_TREND_DIR/reports/scoreManager/scoreManager.html.
EOT
		CHECK_DATE=`date -u +%Y-%m-%d`
		CHECK_MESSAGE=`grep "^scoreHC" $DFO_MON_DIR/DFO_MESSAGE | grep $CHECK_DATE | grep "section 3" | tail -1`
		if [ "Q$CHECK_MESSAGE" = Q ]
		then
			mail -s "scoreHC: inconsistency found for $DFO_INSTRUMENT and HC_PLOT configuration" $OP_ADDRESS <$TMP_DIR/hc_mail
			echo "scoreHC: inconsistency found for HC_PLOT configuration for $CHECK_DATE and ${DFO_INSTRUMENT}, section 3!" >> $DFO_MON_DIR/DFO_MESSAGE
		fi
		cat $TMP_DIR/hc_mail

		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br> <img src=${IMG_SRC1}/nok.gif width=14 height=15 border=0><span style="background-color: #FF9999"> The following HC reports are defined in config.scoreQC but not found in config/trendPlotter. Check your configuration:
</span><br>
EOT
		cat $TMP_DIR/sc_list_inconsist2 | tr "\012" " " | sed "s/ /<br>/"g >> $REPORT_DIR/scoreManager/scoreManager.html
	else
		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><img src=${IMG_SRC1}/ok.gif width=14 height=15 border=0><span style="background-color: #99FF99"> All HC reports configured consistently.</span></font>
EOT
	fi

# b) any HC plot configured in config/trendPlotter but not listed in config.scoreQC or JOBS_TREND
#    (important inconsistency of HC_PLOT section; would lead to lack of HC report update by autoDaily)

	rm -f $TMP_DIR/sc_list_inconsist $TMP_DIR/sc_list_jtrend_missing
	# BW: buggy
	#for HC in `cat $TMP_DIR/sc_list_config.tp`
	#do
	#	CHECK_EXIST=`grep "^${HC}$" $TMP_DIR/sc_list_hc_plots`
	#	if [ Q$CHECK_EXIST = Q ]
	#	then
	#		echo $HC >> $TMP_DIR/sc_list_inconsist
	#	fi
	#	CHECK_EXIST2=`grep -v "^#" $DFO_JOB_DIR/JOBS_TREND | grep $TREND_TOOL | grep "[[:space:]]$HC[[:space:]]" | sort -u`
	#	if [ "Q$CHECK_EXIST2" = Q ]
	#	then
	#		echo $HC >> $TMP_DIR/sc_list_jtrend_missing
	#	fi
	#done

	cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><br><b>4. Consistency check for HC plots not configured in HC_PLOT section of config.scoreQC</b><br>
All HC reports should be configured in \$DFO_CONFIG_DIR/config.scoreQC.<br>
EOT
	if [ -s $TMP_DIR/sc_list_inconsist ]
	then
		cat > $TMP_DIR/hc_mail <<EOT
HC reports defined as \$DFO_CONFIG_DIR/trendPlotter/config.tp_* but not listed in the HC_PLOT section of config.scoreQC.
This is a potential operational issue since those HC reports don't get updated by autoDaily.

Either add the HC plot(s) to config.scoreQC, or remove their config file from config/trendPlotter:
EOT
		cat $TMP_DIR/sc_list_inconsist >> $TMP_DIR/hc_mail
		cat >> $TMP_DIR/hc_mail <<EOT
This mail is sent only once a day.
Find this list on the local HC ScoreManager, \$DFO_TREND_DIR/reports/scoreManager/scoreManager.html.
EOT
		CHECK_DATE=`date -u +%Y-%m-%d`
		CHECK_MESSAGE=`grep "^scoreHC" $DFO_MON_DIR/DFO_MESSAGE | grep $CHECK_DATE | grep "section 4" | tail -1`
		if [ "Q$CHECK_MESSAGE" = Q ]
		then
			mail -s "scoreHC: inconsistency found for $DFO_INSTRUMENT and HC_PLOT configuration" $OP_ADDRESS <$TMP_DIR/hc_mail
			echo "scoreHC: inconsistency found for HC_PLOT configuration for $CHECK_DATE and ${DFO_INSTRUMENT}, section 4!" >> $DFO_MON_DIR/DFO_MESSAGE
		fi
		cat $TMP_DIR/hc_mail
		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br> <img src=${IMG_SRC1}/nok.gif width=14 height=15 border=0><span style="background-color: #FF9999"> The following HC reports are not defined in config.scoreQC. This is critical. Check your configuration:
</span><br>
EOT
		cat $TMP_DIR/sc_list_inconsist | tr "\012" " " | sed "s/ /<br>/"g >> $REPORT_DIR/scoreManager/scoreManager.html
	else
		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><img src=${IMG_SRC1}/ok.gif width=14 height=15 border=0><span style="background-color: #99FF99"> All HC reports configured consistently.</span>
EOT
	fi

	cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><br><b>5. Consistency check for HC plots not configured in JOBS_TREND</b><br>
All HC reports must be listed in \$DFO_JOB_DIR/JOBS_TREND for daily update.<br>
EOT
	if [ -s $TMP_DIR/sc_list_jtrend_missing ]
	then
		cat > $TMP_DIR/hc_mail <<EOT
HC reports defined as \$DFO_CONFIG_DIR/trendPlotter/config.tp_* but not listed in \$DFO_JOB_DIR/JOBS_TREND.
This is a potential operational issue since those HC reports don't get updated daily.

Either add the HC plot(s) to JOBS_TREND, or remove their config file from config/trendPlotter:
EOT
		cat $TMP_DIR/sc_list_jtrend_missing >> $TMP_DIR/hc_mail
		cat >> $TMP_DIR/hc_mail <<EOT
This mail is sent only once a day.
Find this list on the local HC ScoreManager, \$DFO_TREND_DIR/reports/scoreManager/scoreManager.html.
EOT
		CHECK_DATE=`date -u +%Y-%m-%d`
		CHECK_MESSAGE=`grep "^scoreHC" $DFO_MON_DIR/DFO_MESSAGE | grep $CHECK_DATE | grep "section 5" | tail -1`
		if [ "Q$CHECK_MESSAGE" = Q ]
		then
			mail -s "scoreHC: inconsistency found for $DFO_INSTRUMENT and JOBS_TREND content" $OP_ADDRESS <$TMP_DIR/hc_mail
			echo "scoreHC: inconsistency found for HC_PLOT configuration for $CHECK_DATE and ${DFO_INSTRUMENT}, section 5!" >> $DFO_MON_DIR/DFO_MESSAGE
		fi
		cat $TMP_DIR/hc_mail
		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br> <img src=${IMG_SRC1}/nok.gif width=14 height=15 border=0><span style="background-color: #FF9999"> The following HC reports are not listed in JOBS_TREND. This is critical. Check your setup:
</span><br>
EOT
		cat $TMP_DIR/sc_list_jtrend_missing | tr "\012" " " | sed "s/ /<br>/"g >> $REPORT_DIR/scoreManager/scoreManager.html
	else
		cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
<br><img src=${IMG_SRC1}/ok.gif width=14 height=15 border=0><span style="background-color: #99FF99"> All HC reports listed in JOBS_TREND.</span></font>
EOT
	fi
	echo "</font>" >> $REPORT_DIR/scoreManager/scoreManager.html
fi

cat >> $REPORT_DIR/scoreManager/scoreManager.html <<EOT
    </td>
  </tr>
</table>

</body>
</html>
EOT

# ======================================================================
# 4.4 transfer to web server and publish
# ======================================================================

ssh -o BatchMode=yes $DFO_WEB_SERVER "bin/qcDircheck $NAVBAR_DIR" 1>/dev/null
scp -o BatchMode=yes -p $TMP_DIR/score_overview.html $TMP_DIR/score_overview_comment.html $TMP_DIR/score_overview_comment_all.html ${DFO_WEB_SERVER}:${NAVBAR_DIR}/ 1>/dev/null

# edit the local link
sed -i -e "/score_overview_comment.html/s/^.*//"     $REPORT_DIR/scoreManager/scoreManager.html
sed -i -e "/score_overview_comment_all.html/s/^.*//" $REPORT_DIR/scoreManager/scoreManager.html
sed -i -e "s/FFFFFF colspan=99/eeeeee colspan=99/"   $REPORT_DIR/scoreManager/scoreManager.html
sed -i -e "/--LOCAL1!/s/#FFFFFF/#eeeeee/" 	     $REPORT_DIR/scoreManager/scoreManager.html

# ======================================================================
# 5.0 end
# ======================================================================

if [ -s $TMP_DIR/sc_score_query ]
then
	mv $TMP_DIR/sc_score_query   $REPORT_DIR/
	mv $TMP_DIR/sc_score_results $REPORT_DIR/
fi

echo "   ... done. "

if [ $INTERFACE_ONLY = NO ]
then
	echo "Find:
- ISQL query  : \$DFO_TREND_DIR/reports/sc_score_query 
- results     : \$DFO_TREND_DIR/reports/sc_score_results 
- scoreManager: \$DFO_TREND_DIR/reports/scoreManager/scoreManager.html
"
fi

if [ $EXTERNAL = NO ]
then
	rm -r $TMP_DIR
fi

