#!/usr/bin/env bash
# rig-rewrite-imports.sh
# Renomme les imports yesman.epicfight.* vers com.tiedup.remake.rig.*
# dans tous les fichiers Java fraîchement copiés dans v3/rig.
#
# IMPORTANT : ordre du plus spécifique au plus général.
# Si on fait api.animation avant api.client.animation, la première rule
# mange la seconde. Chaque règle utilise un pattern qui matche exactement
# le chemin complet jusqu'au séparateur suivant.
#
# Portabilité : sed -i non-portable BSD (macOS) — utiliser "sed -i.bak"
# si besoin support Mac.

set -u

TARGET="${1:-src/main/java/com/tiedup/remake/rig}"

if [ ! -d "$TARGET" ]; then
    echo "ERROR: target dir not found: $TARGET"
    exit 1
fi

echo "Rewriting imports in $TARGET..."

find "$TARGET" -type f -name "*.java" -exec sed -i \
  -e 's|yesman\.epicfight\.api\.utils\.math|com.tiedup.remake.rig.math|g' \
  -e 's|yesman\.epicfight\.api\.utils\.datastruct|com.tiedup.remake.rig.util.datastruct|g' \
  -e 's|yesman\.epicfight\.api\.utils\.ParseUtil|com.tiedup.remake.rig.util.ParseUtil|g' \
  -e 's|yesman\.epicfight\.api\.utils|com.tiedup.remake.rig.util|g' \
  -e 's|yesman\.epicfight\.api\.exception|com.tiedup.remake.rig.exception|g' \
  -e 's|yesman\.epicfight\.api\.forgeevent|com.tiedup.remake.rig.event|g' \
  -e 's|yesman\.epicfight\.api\.model\.Armature|com.tiedup.remake.rig.armature.Armature|g' \
  -e 's|yesman\.epicfight\.api\.animation\.Joint|com.tiedup.remake.rig.armature.Joint|g' \
  -e 's|yesman\.epicfight\.api\.animation\.JointTransform|com.tiedup.remake.rig.armature.JointTransform|g' \
  -e 's|yesman\.epicfight\.api\.animation\.types\.datapack|com.tiedup.remake.rig.anim.types.datapack|g' \
  -e 's|yesman\.epicfight\.api\.animation\.types\.grappling|com.tiedup.remake.rig.anim.types.grappling|g' \
  -e 's|yesman\.epicfight\.api\.animation\.types\.procedural|com.tiedup.remake.rig.anim.types.procedural|g' \
  -e 's|yesman\.epicfight\.api\.animation\.types|com.tiedup.remake.rig.anim.types|g' \
  -e 's|yesman\.epicfight\.api\.animation\.property|com.tiedup.remake.rig.anim.property|g' \
  -e 's|yesman\.epicfight\.api\.animation|com.tiedup.remake.rig.anim|g' \
  -e 's|yesman\.epicfight\.api\.client\.animation\.property|com.tiedup.remake.rig.anim.client.property|g' \
  -e 's|yesman\.epicfight\.api\.client\.animation|com.tiedup.remake.rig.anim.client|g' \
  -e 's|yesman\.epicfight\.api\.client\.model\.transformer|com.tiedup.remake.rig.mesh.transformer|g' \
  -e 's|yesman\.epicfight\.api\.client\.model|com.tiedup.remake.rig.mesh|g' \
  -e 's|yesman\.epicfight\.api\.client\.physics\.cloth|com.tiedup.remake.rig.cloth|g' \
  -e 's|yesman\.epicfight\.api\.client\.physics|com.tiedup.remake.rig.cloth|g' \
  -e 's|yesman\.epicfight\.api\.client\.forgeevent|com.tiedup.remake.rig.event|g' \
  -e 's|yesman\.epicfight\.api\.asset|com.tiedup.remake.rig.asset|g' \
  -e 's|yesman\.epicfight\.model\.armature\.types|com.tiedup.remake.rig.armature.types|g' \
  -e 's|yesman\.epicfight\.model\.armature|com.tiedup.remake.rig.armature|g' \
  -e 's|yesman\.epicfight\.world\.capabilities\.provider|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.world\.capabilities\.entitypatch\.player|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.world\.capabilities\.entitypatch|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.world\.capabilities\.EpicFightCapabilities|com.tiedup.remake.rig.patch.TiedUpCapabilities|g' \
  -e 's|yesman\.epicfight\.world\.capabilities|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.client\.world\.capabilites\.entitypatch\.player|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.client\.world\.capabilites\.entitypatch|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.client\.world\.capabilites|com.tiedup.remake.rig.patch|g' \
  -e 's|yesman\.epicfight\.client\.renderer\.patched\.entity|com.tiedup.remake.rig.render|g' \
  -e 's|yesman\.epicfight\.client\.renderer\.patched|com.tiedup.remake.rig.render|g' \
  -e 's|yesman\.epicfight\.client\.renderer\.EpicFightRenderTypes|com.tiedup.remake.rig.render.TiedUpRenderTypes|g' \
  -e 's|yesman\.epicfight\.client\.mesh|com.tiedup.remake.rig.mesh|g' \
  {} +

echo ""
echo "Verifying no yesman.epicfight references remain..."
remaining=$(grep -r "yesman\.epicfight" "$TARGET" 2>/dev/null | wc -l)
if [ "$remaining" -eq 0 ]; then
    echo "OK - all imports rewritten."
else
    echo "WARN - $remaining residual refs found:"
    grep -rn "yesman\.epicfight" "$TARGET" | head -20
    echo "..."
    echo "(affichage limité aux 20 premiers)"
fi
