#!/bin/bash

# A config file helper for Welcome's Personal Commands.
# User can drag&drop e.g. .desktop or document files onto this window.
#
# Assumes config file's welcome_own_commands variable has
# the ending parenthesis ')' as the FIRST and ONLY character on a line.

DndDIE() {
    echo "Error: $1" >&2
    exit 1
}

source /usr/share/endeavouros/scripts/eos-script-lib-yad || DndDIE "source failed."

export -f eos_yad

DndCreateConfigFile() {
    if [ ! -r "$configfile" ] ; then
        cat <<EOF > $configfile
#!/bin/bash

local welcome_own_commands=(
    # --field=" Pamac!system-software-install!GUI package manager":fbtn  'pamac-manager'
)
# Note: leave the ')' character above as the first and only character on that line!
EOF
    fi
}

DndAddField() {
    local configfile="$HOME/.config/welcome-own-cmds.conf"
    local mode=""

    # Check definitions (old or new). If you have both old and new definitions, then assume new.

    if [ -r "$configfile" ] ; then
        /usr/bin/grep "^[ \t]*local[ \t][ \t]*welcome_own_commands" "$configfile" && mode=old
        /usr/bin/grep "^[ \t]*personal_commands_init"               "$configfile" && mode=new
        /usr/bin/grep "^[ \t]*personal_commands_add"                "$configfile" && mode=new
    fi

    case "$mode" in
        old)
            local field="  --field=' $name!$icon!$description':fbtn '$command'"
            # make sure the config file exists in a proper format
            DndCreateConfigFile
            # add a field line in the end of variable welcome_own_commands
            sed -i "$configfile" -e "/^)$/i\  $field"
            ;;
        *)
            # new or empty
            cat <<EOF >> "$configfile"

personal_commands_add "$command" \\
                      "$name" \\
                      "$icon" \\
                      "$description"
EOF
            ;;
    esac
}

DndDesktopFile() {
    local file="${arg#file://}"
    local icon="$(grep "^Icon=" "$file" | head -n 1 | cut -d '=' -f 2)"
    local name="$(grep "^Name=" "$file" | head -n 1 | cut -d '=' -f 2)"
    local generic_name="$(grep "^GenericName=" "$file" | head -n 1 | cut -d '=' -f 2)"
    local comment="$(grep "^Comment=" "$file" | head -n 1 | cut -d '=' -f 2)"
    local description="Description not available."
    local command="xdg-open '$file'"

    if [ -n "$generic_name" ] ; then
        description="$generic_name"
    elif [ -n "$comment" ] ; then
        description="$comment"
    fi

    DndAddField
}

DndDocumentFile() {
    local file="${arg#file://}"
    local desktopfile="$(xdg-mime query default $(xdg-mime query filetype "$file"))"
    local dt2="$(find /usr/share/applications -name "$desktopfile")"
    test -n "$dt2" || dt2="$(find "$HOME/.local/share/applications" -name "$desktopfile")"
    case "$file" in
        /*) ;;
        *) file="$PWD/$file" ;;
    esac
    local ending="${file##*.}"
    local name="$(basename "$file")"
    local description="A $ending file"
    local command="xdg-open '$file'"
    local icon="$(grep "^Icon=" "$dt2" | head -n 1 | cut -d '=' -f 2)"

    DndAddField
}

DndUrl() {
    local file="${arg}"
    local name="$file"
    local description="A URL"
    local command="xdg-open '$file'"
    local icon="web-browser"
    #local desktopfile="$(xdg-mime query default $(xdg-mime query filetype $file))"

    DndAddField
}

DndServerStart() {
    local handler="command=/usr/bin/$progname"

    if [ -n "$(ps -ef | grep "$handler" | grep -v "grep $handler")" ] ; then
        echo "$progname server is already running!" >&2
        return
    fi

    local text="Drop item(s) into this window:\n"
    text+="-  URL shortcut from the address line of a web browser\n"
    text+="-  document and file shortcuts from a file manager\n"
    text+="-  .desktop file (=launcher) shortcut from the start menu"

    local yadcmd=(eos_yad --dnd
                  --$handler
                  --title="Welcome: Personal Commands drag & drop support"
                  --text="$text"
                  --width=500 --height=200
                  # --posx=5 --posy=5
                 )

    "${yadcmd[@]}" &
}

RestartWelcomeAndDndServer() {
    local progname2=eos-welcome
    local restart_welcome=no

    pkill -f "$progname2" && restart_welcome=yes
    pkill -f "/usr/bin/yad --window-icon=$EOS_WICON"
    sleep 0.5
    if [ "$restart_welcome" = "yes" ] ; then
        $progname2 &
        sleep 1
    fi
    DndServerStart
}

DndMain()
{
    local progname=welcome-dnd

    if [ "$1" = "" ] ; then
        DndServerStart
        return
    fi

    local arg

    for arg in "$@" ; do
        echo "Dropping '$arg'." >&2
        case "$arg" in
            https://* | http://*)  DndUrl ;;
            *.desktop)             DndDesktopFile ;;
            /* | file://*)         DndDocumentFile ;;
            *)                     DndDIE "'$arg' not supported." ;;
        esac
        sleep 0.5
    done

    RestartWelcomeAndDndServer
}

DndMain "$@"
