#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
el_make_environment
. gettext.sh
TEXTDOMAIN="elive-tools"
export TEXTDOMAIN

main(){
    # dependencies {{{
    if ! el_dependencies_check "yt-dlp" ; then
        el_dependencies_install "yt-dlp"
    fi
    if ! el_dependencies_check "urxvt" ; then
        el_dependencies_install "rxvt-unicode"
    fi

    # }}}

    download_dir="$( xdg-user-dir DOWNLOAD )/Videos"
    mkdir -p "$download_dir"
    cd "$download_dir"

    # messages {{{
    local message_finished
    message_finished="$( printf "$( eval_gettext "Download Finished" )" "" )"
    local message_download_where
    message_download_where="$( printf "$( eval_gettext "Your download has been completed in:" )" "" )\n${download_dir}"
    local message_failed
    message_failed="$( printf "$( eval_gettext "Download Failed" )" "" )"
    local message_failed_where
    message_failed_where="$( printf "$( eval_gettext "Your download has failed, try to download manually using the command yt-dlp" )" "" )"
    local message_delete
    message_delete="$( printf "$( eval_gettext "Delete" )" "" )"
    local message_keep
    message_keep="$( printf "$( eval_gettext "Keep" )" "" )"
    local message_what_to_do
    message_what_to_do="$( printf "$( eval_gettext "Download failed, what do you want to do with the partially downloaded files?" )" "" )"
    local message_open_directory
    message_open_directory="$( printf "$( eval_gettext "Open" )" "" )"
    local message_nothing_title
    message_nothing_title="$( printf "$( eval_gettext "Nothing to download" )" "" )"
    local message_nothing_message
    message_nothing_message="$( printf "$( eval_gettext "Nothing given to download, you should use this tool with a URL argument." )" "" )"


    # - messages }}}

    if [[ -z "$1" ]] ; then
        el_notify normal dialog-error "${message_nothing_title}" "${message_nothing_message}"
        exit
    fi

    ( urxvt -e bash -c "
    #!/bin/bash
    source /usr/lib/elive-tools/functions

    # trap ctrl-c and call ctrl_c()
    trap ctrl_c INT

    function ctrl_c() {
        rm -rf \"${download_dir}/tmp-$$\" || true
    }

    mkdir -p \"${download_dir}/tmp-$$\" ; cd \"${download_dir}/tmp-$$\"

    if echo \"$@\" | grep -qsE \"(youtu.be|youtube.com)\" ; then
        output_filename=\"%(title)s (%(id)s) [%(channel)s] - [%(duration>%Hh%Mm)s - %(view_count>%d)s views - %(upload_date>%Y-%m-%d)s] - [%(vcodec)s+%(acodec)s].%(ext)s\"
        # append playlist number if on a playlist
        if echo \"$@\" | grep -qsE \"(list=)\" ; then
            output_filename=\"%(playlist_index)02d - \$output_filename\"
        fi
    else
        output_filename=\"%(title)s (%(id)s) - [%(duration>%Hh%Mm)s - %(upload_date>%Y-%m-%d)s] - [%(vcodec)s+%(acodec)s].%(ext)s\"
    fi


    if test -n \"$1\" ; then
        for try in \$(seq 6)
        do
            if yt-dlp \
                -f \"bestvideo[ext=mp4][height=720]+bestaudio[ext=m4a]/bestvideo[ext=mp4][height=480]+bestaudio[ext=m4a]/bestvideo[ext=mp4][height>=720]+bestaudio[ext=m4a]/best[ext=mp4]/best/b\" \
                --remux-video \"mp4/mkv/webm/mov\" --no-keep-video \
                -o \"\$output_filename\" \
                --embed-metadata --embed-chapters \
                \"$@\" ; then
                is_downloaded=1
                break
            else
                sleep 1
                mkdir -p \"${download_dir}/tmp-$$\" ; cd \"${download_dir}/tmp-$$\"
                if yt-dlp \
                    -f \"bestvideo[ext=mp4][height=720]+bestaudio[ext=m4a]/bestvideo[ext=mp4][height=480]+bestaudio[ext=m4a]/bestvideo[ext=mp4][height>=720]+bestaudio[ext=m4a]/best[ext=mp4]/best/b\" \
                    --remux-video \"mp4/mkv/webm/mov\" --no-keep-video \
                    -o \"\$output_filename\" \
                    --embed-metadata --embed-chapters \
                    -o \"%(id)s.%(ext)s\" \
                    \"$@\" ; then
                    is_downloaded=1
                    break
                else
                    mkdir -p \"${download_dir}/tmp-$$\" ; cd \"${download_dir}/tmp-$$\"
                    if yt-dlp \
                        -i \"$@\" ; then
                        is_downloaded=1
                        break
                    fi
                fi
            fi
        done
    fi

    if ((is_downloaded)) ; then
        cd \"${download_dir}\"
        find \"${download_dir}/tmp-$$/\" -type f \( ! -iname '*.part' -a ! -iname '*.ytdl' \) -exec mv {} \"${download_dir}/\" \;
        rmdir \"${download_dir}/tmp-$$\" 2>/dev/null || true
        result=\"\$( el_notify important emblem-downloads '${message_finished}' '${message_download_where}' --action=open='${message_open_directory}' )\"
        if [[ \"\$result\" = \"open\" ]] ; then
            thunar \"$download_dir\"
        fi
    else
        el_notify normal dialog-error '${message_failed}' '${message_failed_where}'

        if zenity --question --cancel-label='${message_delete}' --ok-label='${message_keep}' --text='${message_what_to_do}' ; then
            thunar '${download_dir}/tmp-$$'
        else
            rm -rf \"${download_dir}/tmp-$$\"
        fi
    fi

    echo
    echo done

    sleep 8
    " & )

}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
