#!/bin/sh
# Usage: fixsttl media_file
# for a media file, convert its .locale.srt, if any, to plain text .srt

# can be overriden STTL_LANG=da-DK, etc
STTL_LANG=${STTL_LANG:-en-GB}

main() {
        local ext froot srt
        [ -n "$1" ] || exit
        # any other extensions?
        for ext in mp4 mpg mkv; do
                froot=${1%.$ext}
                [ "$1" != "$froot" ] && break
        done
        [  "$1" = "$froot" ] && return 1
        srt=${froot}.${STTL_LANG}.srt
        [ -r "$srt" ] || return
        # *.en-GB.srt -> *.srt
        iconv -f UTF-8 -t LATIN1 "$srt" |
                # strip <tags> and </tags>
                sed -r -e 's@<[/a-zA-Z]+( [^>]*)?>@@g' > "${froot}.srt" &&
                { rm -f -- "$srt"; return 0; }
}

main "$@"
