#!/bin/bash
# lab-check.sh - is my RHCSA practice lab ready? Run on server1:  sudo bash lab-check.sh
# Checks everything the exam needs and tells you exactly how to fix what's missing.
G=$'\e[1;32m'; R=$'\e[1;31m'; Y=$'\e[1;33m'; B=$'\e[1m'; N=$'\e[0m'
SITE=https://rhcsa-practice-exam.pages.dev
FAIL=0
ok(){ printf "  ${G}OK${N}    %s\n" "$1"; }
bad(){ printf "  ${R}FIX${N}   %s\n        ${Y}-> %s${N}\n" "$1" "$2"; FAIL=$((FAIL+1)); }
[ "$(id -u)" = 0 ] || { echo "Run it with sudo: sudo bash lab-check.sh"; exit 1; }
echo; echo "${B}RHCSA practice lab check${N}  ($(hostname), $(date '+%Y-%m-%d %H:%M'))"; echo

# 1. this machine
. /etc/os-release 2>/dev/null
case "$VERSION_ID" in 10*) ok "$NAME $VERSION_ID";; *) bad "This is $PRETTY_NAME" "Use Rocky Linux 10 or RHEL 10 for server1.";; esac
M=$(awk '/MemTotal/{print int($2/1024)}' /proc/meminfo); [ "$M" -ge 1500 ] && ok "Memory ${M} MB" || bad "Only ${M} MB of memory" "Give the VM at least 2 GB of RAM (VM settings, System)."

# 2. lab network
if nmcli -t -f NAME con show 2>/dev/null | grep -qx labnet; then
  ok "Connection labnet exists"
  DEV=$(nmcli -g connection.interface-name con show labnet)
else
  bad "No connection named labnet" "server1-setup.sh has not run (or failed). Run: sudo bash server1-setup.sh"; DEV=""
fi
NET=""; REACH=0
if ping -c1 -W2 192.168.50.1 >/dev/null 2>&1; then
  ok "repo.lab.example (192.168.50.1) answers on the lab network"; REACH=1
elif [ -n "$DEV" ]; then
  # in the exam-start state labnet has no address yet (that is Task 2), so test with a temporary one
  ip link set "$DEV" up 2>/dev/null; ip addr add 192.168.50.250/24 dev "$DEV" 2>/dev/null && NET=tmp; sleep 2
  if ping -c1 -W2 192.168.50.1 >/dev/null 2>&1; then ok "repo.lab.example (192.168.50.1) answers on the lab network"; REACH=1
  else bad "Can't reach 192.168.50.1 on $DEV" "Both VMs need an adapter on the SAME internal network named labnet (VirtualBox: Settings, Network, Adapter 1), and the repo VM must be running."; fi
fi

# 3. what repo.lab.example serves (only meaningful once the network works)
if [ $REACH = 1 ]; then
if curl -s -m 5 -o /dev/null -w '%{http_code}' http://192.168.50.1/BaseOS/repodata/repomd.xml | grep -q 200 && \
   curl -s -m 5 -o /dev/null -w '%{http_code}' http://192.168.50.1/AppStream/repodata/repomd.xml | grep -q 200; then ok "BaseOS and AppStream repositories are served"
else bad "The repo server isn't serving BaseOS/AppStream" "On the repo VM run: sudo bash repo-setup.sh (it needs internet on its NAT adapter the first time)."; fi
curl -s -m 5 -o /dev/null -w '%{http_code}' http://192.168.50.1/flatpak/labhub.flatpakrepo | grep -q 200 && ok "Flatpak repo labhub is served" || bad "No Flatpak repo on the repo server" "Re-run repo-setup.sh on the repo VM."
if command -v showmount >/dev/null 2>&1; then
  showmount -e 192.168.50.1 2>/dev/null | grep -q /exports/docs && ok "NFS export /exports/docs is visible" || bad "NFS export not visible" "On the repo VM: sudo systemctl enable --now nfs-server, and re-run repo-setup.sh."
else
  timeout 3 bash -c '</dev/tcp/192.168.50.1/2049' 2>/dev/null && ok "NFS server is listening" || bad "NFS server not reachable" "On the repo VM: sudo systemctl enable --now nfs-server"
fi
else
  printf "  ${Y}SKIP${N}  Repository, Flatpak and NFS checks: fix the lab network first\n"
fi
[ "$NET" = tmp ] && ip addr del 192.168.50.250/24 dev "$DEV" 2>/dev/null

# 4. spare disk
DISK=$( [ -b /dev/vdb ] && echo /dev/vdb || { [ -b /dev/sdb ] && echo /dev/sdb; } )
if [ -n "$DISK" ]; then
  S=$(( $(blockdev --getsize64 $DISK) / 1024 / 1024 ))
  if [ -z "$(lsblk -no PARTTYPE $DISK | tr -d '[:space:]')" ]; then ok "Spare disk $DISK (${S} MiB) is blank"
  else bad "Spare disk $DISK already has partitions" "Restore your exam-start snapshot for a fresh attempt (or add a new blank 5 GB disk)."; fi
  [ "$S" -ge 4500 ] || bad "Spare disk is only ${S} MiB" "Make it 5 GB: several tasks need the space."
else bad "No spare disk found" "Add a second, blank 5 GB disk to server1 (VirtualBox: Settings, Storage, SATA controller, Add hard disk)."; fi

# 5. exam controller and exam-start state
command -v rhcsa-exam >/dev/null && ok "Exam controller rhcsa-exam is installed" || bad "rhcsa-exam is missing" "Download it next to server1-setup.sh and re-run: sudo bash server1-setup.sh"
[ -x /usr/local/lib/rhcsa-exam/check.sh ] && ok "Grader is in place" || bad "Grader is missing" "Download check.sh next to server1-setup.sh and re-run the setup."
ls /etc/yum.repos.d/*.repo >/dev/null 2>&1 && bad "server1 already has repositories" "That's Task 3. Restore exam-start, or this is a used attempt." || ok "No repositories yet (Task 3 is still to do)"
case "$(cat /var/lib/rhcsa-exam/state 2>/dev/null)" in
  running) bad "An exam is already running" "Finish it, or restore your exam-start snapshot.";;
  graded)  bad "This server holds a graded attempt" "Restore your exam-start snapshot for a fresh exam.";;
  *) ok "No exam in progress";; esac

echo
if [ $FAIL = 0 ]; then
  echo "  ${G}${B}LAB READY.${N} Take a snapshot called exam-start if you haven't, then: ${B}sudo rhcsa-exam start${N}"
else
  echo "  ${R}${B}$FAIL thing(s) to fix.${N} Fix them, run this again, and check the troubleshooting list at $SITE"
fi
echo
