#!/bin/bash

TARGET_DIR="/media/josh/JULES-REDO"

# Check if directory exists
if [[ ! -d "$TARGET_DIR" ]]; then
    echo "Error: $TARGET_DIR is missing. Where are the tunes?"
    exit 1
fi

echo "Initiating deep scan of $TARGET_DIR..."

# Find all .wav files recursively
# -print0 and IFS= '' handle spaces and weird characters in filenames
find "$TARGET_DIR" -type f -name "*.wav" -print0 | while IFS= read -r -d '' f; do

    # Get filename without extension
    base="${f%.*}"

    echo "Crushing: ${f##*/}"

    # Convert to MP3 (VBR 0)
    # -n prevents overwriting if the mp3 already exists
    ffmpeg -i "$f" -q:a 0 "${base}.mp3" -y -loglevel error < /dev/null

    if [[ $? -eq 0 ]]; then
        echo "Success. Reclaiming space from ${f##*/}"
        rm "$f"
    else
        echo "Failed to convert ${f##*/}. Keeping original."
    fi
done

echo "------------------------------------------"
echo "Deep scan complete. Storage should be breathing easier now."
