#!/usr/bin/env bash

coverdata_dir=@abs_builddir@/logs
coverdata_file=${coverdata_dir}/@PACKAGE_NAME@.coverdata
coverdata_app=@PACKAGE_NAME@
analyze_coverdata=@abs_builddir@/analyze_coverdata.escript

color_0="ff2200"
color_5="ff3800"
color_10="ff4e00"
color_15="ff6400"
color_20="ff7a00"
color_25="ff9000"
color_30="ffa500"
color_35="ffbb00"
color_40="ffd100"
color_45="ffe700"
color_50="fdfc00"
color_55="ecff00"
color_60="d6ff00"
color_65="c0ff00"
color_70="aaff00"
color_75="94ff00"
color_80="7eff00"
color_85="69ff00"
color_90="53ff00"
color_95="3dff00"
color_100="27ff00"

output_analyze_coverdata () {
    pretty_name=$(echo $coverdata_app | tr "_" " ")

    # Generate covered modules data. We ignore stdout because cover
    # writes some useless logs.
    $analyze_coverdata $coverdata_file > /dev/null
    covered_mods=${coverdata_app}_covered_mods

    # Prepare index block.
    index=${coverdata_dir}/cover.html
    timestamp=$(date +'%Y-%m-%d at %H:%M:%S')
    cat << EOF > "$index"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testsuite coverage - ${pretty_name}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
body {
  color: #000;
  background-color: #fff;
  font-family: sans-serif;
}
h1 {
  margin: 0px;
}
#timestamp {
  margin-top: 0px;
  margin-bottom: 3em;
  color: #777;
  font-style: italic;
  border-bottom: solid 1px #000;
}
#tests_col {
  padding-left: 30px;
}
.testsuite {
  float: left;
  margin-right: 30px;
  margin-bottom: 30px;
  padding-left: 25px;
}
.testsuite table {
  border: 1px solid #999;
  border-collapse: collapse;
  background-color: #f9f9f9;
}
.testsuite tr:hover {
  background-color: #ebebeb;
}
.testsuite th, .testsuite td {
  padding: 4px;
}
.testsuite th {
  border-bottom: 1px solid #999;
  border-top: 1px solid #999;
  background-color: #ebebeb;
}
</style>
</head>
<body>
<h1>Testsuite coverage - <span style="text-transform: capitalize;">${pretty_name}</span></h1>
<p id="timestamp">Date: ${timestamp}</p>

<div class="testsuite" id="${coverdata_app}">
<h2><span style="text-transform: capitalize;">${pretty_name}</span> modules:</h2>
<table>
<tr>
<th style="width: 200px; text-align: left;">Module</th>
<th style="width: 100px; text-align: right;">Coverage (%)</th>
<th style="width: 100px; text-align: right;">Coveraged (Lines)</th>
<th style="width: 100px; text-align: right;" colspan="2">Not coveraged (Lines)</th>
</tr>
EOF

    # For each module of this test:
    #     o  Write its source code with cover results.
    #     o  Update index block.
    total_covered=0
    total_not_covered=0
    for module in $(cat ${coverdata_dir}/${covered_mods} | sort); do
        in=${coverdata_dir}/${coverdata_app}_${module}.out
        cvd=${coverdata_dir}/${coverdata_app}_${module}.covered
        not_cvd=${coverdata_dir}/${coverdata_app}_${module}.not_covered
        pct=${coverdata_dir}/${coverdata_app}_${module}.percent
        out=${coverdata_dir}/${module}.COVER.html
        link=${module}.COVER.html

        covered=$(cat $cvd)
        not_covered=$(cat $not_cvd)
        percent=$(cat $pct)

        color=$(echo $percent | sed -e 's/\..*$//')
        if [ $((color % 5)) -gt 0 ]; then
            color=$((color + (5 - color % 5)))
        fi
        color=$(eval echo \${color_$color})

        headline=$(head -n 1 "$in")
        timestamp=${headline#*COVER }

        # Update source file to emphasize cover results.
        cat << EOF > "$out"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testsuite coverage / ${pretty_name} / ${module}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
body {
  color: #000;
  background-color: #fff;
  font-family: sans-serif;
}
h1 {
  margin: 0px;
}
#percent {
  position: absolute;
  top: 10px;
  right: 10px;
  margin: 0px;
  font-size: 28px;
  font-weight: bold;
  padding-right: 8px;
  border-right: solid 5px #${color};
}
#timestamp {
  margin-top: 0px;
  madding-bottom: 0px;
  color: #777;
  font-style: italic;
  border-bottom: solid 1px #000;
}
#back {
  margin-top: 0px;
  margin-bottom: 3em;
  font-size: 10px;
  text-align: right;
}
.cvsid, .func {
  font-weight: bold;
}
.cvsid {
  color: #461b7e;
}
.cov {
  color: #007a00;
}
.notcov {
  color: #f00;
}
.comment {
  color: #777;
}
</style>
</head>
<body>
<h1>Testsuite coverage: <span style="text-transform: capitalize;">${pretty_name}</span> / <span style="color: #9fc550;">${module}</span></h1>
<p id="percent">${percent} %</p>
<p id="timestamp">Date: ${timestamp}</p>
<p id="back">[<a href="cover.html">index</a>]</p>
<pre>
EOF

        line_nb=$(wc -l "$in")
        tail -n $((${line_nb% *} - 4)) "$in" | sed \
         -e 's,<,\&lt;,g' -e 's,>,\&gt;,g' \
         -e 's,^\([[:space:]]*0\.\.|.*\),<span class="notcov">\1</span>,' \
         -e 's,^\([[:space:]]*[1-9][0-9]*\.\.|.*\),<span class="cov">\1</span>,' \
         -e 's,^\([[:space:]]*|[[:space:]][[:space:]]*%+ \$Id.*\),<span class="cvsid">\1</span>,' \
         -e 's,^\([[:space:]]*|[[:space:]][[:space:]]*%.*\),<span class="comment">\1</span>,' \
         -e 's,^\([[:space:]]*|[[:space:]]\{2\}[[:alpha:]][a-zA-Z0-9_]*\)\(.*\),<span class="func">\1</span>\2,' \
         >> "$out"

        cat << EOF >> "$out"
</pre>
</body>
EOF

        # Update index block.
        cat << EOF >> "$index"
<tr>
<td style="text-align: left;"><a href="${link}">${module}</a></td>
<td style="text-align: right;">${percent} %</td>
<td style="text-align: right;">${covered}</td>
<td style="text-align: right;">${not_covered}</td>
<td style="width: 5px; padding: 0px; background-color: #$color;"></td>
</tr>
EOF
        # Update total results
        total_covered=$((total_covered + covered))
        total_not_covered=$((total_not_covered + not_covered))

        # Remove file generated by $analyze_coverdata.
        rm -f $in $cvd $not_cvd $pct
    done

    total_percent=$(echo "scale=1; ($total_covered * 100 / ( $total_covered + $total_not_covered))" | bc)

    color=$(echo $total_percent | sed -e 's/\..*$//')
    if [ $((color % 5)) -gt 0 ]; then
        color=$((color + (5 - color % 5)))
    fi
    color=$(eval echo \${color_$color})
    # End index block.
    cat << EOF >> "$index"
<tr>
<th style="text-align: left;">Total</th>
<th style="text-align: right;">${total_percent} %</th>
<th style="text-align: right;">${total_covered}</th>
<th style="text-align: right;">${total_not_covered}</th>
<th style="width: 5px; padding: 0px; background-color: #$color;"></th>
</tr>
</table>
</div>

</body>
</html>
EOF

    rm -f $covered_mods
}

output_analyze_coverdata
