#!/bin/sh
# Generate suitable data-driven content insert for
# note-on-survey-UK-central-heating-on-off-dates.html
# from data/UKCHOnOff/UKCHOnOff-poll-data.csv source.
# The generated HTML5 may omit optional (eg closing) tags.
#
# Usage:
#     $0 [-2|-summary]
#
#         -2
#         Generate output from second dataset,
#
#         -summary
#         Generate summary output from both datasets.

# Expects img/css/ta-XXX.css or later for tar (text align right).
# Expects img/css/dopt-20180522.min.css or later for dopt/doptsml.
# Expects img/css/bargr-20170822.css or later for bar-graph support.

POLLDATA1=data/UKCHOnOff/UKCHOnOff-poll-data.csv
POLLDATA2=data/UKCHOnOff2/UKCHOnOff2-poll-data.csv
DATASET=1
POLLDATA="$POLLDATA1"
if [ "-2" = "$1" ]; then
    DATASET=2
    POLLDATA="$POLLDATA2"
    shift
fi
if [ ! -s $POLLDATA ]; then
    echo "ERROR: missing poll data file $POLLDATA" 1>&2
    exit 1
fi

SUMMARY=false
if [ "-summary" = "$1" ]; then
    SUMMARY=true
    shift
fi


# Input format:
#poll start date, duration (d), social media link, total count, on count, off count, what's off count, other count
#2017-04-03,,"https://twitter.com/EarthOrgUK/status/848829891579695105",12,2,9,1,

echo "<h3 id=\"Summary\">Summary of polls</h3>"

# First a table of poll results.
# This was previously done manually.
cat << EOT
<div>
<table class=tb1>
<caption>UK social media users central heating state snapshots.</caption>
<thead>
<tr><th>Start / Link<th>On %<th>Off %<th>What's Off? %<th class=dopt>Other %<th class=dopt>Total Votes<th class=dopt>Duration (d)
</thead>
<tbody>
EOT
# Generate table row by row.
# Note micro-optimisation to drop unsed styles on empty fields.
if [ "true" = "$SUMMARY" ]; then
    cat "$POLLDATA1" "$POLLDATA2"
else
    cat "$POLLDATA"
fi |
awk -F, '$1 ~ /^20/ {
    printf("<tr>");
    printf("<td>");
        printf("<a href=%s><time>%s</time></a>", $3, $1);
    printf("<td class=tar>");
        printf("%.1f", ($5 * 100) / $4);
    printf("<td class=tar>");
        printf("%.1f", ($6 * 100) / $4);
    if("" != $7) {
         printf("<td class=tar>%.1f", ($7 * 100) / $4); } else { printf("<td>"); }
    if("" != $8) {
        printf("<td class=\"dopt tar\">%.1f", ($8 * 100) / $4); } else { printf("<td class=dopt>"); }
    printf("<td class=\"dopt tar\">");
        printf("%d", $4);
    if("" != $2) {
        printf("<td class=\"dopt tar\">%d", $2); } else { printf("<td class=dopt>"); }
    printf("\n");
    }'
cat << EOT
</tbody>
</table>
</div>
EOT


# Summary by month.
cat << EOT
<div>
<p>
A by-month view of definitely-off UK central heating...
</p>
<table class=tb1 style=border-collapse:collapse>
<caption>UK social media users central heating percent off by month.</caption>
<tbody>
EOT
if [ "true" = "$SUMMARY" ]; then
    cat "$POLLDATA1" "$POLLDATA2"
else
    cat "$POLLDATA"
fi |
awk -F, '$1 ~ /^20/ {
    split($1, d, "-");
    month = 0 + d[2];
    # Count of polls each month.
    pollcount[month]++;
    # Count of votes each month.
    votecount[month] += $4;
    # Count of off votes each month.
    offcount[month] += $6;
    }
    END {
    printf("<tr class=barGr><th class=dopt rowspan=2>%% Off");
    for(i = 1; i <= 12; ++i) {
        oc = offcount[i];
        vc = votecount[i];
        printf("<td style=width:20px>");
        if("" != oc) {
            percentoff = (oc*100)/vc;
            printf("<ul><li style=background-color:green;width:19px;height:%.0fpx>", percentoff);
            if(percentoff > 10) { printf("%.0f", percentoff); }
            printf("</li></ul>");
            }
        }
    printf("<tr class=dopt>"); # <th>%% Off");
    for(i = 1; i <= 12; ++i) {
        oc = offcount[i];
        vc = votecount[i];
        if("" != oc) { percentoff = (oc*100)/vc; printf("<td class=tar>%.0f", percentoff); } else { printf("<td>"); }
        }
    printf("<tr class=dopt><th class=dopt>Votes");
    for(i = 1; i <= 12; ++i) {
        vc = votecount[i];
        if("" != vc) { printf("<td class=tar>%d", vc); } else { printf("<td>"); }
        }
    printf("<tr class=dopt><th class=dopt>Polls");
    for(i = 1; i <= 12; ++i) {
        pc = pollcount[i];
        if("" != pc) { printf("<td class=tar>%d", pc); } else { printf("<td>"); }
        }
    }'
cat << EOT
</tbody>
<tfoot>
<tr><th class=dopt rowspan=2>Month<th>1<th>2<th>3<th>4<th>5<th>6<th>7<th>8<th>9<th>10<th>11<th>12
<tr class=doptsml><th>Jan<th>Feb<th>Mar<th>Apr<th>May<th>Jun<th>Jul<th>Aug<th>Sep<th>Oct<th>Nov<th>Dec
</tfoot>
</table>
</div>
EOT


exit 0
