#!/usr/bin/env bash
# rig-headers.sh
# Ajoute un header attribution Epic Fight + GPLv3 à chaque fichier .java
# forké dans v3/rig qui n'en a pas déjà un.

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

HEADER='/*
 * Derived from Epic Fight (https://github.com/Epic-Fight/epicfight)
 * by the Epic Fight Team, licensed under GPLv3.
 * Modifications © 2026 TiedUp! Remake Contributors, distributed under GPLv3.
 */

'

count=0
skipped=0
find "$TARGET" -type f -name "*.java" | while read f; do
    if head -5 "$f" | grep -q "Derived from Epic Fight"; then
        skipped=$((skipped + 1))
    else
        tmp=$(mktemp)
        printf '%s' "$HEADER" > "$tmp"
        cat "$f" >> "$tmp"
        mv "$tmp" "$f"
        count=$((count + 1))
    fi
done

echo "Headers injected in files (check count via grep)."
echo "Run: grep -l 'Derived from Epic Fight' $TARGET -r | wc -l"
