#!/usr/bin/env bash

wineqgui-open-exe()
{
    local exe
    local action
    local name
    local prefix_choice
    local prefix
    local configdir
    local prefixdir
    local safe_name
    local configfile

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

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

    # -------------------------------------------------
    # Vel EXE-fil
    # -------------------------------------------------

    exe=$(
        qarma --file-selection \
            --title="Select Windows executable" \
            --file-filter="Windows executable (*.exe) | *.exe" \
            --file-filter="All files | *"
    )

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

    if [[ ! -f "$exe" ]]; then
        show_error "The selected file does not exist."
        return 1
    fi


    # -------------------------------------------------
    # Start berre eller legg til i WineQGUI
    # -------------------------------------------------

    action=$(
        qarma --list \
            --radiolist \
            --hide-header \
            --title="Open executable" \
            --width=440 \
            --height=270 \
            --column="" \
            --column="Action" \
            TRUE  "Launch only" \
            FALSE "Add to WineQGUI" \
            FALSE "Cancel"
    )

    case "$action" in

        "Launch only")
            WINEPREFIX="${HOME}/.wine" wine "$exe" &
            return
            ;;

        "Add to WineQGUI")
            ;;

        *)
            return
            ;;
    esac


    # -------------------------------------------------
    # Namn på programmet
    # -------------------------------------------------

    name=$(
        qarma --entry \
            --title="Application name" \
            --text="Name for this application:" \
            --entry-text="$(basename "$exe" .exe)"
    )

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


    # -------------------------------------------------
    # Lag trygt filnamn
    # -------------------------------------------------

    safe_name=$(
        printf '%s' "$name" |
        tr '[:upper:]' '[:lower:]' |
        tr ' ' '-' |
        tr -cd 'a-z0-9._-'
    )

    [[ -z "$safe_name" ]] && safe_name="application"


    # -------------------------------------------------
    # Vel Wine-miljø
    # -------------------------------------------------

    prefix_choice=$(
        qarma --list \
            --radiolist \
            --hide-header \
            --title="Wine environment" \
            --width=520 \
            --height=320 \
            --column="" \
            --column="Option" \
            TRUE  "Use default Wine environment" \
            FALSE "Create a new environment for this application" \
            FALSE "Advanced: choose existing environment" \
            FALSE "Cancel" \
            --text="<b>Choose how WineQGUI should run this application.</b>"
    )

    case "$prefix_choice" in

        "Use default Wine environment")
            prefix="${HOME}/.wine"
            ;;


        "Create a new environment for this application")

            prefix="${prefixdir}/${safe_name}"

            if [[ -e "$prefix" ]]; then
                show_error \
                    "An environment for <b>${name}</b> already exists."
                return 1
            fi

            mkdir -p "$prefix"

            (
                echo "10"
                echo "# Creating Wine environment..."

                WINEPREFIX="$prefix" wineboot -u >/dev/null 2>&1

                echo "100"
                echo "# Wine environment created."
            ) |
            qarma --progress \
                --title="Creating Wine environment" \
                --text="Preparing Wine..." \
                --percentage=0 \
                --auto-close

            if [[ ! -f "${prefix}/system.reg" ]]; then
                show_error \
                    "Wine could not create the application environment."
                return 1
            fi
            ;;


        "Advanced: choose existing environment")

            prefix=$(
                qarma --file-selection \
                    --directory \
                    --title="Select existing Wine environment"
            )

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

            if [[ ! -d "${prefix}/drive_c" || ! -f "${prefix}/system.reg" ]]; then
                show_error \
                    "The selected directory does not appear to be a valid Wine environment."
                return 1
            fi
            ;;


        *)
            return
            ;;
    esac


    # -------------------------------------------------
    # Lagre applikasjonen
    # -------------------------------------------------

    configfile="${configdir}/${safe_name}.conf"

    {
        printf 'NAME=%s\n' "$name"
        printf 'EXE=%s\n' "$exe"
        printf 'PREFIX=%s\n' "$prefix"
        printf 'WINE=%s\n' "wine"
        printf 'ARGS=%s\n' ""
        printf 'GAMEMODE=%s\n' "0"
    } > "$configfile"

    show_info \
        "<b>${name}</b> has been added to WineQGUI."
}
