#!/usr/bin/env fish

set d_dest /torrents/Kim
set d_tmp /torrents/Data/tmp
set d_media /torrents/Data/Media

function rename_subtitle -a f f_dest
    set -f f_base (basename "$f_dest" | string split -r -m1 .)[1]
    set -f dots (math (string split "." (basename "$f") | count) -1 )
    if test $dots -eq 1
        set -f f_ext (basename "$f" | string split -r -m1 .)
        echo $f_base.$f_ext[2]
    else
        set -f f_ext (basename "$f" | string split -r -m4 .)
        echo $f_base.$f_ext[2].$f_ext[3]
    end
end

function print_color -a message color
    echo (set_color $color)"$message"(set_color normal)
end

function separator
    echo
    print_color _________ yellow
    echo
end

function mv_file -a src dst
    if test ! -e $dst
        print_color Moving green
        echo $src
        print_color Into
        echo $dst
        separator
        mv "$src" "$dst"
    end
end

function zip_movie -a d_movie
    set movie_orig_name (basename "$d_movie")
    set movie_year (string match -r "\(\d{4}\)" $movie_orig_name)
    set movie_name (string lower $movie_orig_name \
        | string replace -a a 4 \
        | string replace -a e 3 \
        | string replace -a i 1 \
        | string replace -a o 0\
        | string trim)

    print_color Processing green
    echo $movie_orig_name
    print_color "Encrypted name" green
    echo $movie_name
    separator

    for f in (find "$d_movie" -maxdepth 1 -type f)
        set episode (string match -r "S\d{2}E\d{2}" (basename "$f"))
        set -l f_ext (basename $f | string split -r -m1 .)[2]

        if test -n "$movie_year"
            set f_dest $d_movie/$movie_name.$f_ext

        else if test -n "$episode"
            set episode (string match -r "S\d{2}E\d{2}" (basename "$f"))
            set f_dest $d_movie/$movie_name $episode.$f_ext

        else
            set f_dest $d_movie/$movie_name.$f_ext
        end

        if test ! "$f_ext" = srt
            mv_file "$f" "$f_dest"

        else if test "$f_ext" = srt
            set s_dest $d_movie/(rename_subtitle "$f" "$f_dest")
            mv_file "$f" "$s_dest"
        end
    end

    set -f d_original (find  "$d_media" -mindepth 2 -type d -name "$movie_orig_name")
    set -f f_subtitles (find "$d_movie" -mindepth 1 -name \*.srt)

    if test \( -z "$f_subtitles" \) -a \( -d "$d_original" \)
        print_color "No subtittles found in the tmp directory..." red
        print_color "searching in the original..." green

        for f in (find "$d_original" -mindepth 1 -name \*.srt)
            function cp_file -a src dst
                print_color Found green
                echo $src
                print_color Copying green
                echo $dst
                cp "$src" "$dst"
                separator
            end

            if test -n "$episode"
                if test (string match "*$episode*" (basename "$f"))
                    set s_dest $d_movie/(rename_subtitle "$f" "$f_dest")
                    cp_file "$f" "$s_dest"
                end
            else
                set s_dest $d_movie/(rename_subtitle "$f" "$f_dest")
                cp_file "$f" "$s_dest"
            end
            separator
        end
    end

    print_color "Compressing the files" green
    echo $d_movie/*
    print_color "Into the zip file" green
    echo $d_dest/$movie_name.zip

    7z a -ppredator6969 $d_dest/$movie_name.zip $d_movie/*

    separator
    # GHEKRE
    # 7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on -mhe=on -ptits6969
end

if test -e $d_tmp
    for d_movie in (find $d_tmp -mindepth 1 -type d)
        zip_movie $d_movie
    end
    trash-put $d_tmp
end
