#!/bin/sh -e
# This script is run by cron once a day.

exec > "$HOME/public_html/activity/index.html"

printf "<!DOCTYPE html><html><meta charset=\"utf-8\"/><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"/>\n"
printf "<title>Active users on ctrl-c.club</title>\n"
printf "<p>Top 20 users sorted by total size of HTML posts\n<pre>"
find /home/*/public_html -type f -name \*.html -ls 2>/dev/null |
    awk '{sizes[$5] += $7; counts[$5]++}
        END {
            cmd = "sort -rn | head -20";

            for (i in sizes)
                printf "%12d\t<a href=\"/~%s\">%s</a>\n", sizes[i], i, i | cmd;
            close(cmd);

            printf "</pre></p>\n<p>Top 20 users sorted by total number of HTML pages</p>\n<pre>";
            for (i in counts)
                printf "%12d\t<a href=\"/~%s\">%s</a>\n", counts[i], i, i | cmd;
        }'
printf "</pre>\n<hr/><footer>This page is generated by <a href="activity.sh.txt">this script</a>.</footer>\n</html>\n"
