#!/bin/bash

#set -v

login=`whoami`

comp=$1
bias=$2
season=$3
proj=$4
ref=$5
shift ; shift ; shift ; shift ; shift

sims="$*"
nsims=$#
echo REF $ref
echo SIM $sims

login=`whoami`

hostname=`hostname`
if [ ${hostname:0:5} = cicla ] ; then
DODSDIR=/thredds/ipsl
HTML=http://vesg.ipsl.upmc.fr/thredds/fileServer/IPSLFS/$login/lmdz/MultiSimu/$comp
fi
if [ ${hostname:0:5} = camel ] ; then
DODSDIR=/thredds/ipsl
fi
if [ ${hostname:0:5} = irene ] ; then
DODSDIR=
fi
if [ ${hostname:0:5} = jean- ] ; then
DODSDIR=
fi

ATLAS_DIR=$DODSDIR/$login/lmdz/atlas
COMP_D=$DODSDIR/$login/lmdz/MultiSimu/$comp
ZON_DIR=$COMP_D/ZON

#############################################################################
refb=`echo $ref | sed -e 's/_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]//'`
years=`echo $ref | sed -e 's/'$refb'//'`
echo YEARS $years
# Le nom semble avoir changé ... sref=$COMP_D/ATLAS/index_${proj}_${season}_${ref}.html
sref=$COMP_D/ATLAS/index_${proj}_${season}_${ref}.html

sed -i -e "s:thredds\/ipsl:thredds\/fileServer\/IPSLFS:g" ${sref}
#############################################################################

compf=$COMP_D/$bias$proj$season.html

echo $compf
echo $ref
emptyelt='<td> - </td>'


entete=1

rm -f $compf ; touch $compf

echo PROJ $proj
echo SEASON $season
if [ -f $sref ] ; then
for i in `sed -n -e '/TH ALIGN=LEFT.*.li/=' $sref` ; do
#for i in `sed -n -e '/<TH ALIGN=LEFT> <li>/=' $sref` ; do
   var=`sed -n -e ${i}p $sref | cut -d\( -f2 | cut -d\) -f1`
   echo $i $var
   if [ $entete = 1 ] ; then
      lpdf=`sed -n -e /PDF/= $sref`
      (( lpdfm1 = $lpdf - 1 )) ; (( lpdfp1 = $lpdf + 1 )) ; 
      echo $lpdfm1
      entete=0
      #head -$lpdfm1 $sref | sed -e '/border-color:transparent/d' -e '/font-size:80/d' >> $compf


      ##############################################################
      # MENU DEROULANT 
      ##############################################################:$
      cat $COMP_D/entete.html >> $compf
 
      #####################################################################
      # Fichiers pour l'enetete
      #####################################################################
      echo '<hr/>' >> $compf
      echo '<h1> Multi atlas, '$season', simulation de référence : '$ref' ('$bias') </h1>' >> $compf
     #####################################################################

       sed -n -e $lpdfp1,${i}p $sref >> $compf 

      cat <<eod>> $compf
<TH ALIGN=LEFT>OBSERVATIONS</TH>
<TH ALIGN=LEFT>$ref</TH>
eod
      if [ "$bias" = "BIAS" ] ; then echo '<TH ALIGN=LEFT>'$ref' (BIAS)</TH>' >> $compf ; fi
      for sim in $sims ; do
         echo '<TH ALIGN=LEFT>' $sim '</TH>' >> $compf
      done
      echo ' <TR>' >> $compf
   else # entete=1
      (( iobs   = $i + 4 ))
      (( iref   = $i + 3 ))
      (( ibiais = $i + 5 ))
      # Repetition des noms des variables
      (( ntext = $nsims / 3 + 1 ))
      iii=1
      while [ $iii -le $ntext ] ; do
         sed -n -e ${i}p $sref | sed -e 's:^:<th></th>:' -e 's/LEFT/LEFT colspan="2"/' >> $compf
         (( iii = $iii + 1 ))
      done
      echo ' <TR>' >> $compf
      # Inclusion des moyennes zonales
      if [ -f $ZON_DIR/${var}zon.png ] ; then 
          thredds=/thredds/fileServer/IPSLFS/fabric/lmdz/MultiSimu/$comp/ZON/${var}zon.png
          echo '<TD ALIGN=RIGHT><A HREF="'$thredds'"><IMG HEIGHT=200 WIDTH=200 SRC="'$thredds'"></a></TD>' >> $compf
      else
          echo $emptyelt >> $compf
      fi

      sed -n -e ${iobs}p $sref >> $compf
      sed -n -e ${iref}p $sref >> $compf
      if [ "$bias" = "BIAS" ] ; then sed -n -e ${ibiais}p $sref >> $compf ; fi
      for sim in $sims ; do
         #########################################################
         simb=`echo $sim | sed -e 's/_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]//'`
         years=`echo $sim | sed -e 's/'$simb'//'`
         index=$COMP_D/ATLAS/index_${proj}_${season}_${sim}.html
#        if [ -f $index ] ; then
         is=`sed -n -e '/<TH ALIGN=LEFT> <li.*.('$var')/=' $index | head -1`
         echo $sim $var $is
         #########################################################
         if [ "$is" = "" ] ; then
            echo '<td> Missing </td>' >> $compf
         else
            if [ "$bias" = "BIAS" ] ; then (( isbiais = $is + 5 )) ; else (( isbiais = $is + 3 )) ; fi
            sed -n -e ${isbiais}p $index >> $compf
         fi
#        fi
      done
      echo ' <TR>' >> $compf
   fi
done
fi

#echo VOTRE MULTIATLAS EST DISPONIBLE SUR $HTML/ACCUEIl.html
