#!/bin/bash
## Generate html file to list figures with table
## Also generate descriptions for upper index.html
## column: numbers of figures per row
##         or text or newrow
##    ex. 'c1' 'c2' 'c3' tr    <- text row, tr means end of row
##         3                   <- 3 figures
##         3
##         'title2' tr         <- the row with text only
##         2 3                 <- 2 and 3 figures at this and next row
## figs: the filenames (without suffix) show in table, by order
##       will be convert from ps to png
##       also trim edge and make thumbnails


#;DIAG_NORCPM; FIGFILES: 
    # figure files name "without suffix"
    # should be ps or png file
    # with suffix: make thumbnail only
#;DIAG_NORCPM; HTMLFILENAME: index.html
#;DIAG_NORCPM; TITLE: TITLE here
#;DIAG_NORCPM; COMMENT: COMMENT here
#;DIAG_NORCPM; COLUMN: 1
#;DIAG_NORCPM; THUMBNAIL: 
#;DIAG_NORCPM; ENTRYHTMLFN: entry.html
    ## this file will be read by diag.py, do not modified it.
#;DIAG_NORCPM; ENTRYHTMLCONTAIN: 
    ## this will be the contain of entry.html if not empyt


# parameters
htmlfn="index.html"
comment="norcpm1_TroPac_ana and ERA5 T2m
"
title="NorCPM and ERA5 T2m"
figs="TREFHT_ens_corr_ANN TREFHT_ens_corr_DJF TREFHT_ens_corr_MAM TREFHT_ens_corr_JJA TREFHT_ens_corr_SON
TREFHT_ens_trend_ANN TREFHT_ens_trend_DJF
TREFHT_ens_corr_WNP_ANN TREFHT_ens_corr_WNP_DJF TREFHT_ens_corr_WNP_MAM TREFHT_ens_corr_WNP_JJA TREFHT_ens_corr_WNP_SON"
column=(Annual tr 1 DJF MAM JJA SON tr 4  1 2 4
)
    ## number of figures in a row
    ## should be a array
ncolarr=${#column[*]}
entryfn='entry.html'
entryhtml=''

thumb="TREFHT_ens_corr_ANN_thumb.png"
#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'


nl=$'\n'  #newline
# 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=$(( $icol + 1))
    fi
done

col=${column[$icol]}

## show figures with table
for i in ${figs}; do
    ii=$i
    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"
        convert ${i}.png -fuzz 1% -trim +repage tmp-${i}-${pid}.png
        mv tmp-${i}-${pid}.png ${i}.png
    fi
    if [ -f "${i}.png" ] ; then ## trim white edge and make thumbnail
        ## thumbnail
        test "${i}.png" -nt "${i}_thumb.png" && convert -thumbnail 300 "${i}.png" "${i}_thumb.png"
        ii="${i}.png"
    fi
    if [ -f "${i}" ] ; then ## other files: make thumbnail
        #convert ${i}.png -fuzz 1% -trim +repage tmp-${i}-${pid}.png 
        #mv tmp-${i}-${pid}.png ${i}.png
        ## thumbnail
        test "${i}.png" -nt "${i}_thumb.png" && convert -thumbnail 300 "${i}" "${i%.*}_thumb.png"
        ii="${i}"
    fi
    test -f "$thumb" || thumb="${i%.*}_thumb.png"

    ## fig entry
    text+="<th><a href='${ii}'><img src='${i%.*}_thumb.png'></a></th>"$'\n'
    if [ $row -ge $col ];then
        text+=$'</tr>\n'
        text+=$'<tr>\n'
        icol=$(( $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=$(( $icol + 1))
            else
                text+="<th><b>${dst}</b></th>"$'\n'
                icol=$(( $icol + 1))
            fi
        done
        if [ $icol -ge $ncolarr ];then
            icol=$(( $ncolarr - 1))
        fi
        col=${dst}
        row=1
    else
        row=$(( $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}"


## The entry snipp html for parent index.html

nowdir=$(basename $(pwd -P))
if [ -z "$entryhtml" ]; then
    entryhtml="${entryhtml}    <span style='display:inline-block'>${nl}"
    entryhtml="${entryhtml}    <a href='${nowdir}/index.html'>${nl}"
    entryhtml="${entryhtml}    <h4>${title}</h4>${nl}"
    entryhtml="${entryhtml}    <img ALIGN='left' src='${nowdir}/${thumb}'>${nl}"
    entryhtml="${entryhtml}    </a>${nl}"
    entryhtml="${entryhtml}    <pre>${comment}</pre>${nl}"
    entryhtml="${entryhtml}    </ br>${nl}"
    entryhtml="${entryhtml}    </span>${nl}"
    entryhtml="${entryhtml}    </ br>${nl}"
    entryhtml="${entryhtml}    <hr>${nl}"
fi
echo "${entryhtml}" > "${entryfn}"

