#!/bin/sh
# PURPOSE:      create a navigation bar from a configuration file
# AUTHOR:       Reinhard Hanuschik, ESO/DFO
# VERSIONS:     1.0 -- July 2007
#		1.0.1- transfer to web server generalized (ssh to $WEB_SERVER) (2007-07-13)
#		1.0.2- qc_style always included (2007-07-30)
#		1.1 -- TYPE=HEALTH --> supports HC scoring (2008-10-15)
#		1.1.1- bug fixed for nav-otheritem (2008-10-23)
#		1.1.2- improved ssh/scp; introducing noscoreitem (2008-11-04)
#		1.1.3- improved handling of noscoreitem (2008-11-06) [SMo]
#		1.2 -- supports TYPE=CAL (for calScores) (2012-05-14)
#		1.2.1- bug fix for noscoreitem (2012-11-21)
#		1.3 -- supporting HEALTH_MAIN (same format as CAL, incl. ins scores) (2013-09-17)
#		1.4 -- replace 'switch' by 'change' in awk commands (for OS upgrade); remove w4  (2014-02-19)
#		1.4.1- HC_ENABLED obsolete (2014-10-29)
#		1.5 -- TYPE=CAL: includes tellTracker signal (2015-03-23)
#		1.5.1- minor bug fix in line 391 (2017-03-21)
#		1.6 -- TOC decommissioned (2017-09-06)
#		1.6.1- minor improvements for stability (2018-09-10)
#
# CONFIG:      	config.webNavBar under $DFO_CONFIG_DIR/webNavBar; config files as specified in menue
# OUTPUT:       navigation bar (html file)
# NOTES:	this tool is part of the TQS system
# 		QC_DIR: for v1.4 make sure to replace in config files /home/web/eso/docs/observing/dfo/quality by /home/qc/qc !
# ======================================================================
TOOL_VERSION="1.6.1"

TOOL_URL="http://www.eso.org/~qc/dfos/tools"
QC_URL="http://www.eso.org/qc"
IMG_URL="http://www.eso.org/images/analog"
QC1DB_URL="http://archive.eso.org/bin/qc1_cgi?action=qc1_browse_table&table="

MAX_LENGTH=25

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

CHECK=`printenv | grep DFO`

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

CONFIG_DIR=$DFO_CONFIG_DIR/webNavBar
if [ ! -d $CONFIG_DIR ]
then
	echo "*** ERROR: no config directory $DFO_CONFIG_DIR/webNavBar found! Exit."
	exit -1
fi

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

# =========================================================================
# 0.2 check params
# =========================================================================

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

# =========================================================================
# 0.3 Read configuration
# =========================================================================

grep "^NAVBAR[[:space:]]" $CONFIG_DIR/config.webNavBar | awk '{print $2" - "$4}' | sed "s/^SPACE.*//" | sed "s/_/ /"g
echo ""
echo "Select navigation bar (exit: . ):"
read NAVBAR
if [ "Q$NAVBAR" = "Q." ]
then
	exit
fi
if [ "Q$NAVBAR" = "Q" ]
then
	echo "*** Think about it."
	exit 
fi

CONFIG_NAME=`grep "^NAVBAR[[:space:]]" $CONFIG_DIR/config.webNavBar | grep "[[:space:]]$NAVBAR[[:space:]]" | awk '{print $3}'`

if [ ! -s $CONFIG_DIR/$CONFIG_NAME ] || [ "Q$CONFIG_NAME" = "Q" ]
then
	echo "*** ERROR: No config file $CONFIG_DIR/$CONFIG_NAME found for selected option $NAVBAR. Exit."
	exit -1
fi

QC_DIR=`grep "^QC_DIR"		$CONFIG_DIR/$CONFIG_NAME | awk '{print $2}'`
if [ "Q$QC_DIR" = "Q" ]
then
	echo "***ERROR: No \$QC_DIR defined in $CONFIG_DIR/$CONFIG_NAME. Exit."
	exit -1
fi

HIGHLIGHT=`grep "^HIGHLIGHT"	$CONFIG_DIR/$CONFIG_NAME | awk '{print $2}'`
if [ "Q$HIGHLIGHT" != "QYES" ]
then
	HIGHLIGHT=NO
fi

FULL=`grep "^FULL"		$CONFIG_DIR/$CONFIG_NAME | awk '{print $2}'`
if [ "Q$FULL" != "QYES" ]
then
	FULL=NO
fi

TYPE=`grep "^TYPE[[:space:]]"	$CONFIG_DIR/$CONFIG_NAME | awk '{print $2}'`
if [ "Q$TYPE" = "Q" ]
then
	TYPE=STANDARD
fi

# ======================================================================
# 0.4 procedure createTop to create top part of navigation bar
# ======================================================================
createTop(){
cat > $TMP_DIR/$NAVBAR_NAME <<EOT
<!-- created by webNavBar v$TOOL_VERSION --> 
<LINK rel="StyleSheet" href="/qc/ALL/common/qc_eso.style" type="text/css">

<!-- java script -->
<script type="text/javascript"><!--
 imageOn = new Image();  imageOff = new Image();
 imageOn.src = "/images/itemOn.gif";
 imageOff.src = "/images/itemOff.gif";

 function over(imgname) { imgname.src = imageOn.src }
 function out(imgname)  { imgname.src = imageOff.src }
//--></script>

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

# include INCLUDE_TOP part
rm -f $TMP_DIR/list_includes
grep "^INCLUDE_TOP" $CONFIG_DIR/$CONFIG_NAME | awk '{print $2}' > $TMP_DIR/list_includes
if [ -s $TMP_DIR/list_includes ]
then
	if [ $TYPE != CAL ]
	then
		for IT in `cat $TMP_DIR/list_includes`
		do
			cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
<!--begin include INCLUDE_TOP -->
<!--#include virtual="$IT" -->
<!--end include INCLUDE_TOP -->

EOT
		done
	fi

# for CAL, expand them by another column (to accomodate the tellTracker signal)
	if [ $TYPE = CAL ]
	then
		for IT in `cat $TMP_DIR/list_includes`
		do
			IT1=`echo $IT | sed "s|/observing/dfo/quality|/home/qc/qc|"`
			IT2=`echo $IT1 | sed "s/.html/1.html/"`	
			IT3=`echo $IT | sed "s/.html/1.html/"`
			ssh -o BatchMode=yes $DFO_WEB_SERVER "sed \"s/COLSPAN=2/COLSPAN=3/\" $IT1 | sed \"s/colspan=2/colspan=3/\" > $IT2"
			cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
<!--begin include INCLUDE_TOP -->
<!--#include virtual="${IT3}" -->
<!--end include INCLUDE_TOP -->

EOT
		done
	fi
fi
}

# ======================================================================
# 0.5 procedure createEntry to create an HTML line in the navigation bar
# ======================================================================

createEntry(){
echo "" >> $TMP_DIR/$NAVBAR_NAME
echo "<!-- $ITEM -->" >> $TMP_DIR/$NAVBAR_NAME

# check for TEXT length
if [ $CLASS = "nav-div" ] || [ $CLASS = "nav-head" ]
then
	LENGTH=`echo $TEXT | sed "s/\&nbsp;//"g | wc -c`
	if [ $LENGTH -gt $MAX_LENGTH ]
	then
		echo "***ERROR: text \"$TEXT\" is too long, maximum length is $MAX_LENGTH chars."
		exit -1
	fi
fi

# SPACE
if [ "$TEXT" = SPACE ]
then
	SCORE_COLSPAN="<td COLSPAN=2"

	cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr>${SCORE_COLSPAN} class="$CLASS">&nbsp;</td></tr>
EOT
	return
fi

# items: have no links, don't split for scores
if [ "$CLASS" = "nav-item" ] || [ "$CLASS" = "nav-otheritem" ] || [ "$CLASS" = "nav-dailyitem" ]
then
	case $TYPE in
	"HEALTH"|"HEALTH_MAIN" ) SCORE_COLSPAN="<td COLSPAN=2" ;;
	"CAL" 		       ) SCORE_COLSPAN="<td COLSPAN=3" ;;
	 *             	       ) SCORE_COLSPAN="<td" ;;
	esac

	if [ "$CLASS" = "nav-otheritem" ]
	then
		SCORE_COLSPAN="<td COLSPAN=2"	#always
	fi

# special case: nav-item and UT1 and TYPE=CAL: mark with 'C T'
	if [ "$CLASS" = "nav-item" ] && [ "$TEXT" = UT1 ] && [ $TYPE = CAL ]
	then
      		cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top"><td class="$CLASS" nowrap>$TEXT</a></td><td class="$CLASS"><a title="calScore">C</a></td><td class="$CLASS"><a title="tellTracker issue?">T</a></font></td></tr>
EOT
else	
      		cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top">${SCORE_COLSPAN} class="$CLASS" nowrap>$TEXT</a></td></tr>
EOT
	fi

# noscoreitem: subitem without score box
elif [ "$CLASS" = "nav-noscoreitem" ] || [ "$CLASS" = "this-nav-noscoreitem" ]
then
	SCORE_COLSPAN="<td "

	case $CLASS in
	 "nav-noscoreitem"      ) CLASS="nav-subitem" ;;
	 "this-nav-noscoreitem" ) CLASS="this-nav-subitem" ;;
	esac	

	SCORE_TAG="<td class="$CLASS"> </td>"
        cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top">${SCORE_COLSPAN} class="$CLASS" nowrap><img name="$ITEM" align="bottom" src="/images/itemOff.gif" width="10" height="10" border="0" vspace="0" hspace="3"><a class="$CLASS" onMouseOver="over($ITEM);" onMouseOut="out($ITEM);" href="$LINK" title="$WINDOW_TEXT">$TEXT</a></td>${SCORE_TAG}</tr>
EOT

# subitems: have links
elif [ "$CLASS" = "nav-subitem" ] || [ "$CLASS" = "this-nav-subitem" ] || [ "$CLASS" = "nav-othersubitem" ]
then
	SCORE_NAME=`echo $NNAME | sed "s/vnavbar_HC_/score_grp_/"`
	SCORE_NAME="<!--#include virtual=\"$SCORE_NAME\" -->"
# for HC ins-specific 
	if [ Q$NNAME = "Qvnavbar_HC_scores.html" ] 
	then
		SCORE_NAME="<!--#include virtual=\"score_ins.html\" -->"
	fi

# for HEALTH_MAIN
	if [ Q$NNAME = "Q" ]
	then
		if ( [ $CLASS = "nav-subitem" ] || [ $CLASS = "nav-othersubitem" ] ) && [ Q$LINK != QNONE ]
		then
			HC_INS=`echo $LINK | tr "/" " " | awk '{print $2}'`
			if [ Q$HC_INS != Q ]
			then
				SCORE_NAME="<!--#include virtual=\"/observing/dfo/quality/${HC_INS}/common/score_ins.html\" -->"
			fi
		fi
	fi

	if [ "Q$SCORE_NAME" = "Q<!--#include virtual=\"\" -->" ]
	then
		SCORE_NAME=""
	fi

# for calChecker (cal_scores and tellTracker warnings)
	CAL_INS=`echo $LINK | sed "s/^.*calChecker_//" | sed "s/.html//"`
	CAL_SCORE_NAME="<!--#include virtual=\"/observing/dfo/quality/${CAL_INS}/reports/CAL/cal_score.html\" -->"
	TELL_WARN_NAME="<!--#include virtual=\"/observing/dfo/quality/${CAL_INS}/reports/CAL/tell_warn.html\" -->"

	case $TYPE in 
	"HEALTH_MAIN"|"HEALTH"  ) SCORE_TAG="<td bgcolor=#CCFFFF align=center valign=middle>${SCORE_NAME}</td>" ; SCORE_COLSPAN="<td " ;;
	"CAL" 	       ) SCORE_TAG="<td bgcolor=#CCFFFF align=center valign=middle>${CAL_SCORE_NAME}</td><td bgcolor=#CCFFFF align=center valign=middle>${TELL_WARN_NAME}</td>" ; SCORE_COLSPAN="<td " ;;
	 *             ) SCORE_TAG="" ; SCORE_COLSPAN="<td COLSPAN=2" ;;
	esac

	if [ "$CLASS" = "nav-othersubitem" ] && [ $TYPE != "HEALTH_MAIN" ]
	then
		SCORE_TAG=""
		SCORE_COLSPAN="<td COLSPAN=2"
	fi

# nav-subitem: NONE
	if [ "$LINK" = "NONE" ]
	then
               	cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top">${SCORE_COLSPAN} class="$CLASS" nowrap><img name="$ITEM" align="bottom" src="/images/itemOff.gif" width="10" height="10" border="0" vspace="0" hspace="3">$TEXT</a></td>${SCORE_TAG}</tr>
EOT

# nav-subitem: HR 
	elif [ "$TEXT" = "HR" ]
	then
		cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr>${SCORE_COLSPAN}><hr noshade></td></tr>
EOT
	else
               	cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top">${SCORE_COLSPAN} class="$CLASS" nowrap><img name="$ITEM" align="bottom" src="/images/itemOff.gif" width="10" height="10" border="0" vspace="0" hspace="3"><a class="$CLASS" onMouseOver="over($ITEM);" onMouseOut="out($ITEM);" href="$LINK" title="$WINDOW_TEXT">$TEXT</a></td>${SCORE_TAG}</tr>
EOT
	fi

# subsubitems: have links (only for STANDARD)
elif [ "$CLASS" = "nav-subsubitem" ] || [ "$CLASS" = "this-nav-subsubitem" ]
then
	case $CLASS in
	 "nav-subsubitem"      ) CLASS="nav-subitem"      ;;
	 "this-nav-subsubitem" ) CLASS="this-nav-subitem" ;;
	esac
	cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
  <tr align="left" valign="top"><td class="$CLASS" nowrap>&nbsp;&nbsp;&nbsp;<img name="$ITEM" align="bottom" src="/images/itemOff.gif" width="10" height="10" border="0" vspace="0" hspace="3"><a class="$CLASS" onMouseOver="over($ITEM);" onMouseOut="out($ITEM);" href="$LINK" title="$WINDOW_TEXT">$TEXT</a></td></tr>
EOT

# nav-head, nav-div: no links
elif [ "$CLASS" = "nav-head" ] || [ "$CLASS" = "nav-div" ]
then
	SCORE_COLSPAN="<th COLSPAN=2"

	cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
   <tr align="left" valign="top">${SCORE_COLSPAN} class="$CLASS" nowrap>$TEXT</th></tr>
EOT

# UNKNOWN
else
	echo "***ERROR: Unknown class: $CLASS. Check and re-start."
	exit
fi
}

# ======================================================================
# 0.6 final part to close HTML file
# ======================================================================

createEnd(){
# include INCLUDE_BOTTOM part
rm -f $TMP_DIR/list_includes
grep "^INCLUDE_BOTTOM" $CONFIG_DIR/$CONFIG_NAME | awk '{print $2}' > $TMP_DIR/list_includes
if [ -s $TMP_DIR/list_includes ]
then
	if [ $TYPE != CAL ]
	then
		for IT in `cat $TMP_DIR/list_includes`
		do
			cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
<!--begin include INCLUDE_BOTTOM -->
<!--#include virtual="$IT" -->
<!--end include INCLUDE_BOTTOM -->

EOT
		done
	fi

# for CAL, expand them by another column (to accomodate the tellTracker signal)
	if [ $TYPE = CAL ]
	then
		for IT in `cat $TMP_DIR/list_includes`
		do
			IT1=`echo $IT | sed "s|/observing/dfo/quality|/home/qc/qc|"`
			IT2=`echo $IT1 | sed "s/.html/1.html/"`	
			IT3=`echo $IT | sed "s/.html/1.html/"`
			ssh -o BatchMode=yes $DFO_WEB_SERVER "sed \"s/COLSPAN=2/COLSPAN=3/\" $IT1 | sed \"s/colspan=2/colspan=3/\" > $IT2" 
			cat >> $TMP_DIR/$NAVBAR_NAME <<EOT
<!--begin include INCLUDE_BOTTOM -->
<!--#include virtual="${IT3}" -->
<!--end include INCLUDE_BOTTOM -->

EOT
		done
	fi
fi

SCORE_COLSPAN="<td COLSPAN=99"

cat >> $TMP_DIR/$NAVBAR_NAME <<EOT

  <tr>${SCORE_COLSPAN} class="nav-othersubitem"><font size=-2 color="#FFFFFF"><a class="hide"> [best viewing with style sheets and javascript enabled]</a></font></td></tr>
</table>
EOT
}

# ======================================================================
# 0.7 procedure makeToc to create TOC file: obsolete
# ======================================================================

# ======================================================================
# 0.8 check for already existing score_overview.html; if none found, 
#     write dummy to be overwritten later by scoreHC
# ======================================================================

check4overview(){
rm -f $TMP_DIR/score_overview.html
cd $TMP_DIR
eval "wget -q ${QC_URL}/${DFO_INSTRUMENT}/common/score_overview.html"
if [ ! -s $TMP_DIR/score_overview.html ]
then
	cat > $TMP_DIR/score_overview.html <<EOT
<html>
No scoring overview yet available.
</html>
EOT
	scp -o BatchMode=yes $TMP_DIR/score_overview.html ${DFO_WEB_SERVER}:${QC_DIR}/
	ssh -o BatchMode=yes $DFO_WEB_SERVER "chmod a+rx,u+w ${QC_DIR}/score_overview.html"
	#ssh -o BatchMode=yes $WEB_SERVER "bin/qcWebcp $QC_DIR score_overview.html yes"
fi
}

# ======================================================================
# 1. Start creating navigation bar
# 1.1 TYPE=TOC: create TOC file and exit
# ======================================================================

if [ $TYPE = TOC ]
then
	makeToc
	exit
fi

# ======================================================================
# 1.2 distinguish HIGHLIGHT=YES and NO
# ======================================================================

echo ""
echo "1. Start creating set of configured navigation bars ..."
case $HIGHLIGHT in
 "YES" ) echo "HIGHLIGHT=YES: family of highlighted navigation bars in $CONFIG_NAME ..." ;;
 "NO"  ) echo "HIGHLIGHT=NO: family of navigation bars in $CONFIG_NAME ..." ;;
esac

# ======================================================================
# 1.3 Prepare: extract configuration, find PARENT/CHILD relations
# ======================================================================

rm -f $TMP_DIR/list_cleaned $TMP_DIR/list_cleaned1

# HEALTH: enforce to start with "score overview" (but not HEALTH_MAIN!)
if [ $TYPE = HEALTH ]
then
	grep "^NAVBAR" $CONFIG_DIR/$CONFIG_NAME | grep nav-div > $TMP_DIR/list_cleaned1
	echo "NAVBAR score_overview nav-subitem /qc/${DFO_INSTRUMENT}/common/score_overview.html ${DFO_INSTRUMENT}_scores vnavbar_HC_scores.html" >> $TMP_DIR/list_cleaned1
	echo "NAVBAR SPACE nav-subitem SPACE SPACE" >> $TMP_DIR/list_cleaned1
	grep "^NAVBAR[[:space:]]" $CONFIG_DIR/$CONFIG_NAME | grep -v nav-div >> $TMP_DIR/list_cleaned1

# check for already existing score_overview.html
	check4overview
else
	grep "^NAVBAR[[:space:]]" $CONFIG_DIR/$CONFIG_NAME > $TMP_DIR/list_cleaned1
fi

cat $TMP_DIR/list_cleaned1 | awk '{print "ITEM"navbar"_"NR, $0}' navbar=$NAVBAR |\
   awk '{ 
	if ( $8 == "PARENT" ) {print $0,$7 ; change=1; parent=$7}
	if ( $8 == "CHILD" && change=1 ) {print $0,parent}
	if ( $8 != "CHILD" && $8 != "PARENT" ) {print $0 ; change=0} 
	}' > $TMP_DIR/list_cleaned

if [ $TYPE != STANDARD ]
then
	CHECK_PARENT=`egrep "PARENT|CHILD" $TMP_DIR/list_cleaned`
	if [ "Q$CHECK_PARENT" != "Q" ]
	then
		echo "***ERROR: SUBLEVEL configuration (PARENT/CHILD) only allowed for TYPE=STANDARD."
		exit -1
	fi
fi

# ======================================================================
# 1.4 HIGHLIGHT=YES: family  of navbars, with current item highlighted
# 1.4.1 get HIGHLIGHT names
# ======================================================================

if [ $HIGHLIGHT = YES ]
then
	rm -f $TMP_DIR/list_highlights
	cat $TMP_DIR/list_cleaned | grep -v "#" | awk '{print $7}' | sed "/^$/d" > $TMP_DIR/list_highlights

	if [ ! -s $TMP_DIR/list_highlights ]
	then
		echo "*** ERROR: configuration HIGHLIGHT=YES, but no HIGH_NAME specified. Can't do."
		exit -1
	fi

# ======================================================================
# 1.4.2 start loop over highlight names
# ======================================================================

	echo ""

	for H  in `cat $TMP_DIR/list_highlights | grep navbar`
	do

# ======================================================================
# 1.4.3 include the INCLUDE_TOP part
# ======================================================================

		rm -f $TMP_DIR/$H
		NAVBAR_NAME=$H
		echo "  $NAVBAR_NAME ..."
		if [ $FULL = YES ]
		then
			createTop
		fi

# ======================================================================
# 1.4.4 get entries
# ======================================================================

		THIS_ITEM=`cat $TMP_DIR/list_cleaned | awk '{print $1,$7}' | grep $H | awk '{print $1}' | head -1`
		IS_FAMILY=`grep "^$THIS_ITEM " $TMP_DIR/list_cleaned | awk '{print $8,$9}' | egrep "PARENT|CHILD" | awk '{print $2}'`
		rm -f $TMP_DIR/list_navbaritems
		for ITEM in `cat $TMP_DIR/list_cleaned | awk '{print $1}'`
		do
			CLASS=`grep "^$ITEM[[:space:]]" $TMP_DIR/list_cleaned | awk '{print $4}'`
			if [ "Q$IS_FAMILY" != "Q" ]
			then
				ITEM_IS_MEMBER=`grep "^$ITEM[[:space:]]" $TMP_DIR/list_cleaned | awk '{ if ( $9 == is_family ) print "yes"; if ( $9 != is_family ) print "no"}' is_family=$IS_FAMILY`
			else
				ITEM_IS_MEMBER=no
			fi

			if [ $CLASS != "nav-subsubitem" ]
			then
				grep "^$ITEM[[:space:]]" $TMP_DIR/list_cleaned | awk '{print $1,$4}' >> $TMP_DIR/list_navbaritems
			elif [ $ITEM_IS_MEMBER = yes ]
			then
				grep "^$ITEM[[:space:]]" $TMP_DIR/list_cleaned | awk '{print $1,$4}' >> $TMP_DIR/list_navbaritems
			fi
		done

		for ITEM in `cat $TMP_DIR/list_navbaritems | awk '{print $1}'`
		do
       			TEXT=`grep "^$ITEM[[:space:]]"		$TMP_DIR/list_cleaned | awk '{print $3}' | sed "s/_/ /g"`
        		CLASS=`grep "^$ITEM[[:space:]]"		$TMP_DIR/list_cleaned | awk '{print $4}'`
			if [ $TYPE != HEALTH ] && [ $CLASS = nav-dailyitem ]
			then
				echo "***ERROR: style 'nav-dailyitem' not allowed for TYPE=$TYPE."
				exit -1
			fi
			
        		LINK=`grep "^$ITEM[[:space:]]"		$TMP_DIR/list_cleaned | awk '{print $5}'`
        		WINDOW_TEXT=`grep "^$ITEM[[:space:]]"	$TMP_DIR/list_cleaned | awk '{print $6}' | sed "s/_/ /g"`
			if [ $ITEM = $THIS_ITEM ]
			then
				case $CLASS in
				 "nav-subitem"    ) CLASS="this-nav-subitem"    ;;
				 "nav-noscoreitem" ) CLASS="this-nav-noscoreitem"    ;;
				 "nav-subsubitem" ) CLASS="this-nav-subsubitem" ;;
				esac
			fi
        		NNAME=`grep "^$ITEM[[:space:]]"	$TMP_DIR/list_cleaned | awk '{print $7}'`
			createEntry
		done

# ======================================================================
# 1.4.5 HEALTH: create score files 
# ======================================================================

		if [ $TYPE = HEALTH ]
		then
			SCORE_NAME=`echo $NAVBAR_NAME | sed "s/vnavbar_HC_/score_grp_/"`
			if [ $NAVBAR_NAME = "vnavbar_HC_scores.html" ]
			then
				SCORE_NAME=score_ins.html
			fi
			SCORE_GRP=`echo $NAVBAR_NAME | sed "s/vnavbar_HC_//" | sed "s/.html//"`
			echo "<img src=${IMG_URL}/sqg.png width=1 height=1 border=0 title=\"${SCORE_GRP}: no scoring implemented yet\">" > $TMP_DIR/$SCORE_NAME
			echo $SCORE_NAME >> $TMP_DIR/list_highlights
		fi

# ======================================================================
# 1.4.6 get INCLUDE_BOTTOM added
# ======================================================================

		if [ $FULL = YES ]
		then
			createEnd
		fi
		chmod u+x $TMP_DIR/$H

		if [ $TYPE = HEALTH ]
		then
                        LIST_NOSCORE=`grep noscoreitem $CONFIG_DIR/$CONFIG_NAME | awk '{print $4}'`
			QUICK_NAME=`echo $NAVBAR_NAME | sed "s/vnavbar_HC_/vnavbar_QUICK_/"`
                        CLASSN=`grep $NAVBAR_NAME $CONFIG_DIR/$CONFIG_NAME | awk '{print $3}'`

# version 1.1.3 [SMo]: replace QUICK with HC for noscoreitem links
			rm -f $TMP_DIR/substitute
                        if [ "$CLASSN" = "nav-noscoreitem" ] || [ "$CLASSN" = "this-nav-noscoreitem" ]
			then
				cat $TMP_DIR/$NAVBAR_NAME > $TMP_DIR/$QUICK_NAME
			else
				cat $TMP_DIR/$NAVBAR_NAME | sed "s/_HC.html/_QUICK.html/" > $TMP_DIR/$QUICK_NAME
                            	for link in $LIST_NOSCORE
			    	do
                              		link_quick=`echo $link | sed "s/_HC.html/_QUICK.html/"`
                              		echo "cat $TMP_DIR/$QUICK_NAME | sed -e \"s|$link_quick|$link|\" > $TMP_DIR/XXX" >> $TMP_DIR/substitute
                              		echo "mv $TMP_DIR/XXX $TMP_DIR/$QUICK_NAME " >> $TMP_DIR/substitute
			    	done
	
				if [ -s $TMP_DIR/substitute ]
				then
                        	    	chmod u+x $TMP_DIR/substitute
                            		$TMP_DIR/substitute
				fi
			fi
			echo $QUICK_NAME >> $TMP_DIR/list_highlights
		fi
	done
fi

# ======================================================================
# 1.5 HIGHLIGHT=NO: single navbar
# 1.5.1 get INCLUDE_TOP
# ======================================================================

if [ $HIGHLIGHT = NO ]
then
	NAVBAR_NAME=`echo ${CONFIG_NAME}.html | sed "s/config.//"`
	rm -f $TMP_DIR/$NAVBAR_NAME

	if [ $FULL = YES ]
	then
		createTop
	fi

# ======================================================================
# 1.5.2 get entries
# ======================================================================

	for ITEM in `cat $TMP_DIR/list_cleaned | awk '{print $1}'`
	do
       		TEXT=`grep "^$ITEM[[:space:]]"	$TMP_DIR/list_cleaned | awk '{print $3}' | sed "s/_/ /g"`
        	CLASS=`grep "^$ITEM[[:space:]]"	$TMP_DIR/list_cleaned | awk '{print $4}'`
        	LINK=`grep "^$ITEM[[:space:]]"	$TMP_DIR/list_cleaned | awk '{print $5}'`
        	WINDOW_TEXT=`grep "^$ITEM[[:space:]]" $TMP_DIR/list_cleaned | awk '{print $6}' | sed "s/_/ /g"`

		createEntry
	done

# ======================================================================
# 1.5.3 get INCLUDE_BOTTOM
# ======================================================================

	if [ $FULL = YES ]
	then
		createEnd
	fi
	chmod u+x $TMP_DIR/$NAVBAR_NAME
fi

# ======================================================================
# 2. transfer to $DFO_WEB_SERVER and publish
# ======================================================================

echo ""
echo "2. Transfer to $DFO_WEB_SERVER and publish (Y/N)? (Y) "
read TRANSFER_YN

if [ "Q$TRANSFER_YN" != "QN" ]
then
	ssh -o BatchMode=yes $DFO_WEB_SERVER "bin/qcDircheck $QC_DIR"

	if [ $HIGHLIGHT = NO ]
	then
		echo $NAVBAR_NAME > $TMP_DIR/list_highlights
	fi

	for H in `cat $TMP_DIR/list_highlights | grep -v "^score"`
	do
		scp -o BatchMode=yes $TMP_DIR/$H ${DFO_WEB_SERVER}:${QC_DIR}/
		ssh -o BatchMode=yes $DFO_WEB_SERVER "chmod a+rx,u+w ${QC_DIR}/$H"
		echo "$H transferred to $DFO_WEB_SERVER and published."
	done

	if [ $TYPE = HEALTH ] 
	then
		echo "
3. Scores: This navbar set is of type HEALTH and supports HC scoring. This is normally 
managed automatically. You can however erase existing scores for later refresh by trendPlotter.

Erase (transfer empty score files to the web server)?
(Y:  all scores erased, also required for the first time. 
 N:  scores preserved. DEFAULT!)"
		read TRANSFER_YN

		if [ Q$TRANSFER_YN = QY ]
		then
			for H in `cat $TMP_DIR/list_highlights | grep score`
			do
				scp -o BatchMode=yes $TMP_DIR/$H ${DFO_WEB_SERVER}:${QC_DIR}/
				ssh -o BatchMode=yes $DFO_WEB_SERVER "chmod a+rx,u+w ${QC_DIR}/$H"
				echo "$H transferred to $DFO_WEB_SERVER and published."
			done
		else
			echo "No empty score files transferred."
		fi
	fi
else
	echo "... no transfer to web servers done. "
	echo "Check out result under $TMP_DIR/$NAVBAR_NAME ."
fi

echo ""
echo "... done."
exit
