#!/bin/sh
# fillrate-task: driver-post-report
# Read-only. Prints the NVIDIA driver post-reboot unit, the driver-update
# state directory, package and GPU status, and a container GPU check.
# This script does not change the machine. The container check may pull
# nvidia/cuda:12.4.1-base-ubuntu22.04 when that image is not already present.
#
# FILLRATE_DRIVER_STATE overrides the state directory in tests. The runner
# starts this script with a clean environment and does not set that variable,
# so a real run reads /var/lib/fillrate-runner/nvidia-driver-update.

set -u

STATE=${FILLRATE_DRIVER_STATE:-/var/lib/fillrate-runner/nvidia-driver-update}
IMAGE=nvidia/cuda:12.4.1-base-ubuntu22.04
UNIT=fillrate-nvidia-driver-update-post

sensitive_name() {
  printf '%s\n' "$1" | grep -E -i -q 'ticket|key|nonce|sig|secret'
}

# Last 80 lines. Lines that carry ticket, key, nonce, or secret material
# are left out. Symlinks are not followed.
print_file() {
  path=$1
  base=$(basename "$path")
  echo "=== $base ==="
  if [ -L "$path" ]; then
    echo "skipped symlink"
    return 0
  fi
  if [ ! -f "$path" ]; then
    echo "not a regular file"
    return 0
  fi
  text=$(
    grep -a -E -i -v \
      'nonce|ticket|secret|ssh-ed25519|private key|begin ssh signature|(^|[^A-Za-z0-9])(key|sig)([^A-Za-z0-9]|$)' \
      "$path" | tail -n 80 || true
  )
  if [ -n "$text" ]; then
    printf '%s\n' "$text"
  else
    echo "(no lines after skipping ticket, key, nonce, and secret material)"
  fi
}

echo "=== journalctl -u $UNIT --no-pager -b 0 ==="
if command -v journalctl >/dev/null 2>&1; then
  journalctl -u "$UNIT" --no-pager -b 0 2>&1 | tail -n 80 || true
else
  echo "journalctl not found"
fi

echo "=== systemctl status $UNIT --no-pager ==="
if command -v systemctl >/dev/null 2>&1; then
  systemctl status "$UNIT" --no-pager 2>&1 || true
else
  echo "systemctl not found"
fi

echo "=== ls -la $STATE ==="
if [ -d "$STATE" ] && [ ! -L "$STATE" ]; then
  ls -la "$STATE" 2>&1 || true
else
  echo "directory missing"
fi

echo "=== pending-post ==="
if [ -e "$STATE/pending-post" ]; then
  echo "pending-post exists"
else
  echo "pending-post absent"
fi

echo "=== state files ==="
if [ -d "$STATE" ] && [ ! -L "$STATE" ]; then
  names=$(
    printf '%s\n' "$STATE"/*.txt "$STATE"/*.json "$STATE"/facts 2>/dev/null | while IFS= read -r path; do
      [ -e "$path" ] || [ -L "$path" ] || continue
      printf '%s\n' "$path"
    done | sort
  )
  if [ -z "$names" ]; then
    echo "no txt, json, or facts files"
  else
    printf '%s\n' "$names" | while IFS= read -r path; do
      [ -n "$path" ] || continue
      base=$(basename "$path")
      if sensitive_name "$base"; then
        echo "=== skip $base ==="
        continue
      fi
      print_file "$path"
    done
  fi
else
  echo "directory missing"
fi

echo "=== apt-mark showhold ==="
if command -v apt-mark >/dev/null 2>&1; then
  apt-mark showhold 2>&1 || true
else
  echo "apt-mark not found"
fi

echo "=== dpkg --audit ==="
if command -v dpkg >/dev/null 2>&1; then
  dpkg --audit 2>&1 || true
else
  echo "dpkg not found"
fi

echo "=== dkms status ==="
if command -v dkms >/dev/null 2>&1; then
  dkms status 2>&1 || true
else
  echo "dkms not found"
fi

echo "=== nvidia-smi ==="
if command -v nvidia-smi >/dev/null 2>&1; then
  nvidia-smi 2>&1 || true
else
  echo "nvidia-smi not found"
fi

echo "=== systemctl is-active nvidia-persistenced vastai docker ==="
if command -v systemctl >/dev/null 2>&1; then
  systemctl is-active nvidia-persistenced vastai docker 2>&1 || true
else
  echo "systemctl not found"
fi

echo "=== docker image ls $IMAGE ==="
if ! command -v docker >/dev/null 2>&1; then
  echo "docker not found"
  echo "image_present=no"
  echo "docker_gpu_exit=127"
  exit 0
fi

docker image ls "$IMAGE" 2>&1 || true
if ids=$(docker image ls -q "$IMAGE" 2>/dev/null); then
  if printf '%s\n' "$ids" | grep -q .; then
    echo "image_present=yes"
  else
    echo "image_present=no"
  fi
else
  echo "image_present=unknown"
fi

echo "=== timeout 120 docker run --rm --gpus all $IMAGE nvidia-smi ==="
if command -v timeout >/dev/null 2>&1; then
  timeout 120 docker run --rm --gpus all "$IMAGE" nvidia-smi
  code=$?
  echo "docker_gpu_exit=$code"
else
  echo "timeout not found"
  echo "docker_gpu_exit=127"
fi

exit 0
