#!/bin/bash


walk_dir () {    
    shopt -s nullglob dotglob
    for pathname in "$1"/*; do
        if [ -d "$pathname" ]; then
            walk_dir "$pathname"
        else
            my_dir_name=$(dirname "$(readlink -f "$pathname")")
            my_basename=$(basename "$pathname")
            my_basename_no_ext="${pathname%.*}"
            my_audio_out="$my_basename_no_ext.out.srt"
            case "$pathname" in
                *.avi|*.mkv|*.mp4)
                    if [ ! -f "$my_basename_no_ext.ok" ]; then
                        echo "my_basename: $my_basename, my_basename_no_ext: $my_basename_no_ext, pathname: $pathname, my_dir_name: $my_dir_name, my_audio_out: $my_audio_out "
                        mkdir -p "$my_dir_name"/archive
                        if [ -f "$my_basename_no_ext.en.srt" ]; then    # no en sub available
                            # if alass "$pathname" "$my_basename_no_ext.en.srt" "$my_basename_no_ext.en2.srt"; then # If alass worked ok
                            if [ ! -f "$my_audio_out" ]; then 
                                alass "$my_basename_no_ext.mkv" "_" "$my_audio_out"; # Extracts Audio File
                            fi
                            if alass "$my_audio_out" "$my_basename_no_ext.en.srt" "$my_basename_no_ext.en2.srt"; then
                                mv "$my_basename_no_ext.en.srt" "$my_dir_name"/archive
                                mv "$my_basename_no_ext.en2.srt" "$my_basename_no_ext.en.srt"
                                if [ -f "$my_basename_no_ext.fr.srt" ]; then    # no fr sub available
                                    if alass "$my_basename_no_ext.en.srt" "$my_basename_no_ext.fr.srt" "$my_basename_no_ext.fr2.srt"; then #il alass worked ok
                                        mv "$my_basename_no_ext.fr.srt" "$my_dir_name"/archive
                                        mv "$my_basename_no_ext.fr2.srt" "$my_basename_no_ext.fr.srt"
                                    fi
                                fi
                                touch "$my_basename_no_ext.ok"
                            fi
                        elif [ -f "$my_basename_no_ext.fr.srt" ]; then 
                            if [ ! -f "$my_audio_out" ]; then 
                                alass "$my_basename_no_ext.mkv" "_" "$my_audio_out" # Extracts Audio File
                            fi
                            if alass "$my_audio_out" "$my_basename_no_ext.fr.srt" "$my_basename_no_ext.fr2.srt"; then #il alass worked ok
                                mv "$my_basename_no_ext.fr.srt" "$my_dir_name"/archive
                                mv "$my_basename_no_ext.fr2.srt" "$my_basename_no_ext.fr.srt"
                                touch "$my_basename_no_ext.ok"
                            fi 
                        fi
                    fi
                ;;
            esac
        fi
    done
}

alass_run () {    
    if alass "$1" "$2" "$3"; then # If alass worked ok
        exit 0
    else
        exit 1
    fi
}

mydir=$1

walk_dir "$mydir"
