#!/bin/bash

echo "Removing numbers from filenames and titles (keeping track numbers)..."
echo ""

for file in *.mp3; do
    filename="${file%.*}"
    
    # Extract track number before removing it from filename
    track_num=$(echo "$filename" | grep -oP '^\d+')
    
    # Remove leading numbers and dot/space from filename
    new_filename=$(echo "$filename" | sed 's/^[0-9]\+[. ]\+//')
    new_file="${new_filename}.mp3"
    
    if [ "$file" != "$new_file" ]; then
        echo "Processing: $file"
        echo "  New filename: $new_file"
        echo "  New title: $new_filename"
        echo "  Track number: $track_num (kept)"
        
        # Retag with new title (without number) but keep track number
        ffmpeg -i "$file" -c copy \
          -metadata title="$new_filename" \
          -metadata track="$track_num" \
          "${new_filename}_temp.mp3" 2>/dev/null
        
        # Remove original and rename temp file
        rm "$file"
        mv "${new_filename}_temp.mp3" "$new_file"
        
        echo "  ✓ Done"
        echo ""
    fi
done

echo "Complete! All files processed."
