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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
LOCAL_DIR="$ROOT_DIR/local"
STACK_DIR="$LOCAL_DIR/docker-jitsi-meet"
CONFIG_DIR="$LOCAL_DIR/.jitsi-meet-cfg"

if [[ ! -d "$STACK_DIR" ]]; then
    echo "Missing $STACK_DIR. Run ./scripts/local-jitsi-setup.sh first."
    exit 1
fi

mkdir -p "$CONFIG_DIR"/{web,transcripts,prosody/config,prosody/prosody-plugins-custom,jicofo,jvb,jigasi,jibri}

cp "$LOCAL_DIR/custom-config.js" "$CONFIG_DIR/web/custom-config.js"
cp "$LOCAL_DIR/custom-interface_config.js" "$CONFIG_DIR/web/custom-interface_config.js"
cp "$LOCAL_DIR/docker-compose.override.yml" "$STACK_DIR/docker-compose.override.yml"
find "$CONFIG_DIR/prosody/prosody-plugins-custom" -maxdepth 1 -name 'mod_politictalk_*.lua' -type f -delete
find "$ROOT_DIR/prosody-plugins" -maxdepth 1 -name 'mod_politictalk_*.lua' -type f -print0 | while IFS= read -r -d '' file; do
    cp "$file" "$CONFIG_DIR/prosody/prosody-plugins-custom/"
done

echo "Synced PoliticTalk local overrides into Docker Jitsi runtime."
