#!/bin/sh 

show_help(){
cat << EOF
This is a very simple script to convert a .srt file into a .vtt

At first glance, the difference is that .vtt starts with WEBVTT and a newline,
and that the separators for seconds are dots instead of komma.

Parameters
    -h, --help  Show this help message

    FILE        The file to convert The file needs to end with .srt and will output a file of the same name, but with .vtt extention.

Usage
    ./srt_to_vtt.sh FILE

For a bulk, you can do
    for f in *.srt; do ./srt_to_vtt.sh $f; done

EOF
exit 0
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
show_help
fi

SRT_FILE="$1"
VTT_FILE=$(echo "$SRT_FILE" | sed -E 's/\.srt$/.vtt/g')

if [ "$SRT_FILE" = "$VTT_FILE" ]
then
echo 'ERROR: PLEASE PROVIDE A .srt FILE, or provide the --help parameter for help.'
exit 1
fi

echo 'WEBVTT' > "$VTT_FILE"
echo '' >> "$VTT_FILE"

cat "$SRT_FILE" | sed -E 's/^([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3} --> [0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3})/\1.\2.\3/g' >> "$VTT_FILE"
