#!/usr/bin/env bash

wineqgui-delete-prefix()
{
    local configdir
    local prefixdir
    local selection
    local file
    local name
    local exe
    local prefix
    local action
    local entries=()

    configdir="${HOME}/.local/share/wineqgui/applications"
    prefixdir="${HOME}/.local/share/wineqgui/prefixes"

    mkdir -p "$configdir"
    mkdir -p "$prefixdir"


    # -------------------------------------------------
    # Finn registrerte applikasjonar
    # -------------------------------------------------

    while IFS= read -r -d '' file; do

        name=$(read_config_value "$file" "NAME")

        [[ -z "$name" ]] && continue

        entries+=("$name" "$file")

    done < <(
        find "$configdir" \
            -maxdepth 1 \
            -type f \
            -name '*.conf' \
            -print0
    )


    # -------------------------------------------------
    # Ingen registrerte program
    # -------------------------------------------------

    if (( ${#entries[@]} == 0 )); then
        show_info "There are no WineQGUI applications to remove."
        return
    fi


    # -------------------------------------------------
    # Vel program
    # -------------------------------------------------

    selection=$(
        qarma --list \
            --title="Remove Wine application" \
            --width=600 \
            --height=400 \
            --hide-header \
            --column="Application" \
            --column="Config" \
            "${entries[@]}"
    )

    [[ -z "$selection" ]] && return


    # -------------------------------------------------
    # Les informasjon
    # -------------------------------------------------

    name=$(read_config_value "$selection" "NAME")
    exe=$(read_config_value "$selection" "EXE")
    prefix=$(read_config_value "$selection" "PREFIX")


    # -------------------------------------------------
    # Vel kva som skal fjernast
    # -------------------------------------------------

    action=$(
        qarma --list \
            --radiolist \
            --hide-header \
            --title="Remove ${name}" \
            --width=540 \
            --height=320 \
            --column="" \
            --column="Action" \
            TRUE  "Remove from WineQGUI only" \
            FALSE "Remove and delete Wine environment" \
            FALSE "Cancel" \
            --text="<b>${name}</b>\n\nExecutable:\n${exe}"
    )

    case "$action" in

        "Remove from WineQGUI only")

            if qarma --question \
                --title="Remove application" \
                --text="Remove <b>${name}</b> from WineQGUI?"
            then
                rm -f -- "$selection"

                show_info \
                    "<b>${name}</b> has been removed from WineQGUI."
            fi
            ;;


        "Remove and delete Wine environment")

            # Berre slett prefix som WineQGUI sjølv eig
            case "$prefix" in

                "${prefixdir}"/*)
                    ;;

                *)
                    show_error \
                        "WineQGUI will not delete this Wine environment.\n\n"\
                        "It is outside WineQGUI's managed prefix directory:\n\n"\
                        "<b>${prefix}</b>\n\n"\
                        "The application can still be removed from WineQGUI."
                    return
                    ;;
            esac


            # Ekstra kontroll
            if [[ ! -d "$prefix" ]]; then
                show_error \
                    "Wine environment does not exist:\n\n<b>${prefix}</b>"
                return 1
            fi


            # -------------------------------------------------
            # Ekstra stadfesting
            # -------------------------------------------------

            if ! qarma --question \
                --title="Delete Wine environment" \
                --text="<b>Warning</b>\n\n"\
                "This will permanently delete the Wine environment for:\n\n"\
                "<b>${name}</b>\n\n"\
                "${prefix}\n\n"\
                "Continue?"
            then
                return
            fi


            rm -rf -- "$prefix"
            rm -f -- "$selection"

            show_info \
                "<b>${name}</b> and its Wine environment have been deleted."
            ;;


        *)
            return
            ;;
    esac
}
