#!/bin/bash
# Pull files from generated/ into src/
# Only overrides files that exist in both locations
# Run from project root

set -e

PATCHED_SRC="vendor/hytale-server/src"
GENERATED_SRC="vendor/hytale-server/generated"

if [ ! -d "$PATCHED_SRC" ]; then
    echo "Error: vendor/hytale-server/src does not exist"
    exit 1
fi

if [ ! -d "$GENERATED_SRC" ]; then
    echo "Error: vendor/hytale-server/generated does not exist"
    exit 1
fi

count=0

# Find all files in src
while IFS= read -r -d '' file; do
    # Get relative path from PATCHED_SRC
    rel_path="${file#$PATCHED_SRC/}"
    
    # Check if corresponding file exists in generated
    generated_file="$GENERATED_SRC/$rel_path"
    
    if [ -f "$generated_file" ]; then
        cp "$generated_file" "$file"
        echo "Updated: $rel_path"
        count=$((count + 1))
    fi
done < <(find "$PATCHED_SRC" -type f -print0)

echo ""
echo "Done. Updated $count file(s)."
