#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SITE_VOL="/var/volumes/web_jailgames"
NGINX_VOL="/var/volumes/web_jailgames-nginx"

cd "$SCRIPT_DIR"

echo "==> Instalando dependencias..."
npm ci --silent

echo "==> Compilando sitio..."
npm run build --silent

echo "==> Copiando sitio a $SITE_VOL ..."
rsync -a --delete _site/ "$SITE_VOL/"

echo "==> Copiando descargas a $SITE_VOL/downloads/ ..."
rsync -a downloads/ "$SITE_VOL/downloads/"

echo "==> Copiando nginx.conf a $NGINX_VOL/jailgames.conf ..."
cp nginx.conf "$NGINX_VOL/jailgames.conf"

echo "==> Hecho. Recargando nginx en el contenedor..."
if docker exec jailgames nginx -s reload 2>/dev/null; then
  echo "    Nginx recargado."
else
  echo "    (contenedor 'jailgames' no encontrado o no activo, omitiendo reload)"
fi

echo "==> Deploy completado."
