#!/bin/sh
# The operator's commands to the game server of the Docker setup in this folder:
# copy it next to compose.yml (/srv/nfsu2), chmod +x, then
#   ./nfsu2 who [name]                     the players on the switch, and who left in the last hour (a name: theirs,
#                                          or the one over their car)
#   ./nfsu2 kick <player> [message]        off playing with others, with that message (a reload brings them back)
#   ./nfsu2 ban <player> <for> [message]   off until the ban ends: <for> is 30m, 12h, 3d, 2w or perm
#   ./nfsu2 raceban <player> <for> [msg]   off races with others until it ends (Online, LAN, free roam's lobby races
#                                          and outruns); free roam and chat stay
#   ./nfsu2 rename <player> [message]      its name taken away from everyone: its page asks for another
#   ./nfsu2 unban <name | #ban>            lift a ban
#   ./nfsu2 change #ban <races|full|for>   make a ban a race ban or a full one, or last <for> from now on
#   ./nfsu2 bans                           the bans in force
#   ./nfsu2 history <player | #ban>        a player's bans and renames, in force and ended in the last year
#   ./nfsu2 names | unname <name>          the names taken away; give one back
#   ./nfsu2 days [n]                       how many came each day (UTC), the last n days and today so far
#   ./nfsu2 settings [n] [setting]         the settings players changed, the last n days (7) and today so far,
#                                          most changed first; a setting's name: only it, with every value
#   ./nfsu2 logs [days] > logs.gz          the server's log of the last days (1: today so far), gzipped ("What the log
#                                          keeps"); from your computer: ssh root@<vps> /srv/nfsu2/nfsu2 logs 2 > logs.gz
# <player> is an address (10.77.x.y) or a name, as `who` shows them, or a [cheat] line's handle (c17); the message
# needs no quotes.
cd "$(dirname "$0")" || exit 1
if [ "$1" = logs ]; then
  days=${2:-1}
  case $days in
    '' | *[!0-9]*) echo "logs [days]: a number of days, 1 for today so far" >&2; exit 2 ;;
  esac
  if [ -t 1 ]; then
    echo "logs [days] > logs.gz: it is gzipped, send it to a file" >&2
    exit 2
  fi
  # each day's file, the last $days of them: the days that ended as they are (gzipped), today's gzipped now
  exec docker compose exec -T server sh -c '
    cd /state/logs || exit 1
    for f in $(ls | grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}[.]log([.]gz)?$" | sort | tail -n "$1"); do
      case $f in
        *.gz) cat "$f" ;;
        *) gzip -c "$f" ;;
      esac
    done' sh "$days"
fi
exec docker compose exec -T server /app/nfsu2-server ctl "$@"
