#!/bin/sh
# Run on the new server BEFORE installing: sh deploy/check-server.sh school.aromanish.com
DOMAIN=${1:?Usage: sh deploy/check-server.sh your.domain.name}
ok=1
echo "OS: $(. /etc/os-release 2>/dev/null; echo "$PRETTY_NAME")"

virt=$(systemd-detect-virt 2>/dev/null || echo unknown)
echo "Virtualization: $virt"
case "$virt" in openvz|lxc) echo "WARNING: Docker often does not work on $virt. Ask the host for a KVM VPS."; ok=0;; esac

mem=$(awk '/MemTotal/{printf "%d",$2/1024}' /proc/meminfo)
echo "RAM: ${mem} MB"
[ "$mem" -lt 1800 ] && { echo "WARNING: under 2 GB RAM. Use 2 GB or more (4 GB is more comfortable)."; ok=0; }

disk=$(df -Pm / | awk 'NR==2{printf "%d",$4/1024}')
echo "Free disk: ${disk} GB"
[ "$disk" -lt 15 ] && { echo "WARNING: under 15 GB free disk."; ok=0; }

for p in 80 443; do
  if ss -ltn "( sport = :$p )" 2>/dev/null | grep -q LISTEN; then
    echo "WARNING: port $p is already in use (Apache/Nginx/cPanel?). Caddy needs ports 80 and 443."; ok=0
  else echo "Port $p: free"; fi
done

pub=$(curl -fsS --max-time 8 https://api.ipify.org 2>/dev/null || echo unknown)
dns=$(getent ahostsv4 "$DOMAIN" 2>/dev/null | awk 'NR==1{print $1}')
echo "Server public IP: $pub"
echo "$DOMAIN resolves to: ${dns:-nothing yet}"
[ "$pub" != "$dns" ] && { echo "WARNING: DNS does not point at this server yet. Add the A record and wait for it to spread."; ok=0; }

[ "$ok" = 1 ] && echo "ALL CHECKS PASSED" || echo "Fix the warnings above before installing."
