#!/usr/bin/env bash
# pull_audios.sh — Descarga los audios generados en el servidor al directorio local
#
# Uso:
#   bash scripts/pull_audios.sh
#
# Requiere: rsync y SSH configurado (clave pública o ~/.ssh/config)
# En Windows ejecutar desde WSL o Git Bash.
#
# Variables de entorno opcionales:
#   TAIAGE_USER    — usuario SSH  (default: tatvil)
#   TAIAGE_HOST    — servidor SSH (default: taiage.tatvil.es)
#   TAIAGE_REMOTE  — ruta remota a la carpeta audios

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

TAIAGE_USER="${TAIAGE_USER:-tatiana}"
TAIAGE_HOST="${TAIAGE_HOST:-tatvil.es}"
TAIAGE_REMOTE="${TAIAGE_REMOTE:-/home/tatiana/trabajo/oposiciones/taiage-spring/src/main/resources/static/audios/}"
LOCAL_AUDIOS="$PROJECT_DIR/src/main/resources/static/audios/"

echo "[$(date '+%Y-%m-%d %H:%M:%S')] Descargando audios desde ${TAIAGE_USER}@${TAIAGE_HOST}..."
rsync -avz --progress \
    "${TAIAGE_USER}@${TAIAGE_HOST}:${TAIAGE_REMOTE}" \
    "${LOCAL_AUDIOS}"

echo "[$(date '+%Y-%m-%d %H:%M:%S')] ¡Listo! Audios sincronizados en: ${LOCAL_AUDIOS}"
