#!/bin/bash

#;DIAG_NORCPM; FIGFILES: 
    # figure files name "without suffix"
    # should be ps or png file
#;DIAG_NORCPM; HTMLFILENAME: index.html
#;DIAG_NORCPM; TITLE: TITLE here
#;DIAG_NORCPM; COMMENT: COMMENT here
#;DIAG_NORCPM; COLUMN: 1

# parameters
htmlfn="index.html"
comment="BLOM ensemble SST and HadISST time series
Monthly data, anomaly correlation and Rank histogram.
"
title="TITLE here"
figs="sst_ano_ens_cor box_NINO34   box_NPac   box_NAtl NINO34_monts  NPac_monts  NAtl_monts rhisto_NINO34_t01 rhisto_NINO34_t02 rhisto_NINO34_t03 rhisto_NINO34_t04 rhisto_NINO34_t05 rhisto_NINO34_t06 rhisto_NINO34_t07 rhisto_NINO34_t08 rhisto_NINO34_t09 rhisto_NINO34_t10 rhisto_NINO34_t11 rhisto_NINO34_t12 rhisto_NINO34_t13 rhisto_NINO34_t14 rhisto_NINO34_t15 rhisto_NINO34_t16 rhisto_NINO34_t17 rhisto_NINO34_t18 rhisto_NINO34_t19 rhisto_NINO34_t20 rhisto_NINO34_t21 rhisto_NINO34_t22 rhisto_NINO34_t23 rhisto_NINO34_t24 rhisto_NINO34_t25 rhisto_NINO34_t26 rhisto_NINO34_t27 rhisto_NINO34_t28 rhisto_NINO34_t29 rhisto_NINO34_t30 rhisto_NINO34_t31 rhisto_NINO34_t32 rhisto_NINO34_t33 rhisto_NINO34_t34 rhisto_NINO34_t35 rhisto_NINO34_t36
rhisto_NPac_t01 rhisto_NPac_t02 rhisto_NPac_t03 rhisto_NPac_t04 rhisto_NPac_t05 rhisto_NPac_t06 rhisto_NPac_t07 rhisto_NPac_t08 rhisto_NPac_t09 rhisto_NPac_t10 rhisto_NPac_t11 rhisto_NPac_t12 rhisto_NPac_t13 rhisto_NPac_t14 rhisto_NPac_t15 rhisto_NPac_t16 rhisto_NPac_t17 rhisto_NPac_t18 rhisto_NPac_t19 rhisto_NPac_t20 rhisto_NPac_t21 rhisto_NPac_t22 rhisto_NPac_t23 rhisto_NPac_t24 rhisto_NPac_t25 rhisto_NPac_t26 rhisto_NPac_t27 rhisto_NPac_t28 rhisto_NPac_t29 rhisto_NPac_t30 rhisto_NPac_t31 rhisto_NPac_t32 rhisto_NPac_t33 rhisto_NPac_t34 rhisto_NPac_t35 rhisto_NPac_t36
rhisto_NAtl_t01 rhisto_NAtl_t02 rhisto_NAtl_t03 rhisto_NAtl_t04 rhisto_NAtl_t05 rhisto_NAtl_t06 rhisto_NAtl_t07 rhisto_NAtl_t08 rhisto_NAtl_t09 rhisto_NAtl_t10 rhisto_NAtl_t11 rhisto_NAtl_t12 rhisto_NAtl_t13 rhisto_NAtl_t14 rhisto_NAtl_t15 rhisto_NAtl_t16 rhisto_NAtl_t17 rhisto_NAtl_t18 rhisto_NAtl_t19 rhisto_NAtl_t20 rhisto_NAtl_t21 rhisto_NAtl_t22 rhisto_NAtl_t23 rhisto_NAtl_t24 rhisto_NAtl_t25 rhisto_NAtl_t26 rhisto_NAtl_t27 rhisto_NAtl_t28 rhisto_NAtl_t29 rhisto_NAtl_t30 rhisto_NAtl_t31 rhisto_NAtl_t32 rhisto_NAtl_t33 rhisto_NAtl_t34 rhisto_NAtl_t35 rhisto_NAtl_t36"
column=(1 NINO34        NPac       NAtl    tr 3   3 RankedHisto_by_time tr NINO34  tr 12 12 12 NPac    tr 12 12 12 NAtl    tr 12 12 12
)
    ## number of figures in a row
    ## can be a array
ncolarr=${#column[*]}

#export MAGICK_THREAD_LIMIT=1   ## avoid "libgomp: Thread creation failed"

# html header
text=$'\n'
text+=$'<!DOCTYPE html>\n'
text+=$'<html>\n'
text+=$'<body>\n'

# put comment on top
text+=$'<div id=comment>\n'
text+=$'<p>\n'
text+=$'<pre>\n'
text+="$comment"
text+=$'</pre>\n'
#text+=$'</p>\n'
text+=$'</div>\n'


# convert ps to png and output entry to html
pid=$$
row=1
icol=0


text+="<table>"$'\n'
text+="<tr>"$'\n'

# if column start with non integer
while [ ! -z "${column[$icol]}" ] ; do
    dst="${column[$icol]}"
    if [[ "${dst}" =~ ^[0-9]+$ ]] ; then
        break
    else
        if [ "${dst}" = 'tr' ]; then
            dst='</tr><tr>'
        fi
        if [ "${dst}" = '</tr><tr>' ]; then
            text+="${dst}"$'\n'
        else
            text+="<th>${dst}</th>"$'\n'
        fi
        icol=$(expr $icol + 1)
    fi
done

col=${column[$icol]}

## show figures with table
for i in ${figs}; do
    if [ -f "${i}.ps" ] ; then ## ps2png
        convert -density 300 "${i}.ps" ${i}.png
        if [  $? != 0 ];then
            MAGICK_THREAD_LIMIT=1 convert -density 300 "${i}.ps" ${i}.png
        fi
        gzip -f "${i}.ps"
    fi
    if [ -f "${i}.png" ] ; then ## trim white edge and make thumbnail
        convert ${i}.png -fuzz 1% -trim +repage tmp-${i}-${pid}.png 
        mv tmp-${i}-${pid}.png ${i}.png
        ## thumbnail
        convert -thumbnail 300 "${i}.png" "${i}_thumb.png"
    fi

    ## fig entry
    text+="<th><a href='${i}.png'><img src='${i}_thumb.png'></a></th>"$'\n'
    if [ $row -ge $col ];then
        text+=$'</tr>\n'
        text+=$'<tr>\n'
        icol=$(expr $icol + 1)
        while [ ! -z "${column[$icol]}" ] ; do
            dst="${column[$icol]}"
            if [ "${dst}" = 'tr' ]; then
                dst='</tr><tr>'
            fi
            if [[ "${dst}" =~ ^[0-9]+$ ]] ; then
                break
            elif [ "${dst}" = '</tr><tr>' ]; then
                text+="${dst}"$'\n'
                icol=$(expr $icol + 1)
            else
                text+="<th><b>${dst}</b></th>"$'\n'
                icol=$(expr $icol + 1)
            fi
        done
        if [ $icol -ge $ncolarr ];then
            icol=$(expr $ncolarr - 1)
        fi
        col=${dst}
        row=1
    else
        row=$(expr $row + 1)
    fi
done
text+="</tr>"$'\n'
text+="</table>"$'\n'

# html footer
text+=$'</body>\n'
text+=$'</html> \n'

# output to html file
echo "${text}" >  "${htmlfn}"

