#!/bin/sh
# PURPOSE:      dfosExplorer: create a GUI to check and overlook DFOS installation
# AUTHOR:       Reinhard Hanuschik / ESO
# VERSIONS:     1.0 -- written from scratch (2004-09-22)
#		1.1 -- modifications to align with dvdExplorer; uses dfosInstall if available (2004-09-28)
#		1.2 -- modifications to handle multiple config files (2004-10-01)
#		1.2.1- bug fixed with rules files (2004-10-18)
#		1.3 -- uses CSS; searches for all fitsreport*cfg files (2004-11-11)
#		1.3.1- introducing dfos web site, HISTO_URL (2004-12-08)
#		1.3.2- supports new structure of CONFIG_DIR for trendPlotter (2005-08-10)
#		1.3.3- browser calls generalized (2006-03-08)
#		1.3.4- minor modifications for createAB (2006-03-15)
#		1.3.5- minor modifications to split createAB and ABbuilder distributions (2006-04-03)
#		1.3.6- include utilPack handling (2006-05-17)
#		1.3.7- include qcDocu handling (2006-06-14)
#		1.3.8- modified trendPlotter handling (2006-07-06)
#		1.3.9- include filterRaw handling; dependency from config.gui_navbar removed (2007-02-13)
#		1.3.10-include scoreQC (2007-05-25)
#		1.3.11-include webNavBar (2007-07-09)
#		1.3.12-include writeQC and scoreHC (2008-04-17)
#		1.3.13-include calChecker (2008-04-23)
#		1.3.14-ABbuilder removed since this is now standard DFS software (2010-08-03)
#		1.3.15-DFOS_EDITOR replaced by $DFO_EDITOR (2011-04-21)
#		1.4 -- DFOS and PHOENIX aware (2014-08-14) 
#		1.4.1- minor change at the end (2014-08-27) 
#		1.5 -- DFOS, PHOENIX and OPSHUB aware (2018-08-02)
#		1.5.1- minor changes related to OPSHUB (2019-03-12)
#		1.5.2- some bug fixes for TQS docu pages; config file obsolete (2019-06-05)
#
# PARAMETERS:  	NONE 
#
# CONFIG:       NONE
#
# TOOLS CALLED: wget; needs .reference.installation from http://www.eso.org/~qc/dfos/tools
#
# OUTPUT:       dfosExplorer.html under $DFO_MON_DIR
# COMMENTS:    	This tool checks your installation against the reference installation.
#		It offers interactive update and refresh options.
#		Because of heavy use of wget, it is a bit slow.
#		supporting DFOS, PHOENIX and OPSHUB environments.
# ===================================================================================
TOOL_VERSION="1.5.2"
TOOL_NAME="dfosExplorer"

source ~/.dfosrc		

OPSHUB_TOOL="distillery"			# name of opshub workflow tool
ALL_URL="http://www.eso.org/qc/ALL"		# URL for help page

DFOS_URL="http://www.eso.org/~qc/dfos"		# documentation for DFOS
TQS_URL="http://www.eso.org/~qc/tqs"    	# documentation for TQS
TOOL_URL="http://www.eso.org/~qc/dfos/tools"	# DFOS tools
HISTO_URL="http://www.eso.org/~qc/dfos/history"	# history for DFOS tools
IMG_URL="http://www.eso.org/images"		# images

XTERM_GEOM=120x25+10+500 			# size of xterm windows being launched by browser
BROWSER_REFRESH=NO

# ===================================================================================
# 0. Initialization
# 0.1 check for directory variables which should be set globally in .dfosrc
# ===================================================================================

if [ Q$THIS_IS_OPSHUB != QYES ]
then
	THIS_IS_OPSHUB=NO
fi

if [ Q$THIS_IS_PHOENIX != QYES ]
then
	THIS_IS_PHOENIX=NO
fi

if [ $THIS_IS_PHOENIX = NO ] && [ $THIS_IS_OPSHUB = NO ]
then
	THIS_IS_DFOS=YES
else
	THIS_IS_DFOS=NO
fi

CHECK=`printenv | grep DFO`

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

if [ -s $DFO_CONFIG_DIR/config.dfosExplorer ]
then
        echo "*** INFO: No configuration file needed, removed."
	rm $DFO_CONFIG_DIR/config.dfosExplorer
fi

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

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

# =========================================================================
# 0.2 Help and version
# =========================================================================

while getopts hv OPTION
do
        case "$OPTION" in
         h ) cat $DFO_DOC_DIR/dfosExplorer.h | more
             exit 0 ;;
         v ) echo "$TOOL_VERSION"
             exit 0 ;;
	esac
done

# =========================================================================
# 0.3 Check params
# =========================================================================

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

if [ Q$DFO_EDITOR = Q ]
then
	echo "***ERROR: you need to define \$DFO_EDITOR (e.g. vi, vim, emacs) in .dfosrc, then source and start again. Hit return:"
	read input
	exit
fi

# =========================================================================
# 0.4 Initialize some parameters
# =========================================================================

# COLORS etc. hard-coded
TABLE_WIDTH="100%"

TITLE_COLOR="#006A9D"   	#dark blue: title color
QC_BGCOLOR=#CCCCFF		#light_blue: color code for QC tools
DFS_BGCOLOR=#FF9999		#light_red : color code for DFS tools
ALARM_COLOR=#CC0000		#alarm_red
NOT_THISPAGE_COLOR=#CCCCCC	#grey

# config file obsolete
if [ $THIS_IS_OPSHUB = YES ]
then
	INSTR_DFOMONITOR=`grep "^SUPPORTED" $DFO_CONFIG_DIR/config.${OPSHUB_TOOL} | head -1 | awk '{print $2}'`
fi

# helper scripts
cat > $DFO_GUI_DIR/AA_README <<EOT
This is a place for helper scripts related to the workflow monitor tools.
EOT

rm -f $DFO_GUI_DIR/download*.esh

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/refresh_dfosExplorer.esh <<EOT
#!/bin/sh
echo "Refresh dfosExplorer ..."
dfosExplorer
echo "Hit return:"
read input
exit
EOT
	chmod u+x $DFO_GUI_DIR/refresh_dfosExplorer.esh
fi

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

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

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

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

# =========================================================================
# 0.5 procedures
# 0.5.1 getHistory: analyze history files for latest version
# 	slow but reliable ...
# =========================================================================
getHistory(){
LATEST_VERSION=""
cd $TMP_DIR
rm -f history_${TOOL}.html
eval "wget -q ${HISTO_URL}/history_${TOOL}.html"
LATEST_VERSION=`grep "<pre>[0-9]" history_${TOOL}.html | tail -1 | awk '{print $1}' | sed "s/<pre>//"`
rm -f history_${TOOL}.html 
}

# =========================================================================
# 0.5.2 createGuiJob: editor to see config files via browser
# =========================================================================
createGuiJob(){
SUBDIR=""

case $INDEX in
 "PLUGIN_TRENDP" ) SUBDIR="/trendPlotter" ;;
 "PLUGIN_RULES"  ) SUBDIR="/OCA" ;;
esac
case $TOOL in
 "qcDocu"|"webNavBar"|"webCMS"|"webKPI" ) SUBDIR="/${TOOL}"     ;;
 "createAB"|"filterRaw"    		) SUBDIR="/OCA"       ;;
 "calChecker"		     		) SUBDIR="/CALCHECK/" ;;
esac

DFOS_EDITOR="$DFO_EDITOR ${DFO_CONFIG_DIR}$SUBDIR/$CFG"

if [ $THIS_IS_OPSHUB = NO ]
then
	cat > $DFO_GUI_DIR/edit_${CFG_VI}.esh <<EOT
#!/bin/sh
# job to be launched from dfosExplorer
$DFOS_EDITOR
exit
EOT
	chmod u+x $DFO_GUI_DIR/edit_${CFG_VI}.esh
fi
}

# =========================================================================
# 0.5.3 includeStyles: create style sheet
# =========================================================================
includeStyles(){
CSS_FONTSIZE="small"

cat > $TMP_DIR/styles <<EOT
<style TYPE="text/css">
<!--
body.page {
	font-family: sans-serif;
	background-color: #FFFFFF;
}
tr.entry {
        font-size: ${CSS_FONTSIZE};
        text-align:  left;
	vertical-align: top;
        color: #000000;
}
tr.header {
        font-size: normal;
	font-weight: bold;
        text-align:  left;
	vertical-align: top;
        color: #000000;
	background-color: #CCCCCC ;
-->
</style>
EOT
}

# ===================================================================================
# 0.5.4 createGuiDownload: create editor jobs
# ===================================================================================
createGuiDownload(){
cat > $DFO_GUI_DIR/download_${TOOL}.esh <<EOT
#!/bin/sh
echo "Do you really want to install or upgrade ${TOOL} (Y/N) ?"
read input
if [ "Q\$input" != "QY" ]
then
	echo "Think about it."
	exit
fi

if [ -s $DFO_BIN_DIR/dfosInstall ]
then
	echo "Automatic installation going on ..."
	dfosInstall -t ${TOOL}
else
	cd $DFO_INSTALL_DIR
	if [ ! -d ${TOOL} ]
	then
		mkdir ${TOOL}
	fi
	cd ${TOOL}
	if [ -s ${TAR_NAME} ]
	then
		mv ${TAR_NAME} ${TAR_NAME}.old
	fi
	wget -q ${TOOL_URL}/${TAR_NAME}
	echo " -${TAR_NAME} downloaded into $DFO_INSTALL_DIR/${TOOL}."
	echo " -Go there, type 'tar -xvf ${TAR_NAME}' and proceed along the README file."
	echo "  [Fully automatic installation not yet available.]"
	echo "Hit return:"
	read input
fi
echo " -Refresh dfosExplorer ..."
dfosExplorer
exit
EOT
chmod u+x $DFO_GUI_DIR/download_${TOOL}.esh
}

# ===================================================================================
# 1. Get config table with dfos tools
# ===================================================================================

if [ -s $DFO_BIN_DIR/dfosInstall ]
then
	echo "*** INFO: dfosInstall found. Automatic installation possible."
else

	echo "*** INFO: dfosInstall not found. Only manual installation possible."
fi
echo ""

# set in .dfosrc
if [ $THIS_IS_DFOS = YES ]
then
	REFERENCE=".reference.installation"
elif [ $THIS_IS_PHOENIX = YES ]
then
	REFERENCE=".reference.installation_phoenix"
elif [ $THIS_IS_OPSHUB = YES ]
then
	REFERENCE=".reference.installation_opshub"
fi

rm -f $TMP_DIR/$REFERENCE
cd $TMP_DIR
eval "wget -q ${TOOL_URL}/$REFERENCE"
REFERENCE=$TMP_DIR/$REFERENCE

if [ ! -s $REFERENCE ]
then
	echo "***ERROR: $REFERENCE not found, can't continue. Exit."
	exit -1
fi

# ===================================================================================
# 2. Create table 
# ===================================================================================

includeStyles

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

cat > $TMP_DIR/dfosExplorer.html <<EOT
<html>
<head>
EOT

# include styles
cat $TMP_DIR/styles >> $TMP_DIR/dfosExplorer.html

if [ $THIS_IS_PHOENIX = YES ]
then
	INSTR_TAG="instrument: PHOENIX/$DFO_INSTRUMENT"
elif [ $THIS_IS_OPSHUB = YES ]
then
	INSTR_TAG="installation: OPSHUB"
else
	INSTR_TAG="instrument: $DFO_INSTRUMENT"
fi


cat >> $TMP_DIR/dfosExplorer.html <<EOT
<title>DFOS explorer (${INSTR_TAG}) </title>
</head>

<body class=page>
<table BORDER=0 WIDTH=$TABLE_WIDTH>
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td width=5% rowspan=2>
EOT

if [ -s $DFO_MON_DIR/vnavbar_dfosExplorer.html ] && [ $THIS_IS_OPSHUB = NO ]
then
	cat $DFO_MON_DIR/vnavbar_dfosExplorer.html >> $TMP_DIR/dfosExplorer.html

elif [ $THIS_IS_OPSHUB = NO ]
then
	echo "***WARNING: no \$DFO_MON_DIR/vnavbar_dfosExplorer.html found. Create with dfoMonitor >> navigation bar: refresh"
else
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
      <table cellpadding="0" cellspacing="0" border=0>
        <tr align="left" valign="bottom">
         <td>

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

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

      <a href=${INSTR_DFOMONITOR}/dfoMonitor.html title="back to open PROJECTS"><font size=2 color=${TITLE_COLOR}>back to PROJECTS monitor</font></a>
EOT
fi

cat >> $TMP_DIR/dfosExplorer.html <<EOT
    </td>
    <td COLSPAN=2 WIDTH="95%">
      <b><font size=+2 color=$TITLE_COLOR> DFOS installation explorer (${INSTR_TAG}) </font></b><br>
      <font size=2>last update: $UPDATE; machine: <b>`hostname`</b>; account: <b>`whoami`</b>
EOT

if [ $THIS_IS_OPSHUB = YES ]
then
	HELPER=${ALL_URL}/${TOOL_NAME}_help_opshub.html
else
	HELPER=${ALL_URL}/${TOOL_NAME}_help.html
fi

if [ $THIS_IS_PHOENIX = YES ]
then
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
<br>
This is a PHOENIX installation with a subset of DFOS tools and the PHOENIX-related tools added.
EOT

elif [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
<br>
This is an OPSHUB installation with a subset of DFOS tools and the OPSHUB-related tools added.
EOT
fi

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

  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td colspan=2>
      <p><font size=2>This is an overview of the status of your DFOS installation on '`whoami`@`hostname`'. It checks the availibility and the version of 
the DFOS tools on your account against the reference installation. Any mismatches are indicated.<br>
EOT

if [ $THIS_IS_OPSHUB = YES ]
then
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
To fix, call the given helper tools on the command line. </p>
EOT
else
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
<p>
EOT
fi

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

  <tr BGCOLOR=$TITLE_COLOR>
    <td WIDTH=5% nowrap ALIGN=LEFT>
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
      <img src=$IMG_URL/back.gif width=20 height=20 border=0> 
      <a href=$DFO_GUI_DIR/refresh_dfosExplorer.esh title="refresh this page"><img src="${DFOS_URL}/images/refresh.png" border=0></a> 
EOT
else
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
<font size=2 color=white>Refresh:<br><tt> $TOOL_NAME </tt>
EOT
fi

cat >> $TMP_DIR/dfosExplorer.html <<EOT
    </td>   
    <td WIDTH=80% nowrap>&nbsp;</td>       
    <td WIDTH=10% nowrap ALIGN=right>  
      <a title="help for this page" href=$HELPER><font COLOR=#FFFFFF size=2>HELP</a> |   
EOT

if [ $THIS_IS_OPSHUB = NO ]
then
	cat >> $TMP_DIR/dfosExplorer.html <<EOT
      <a title="edit config file"   href=${DFO_GUI_DIR}/edit_dfosExplorer.esh><font COLOR=#FFFFFF>config</a> |  
EOT
fi

cat >> $TMP_DIR/dfosExplorer.html <<EOT
      <a title="dfos tools"         href=${DFOS_URL}/index.html><font COLOR=#FFFFFF>dfos</a>
    </td>
  </tr>
</table>
EOT

# ===================================================================================
# 2. DFOS tool table
# 2.1 prepare list of items
# ===================================================================================

rm -f $TMP_DIR/dfos_list $TMP_DIR/cfg_list
grep "^INFRA_DFOS" 	$REFERENCE >  $TMP_DIR/dfos_list
grep "^DAILY_DFOS" 	$REFERENCE >> $TMP_DIR/dfos_list
grep "^PACKING_DFOS" 	$REFERENCE >> $TMP_DIR/dfos_list
grep "^CONFIG"		$REFERENCE >  $TMP_DIR/cfg_list

# ===================================================================================
# 2.2 Loop over INDEX
# ===================================================================================
cat >> $TMP_DIR/dfosExplorer.html <<EOT
<table BORDER=0 WIDTH="$TABLE_WIDTH">
EOT

for INDEX in `cat $TMP_DIR/dfos_list | awk '{print $2}'`
do

# ===================================================================================
# 2.2.1 Headline
# ===================================================================================

	INDEX_1=`echo $INDEX | sed "s/HEADLINE.*/HEADLINE/"`
	if [ "Q${INDEX_1}" = "QHEADLINE" ]
	then
		TITLE=`grep ${INDEX} $TMP_DIR/dfos_list | sed "s/^.*&&//"`

	cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr VALIGN=TOP BGCOLOR=#CCCCCC>
    <td COLSPAN="9"> <b><font size=+2> $TITLE </font></b></td>
  </tr>
EOT
		continue
	fi

# ===================================================================================
# 2.2.2 Horizontal ruler
# ===================================================================================

	if [ $INDEX = "RULER" ]
	then
		echo "  <tr><td colspan=9><hr noshade size=1></td></tr>" >> $TMP_DIR/dfosExplorer.html
		continue
	fi

# ===================================================================================
# 2.2.3 Header
# ===================================================================================

	if [ $INDEX = "HEADER" ]
	then
		cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr class=header>
    <td>workflow part</td>
    <td>step</td>
    <td>DFOS tool<br><font size=2>(local version)</font></td>
    <td width=300>installed version</td>
    <td>latest available version</td>
    <td>documentation</td>
EOT
		if [ $THIS_IS_OPSHUB = YES ]
		then
			cat >> $TMP_DIR/dfosExplorer.html <<EOT
    <td>configuration file</td>
    <td>comment</td>
  </tr>
EOT
		else
			cat >> $TMP_DIR/dfosExplorer.html <<EOT
    <td>configuration file(s) (click to edit)</td>
    <td>comment</td>
    <td>applicable</td>
  </tr>
EOT
		fi

		continue
	fi

# ===================================================================================
# 2.2.4 Find rules files
# ===================================================================================

	if [ $INDEX = "PLUGIN_RULES" ]
	then
		rm -f $TMP_DIR/rules
		cd $DFO_CONFIG_DIR/OCA
		ls *.h | grep -v filter > $TMP_DIR/rules
		ls log4j.cf >> $TMP_DIR/rules
		echo " " >> $TMP_DIR/rules
		ls | grep ".rules$" >> $TMP_DIR/rules
		if [ -s $TMP_DIR/rules ]
		then
			for RULE in `cat $TMP_DIR/rules`
			do
				CFG=`echo $RULE`
				CFG_VI=`echo $CFG | sed "s/config.//" | sed "s/\.cfg//"`
				TOOL=`echo $RULE`
				createGuiJob
			done
	
			echo "  <tr class=entry>
    <td COLSPAN=5 BGCOLOR=#CCCCFF>&nbsp;</td><td BGCOLOR=#CCCCFF>all under \$DFO_CONFIG_DIR/OCA:" >> $TMP_DIR/dfosExplorer.html	
			cat $TMP_DIR/rules | sed "s/^.*/<a href=\$DFO_GUI_DIR\/edit_&.esh><font color=#666666>&<\/a>/" | sed "s/^.*/| &/" | sed 1,1"s/|//" | sed "s/^.*/echo \"&\"/" > $TMP_DIR/rules1
			mv $TMP_DIR/rules1 $TMP_DIR/rules
			chmod u+x $TMP_DIR/rules
			$TMP_DIR/rules >> $TMP_DIR/dfosExplorer.html
			echo "    </td>
    <td BGCOLOR=#CCCCFF COLSPAN=2>&nbsp;</td>
  </tr>" >> $TMP_DIR/dfosExplorer.html
		else
			echo "  <tr>
    <td>&nbsp;</td>
    <td COLSPAN=7 class=entry>no association rules files installed!
    </td>
  </tr>" >> $TMP_DIR/dfosExplorer.html	
			
		fi
		continue
	fi

# ===================================================================================
# 2.2.5 Find trendPlotter config files
# ===================================================================================

	if [ $INDEX = "PLUGIN_TRENDP" ]
	then
		rm -f $TMP_DIR/tpl
		if [ -d $DFO_CONFIG_DIR/trendPlotter ]
		then
			cd $DFO_CONFIG_DIR/trendPlotter
			ls | grep "^config.trendPlotter" > $TMP_DIR/tpl
			ls | grep "^config.tp_" | grep -v KPI >> $TMP_DIR/tpl
			ls | grep "^config.tp_" | grep KPI >> $TMP_DIR/tpl
			if [ -s $TMP_DIR/tpl ]
			then
				for TPL in `cat $TMP_DIR/tpl`
				do
					CFG_VI=`echo $TPL | sed "s/config.//"`
					CFG=`echo $TPL`
					createGuiJob
				done
	
				echo "  <tr class=entry bgcolor=#CCCCFF>
    	<td COLSPAN=6>&nbsp;</td><td BGCOLOR=#CCCCFF>all under \$DFO_CONFIG_DIR/trendPlotter:" >> $TMP_DIR/dfosExplorer.html	
				cat $TMP_DIR/tpl |\
				 sed "s/config.//" |\
				 awk '{print "<a href=$DFO_GUI_DIR/edit_"$1".esh><font color=#666666>config."$1"</a>"}' |\
				 sed "s/^.*/| &/" |\
				 sed 1,1"s/|//" |\
				 sed "s/^.*/echo \"&\"/" > $TMP_DIR/tpl1
				mv $TMP_DIR/tpl1 $TMP_DIR/tpl
				chmod u+x $TMP_DIR/tpl
				$TMP_DIR/tpl >> $TMP_DIR/dfosExplorer.html
				echo "    </td><td COLSPAN=2>&nbsp;</td></tr>" >> $TMP_DIR/dfosExplorer.html
			else
				echo "  <tr>
    <td>&nbsp;</td>
    <td COLSPAN=7 class=entry>no report configuration files installed!
    </td>
  </tr>" >> $TMP_DIR/dfosExplorer.html	
			fi
		else
			                                echo "  <tr bgcolor=#cccccc>
    <td>&nbsp;</td>
    <td COLSPAN=8 class=entry>no trending report configuration files installed!
    </td>
  </tr>" >> $TMP_DIR/dfosExplorer.html 
 		fi
		continue
	fi

# ===================================================================================
# 2.3 DFOS tools
# ===================================================================================

	PART=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $3}'`
	STEP=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $4}'`
	LEVEL=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $5}'`
	TYPE=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $6}'`
	TOOL=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $7}'`
	URL=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $8}'`
	LINK_YN=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $9}'`
	APPL=`grep $INDEX 	$TMP_DIR/dfos_list | awk '{print $10}' | sed "s/CALIB_SCIENCE/CALIB, SCIENCE/"`
	STD_INSTALL=`grep $INDEX $TMP_DIR/dfos_list | awk '{print $11}'`	#part of dfos std installation?
	TAR_NAME=`grep $INDEX	$TMP_DIR/dfos_list | awk '{print $12}'`		#name of tar file (for download)

	echo "  $TOOL..."

	if [ "$APPL" = "NONE" ]
	then
		APPL="&nbsp;"
	fi

	case $TYPE in
	 "PROTO" ) BGCOLOR="$QC_BGCOLOR" ;;
	 "BET"   ) BGCOLOR="$DFS_BGCOLOR" ;;
	esac

	if [ $STD_INSTALL = "YES" ]
	then
		case $TOOL in
		 "utilPack"  ) CHECK_EXIST=`ls -d $DFO_INSTALL_DIR/* | grep utilPack`
				if [ "Q$CHECK_EXIST" != "Q" ]
				then
					TOOL_EXIST=YES
				else
					TOOL_EXIST=""
				fi ;;
		  *          ) TOOL_EXIST=`ls ${DFO_BIN_DIR} | sed "s/^.*/&YY/" | grep "${TOOL}YY"` ;;
		esac
	else
		TOOL_EXIST="YES"
	fi

	if [ "Q${TOOL_EXIST}" = "Q" ]
	then
# tool not installed
		TOOL_EXIST="NO"
		GUI_LINK=none
		if [ $THIS_IS_OPSHUB = NO ]
		then
			TOOL_VERSION1=`echo "not installed! <br><a href=$DFO_GUI_DIR/download_${TOOL}.esh><img src=$IMG_URL/ball.red.gif border=0></a><font color=#FFFFFF>[download]"`
		else
			TOOL_VERSION1=`echo "not installed! <br><a href=$DFO_GUI_DIR/download_${TOOL}.esh></a><font color=#FFFFFF>[download: call $DFO_GUI_DIR/download_${TOOL}.esh]"`
		fi
		CFG_STRING="&nbsp;"
		getHistory 
		createGuiDownload
	else
# tool installed
		TOOL_EXIST="YES"
	
		rm -f $TMP_DIR/cfg_list1
		CFG_STRING=""
		GUI_LINK=""
		grep "^CONFIG" $TMP_DIR/cfg_list | grep "$INDEX" | awk '{print $4}' > $TMP_DIR/cfg_list1
		if [ -s $TMP_DIR/cfg_list1 ]
		then
			for CFG in `cat $TMP_DIR/cfg_list1`
			do
				CFG_VI=`echo $CFG | sed "s/config.//" | sed "s/\.cfg//"`
				if [ ${CFG_VI} != "NONE" ]
				then
					createGuiJob
					if [ $THIS_IS_OPSHUB = NO ]
					then
						#GUI_LINK=`echo "edit_${CFG_VI}.esh"`
						GUI_LINK="${DFO_GUI_DIR}/edit_${CFG_VI}.esh"
					else
						GUI_LINK="${DFO_CONFIG_DIR}/${CFG}"
					fi
						
					if [ "Q$CFG_STRING" = "Q" ]
					then
						CFG_STRING=`echo "<a href=${GUI_LINK}><font color=#666>${CFG}</font></a>"`
					else
						CFG_STRING=`echo "$CFG_STRING | <a href=${GUI_LINK}><font color=#666>${CFG}</font></a>"`
					fi
				fi
			done
		fi

		case "$LINK_YN" in
			YES )	TOOL_VERSION1=`grep "^TOOL_VERSION=" $DFO_BIN_DIR/$TOOL | sed "s/TOOL_VERSION=//" | sed "s/\"//g"` 
				getHistory ;;

			NO )	if [ $STD_INSTALL != "NO" ] 
				then
					case $TOOL in
					 "utilPack"  ) CHECK_EXIST=`ls -d $DFO_INSTALL_DIR/* | grep utilPack`
							if [ "Q$CHECK_EXIST" != "Q" ]
							then
								TOOL_VERSION1=`ls $DFO_INSTALL_DIR/utilPack/* | grep TOOL | sed "s/=/ /" | awk '{print $2}' | sed "s/\"//g"` 
							else
								TOOL_VERSION1="not installed"
							fi ;;
					 *           ) TOOL_VERSION1=`$DFO_BIN_DIR/$TOOL -v | awk '{print $3}' | head -2 | tail -1` ;;
					esac
					getHistory
				else
					TOOL_VERSION1="&nbsp;" 
					LATEST_VERSION="&nbsp;" 
				fi ;;

		esac
	fi

	COMMENT=`grep "^COMMENT" $REFERENCE | grep -v "^#" | grep $INDEX | sed "s/^.*&&//" | grep -v COMMENT | grep -v INDEX`

	case "$PART" in 
		VOID )	PART_STRING="";;
		* )	LABEL=`echo	$PART | sed "s/_/ /" | awk '{print $1}'`
			ROWSPAN=`echo	$PART | sed "s/_/ /" | awk '{print $2}'`
			if [ "Q$ROWSPAN" = "Q" ]
			then
				ROWSPAN=1
			fi
			PART_STRING=`echo "    <td ROWSPAN=$ROWSPAN BGCOLOR=#FF9999>$LABEL</td>"`;;
	esac

	case $LEVEL in
		"1" )	ALIGN=LEFT ;;
		"2" )	ALIGN=RIGHT ;;
	esac

	case $PART in
	 "TQS" ) URL_STRING=`echo "<a href=${TQS_URL}/${URL}>$URL</a>"` ;;
	 *     ) URL_STRING=`echo "<a href=${DFOS_URL}/${URL}>$URL</a>"` ;;
	esac

	if [ $URL = "NONE" ]
	then
		URL_STRING=`echo "&nbsp;"`
	fi

	ALARM=""

	if [ $TOOL_EXIST = "NO" ]
	then
		echo "    $TOOL is not installed!"
		ALARM="<font color=${ALARM_COLOR}>!"
	elif [ "$TOOL_VERSION1" != "$LATEST_VERSION" ] 
	then
		echo "    version mismatch found: $TOOL has local version $TOOL_VERSION1, latest is $LATEST_VERSION."
		ALARM="<font color=${ALARM_COLOR}>!"
		if [ $THIS_IS_OPSHUB = NO ]
		then
			TOOL_VERSION1=`echo "outdated!<br><a href=$DFO_GUI_DIR/download_${TOOL}.esh><img src=$IMG_URL/ball.red.gif border=0></a><font color=#FFFFFF>[upgrade]"`
		else
			TOOL_VERSION1=`echo "outdated!<br><a href=$DFO_GUI_DIR/download_${TOOL}.esh></a><font color=#FFFFFF>[update: call<br> $DFO_GUI_DIR/download_${TOOL}.esh]"`
		fi
		createGuiDownload
	fi

	if [ $THIS_IS_OPSHUB = NO ]
	then
    		APPL_CELL="<td nowrap>$APPL</td>"
# OPSHUB: no APPLICABLE required
	else
		APPL_CELL=""
	fi
	
	if [ $TOOL_EXIST = "NO" ]
	then
		cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr class=entry BGCOLOR=$BGCOLOR>
${PART_STRING}
    <td ALIGN=$ALIGN>$STEP</td>
    <td ALIGN=$ALIGN>$TOOL</td>
    <td nowrap>${ALARM}${TOOL_VERSION1}</td>
    <td>${LATEST_VERSION}</td>
    <td>${URL_STRING}</td>
    <td>${CFG_STRING}</td>
    <td>$COMMENT</td>
    $APPL_CELL
  </tr>
EOT

	elif [ $LINK_YN = "YES" ]
	then
		cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr class=entry BGCOLOR=$BGCOLOR>
${PART_STRING}
    <td ALIGN=$ALIGN>$STEP</td>
    <td ALIGN=$ALIGN><a href=$DFO_BIN_DIR/$TOOL>$TOOL</a></td>
    <td>${ALARM}${TOOL_VERSION1}</td>
    <td>${LATEST_VERSION}</td>
    <td>${URL_STRING}</td>
    <td>${CFG_STRING}</td>
    <td>$COMMENT</td>
    $APPL_CELL
  </tr>
EOT
	else
		cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr class=entry BGCOLOR=$BGCOLOR>
${PART_STRING}
    <td ALIGN=$ALIGN>$STEP</td>
    <td ALIGN=$ALIGN>$TOOL</td>
    <td nowrap>${ALARM}${TOOL_VERSION1}</td>
    <td>${LATEST_VERSION}</td>
    <td>${URL_STRING}</td>
    <td>${CFG_STRING}</td>
    <td>$COMMENT</td>
    $APPL_CELL
  </tr>
EOT
	fi
done

CFG_VI="dfosExplorer"
#CFG="config.dfosExplorer"
if [ $THIS_IS_OPSHUB = NO ]
then
	createGuiJob
fi

# ===================================================================================
# 2.4 Finish the html page
# ===================================================================================

cat >> $TMP_DIR/dfosExplorer.html <<EOT
  <tr BGCOLOR=#CCCCCC>
    <td ALIGN=RIGHT COLSPAN=9><font size=2 COLOR=#FFFFFF>
      created by dfosExplorer v${TOOL_VERSION}, a <b>dfos</b> tool</font>
    </td>
  </tr>
</table>
</body>
</html>
EOT

mv $TMP_DIR/dfosExplorer.html $DFO_MON_DIR/

# ===================================================================================
# 3.0 call browser and refresh
# ===================================================================================

BROWSER_ACTIVE=`ps -wfC $DFO_BROWSER | grep -v CMD`

if [ "Q$BROWSER_ACTIVE" != "Q" ] && [ $BROWSER_REFRESH = "YES" ]
then
	$DFO_GUI_DIR/refresh_browser $DFO_MON_DIR/dfosExplorer.html
else
	echo "Watch results under $DFO_MON_DIR/dfosExplorer.html."	
fi

echo "... done."
exit
