#!/bin/bash
# RHCSA EX200 (RHEL 10) Practice Exam - server1 setup
# Run ONCE as root on a fresh Rocky Linux / RHEL 10 VM that has:
#   - a network interface on the same network as repo.lab.example (192.168.50.1), enp0s3 by default (see LABIF)
#   - a blank spare disk of 5 GiB (/dev/vdb on KVM, /dev/sdb on VirtualBox)
# It prepares the starting state for the 20 tasks. Everything it does is listed here, nothing is hidden.
set -euo pipefail
REPO=192.168.50.1
# The NIC on the lab network. VirtualBox: Adapter 1 = enp0s3. KVM/virt-manager: often enp1s0. Override: sudo LABIF=enp1s0 bash server1-setup.sh
LABIF=${LABIF:-enp0s3}
# The blank spare disk: /dev/vdb on KVM, /dev/sdb on VirtualBox. Override with DISK=/dev/xxx
DISK=${DISK:-$( [ -b /dev/vdb ] && echo /dev/vdb || echo /dev/sdb )}
ip link show "$LABIF" >/dev/null 2>&1 || { echo "No interface $LABIF. Run 'nmcli device', then: sudo LABIF=<name> bash $0"; exit 1; }
[ -b "$DISK" ] || { echo "No spare disk found. Add a blank 5 GiB disk to this VM, or run: sudo DISK=/dev/<name> bash $0"; exit 1; }
[ -z "$(lsblk -no PARTTYPE "$DISK" | tr -d '[:space:]')" ] || { echo "$DISK is not blank. Use a new empty 5 GiB disk."; exit 1; }

echo "[1/9] Temporary address on $LABIF to fetch lab files"
nmcli -t con delete labnet >/dev/null 2>&1 || true
nmcli con add type ethernet ifname $LABIF con-name labnet ipv4.method manual ipv4.addresses 192.168.50.99/24 ipv6.method disabled >/dev/null
nmcli con up labnet >/dev/null
curl -fsS -o /tmp/labjunk-1.0-1.noarch.rpm http://$REPO/kit/labjunk-1.0-1.noarch.rpm
curl -fsS -o /root/labtools-1.0-1.noarch.rpm http://$REPO/kit/labtools-1.0-1.noarch.rpm

echo "[2/9] Standard server packages (firewalld, tuned, vim) + labjunk (Task 4 removes it)"
cat > /etc/yum.repos.d/labsetup.repo <<EOF
[labsetup-baseos]
name=lab setup BaseOS
baseurl=http://$REPO/BaseOS
gpgcheck=0
[labsetup-appstream]
name=lab setup AppStream
baseurl=http://$REPO/AppStream
gpgcheck=0
EOF
dnf -q -y --disablerepo='*' --enablerepo='labsetup-*' install firewalld tuned vim-enhanced >/dev/null
rm -f /etc/yum.repos.d/labsetup.repo
systemctl enable --now firewalld >/dev/null 2>&1
systemctl disable --now tuned >/dev/null 2>&1 || true
rpm -i /tmp/labjunk-1.0-1.noarch.rpm

echo "[3/9] Leave labnet unconfigured (Task 2 configures it)"
nmcli con mod labnet ipv4.method disabled ipv4.addresses "" ipv6.method disabled
nmcli con up labnet >/dev/null || true

echo "[4/9] Move the default repos aside (Task 3 adds the lab repos)"
mkdir -p /root/repos.orig
mv /etc/yum.repos.d/*.repo /root/repos.orig/ 2>/dev/null || true

echo "[5/9] A broken report script (Task 10)"
mkdir -p /srv/reports
printf '#!/bin/bash\necho "Report OK: $(date +%%F)"\n' > /srv/reports/run.sh
chown root:root /srv/reports /srv/reports/run.sh
chmod 700 /srv/reports
chmod 600 /srv/reports/run.sh

echo "[6/9] A web page in a non-standard place (Task 19)"
mkdir -p /srv/web
echo '<h1>RHCSA practice exam: it works!</h1>' > /srv/web/index.html

echo "[7/9] Starting state for Task 20: graphical default target, volatile journal, a CPU hog"
systemctl set-default graphical.target >/dev/null
rm -rf /var/log/journal
install -m 755 /usr/bin/sha256sum /usr/local/sbin/labburn
cat > /etc/systemd/system/labburn.service <<'EOF2'
[Unit]
Description=Lab background job
[Service]
ExecStart=/usr/local/sbin/labburn /dev/zero
[Install]
WantedBy=multi-user.target graphical.target
EOF2
systemctl daemon-reload
systemctl enable --now labburn.service >/dev/null 2>&1

echo "[+] Exam controller: rhcsa-exam (clock, time-up, automatic grading after reboot)"
HERE=$(cd "$(dirname "$0")" && pwd)
for f in check.sh rhcsa-exam; do [ -f "$HERE/$f" ] || curl -fsS -o "$HERE/$f" "https://rhcsa-practice-exam.pages.dev/kit/$f"; done
install -d -m 700 /usr/local/lib/rhcsa-exam
install -m 700 "$HERE/check.sh" /usr/local/lib/rhcsa-exam/check.sh
install -m 755 "$HERE/rhcsa-exam" /usr/sbin/rhcsa-exam
cat > /etc/systemd/system/rhcsa-exam.service <<'EOF2'
[Unit]
Description=RHCSA practice exam clock and grader
[Service]
Type=oneshot
ExecStart=/usr/sbin/rhcsa-exam tick
EOF2
cat > /etc/systemd/system/rhcsa-exam.timer <<'EOF2'
[Unit]
Description=RHCSA practice exam clock
[Timer]
OnBootSec=20s
OnUnitActiveSec=60s
AccuracySec=5s
[Install]
WantedBy=timers.target
EOF2
cat > /etc/profile.d/rhcsa-exam.sh <<'EOF2'
[ -f /var/lib/rhcsa-exam/state ] && [ "$(cat /var/lib/rhcsa-exam/state)" = running ] && /usr/sbin/rhcsa-exam status
EOF2
systemctl daemon-reload
rm -f "$HERE/check.sh"   # the grader lives where the real exam keeps it: out of your way

echo "[8/9] Unknown root password (Task 1)"
echo "root:$(openssl rand -base64 24)" | chpasswd

echo "[9/9] Spare disk check"
lsblk "$DISK"
echo "Your spare disk is $DISK. Where a task says /dev/vdb, use $DISK."
echo "server1 is ready. Shut it down, take a snapshot called exam-start, start it, then: sudo rhcsa-exam start"
