#!/usr/bin/env bash
# lp-rollback.sh — undo lp-migration.sh, restore Carbon perms.
# racked.ru / 2026-05-07
#
# Run if the swap window aborts and Carbon JAR is restored.

set -euo pipefail

SSH_TARGET="${SSH_TARGET:-user@192.168.0.100}"

rcon() {
  local cmd="$1"
  echo ">>> $cmd"
  ssh "$SSH_TARGET" "docker exec minecraft-mc rcon-cli '$cmd'" || {
    echo "!!! command failed: $cmd" >&2
    return 1
  }
}

echo "=== Rolling back ChatChat perms, restoring Carbon perms ==="

# 1. Re-grant Carbon perms.
rcon "lp group default permission set carbon.channel.global.see true"
rcon "lp group default permission set carbon.channel.global.speak true"

# 2. Drop ChatChat perms (harmless if dead but tidier).
rcon "lp group default permission unset chatchat.channel.see.global"
rcon "lp group default permission unset chatchat.channel.use.global"
rcon "lp group default permission unset chatchat.pm"
rcon "lp group default permission unset chatchat.pm.toggle"
rcon "lp group default permission unset chatchat.ignore"
rcon "lp group default permission unset chatchat.ignorelist"
rcon "lp group default permission unset chatchat.mention.personal"
rcon "lp group default permission unset chatchat.mention.personal.block"
rcon "lp group default permission unset chatchat.mention.everyone.block"
rcon "lp group default permission unset chatchat.tag.color"
rcon "lp group default permission unset chatchat.tag.hover"
rcon "lp group default permission unset chatchat.tag.item"

echo ""
echo "=== verify ==="
rcon "lp group default permission check carbon.channel.global.speak"

echo ""
echo "=== rollback done. Restart server so Carbon picks up perms cleanly. ==="
