#!/usr/bin/env bash
# Preparación única del VPS (ejecutar como root). Idempotente.
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "ejecuta como root"; exit 1; }

DIR="$(cd "$(dirname "$0")/.." && pwd)"

getent group alumnos   >/dev/null || groupadd alumnos
getent group nginxsock >/dev/null || groupadd -g 2000 nginxsock
[ "$(getent group nginxsock | cut -d: -f3)" = "2000" ] \
  || { echo "el grupo nginxsock debe tener gid 2000 (coincide con relay/nginx/Dockerfile)"; exit 1; }

install -d -o root -g root -m 0755 /var/lib/alumnos
install -d -o root -g root -m 0755 /etc/ssh/authorized_keys

install -m 0644 "$DIR/sshd/50-alumnos.conf" /etc/ssh/sshd_config.d/50-alumnos.conf
sshd -t
systemctl reload ssh 2>/dev/null || systemctl reload sshd

echo "OK. Siguiente: relay/bin/alta-alumno.sh <usuario> '<clave pública>'"
