#!/bin/bash
exec 1> /tmp/EMBR_MFR_html.log 2>&1
set -x # echo on

. `dirname $0`'/EMBR_MFR_config.sh'

echo "START: "`basename $0`

#--------------------------------------------------------------------------------
# from list of files, define first and last days
#--------------------------------------------------------------------------------

# list of day
dlist=${figsout}/List_Days.txt

# get first and last days
first_day=`head -n 1 $dlist`
first_day=${first_day:0:8}
echo "First day= $first_day"
yi=${first_day:0:4}
mi=${first_day:4:2}; mi=${mi#0}

last_day=`tail -n 1 $dlist`
last_day=${last_day:0:8}
echo "Last day = $last_day" 
yf=${last_day:0:4}
mf=${last_day:4:2}; mf=${mf#0}

#--------------------------------------------------------------------------------
# Copy figures from FTP to APACHE server
#--------------------------------------------------------------------------------
dout=${htmlout}/sites/embrapa/MFR
/bin/rm -f $dout/*.png
/bin/cp ${figsout}/*.png $dout/

#--------------------------------------------------------------------------------
# Create one HTML for the graphs of each year
#--------------------------------------------------------------------------------
yy=$yi
while (( $yy <= $yf )) ; do
    out=$dout/mfr_$yy.html
    /bin/rm -f $out
    
    # craete HTML for each month
    echo "<html>" >> $out
    echo "<H1><a href=\"index.html\">EMBR_MFR</a>: $yy </H1>"  >> $out
    
    # filter year
    grep "^$yy" $dlist | awk '{ if ($2>0) print "<img src=\"Day"$1".png\">"; else print "Day"$1".png missing<p>"; }' >> $out
    
    echo "</html>"  >> $out

    yy=$((yy+1))
done

#--------------------------------------------------------------------------------
# Create one HTML for the graphs of each month
#--------------------------------------------------------------------------------
yy=$yi
while (( $yy <= $yf )) ; do
    for mon in 01 02 03 04 05 06 07 08 09 10 11 12 ; do

	out=$dout/mfr_$yy$mon.html
	/bin/rm -f $out

	# craete HTML for each month
	echo "<html>" >> $out
	echo "<H1><a href=\"index.html\">EMBR_MFR</a>: <a href=\"mfr_$yy.html\">$yy</a> / $mon</H1>"  >> $out

	# filter year and month
	grep "^$yy$mon" $dlist | awk '{ if ($2>0) print "<img src=\"Day"$1".png\">"; else print "Day"$1".png missing<p>"; }' >> $out
	echo "</html>"  >> $out
    done

    yy=$((yy+1))
done

#--------------------------------------------------------------------------------
# One HTML for SUMMARY TABLE
#--------------------------------------------------------------------------------
echo "Summary table"

out=$dout/index.html
/bin/rm -f $out

# open HTML
rm -f /tmp/EMBR_MFR_tmp
echo "<html>" >> $out
echo "<H1><a href=\"index.html\">EMBR_MFR</a></H1>"  >> $out

# first create a summary table
echo "<H3>Summary table</H3>" >> $out
echo "<table border=1>"  >> $out
echo "<tr><td></td><td>Jan</td><td>Feb</td><td>Mar</td><td>Apr</td><td>May</td><td>Jun</td> 
<td>Jul</td><td>Aug</td><td>Sep</td><td>Oct</td><td>Nov</td><td>Dec</td></tr>"  >> $out
yy=$yi
while (( $yy <= $yf )) ; do
    
    echo "<tr><td><a href=\"mfr_$yy.html\">$yy</a></td>"  >> $out
    for mon in 01 02 03 04 05 06 07 08 09 10 11 12 ; do
	# sum the 1's in the list for this year and this month
	tmp=`grep "^$yy$mon" $dlist | awk 'BEGIN {x=0;} {x=x+$2;} END {print x;}'`

	# calculate colors
	if (( $tmp < 10 )) ; then
	    color="#FF0000" 
	else
	    if (( $tmp >= 25 )) ; then
		color="#00FF5A"
	    else
		color="#F6FF00"
	    fi
	fi
	mm=${mon#0} # remove leading zero
	if (( ($((yy*100+mm)) < $((yi*100+mi))) || ($((yy*100+mm)) > $((yf*100+mf))) )) ; then
	    color="#999999"
	fi
	echo "<td bgcolor=\"$color\"><a href=\"mfr_$yy$mon.html\">$tmp</a></td>" >> $out

    done
    echo "</tr>"  >> $out

    yy=$((yy+1))
done 
echo "</table>" >> $out

# second add the figure of the last data available
good=`cat $dlist | awk '{if ($2>0) print $0;}' | tail -n 1`
arr=($good)
julnow=`date +%s`
dt=$((julnow-arr[2]))
dt=`echo "($dt/86400.)" | bc -l`
dt=${dt:0:4}

echo "<H3>Last data available ($dt days ago)</H3>" >> $out
echo "<img src=\"Day${arr[0]}.png\"><p>" >> $out

# third create a table of the last 30 days 
julnow=`date +%s`
jul30=`date --date="30 days ago" +%s`
echo "<H3>Files from last 30 days</H3>" >> $out
while read line ; do
    arr=($line)
    if (( ${arr[2]} > $jul30 && ${arr[2]} < $julnow )); then
	if (( ${arr[1]} > 0 )) ; then
	    echo "<a href=\"Day${arr[0]}.png\">Day${arr[0]}.png</a><p>" >> $out
	else
	    echo "Day${arr[0]}.png is missing!<p>" >> $out
	fi
    fi
done < $dlist

# close HTML
echo "</html>"  >> $out


#
echo "END: $0" 
#