#!/usr/bin/env bash
# lp-migration.sh — Carbon → ChatChat permission migration
# racked.ru / 2026-05-07
#
# Run from any host with SSH access to nullstone (192.168.0.100). Idempotent —
# safe to re-run if a step half-completes. Each command is dispatched via RCON
# so LuckPerms picks it up live without restart.
#
# DOES NOT touch LP rank prefixes/suffixes (locked per CLAUDE.md feedback rule).

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 "=== ChatChat permission migration — default group ==="

# 1. Revoke Carbon perms on default group (Carbon JAR will be removed; perms become dead nodes
#    if left behind, harmless but cleaner to drop).
rcon "lp group default permission unset carbon.channel.global.see"
rcon "lp group default permission unset carbon.channel.global.speak"

# 2. Grant ChatChat equivalents.
#    Channel-level: lets every default-group player SEE and TALK in `global`.
rcon "lp group default permission set chatchat.channel.see.global true"
rcon "lp group default permission set chatchat.channel.use.global true"

# 3. Grant private-message + reply (Carbon had no perm gate; ChatChat /msg is OP by default).
rcon "lp group default permission set chatchat.pm true"
rcon "lp group default permission set chatchat.pm.toggle true"

# 4. Grant ignore + ignore-list (everyone should be able to mute another player, like Carbon).
rcon "lp group default permission set chatchat.ignore true"
rcon "lp group default permission set chatchat.ignorelist true"

# 5. Grant personal mentions (Carbon allowed @-pings; channel-mention `@everyone` stays OP-only).
rcon "lp group default permission set chatchat.mention.personal true"
rcon "lp group default permission set chatchat.mention.personal.block true"
rcon "lp group default permission set chatchat.mention.everyone.block true"

# 6. Grant the safe MM tags everyone can already use in Carbon (color, hover, item).
#    NOTE: chatchat.tag.color = uses <color> tags; restrict if abuse appears.
rcon "lp group default permission set chatchat.tag.color true"
rcon "lp group default permission set chatchat.tag.hover true"
rcon "lp group default permission set chatchat.tag.item true"

echo ""
echo "=== verify (will show 'true' for each) ==="
rcon "lp group default permission check chatchat.channel.use.global"
rcon "lp group default permission check chatchat.pm"

echo ""
echo "=== done. Restart ChatChat (or whole server) so plugin re-evaluates perms. ==="
