#!/bin/bash

# Function to rename a single file by replacing commas, underscores,
# and long dashes with spaces
rename_file() {
    local file="$1"

    if [[ "$file" == *,* ]] || [[ "$file" == *_* ]] || [[ "$file" == *—* ]]; then
        new_name=$(echo "$file" | sed \
            -e 's/,/ /g' \
            -e 's/_/ /g' \
            -e 's/—/ /g')

        if mv -n "$file" "$new_name"; then
            echo "Renamed $file to $new_name"
            file="$new_name"
        else
            echo "Failed to rename $file"
            return 1
        fi
    fi

    echo "$file"
}


# Function to standardize a single filename
# by replacing spaces with hyphens and removing leading hyphens/underscores
standardize_filename() {
    local file="$1"

    if [[ "$file" != *\ * ]]; then
        echo "$file"
        return 0
    fi

    new_name="${file// /-}"
    new_name=$(echo "$new_name" | sed -E 's/-{2,}/-/g' | sed 's/^[-_]//')

    if mv -n "$file" "$new_name"; then
        echo "Renamed $file to $new_name"
        echo "$new_name"
    else
        echo "Failed to standardize $file"
        return 1
    fi
}


# Get list of .mp4, .wav, .m4a and .mp3 files
files=( *.mp4 *.wav *.m4a *.mp3 )

if [ ${#files[@]} -eq 0 ]; then
    echo "No .mp4, .wav, .m4a or .mp3 files found in the current directory."
    exit 1
fi


# Process each file one by one
for input_file in "${files[@]}"; do

    if [ ! -f "$input_file" ]; then
        continue
    fi

    # Do not process already converted MP3 files
    if [[ "$input_file" == *-converted.mp3 ]]; then
        echo "Skipping already converted file: $input_file"
        continue
    fi

    echo
    echo "Processing $input_file..."


    # Step 1: Rename the file (if needed)
    renamed_file=$(rename_file "$input_file")

    if [ $? -ne 0 ]; then
        echo "Skipping $input_file due to rename error."
        read -t 3 -p "Press Enter to continue to the next file, or wait 3 seconds..."
        continue
    fi


    # Step 2: Standardize the filename (if needed)
    standardized_file=$(standardize_filename "$renamed_file")

    if [ $? -ne 0 ]; then
        echo "Skipping $renamed_file due to standardization error."
        read -t 3 -p "Press Enter to continue to the next file, or wait 3 seconds..."
        continue
    fi


    # Step 3: Determine output filename
    extension="${standardized_file##*.}"

    if [[ "${extension,,}" == "mp3" ]]; then

        # MP3 input → add -converted before .mp3
        mp3_file="${standardized_file%.mp3}-converted.mp3"

    else

        # MP4/WAV/M4A → normal .mp3
        mp3_file="${standardized_file%.*}.mp3"

    fi


    # Step 4: Convert to MP3 128 kbps
    if ffmpeg -y -i "$standardized_file" -b:a 128k "$mp3_file"; then
        echo "Converted:"
        echo "  $standardized_file"
        echo "       ↓"
        echo "  $mp3_file"
    else
        echo "Failed to convert $standardized_file"
        read -t 3 -p "Press Enter to continue to the next file, or wait 3 seconds..."
        continue
    fi


    read -t 3 -p "Press Enter to continue to the next file, or wait 3 seconds..."

done


echo
echo "All files processed."
