#!/bin/bash
# deploy-gitea-a11y.sh — Deploy Gitea a11y fixes to the Forge VPS.
#
# Applies custom template overrides for WCAG 2.1 AA compliance fixes
# identified by the accessibility audit (#492).
#
# Fixes:
#   R1: Password visibility toggle on sign-in (#551)
#   R2: aria-required on required form fields (#552)
#   R3: aria-label on star/fork count links (#553)
#   R4: <time> elements for relative timestamps (#554)
#
# Usage:
#   bash deploy/gitea-a11y/deploy-gitea-a11y.sh [ssh-host]
#
# Prerequisites:
#   - SSH access to the Gitea host
#   - Gitea installed with custom/ directory writable

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GITEA_HOST="${1:-root@forge.alexanderwhitestone.com}"
GITEA_CUSTOM="/var/lib/gitea/custom"
TEMPLATES_DIR="${SCRIPT_DIR}/custom/templates"
PUBLIC_DIR="${SCRIPT_DIR}/custom/public"

echo "=== Deploying Gitea a11y fixes to ${GITEA_HOST} ==="

# Upload templates
echo "  [1/4] Uploading template overrides..."
rsync -avz --relative "${TEMPLATES_DIR}/" "${GITEA_HOST}:${GITEA_CUSTOM}/templates/"

# Upload public assets (if any)
if [ -d "${PUBLIC_DIR}" ] && [ "$(ls -A ${PUBLIC_DIR} 2>/dev/null)" ]; then
    echo "  [2/4] Uploading public assets..."
    rsync -avz --relative "${PUBLIC_DIR}/" "${GITEA_HOST}:${GITEA_CUSTOM}/public/"
else
    echo "  [2/4] No public assets to upload"
fi

# Set permissions
echo "  [3/4] Setting permissions..."
ssh "${GITEA_HOST}" "chown -R gitea:gitea ${GITEA_CUSTOM}/templates/ ${GITEA_CUSTOM}/public/ 2>/dev/null || true"

# Restart Gitea to pick up template changes
echo "  [4/4] Restarting Gitea..."
ssh "${GITEA_HOST}" "systemctl restart gitea"

echo ""
echo "=== Deployed ==="
echo "  R1: Password toggle on /user/sign_in"
echo "  R2: aria-required on required fields"
echo "  R3: aria-label on star/fork counts"
echo "  R4: <time> elements on timestamps"
echo ""
echo "Verify at: https://forge.alexanderwhitestone.com/user/sign_in"
