#!/usr/bin/env bash

echo -e '20 text/gemini\r\n'
echo
if  [[ "$QUERY_STRING" == "" ]]; then
    echo '# Server Stats'
    echo
    echo 'As far as I can tell, this is the first Gemini server stats page!'
    echo
    echo 'These stats apply from 2020-11-23 onwards.'
    echo
    echo '```server stats'
    echo "Total visits, to all pages:     $(wc -l /var/log/molly/access.log | cut -d' ' -f1)"
    echo "Unique visitors:                $(awk -F'\t' '{ print $2 }' /var/log/molly/access.log | sort | uniq | wc -l)" 
    echo "Gemini server last started at:  $(systemctl show molly-brown.service -p ExecMainStartTimestamp | cut -b 24-)"
    echo "Overall server:                 $(uptime -p)"
    echo '```'
    echo
    echo "=> ../one-liners.gmi Visit my one-liners page to see how these are generated."
    echo
    echo 'Note that these stats include robots or crawlers.'
else
    echo "# Stats for string \"$QUERY_STRING\""
    echo
    echo 'These stats apply from 2020-11-23 onwards.'
    echo
    echo '```'
    echo "Total visits:     $(grep $QUERY_STRING /var/log/molly/access.log | wc -l)"
    echo "Unique visitors:  $(grep $QUERY_STRING /var/log/molly/access.log | awk -F'\t' '{ print $2 }' | sort | uniq | wc -l)" 
    echo '```'
    echo
    echo 'Note that these stats include robots or crawlers.'
fi
