#!/usr/bin/env bash
# rename-mods.sh - run on the Wings host, with the server STOPPED.
# Renames uploaded "@Display Name" mod folders to short lowercase names,
# matched by the publishedid in each folder's meta.cpp (so odd characters
# in the folder names don't matter). Also lowercases everything inside and
# copies .bikey files into keys/.
#
# Usage:  sudo ./rename-mods.sh          (dry run, shows what it would do)
#         sudo ./rename-mods.sh --apply  (actually do it)
set -euo pipefail

SERVER_UUID="89359bb7-9908-47d0-a6df-90b80ee50062"
VOL="/var/lib/pterodactyl/volumes/${SERVER_UUID}"
APPLY=false; [[ "${1:-}" == "--apply" ]] && APPLY=true
cd "$VOL"

declare -A NAME=(
  [450814997]=cba_a3              # CBA_A3
  [463939057]=ace                 # ace
  [3053169823]=ace_nomedical      # ACE No Medical
  [2522638637]=ace_arsenal_ext    # ACE3 Arsenal Extended - Core
  [843425103]=rhsafrf             # RHSAFRF
  [843577117]=rhsusaf             # RHSUSAF
  [843593391]=rhsgref             # RHSGREF
  [843632231]=rhssaf              # RHSSAF
  [3553838240]=rhs_additions      # RHS Additions - Rewrite
  [1673456286]=3cb_factions       # 3CB Factions
  [3805259447]=a3ue_coalition     # [A3UE] - Coalition Factions Extender
  [3792065519]=a3ue_frontlines    # [A3UE] - Frontlines & Patrols
  [3800012076]=a3ue_contraband    # A3UE Contraband
  [3720138990]=aas_civ_insurgent  # [THEME] AAS - CIVILIAN- INSURGENT
  [3699105433]=aas_core           # ADAPTATIVE ARMA SUPPORTS (AAS - Core)
  [2800112936]=asl_refactored     # Advanced Sling Loading Refactored
  [903134884]=align               # Align
  [3283612524]=animate_rewrite    # Animate - Rewrite
  [2935338016]=anim_grenade       # Animated Grenade Throwing
  [3554744365]=anim_smoke         # Animated Smoke Effects
  [3799436547]=antistasi_bazaar   # Antistasi Bazaar
  [3020755032]=antistasi_ultimate # Antistasi Ultimate - Mod
  [3480941970]=arlit_toyotas      # Arlit Toyotas
  [491016790]=arma3_command       # ARMA 3 COMMAND
  [2791403093]=better_inventory   # Better Inventory
  [2257686620]=blastcore_murr     # Blastcore Murr Edition
  [1200127537]=bwmod              # BWMod
  [2872102180]=cdf_gsfl           # CDF-GSFL
  [2395553729]=dega_pmc_suv       # DEGA: ArmA 2 PMC SUV Modification
  [1638341685]=dui_radar          # DUI - Squad Radar
  [333310405]=enhanced_movement   # Enhanced Movement
  [2034363662]=em_rework          # Enhanced Movement Rework
  [2057294714]=ets                # ETS - Enemy Tagging System
  [2899715306]=gsb_gear           # Green Sea Battalion Gear 2022
  [3375788189]=immersive_cigs     # Immersive Cigs - Rewrite
  [2834576684]=itn                # ITN - Illuminate The Night
  [3091577783]=itn_compat_rhs     # ITN Compat RHS All-in-One
  [3091582458]=itn_compat_t1      # ITN Compat T1
  [3543364649]=jca_facewear       # JCA - Tactical Facewear
  [3407948300]=jsrs               # JSRS SOUNDMOD 2025
  [3032131959]=maptools           # MapTools Remastered
  [929396506]=mrb_air             # MRB Air Visibility
  [1845100804]=mrb_sea            # MRB Sea Vessel Visibility
  [930903722]=mrb_vehicle         # MRB Vehicle Visibility
  [2955627408]=racs               # Project RACS 2023
  [2955691343]=racs_sla           # Project RACS SLA 2023
  [3639557777]=ragdoll            # Realistic Ragdoll Physics
  [3686863566]=ser_grenades       # Some Effects Rework: Grenades
  [3596299237]=ser_impacts        # Some Effects Rework: Impacts
  [3283642267]=speshal_core       # Speshal Core
  [825174634]=suppress            # Suppress
  [2268351256]=t1_weapons         # Tier One Weapons
  [3632946871]=true_death         # True Death
)

OWNER=$(stat -c '%u:%g' "$VOL")
$APPLY && mkdir -p keys

shopt -s nullglob
for dir in @*/; do
  dir="${dir%/}"
  meta=$(find "$dir" -maxdepth 1 -iname 'meta.cpp' | head -n1)
  if [[ -z "$meta" ]]; then echo "SKIP (no meta.cpp): $dir"; continue; fi
  id=$(grep -oiP 'publishedid\s*=\s*\K[0-9]+' "$meta" | head -n1 || true)
  new="${NAME[$id]:-}"
  if [[ -z "$new" ]]; then echo "SKIP (id ${id:-?} not in list): $dir"; continue; fi
  new="@$new"

  if [[ "$dir" != "$new" ]]; then
    if [[ -e "$new" ]]; then echo "SKIP (target exists): $dir -> $new"; continue; fi
    echo "RENAME: $dir -> $new"
    $APPLY && mv -T -- "$dir" "$new"
  else
    echo "OK:     $new"
  fi

  if $APPLY; then
    find "$new" -depth -mindepth 1 -execdir bash -c \
      'for f; do l="${f,,}"; if [[ "$f" != "$l" ]]; then mv -T -- "$f" "$l"; fi; done' _ {} +
    find "$new" -iname '*.bikey' -exec cp -f {} keys/ \;
  fi
done

$APPLY && chown -R "$OWNER" keys
$APPLY || echo -e "\nDry run only. Re-run with --apply to make changes."
