#!/bin/bash

VERSION='2.4.21'
if [[ "$1" == "-v" ]] || [[ "$1" == "--version" ]] || [[ "$1" == "version" ]]; then
    echo "$( basename "$0" ) - $VERSION"
    exit 0
fi

[[ ! -d /var/lib/backuppc/pc ]] && echo "BackupPC directory not found (/var/lib/backuppc/pc)." && exit 1
if ! cd "/var/lib/backuppc/pc"; then
    echo "Failed to enter in BackupPC directory (/var/lib/backuppc/pc)."
    exit 1
fi

OUT="<table align=center>
<thead>
  <tr>
    <th rowspan=2 align=left>Host</th>
    <th colspan=2>Nb backups</th>
  <tr>
    <th>Before V4</th>
    <th>After V4</th>
  </tr>
</thead>
<tbody>"
t_v3=0
t_v4=0
for h in *; do
    if [[ ! -d "$h" ]] || [[ ! -e "$h/backups" ]]; then
        continue
    fi
    vers=$( cut -f24 < "$h/backups" )
    v3=$( grep -Evc '^4' <<< "$vers" )
    ((t_v3=t_v3+v3))
    [[ "$v3" -ne 0 ]] && v3="<strong>$v3</strong>"
    v4=$( grep -Ec '^4' <<< "$vers" )
    ((t_v4=t_v4+v4))
    OUT="$OUT<tr><td align=left>$h</td><td>$v3</td><td>$v4</td></tr>"
done
[[ $t_v3 -gt 0 ]] && t_v3="<strong>$t_v3</strong>"
OUT="$OUT
</tbody>
<tfooter>
  <tr>
    <th align=right>Total :</th>
    <th>$t_v3</th>
    <th>$t_v4</th>
  </tr>
</tfooter>
</table>"
lynx -stdin <<< "$OUT"
# vim:set sw=4 ts=4 sts=4 ft=bash expandtab:
