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

usage() {
  cat <<'TXT'
Uso:
  deploy-booking-engine-public.sh <client_btec_root>

Ejemplo:
  deploy-booking-engine-public.sh /home/bitnami/htdocs/loscabostransfer/btec_v1

Este script:
  - copia la carpeta publica booking/ al root del sitio del cliente
  - copia las pantallas admin del motor al root btec_v1 del cliente
  - aplica ACL para que Apache/daemon pueda completar futuras instalaciones desde el panel
TXT
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
  usage
  exit 0
fi

if [[ $# -lt 1 ]]; then
  usage
  exit 1
fi

client_root="${1%/}"
if [[ ! -d "$client_root" ]]; then
  echo "Error: client_btec_root no existe: $client_root" >&2
  exit 1
fi

if [[ ! -d "$client_root/func" ]]; then
  echo "Error: no se encontro carpeta func en: $client_root" >&2
  exit 1
fi

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
core_root="$(cd "$script_dir/.." && pwd)"

release_root=""
if [[ -L "$client_root/.btec-core" || -d "$client_root/.btec-core" ]]; then
  release_root="$(cd "$client_root/.btec-core" && pwd)"
else
  release_root="$(cd "$core_root/current" && pwd)"
fi

package_root="$release_root/clients/booking-engine-public"
if [[ ! -d "$package_root" ]]; then
  echo "Error: no se encontro package booking-engine-public en: $package_root" >&2
  exit 1
fi

site_root="$(dirname "$client_root")"
mkdir -p "$site_root/booking"
mkdir -p "$client_root"

if command -v rsync >/dev/null 2>&1; then
  rsync -a --delete "$package_root/booking/" "$site_root/booking/"
else
  rm -rf "$site_root/booking"
  mkdir -p "$site_root/booking"
  cp -R "$package_root/booking/." "$site_root/booking/"
fi

if command -v rsync >/dev/null 2>&1; then
  rsync -a "$package_root/btec_v1/" "$client_root/"
else
  cp -R "$package_root/btec_v1/." "$client_root/"
fi

if command -v setfacl >/dev/null 2>&1; then
  setfacl -m u:daemon:rwx "$site_root" "$client_root" "$site_root/booking" || true
  setfacl -d -m u:daemon:rwx "$site_root" "$client_root" "$site_root/booking" || true
  setfacl -R -m u:daemon:rwX "$site_root/booking" || true
  setfacl -R -m u:daemon:rwX \
    "$client_root/booking-engine.php" \
    "$client_root/booking-public-settings.php" \
    "$client_root/restaurant-tarifas.php" \
    "$client_root/open-service-tarifas.php" \
    "$client_root/func/get_restaurant_tarifas.php" \
    "$client_root/func/get_open_service_tarifas.php" \
    "$client_root/func/js/restaurant_tarifas.js" \
    "$client_root/func/js/open_service_tarifas.js" 2>/dev/null || true
  for writable_file in \
    "$client_root/booking-engine.php" \
    "$client_root/asistente-whatsapp.php" \
    "$client_root/email-marketing.php" \
    "$client_root/cupones.php" \
    "$client_root/notificaciones.php" \
    "$client_root/facturas-btec.php" \
    "$client_root/admin-facturas-btec.php"
  do
    if [[ -f "$writable_file" ]]; then
      setfacl -m u:daemon:rw "$writable_file" || true
    fi
  done
  echo "ACL daemon aplicadas para futuras instalaciones desde el panel."
else
  echo "Aviso: setfacl no esta disponible; los permisos web no fueron actualizados." >&2
fi

echo "Booking Engine Public desplegado correctamente."
echo "Cliente btec: $client_root"
echo "Publico booking: $site_root/booking"
echo "Siguiente paso: activar el modulo desde $client_root/modulos-btec.php"
