#!/bin/sh

# It renames the .srt files in a directory to match the .mp4 movie names (since those are the formats I usually need).
# If the subtitles are in a subfolder, the script should still work as long as there's only one .srt file.
# It makes the subs discoverable by virtually any video player.
# Optionally takes a language arguments e.g. en to rename to .en.srt

for f in *.mp4 *.mkv; do
    if [ -f "$f" ]; then
        h=$(basename "$f" .mp4)
        echo "Renaming the subtitle (if any) accompanying $h.mp4..."
        n=$(find . -maxdepth 2 -name "*.srt" -printf '.' | wc -m)
        if [ $n -eq 1 ] && ! [ -f "$h.srt" ]; then
            # Check if language was given
            [ -z $1 ] && ext="srt" || ext="$1.srt"
            find . -maxdepth 2 -name "*.srt" -exec mv "{}" "$h.$ext" \;
            echo "Done!"
        else
            echo "Unable to find unique srt file that needs to be renamed. It might already have the proper name."
        fi
    fi
done