#!/bin/sh

if [ "$3" = "latest" ]; then
  echo "The latest messages logged to the QEC:"
  /usr/local/bin/latest
  exit 0
fi

if [ "$3" = "time" ]; then
  echo The time is...
  d="$(date -u)"
  beatTAI=$(echo "x = ($(date +%s)) % 86400; scale=3; x / 86.4" | bc)
  j=$(echo "x = $(date +%s); scale=5; x / 86400 + 2440587.5" | bc)
  printf "Gregorian Date : %s\n" "$d"
  printf "beatTAI        : %s\n" "$beatTAI"
  printf "Julian Date    : %s\n" "$j"
  exit 0
fi

if [ "$3" = "ping" ]; then
  echo "PONG!"
  ping -c 5 "$2"
  exit 0
fi

if [ "$3" = "fortune" ]; then
  /usr/games/fortune
  exit 0
fi

if [ "$3" = "uptime" ]; then
  /usr/bin/uptime
  exit 0
fi

if [ "$3" = "changelog" ]; then
  head -n 30 /var/wiki/changelog.html | awk '/^  / { print $0 }'
  echo "  ..."
  exit 0
fi

# shellcheck source=log
# shellcheck disable=SC1091
. /etc/efingerd/log

cat <<EOM
QEC STATUS: ERROR
SHIP OR USER NOT DEFINED.
THIS REQUEST HAS BEEN LOGGED.
EOM
