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

DOMAIN="${DOMAIN:-politictalk.parallelglobe.io}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JITSI_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BACKUP_ROOT="${BACKUP_ROOT:-/root/jitsi-backups}"
STAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP_DIR="$BACKUP_ROOT/$STAMP"

CONFIG_SRC="$JITSI_DIR/config/$DOMAIN-config.js"
ASSETS_SRC="$JITSI_DIR/assets/public/politictalk"
PROSODY_PLUGINS_SRC="$JITSI_DIR/prosody-plugins"
TITLE_SRC="$JITSI_DIR/web/title.html"
PLUGIN_HEAD_SRC="$JITSI_DIR/web/plugin.head.html"
INTERFACE_SRC="$JITSI_DIR/interface_config/politictalk-overrides.js"
NGINX_SRC="$JITSI_DIR/nginx/$DOMAIN.conf"

CONFIG_DEST="/etc/jitsi/meet/$DOMAIN-config.js"
ASSETS_DEST="/etc/jitsi/meet/public/politictalk"
WEB_IMAGES_DEST="/usr/share/jitsi-meet/images/politictalk"
PROSODY_PLUGINS_DEST="/usr/share/jitsi-meet/prosody-plugins"
TITLE_DEST="/usr/share/jitsi-meet/title.html"
PLUGIN_HEAD_DEST="/usr/share/jitsi-meet/plugin.head.html"
INTERFACE_DEST="/usr/share/jitsi-meet/interface_config.js"
NGINX_DEST="/etc/nginx/sites-available/$DOMAIN.conf"

INTERFACE_MARKER_START="// BEGIN POLITICTALK INTERFACE OVERRIDES"
INTERFACE_MARKER_END="// END POLITICTALK INTERFACE OVERRIDES"
PLUGIN_MARKER_START="<!-- BEGIN POLITICTALK PLUGIN HEAD -->"
PLUGIN_MARKER_END="<!-- END POLITICTALK PLUGIN HEAD -->"

if [[ "$(id -u)" -ne 0 ]]; then
    echo "Run this script with sudo on the VPS."
    exit 1
fi

if [[ ! -f "$CONFIG_SRC" ]]; then
    echo "Missing config source: $CONFIG_SRC"
    exit 1
fi

if [[ ! -d "$ASSETS_SRC" ]]; then
    echo "Missing assets source: $ASSETS_SRC"
    exit 1
fi

if [[ ! -d "$PROSODY_PLUGINS_SRC" ]]; then
    echo "Missing Prosody plugins source: $PROSODY_PLUGINS_SRC"
    exit 1
fi

if [[ ! -f "$TITLE_SRC" ]]; then
    echo "Missing title source: $TITLE_SRC"
    exit 1
fi

if [[ ! -f "$PLUGIN_HEAD_SRC" ]]; then
    echo "Missing plugin head source: $PLUGIN_HEAD_SRC"
    exit 1
fi

if [[ ! -f "$INTERFACE_SRC" ]]; then
    echo "Missing interface source: $INTERFACE_SRC"
    exit 1
fi

if [[ ! -f "$INTERFACE_DEST" ]]; then
    echo "Missing Jitsi interface config destination: $INTERFACE_DEST"
    exit 1
fi

install -d -m 0755 "$BACKUP_DIR"

if [[ -f "$CONFIG_DEST" ]]; then
    cp -a "$CONFIG_DEST" "$BACKUP_DIR/"
fi

if [[ -d "$ASSETS_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/public"
    cp -a "$ASSETS_DEST" "$BACKUP_DIR/public/"
fi

if [[ -d "$WEB_IMAGES_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/usr-share-jitsi-meet/images"
    cp -a "$WEB_IMAGES_DEST" "$BACKUP_DIR/usr-share-jitsi-meet/images/"
fi

if [[ -f "$TITLE_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/usr-share-jitsi-meet"
    cp -a "$TITLE_DEST" "$BACKUP_DIR/usr-share-jitsi-meet/"
fi

if [[ -f "$PLUGIN_HEAD_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/usr-share-jitsi-meet"
    cp -a "$PLUGIN_HEAD_DEST" "$BACKUP_DIR/usr-share-jitsi-meet/"
fi

if [[ -f "$INTERFACE_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/usr-share-jitsi-meet"
    cp -a "$INTERFACE_DEST" "$BACKUP_DIR/usr-share-jitsi-meet/"
fi

if [[ -d "$PROSODY_PLUGINS_DEST" ]]; then
    install -d -m 0755 "$BACKUP_DIR/usr-share-jitsi-meet/prosody-plugins"
    find "$PROSODY_PLUGINS_DEST" -maxdepth 1 -name 'mod_politictalk_*.lua' -type f -exec cp -a {} "$BACKUP_DIR/usr-share-jitsi-meet/prosody-plugins/" \;
fi

if [[ "${DEPLOY_NGINX:-0}" == "1" && -f "$NGINX_DEST" ]]; then
    cp -a "$NGINX_DEST" "$BACKUP_DIR/"
fi

install -d -m 0755 "$ASSETS_DEST"
install -d -m 0755 "$WEB_IMAGES_DEST"
install -m 0644 "$CONFIG_SRC" "$CONFIG_DEST"
install -m 0644 "$TITLE_SRC" "$TITLE_DEST"
find "$ASSETS_SRC" -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file; do
    install -m 0644 "$file" "$ASSETS_DEST/"
    install -m 0644 "$file" "$WEB_IMAGES_DEST/"
done

install -d -m 0755 "$PROSODY_PLUGINS_DEST"
find "$PROSODY_PLUGINS_SRC" -maxdepth 1 -name 'mod_politictalk_*.lua' -type f -print0 | while IFS= read -r -d '' file; do
    install -m 0644 "$file" "$PROSODY_PLUGINS_DEST/"
done

tmp_plugin_head="$(mktemp)"
awk -v start="$PLUGIN_MARKER_START" -v end="$PLUGIN_MARKER_END" '
    $0 == start { skip = 1; next }
    $0 == end { skip = 0; next }
    !skip { print }
' "$PLUGIN_HEAD_DEST" > "$tmp_plugin_head"
printf '\n%s\n' "$PLUGIN_MARKER_START" >> "$tmp_plugin_head"
cat "$PLUGIN_HEAD_SRC" >> "$tmp_plugin_head"
printf '\n%s\n' "$PLUGIN_MARKER_END" >> "$tmp_plugin_head"
install -m 0644 "$tmp_plugin_head" "$PLUGIN_HEAD_DEST"
rm -f "$tmp_plugin_head"

tmp_interface="$(mktemp)"
awk -v start="$INTERFACE_MARKER_START" -v end="$INTERFACE_MARKER_END" '
    $0 == start { skip = 1; next }
    $0 == end { skip = 0; next }
    !skip { print }
' "$INTERFACE_DEST" > "$tmp_interface"
printf '\n' >> "$tmp_interface"
cat "$INTERFACE_SRC" >> "$tmp_interface"
install -m 0644 "$tmp_interface" "$INTERFACE_DEST"
rm -f "$tmp_interface"

if [[ "${DEPLOY_NGINX:-0}" == "1" ]]; then
    if [[ ! -f "$NGINX_SRC" ]]; then
        echo "Missing nginx source: $NGINX_SRC"
        exit 1
    fi
    install -m 0644 "$NGINX_SRC" "$NGINX_DEST"
    nginx -t
fi

systemctl reload nginx

echo "Deployed PoliticTalk Jitsi files."
echo "Backup: $BACKUP_DIR"
