#!/usr/bin/env bash
set -euo pipefail

{
  for f in *; do
    [ -f "$f" ] || continue
    case "$f" in
      *.sh|*.csv) continue ;;
    esac

    case "$f" in
      *.gz) gzip -dc -- "$f" 2>/dev/null ;;
      *)    cat -- "$f" ;;
    esac
  done
} | \
awk -F'"' '
BEGIN { IGNORECASE = 1 }
{
  if (index($0, "socware") == 0 || index($0, ".zip") == 0) next
  if (index($0, " 404 ") > 0) next

  ua = $6
  if (ua !~ /Mozilla\//) next
  if (ua !~ /(Chrome\/|Safari\/|Firefox\/|Edg\/)/) next

  left = $1

  n = split(left, a, " ")
  if (n < 1) next
  ip = a[1]

  if (match(left, /\[([0-9]{2})\/[A-Za-z]{3}\/([0-9]{4}):/, m)) {
    year = m[2]
  } else next

  hits[year]++

  key = year "\t" ip
  if (!(key in seen)) {
    seen[key] = 1
    uniq[year]++
  }
}
END {
  print "year,total_hits,unique_ips"

  n = 0
  for (y in hits) years[++n] = y
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      if ((years[i] + 0) > (years[j] + 0)) {
        tmp = years[i]
        years[i] = years[j]
        years[j] = tmp
      }
    }
  }

  for (i = 1; i <= n; i++) {
    y = years[i]
    printf "%s,%d,%d\n", y, hits[y], uniq[y] + 0
  }
}
'
