#!/usr/bin/env bash
# Author: Castro-Fidel (linux-gaming.ru)
# shellcheck disable=SC2140,SC2034,SC2068,SC2206
########################################################################
$PW_DEBUG
print_error () { printf "\E[31m%s Error: $@ %s\e[0m\n" ;}
export -f print_error

print_warning () { printf "\E[33m%s Warning: $@ %s\e[0m\n" ;}
export -f print_warning

print_info () { printf "\E[36m%s Info: $@ %s\e[0m\n" ;}
export -f print_info

print_ok () { printf "\E[35m%s OK: $@ %s\e[0m\n" ;}
export -f print_ok

print_var () { for vp in $@ ; do print_info "${vp}=${!vp}" ; done ;}
export -f print_var

check_variables () { [[ -z ${!1} ]] && export $1="$2" ;}

add_to_var () {
    if ! echo ${!1} | grep "$2" &>/dev/null
    then export $1="${!1} $2"
    fi
}

rm_from_var () {
    if echo ${!1} | grep "$2" &>/dev/null
    then export $1="$(echo "${!1//$2/}" | tr -s " ")"
    fi
}

fatal () {
    print_error "$@"
    exit 1
}

change_locale () {
    if [[ -f "${PORT_WINE_TMP_PATH}/PortProton_loc" ]] ; then
        LANGUAGE=$(<"${PORT_WINE_TMP_PATH}/PortProton_loc")
        # TODO: drop legacy check language after 01.01.2025
        if [[ "$LANGUAGE" == "RUS" ]] ; then
            LANGUAGE="ru"
            echo "ru" > "${PORT_WINE_TMP_PATH}/PortProton_loc"
        elif [[ "$LANGUAGE" == "ENG" ]] ; then
            LANGUAGE="en"
            echo "en" > "${PORT_WINE_TMP_PATH}/PortProton_loc"
        fi
    else
        [[ ! -f "${pw_yad}" ]] && pw_yad="yad"
        SET_LANG=(
            "${pw_yad}" --list
            --window-icon="$PW_GUI_ICON_PATH/portproton.svg"
            --title="Select language for PortProton"
            --column="Language" --column=""
            --width=400 --height=200

            "Russian (Русский)"                     ru
            "English"                               en
            "Spanish (Español・Española)"            es
        )

        out=$("${SET_LANG[@]}")
        if [[ -z $out ]] ; then
            fatal "Language is not set!"
        else
            LANGUAGE=$(echo "$out" | cut -d '|' -f2)
            echo "${LANGUAGE}" > "${PORT_WINE_TMP_PATH}/PortProton_loc"
        fi
    fi
    export LANGUAGE

    case "$LANGUAGE" in
        ru)
            check_start_from_steam && export LC_ALL=ru_RU.UTF-8
            export FULL_LN="russian"
            export LANGUAGE_GIF="ru"
            ;;
        es)
            check_start_from_steam && export LC_ALL=es_ES.UTF-8
            export FULL_LN="spanish"
            export LANGUAGE_GIF="en"
            ;;
         *)
            export FULL_LN="english"
            export LANGUAGE_GIF="en"
            ;;
    esac
}
export -f change_locale

generate_pot () {
    for lang in $LANGUAGES_LIST
    do
        echo "${lang}"
        create_new_dir "${PORT_WINE_PATH}/data/locales/${lang}/LC_MESSAGES/"
        LANG_PO="${PORT_WINE_PATH}/data/locales/${lang}/LC_MESSAGES/PortProton.po"
        LANG_MO="${PORT_WINE_PATH}/data/locales/${lang}/LC_MESSAGES/PortProton.mo"
        TEMPLATE_POT="${PORT_WINE_PATH}/data/locales/PortProton.pot"
        pushd "${PORT_WINE_PATH}" 1>/dev/null || fatal
        xgettext --no-location --from-code=UTF-8 --language Shell -i \
        "data/scripts/start.sh" \
        "data/scripts/setup.sh" \
        "data/scripts/functions_helper" \
        -o "$TEMPLATE_POT"
        popd 1>/dev/null || fatal
        if [[ -f "$LANG_PO" ]] ; then
            echo "update $LANG_PO file"
            msgmerge --update "$LANG_PO" "$TEMPLATE_POT"
        else
            echo "create $LANG_PO file"
            msginit --input="$TEMPLATE_POT" --locale="${lang}" --output="$LANG_PO"
        fi
        msgfmt --output-file="$LANG_MO" "$LANG_PO"
    done
}
export -f generate_pot

eval_gettext () {
  gettext "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
}

try_copy_file () {
    if [[ ! -f "$1" ]] ; then print_info "file $1 not found for copy" && return 1
    elif [[ -z "$2" ]] ; then print_error "no way to copy file $1" && return 1
    elif [[ -L "$2" ]] ; then print_warning "$2 is a file with a symbolic link"
        try_remove_file "$2"
        cp -f "$1" "$2" && return 0 || return 1
    elif [[ -f "$2.sha256sum" ]] ; then print_warning "$2 this file has sha256sum"
        try_remove_file "$2"
        try_remove_file "$2.sha256sum"
        cp -f "$1" "$2" && return 0 || return 1
    else
        [[ -e "$2/$(basename "$1")" ]] && rm -f "$2/$(basename "$1")"
        cp -f "$1" "$2" && return 0 || return 1
    fi
}
export -f try_copy_file

try_copy_file_with_checksums () {
    if [[ ! -f "$1" ]] ; then print_info "file $1 not found for copy" && return 1
    elif [[ -z "$2" ]] ; then print_error "no way to copy file $1" && return 1
    elif [[ -L "$2" ]] ; then print_warning "$2 is a file with a symbolic link"
        try_remove_file "$2"
        if cp -f "$1" "$2" ; then
            if [[ "${PW_FILESYSTEM}" == "ext2/ext3" ]] \
            || [[ "${PW_FILESYSTEM}" == "f2fs" ]] \
            || [[ "${PW_FILESYSTEM}" != "btrfs" ]]
            then
                checksum1="$(sha256sum "$1")"
                echo "${checksum1// */}" > "$2.sha256sum"
            fi
            return 0
        else
            return 1
        fi
    else
        if [[ "${PW_FILESYSTEM}" == "ext2/ext3" ]] \
        || [[ "${PW_FILESYSTEM}" == "f2fs" ]] \
        || [[ "${PW_FILESYSTEM}" != "btrfs" ]]
        then
            checksum1="$(sha256sum "$1")"
            if [[ ! -f "$2" ]] ; then
                if cp -f "$1" "$2" ; then
                    echo "${checksum1// */}" > "$2.sha256sum"
                    return 0
                else
                    return 1
                fi
            else
                if [[ ! -f "$2.sha256sum" ]] ; then
                    checksum2="$(sha256sum "$2")"
                    echo "${checksum2// */}" > "$2.sha256sum"
                else
                    checksum2="$(<"$2.sha256sum")"
                fi
            fi
            if [[ "${checksum1// */}" == "${checksum2// */}" ]] ; then
                return 0
            else
                try_remove_file "$2"
                if cp -f "$1" "$2" ; then
                    echo "${checksum1// */}" > "$2.sha256sum"
                    return 0
                else
                    return 1
                fi
            fi
        else
            [[ -e "$2/$(basename "$1")" ]] && rm -f "$2/$(basename "$1")"
            cp -f "$1" "$2" && return 0 || return 1
        fi
    fi
}
export -f try_copy_file_with_checksums

try_copy_dir () {
    if [ ! -d "$1" ] ; then print_info "directory $1 not found for copy"
    elif [ -z "$2" ] ; then print_error "no way to copy directory $1"
    else
        cp -fr "$1" "$2"
        [ "$?" != 0 ] && print_error "failed to copy directory $1 to $2" || return 0
    fi
    return 1
}
export -f try_copy_dir

try_remove_file () {
    if [ -f "$1" ] || [ ! -e "$1" ] ; then
        rm -f "$1"
        [ "$?" == 0 ] && return 0 || return 1
    fi
}
export -f try_remove_file

try_remove_dir () {
    if [ -d "$1" ] ; then
        rm -fr "$1"
        [ "$?" == 0 ] && return 0 || return 1
    fi
}
export -f try_remove_dir

create_new_dir () {
    if [ ! -d "$1" ] ; then
        mkdir -p "$1"
    fi
    return 0
}

try_force_link_file () {
    if [[ ! -f "$1" ]] ; then print_warning "file not found for link: $1" && return 1
    elif [[ -z "$2" ]] ; then print_error "no way to link file $1" && return 1
    elif [[ -f "$2.sha256sum" ]] ; then print_warning "$2 this file has sha256sum"
        try_remove_file "$2"
        try_remove_file "$2.sha256sum"
        ln -s -f -r "$1" "$2"
        return 0
    else
        try_remove_file "$2"
        ln -s -f -r "$1" "$2"
        return 0
    fi
    return 1
}
export -f try_force_link_file

check_symlink () {
    CHK_SYMLINK_FILE="$(file "$1")"
    if [[ ! -z "$(echo "$CHK_SYMLINK_FILE" | grep -v "broken" | grep "symbolic link to" | awk '{print $1}')" ]] ; then
        return 0
    elif [[ ! -z "$(echo "$CHK_SYMLINK_FILE" | grep "broken symbolic link to" | awk '{print $1}')" ]] ; then
        print_error "remove broken symlink: $CHK_SYMLINK_FILE"
        rm -fr "$CHK_SYMLINK_FILE"
        return 1
    else
        return 1
    fi
}
export -f check_symlink

try_force_link_dir () {
    if [ ! -d "$1" ] ; then print_info "directory $1 not found for link"
    elif [ -z "$2" ] ; then print_error "no way to link directory $1"
    else
        ln -s -f -r "$1" "$2"
        [ "$?" != 0 ] && print_error "failed to link directory $1 to $2" || return 0
    fi
    return 1
}
export -f try_force_link_dir

check_process () {
    [ ! -n "$(ps cax | grep "$1" | awk '{print $1}')" ] && return 0 || return 1
}
export -f check_process

try_check_sha256sum () {
    SHA256SUM_EXT=$(curl --silent -L "${1//.tar*/}.sha256sum" | awk '{print $1}')
    if [[ ${#SHA256SUM_EXT} == 64 ]] ; then
        SHA256SUM_INT=$(sha256sum "$dest" | awk '{print $1}')
        if [[ "$SHA256SUM_EXT" == "$SHA256SUM_INT" ]] ; then
            print_ok "Verification sha256sum was successfully."
            return 0
        else
            print_error "Verification sha256sum was failed."
            return 1
        fi
    else
        print_warning "sha256sum not found for $(basename "$dest") on server. Skip it."
        return 0
    fi
}

try_download () {
    export dest="$2"
    local silent="false"
    local no_mirror="false"
 
    case "$3" in
           silent)
                silent=true ;;
        no_mirror)
                no_mirror=true
    esac

    if [[ -n "${PW_AUTOINSTALL_EXE}" ]] \
    && [[ "$no_mirror" == "true" ]]
    then
        local filename="$(basename "${PW_AUTOINSTALL_EXE}")"
    else
        local filename="$(basename "$1")"
    fi

    if [[ "${MIRROR}" == CLOUD ]] \
    && [[ "$no_mirror" != "true" ]]
    then
        FIRST_URL=("$url_cloud/$filename")
        SECOND_URL=($1)
    else
        FIRST_URL=($1)
        SECOND_URL=("$url_cloud/$filename")
    fi

    [[ ! -f "${pw_yad}" ]] && local pw_yad="yad"

    # If gamescope session is active, use PW_TERM for downloading
    if check_gamescope_session ; then
        $PW_TERM curl -f -# -A 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)' -H 'Cache-Control: no-cache, no-store' \
        -H 'Pragma: no-cache' -L ${FIRST_URL[@]} -o "$dest"
        [ "$?" != 0 ] && return 1 || return 0
    fi

    # Normal download
    print_info "Download $filename from ${FIRST_URL[0]}..."
    set -o pipefail
    if [[ "$silent" == "true" ]] ; then
        curl -f -# -A 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)' -H 'Cache-Control: no-cache, no-store' \
        -H 'Pragma: no-cache' -L ${FIRST_URL[@]} -o "$dest" 2>&1
    else
        curl -f -# -A 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)' -H 'Cache-Control: no-cache, no-store' \
        -H 'Pragma: no-cache' -L ${FIRST_URL[@]} -o "$dest" 2>&1 | \
        tr '\r' '\n' | sed -ur 's|[# ]+||g;s|.*=.*||g;s|.*|#Downloading at &\n&|g' | \
        "$pw_yad" --progress --text="$(gettext "Downloading") $filename" --auto-close --no-escape \
        --auto-kill --text-align="center" --fixed --no-buttons --title "PortProton" --width=500 --height=90 \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --borders="$PROGRESS_BAR_BORDERS_SIZE"
    fi

    if [[ "${PIPESTATUS[0]}" != 0 ]] ; then
        if [[ "$no_mirror" != "true" ]] ; then
            print_warning "Failed download $filename from ${FIRST_URL[0]}, trying mirror: ${SECOND_URL[0]}"
            curl -f -# -A 'Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)' -H 'Cache-Control: no-cache, no-store' \
            -H 'Pragma: no-cache' -L ${SECOND_URL[@]} -o "$dest" 2>&1 | \
            tr '\r' '\n' | sed -ur 's|[# ]+||g;s|.*=.*||g;s|.*|#Downloading at &\n&|g' | \
            "$pw_yad" --progress --text="$(gettext "Downloading") $filename" --auto-close --no-escape \
            --auto-kill --text-align="center" --fixed --no-buttons --title "PortProton" --width=500 --height=90 \
            --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --borders="$PROGRESS_BAR_BORDERS_SIZE"
        fi
        if [[ "${PIPESTATUS[0]}" != 0 ]] ; then
            print_error "Failed to download $filename from mirror. Skipping..."
            return 1
        else
            print_ok "File downloaded successfully: $filename from ${SECOND_URL[0]}"
            if try_check_sha256sum ${SECOND_URL[@]} ; then
                return 0
            else
                try_remove_file "$dest"
                return 1
            fi
        fi
    fi
    print_ok "File downloaded successfully: $filename from ${FIRST_URL[0]}"
    if try_check_sha256sum ${FIRST_URL[@]} ; then
        return 0
    else
        try_remove_file "$dest"
        return 1
    fi
}

var_winedlloverride_update () {
    if [[ "$WINEDLLOVERRIDES" ]]
    then export WINEDLLOVERRIDES="$1;$WINEDLLOVERRIDES"
    else export WINEDLLOVERRIDES="$1"
    fi
    return 0
}

var_vkd3d_config_update () {
    if echo "$VKD3D_CONFIG" | grep "$1"
    then return 0
    elif [[ "$VKD3D_CONFIG" ]]
    then export VKD3D_CONFIG="$1;$VKD3D_CONFIG"
    else export VKD3D_CONFIG="$1"
    fi
    return 0
}

var_radv_perftest_config_update () {
    if echo "$RADV_PERFTEST" | grep "$1"
    then return 0
    elif [[ "$RADV_PERFTEST" ]]
    then export RADV_PERFTEST="$1;$RADV_PERFTEST"
    else export RADV_PERFTEST="$1"
    fi
    return 0
}

var_pw_vk_istance_layers_config_update () {
    if echo "$PW_VK_INSTANCE_LAYERS" | grep "$1"
    then return 0
    elif [[ "$PW_VK_INSTANCE_LAYERS" ]]
    then export PW_VK_INSTANCE_LAYERS="$1:$PW_VK_INSTANCE_LAYERS"
    else export PW_VK_INSTANCE_LAYERS="$1"
    fi
    return 0
}

var_ld_library_path_update () {
    if echo "$LD_LIBRARY_PATH" | grep "$1"
    then return 0
    elif [[ "$LD_LIBRARY_PATH" ]]
    then export LD_LIBRARY_PATH="$1:$LD_LIBRARY_PATH"
    else export LD_LIBRARY_PATH="$1"
    fi
    return 0
}

# GUI NOTIFY SEND
pw_notify_send () {
    if command -v gdbus &>/dev/null ; then
        local app="PortProton"
        local icon=""
        local timeout="5000"
        local title=""
        local body=""

        local OPTIND="1"
        while getopts a:i:t: opt ; do
            case "$opt" in
                a)  
                    local app=$OPTARG
                    ;;
                i)  
                    case "$OPTARG" in
                        info) local OPTARG="${PORT_WINE_PATH}/data/img/gui/notify_info.svg";;
                        warning) local OPTARG="${PORT_WINE_PATH}/data/img/gui/notify_warning.svg";;
                        error) local OPTARG="${PORT_WINE_PATH}/data/img/gui/notify_error.svg";;
                    esac
                    local icon=$OPTARG
                    ;;
                t) 
                    local timeout=$OPTARG
                    ;;
                *) 
                    echo "usage: $0
                    [-a] name application (PortProton default)
                    [-i] info, warning, error (none default)
                    [-t] timeout in ms (5000 default)" >&2
                    return 1
                    ;;
            esac
        done
        shift $(( OPTIND - 1 ))
        [[ "$1" == "--" ]] && shift

        local title="$1"
        shift
        local body="$*"

        gdbus call --session --dest org.freedesktop.Notifications \
            --object-path /org/freedesktop/Notifications \
            --method org.freedesktop.Notifications.Notify \
            "$app" 0 "$icon" "$title" "$body" "[]" "{}" "$timeout" &>/dev/null
        return 0
    fi
}
export -f pw_notify_send

check_start_from_steam () {
    if [[ "${START_FROM_STEAM}" == 1 ]]
    then return 0
    else return 1
    fi
}

check_gamescope_session () {
    if [[ ! -z "$GAMESCOPE_IN_USE" ]]
    then [[ "$GAMESCOPE_IN_USE" == 1 ]] && return 0 || return 1
    fi
    if echo "${DESKTOP_SESSION}" | grep -i "gamescope" &>/dev/null ; then
        export GAMESCOPE_IN_USE=1
        return 0
    else
        export GAMESCOPE_IN_USE=0
        return 1
    fi
}

check_wayland_session () {
    if [[ ! -z "$WAYLAND_IN_USE" ]]
    then [[ "$WAYLAND_IN_USE" == 1 ]] && return 0 || return 1
    fi
    if echo "${XDG_SESSION_TYPE}" | grep -i "wayland" &>/dev/null ; then
        export WAYLAND_IN_USE=1
        return 0
    else
        export WAYLAND_IN_USE=0
        return 1
    fi
}
export -f check_wayland_session

check_flatpak () {
    if [[ ! -z "$FLATPAK_IN_USE" ]]
    then [[ "$FLATPAK_IN_USE" == 1 ]] && return 0 || return 1
    fi
    if grep -i "flatpak" /etc/os-release &>/dev/null ; then
        export FLATPAK_IN_USE=1
        if grep -i "Alpine Linux" "/run/host/etc/os-release" &>/dev/null ; then
            export ALPINE_FP=1
        fi
        return 0
    else
        export FLATPAK_IN_USE=0
        return 1
    fi
}
export -f check_flatpak

unpack () {
    case $1 in
         *.tar.xz) local command="tar -Jxhf";;
         *.tar.gz) local command="tar -xhzf" ;;
        *.tar.zst) local command="tar -I zstd -xhf" ;;
            *.tar) local command="tar -xhf" ;;
    esac

    case $3 in
        silent) local silent=true ;;
    esac

    if [[ "$silent" != "true" ]] ; then
        set -o pipefail
        pw_start_progress_bar_cover_block "${COVERS_PATH}/unpacking_${LANGUAGE_GIF}.gif"
        $command "$1" -C "$2" 2>/dev/null
        pw_stop_progress_bar_cover_block
        [ "${PIPESTATUS[0]}" != 0 ] && print_error "File $1 unpacking error." && return 1 || return 0
    else
        $command "$1" -C "$2" && return 0 || return 1
    fi
}

debug_timer () {
    if [[ "$1" == "--start" ]] ; then
        START=$(date +%s%N)
    elif [[ "$1" == "--end" ]] ; then
        END=$(date +%s%N)
        DIFF=$((($END - $START)/1000000))
        if [[ -n "$2" ]] ; then
            print_warning "It took $DIFF milliseconds for $2"
        else
            print_warning "It took $DIFF milliseconds"
        fi
    fi
    return 0
}

combobox_fix () {
    if [[ -z "$1" ]] ; then
        print_error "no argument specified for combobox_fix"
        return 1
    else
        if [[ "$1" == "--disabled" ]] ; then
            if [[ -z "$3" ]] ; then
                echo "disabled"
                return 0
            fi
            name1="$2"
            name2="$3"
        elif [[ "$1" == "--empty" ]] ; then
            if [[ -z "$3" ]] ; then
                echo ""
                return 0
            fi
            name1="$2"
            name2="$3"
        else
            name1="$1"
            if [[ -z "$2" ]] ; then
                echo "${name1}"
                return 0
            fi
            name2="$2"
        fi
        name3="${name2//\!${name1}\!/\!}"
        name3="!${name3//${name1}\!/\!}"
        name3="${name3//\!\!/\!}"
        name4="${name3//*\!/}"
        if [[ ${name1} == "${name4}" ]] || [[ ${name1} == "\\${name4}" ]]
        then name3="${name3%\!${name1}}"
        fi
        if [[ "$1" == "--disabled" ]] ; then
            if [[ ${name1} != "disabled" ]]
            then fix_disabled="!disabled"
            fi
            echo "${name1}${name3}${fix_disabled}"
        elif [[ "$1" == "--empty" ]] ; then
            if [[ ${name1} != "" ]] ; then
                if [[ ${name1} == "\\" ]]
                then fix_empty=""
                else fix_empty="!"
                fi
            fi
            echo "${name1}${name3}${fix_empty}"
        else
            echo "${name1}${name3}"
        fi
        return 0
    fi
}

pw_clear_pfx () {
    rm -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/.dot*
    rm -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/.prog*
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/userdef.reg"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.update-timestamp"
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/.windows-serial"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/ProgramData/Setup"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/ProgramData/Windows"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/ProgramData/WindowsTask"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/ProgramData/Package Cache"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Local Settings/Application Data/Microsoft"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Local Settings/Application Data/Temp"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Local Settings/Temporary Internet Files"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Application Data/Microsoft"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Application Data/wine_gecko"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Temp"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Local Settings/Application Data/Microsoft"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Local Settings/Application Data/Temp"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Local Settings/Temporary Internet Files"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Application Data/Microsoft"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Application Data/wine_gecko"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Temp"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files/Internet Explorer"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files/Windows Media Player"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files/Windows NT"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files (x86)/Internet Explorer"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files (x86)/Windows Media Player"
    try_remove_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/Program Files (x86)/Windows NT"
    rm -f "${PORT_WINE_TMP_PATH}"/*.bin
    rm -f "${PORT_WINE_TMP_PATH}"/*.foz
    return 0
}

pw_reinstall_pp () {
    if [[ "${PW_REINSTALL_FROM_TERMINAL}" == 1 ]] ; then
        read -p "Do you want to reinstall PortProton? (Y,y - Yes):   " -n 1 -r
        if [[ ! $REPLY =~ ^[Yy]$ ]]
        then
            echo ""
            exit 1
        fi
    elif ! yad_question "$(gettext "Do you really want to reinstall PortProton?\nFor this, an internet connection will be required.")"
    then exit 1
    fi
    pw_clear_pfx
    try_remove_dir "${PORT_WINE_PATH}/data/dist"
    create_new_dir "${PORT_WINE_PATH}/data/dist"
    try_remove_dir "${PORT_WINE_TMP_PATH}/VULKAN"
    try_remove_file "${PORT_WINE_TMP_PATH}/scripts_update_notifier"
    try_remove_file "${PORT_WINE_PATH}/data/user.conf"
    try_remove_file "${PORT_WINE_TMP_PATH}/scripts_ver"
    echo
    print_info "Restarting PP for reinstall files..."
    export SKIP_CHECK_UPDATES="0"
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

check_user_conf () {
    if [[ ! -f "${USER_CONF}" ]] ; then
        cat << EOF > "${USER_CONF}"
#!/usr/bin/env bash"
# User overides db and var settings..."
# export DXVK_HUD=full"
# export GALLIUM_HUD=fps"
EOF
    fi
}

init_wine_ver () {
    if [[ "${PW_WINE_VER}" == "USE_SYSTEM_WINE" ]]
    then export PW_WINE_USE="USE_SYSTEM_WINE"
    elif [[ "${PW_WINE_VER}" == "GET-OTHER-WINE" ]]
    then gui_proton_downloader
    elif [[ ! -z "${PW_WINE_VER}" ]]
    then export PW_WINE_USE="$PW_WINE_VER"
    fi
    unset PW_WINE_VER

    if [[ "${PW_WINE_USE}" == "USE_SYSTEM_WINE" ]] \
    && command -v wine &>/dev/null ; then
        export WINEDIR="$RT_PREFIX/usr"
        export WINE="$RT_PREFIX$(command -v wine)"
        export WINELOADER="$WINE"
        export WINESERVER="$RT_PREFIX$(command -v wineserver)"
        export PW_NO_FSYNC=1
        unset WINEDLLPATH
    else
        if [[ -d "${PORT_WINE_PATH}/data/dist" ]] ; then
        IFS=$'\n'
        for dist_dir in $(ls -1 "${PORT_WINE_PATH}/data/dist/") ; do
            dist_dir_new=$(echo "${dist_dir}" | awk '$1=$1' | sed -e s/[[:blank:]]/_/g)
            if [[ ! -d "${PORT_WINE_PATH}/data/dist/${dist_dir_new^^}" ]] ; then
                mv -- "${PORT_WINE_PATH}/data/dist/$dist_dir" "${PORT_WINE_PATH}/data/dist/${dist_dir_new^^}"
            fi
        done
        IFS="$orig_IFS"
        fi
        if [[ -z $(ls "${PORT_WINE_PATH}/data/dist/" | grep "${PW_WINE_USE}") ]] ; then
            if [[ "$PW_WINE_USE" =~ PROTON_LG* ]]
            then export PW_WINE_USE=PROTON_LG
            else export PW_WINE_USE=WINE_LG
            fi
        fi
        if [[ "$PW_WINE_USE" == PROTON_LG ]]
        then export PW_WINE_USE="${PW_PROTON_LG_VER}"
        elif [[ "$PW_WINE_USE" == WINE_LG ]]
        then export PW_WINE_USE="${PW_WINE_LG_VER}"
        fi

        export WINEDIR="${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}"
        export WINE="${WINEDIR}/bin/wine"
        export WINELOADER="${WINEDIR}/bin/wine"
        export WINESERVER="${WINEDIR}/bin/wineserver"
        if [[ -d "${WINEDIR}/files" && ! -d "${WINEDIR}/dist" ]] ; then
            for clear_dist_files in $(ls "${WINEDIR}" | sed -e "s/^files$//g" | sed -e "s/^version$//g") ; do
                rm -fr "${WINEDIR}/$clear_dist_files"
            done
            mv -f "${WINEDIR}/files"/* "${WINEDIR}/"
            rm -fr "${WINEDIR}/files"
        elif [[ ! -d "${WINEDIR}/files" && -d "${WINEDIR}/dist" ]] ; then
            for clear_dist_files in $(ls "${WINEDIR}" | sed -e "s/^dist$//g" | sed -e "s/^version$//g") ; do
                rm -fr "${WINEDIR}/$clear_dist_files"
            done
            mv -f "${WINEDIR}/dist"/* "${WINEDIR}/"
            rm -fr "${WINEDIR}/dist"
        elif [ -f "${WINEDIR}/proton_dist.tar" ] ; then
            unpack "${WINEDIR}/proton_dist.tar" "${WINEDIR}/"
            for clear_dist_files in $(ls "${WINEDIR}" | sed -e "s/^bin$//g" | sed -e "s/^lib$//g" | sed -e "s/^lib64$//g" | sed -e "s/^share$//g" | sed -e "s/^version$//g") ; do
                rm -fr "${WINEDIR}/$clear_dist_files"
            done
        fi
        if [[ -d "${WINEDIR}" ]] ; then
            [[ ! -f "${WINEDIR}/version" ]] && echo "${PW_WINE_USE}" > "${WINEDIR}/version"

            if [[ ! -d "${WINEDIR}/lib64/" ]] && [[ -d "${WINEDIR}/lib/wine/x86_64-unix" ]] ; then
                create_new_dir "${WINEDIR}/lib64/wine"
                mv -f "${WINEDIR}/lib/wine/x86_64-unix" "${WINEDIR}/lib64/wine/"
                mv -f "${WINEDIR}/lib/wine/x86_64-windows" "${WINEDIR}/lib64/wine/"
            fi

            for mono_gecko_chk in "mono" "gecko" ; do
                if check_symlink "${WINEDIR}/share/wine/${mono_gecko_chk}" ; then
                    print_info "${WINEDIR}/share/wine/${mono_gecko_chk} is symlink. OK."
                elif [[ -d "${WINEDIR}/share/wine/${mono_gecko_chk}" ]] ; then
                    pw_start_progress_bar_cover_block "${COVERS_PATH}/loading_${LANGUAGE_GIF}.gif"
                    try_copy_dir "${WINEDIR}/share/wine/${mono_gecko_chk}" "${PORT_WINE_TMP_PATH}"
                    try_remove_dir "${WINEDIR}/share/wine/${mono_gecko_chk}"
                    try_force_link_dir "${PORT_WINE_TMP_PATH}/${mono_gecko_chk}" "${WINEDIR}"/share/wine/
                    print_info "Copy ${WINEDIR}/share/wine/${mono_gecko_chk} to tmp and create symlink to ${WINEDIR}/share/wine/. OK."
                    pw_stop_progress_bar_cover_block
                else
                    try_remove_dir "${WINEDIR}/share/wine/${mono_gecko_chk}"
                    try_force_link_dir "${PORT_WINE_TMP_PATH}/${mono_gecko_chk}" "${WINEDIR}"/share/wine
                    print_warning "${WINEDIR}/share/wine/${mono_gecko_chk} is broken symlink. Repair... OK."
                fi
            done

            if ! grep 'Global,"{41FCC608-8496-4DEF-B43E-7D9BD675A6FF}",0x10001,0x00000001' "${WINEDIR}/share/wine/wine.inf" &>/dev/null ; then
                echo 'HKLM,Software\NVIDIA Corporation\Global,"{41FCC608-8496-4DEF-B43E-7D9BD675A6FF}",0x10001,0x00000001' >> "${WINEDIR}/share/wine/wine.inf"
                echo 'HKLM,System\ControlSet001\Services\nvlddmkm,"{41FCC608-8496-4DEF-B43E-7D9BD675A6FF}",0x10001,0x00000001' >> "${WINEDIR}/share/wine/wine.inf"
                sed -i '/Steam.exe/d' "${WINEDIR}/share/wine/wine.inf"
                sed -i '/\\Valve\\Steam/d' "${WINEDIR}/share/wine/wine.inf"
                sed -i '/winemenubuilder/d' "${WINEDIR}/share/wine/wine.inf"
                rm -f "${WINEDIR}"/lib*/*steam* &>/dev/null
                rm -f "${WINEDIR}"/lib*/wine/*/*steam* &>/dev/null
                rm -f "${WINEDIR}"/lib*/wine/*-windows/winemenubuilder.exe &>/dev/null
            fi
        fi

        export WINEDLLPATH="${WINEDIR}/lib64/wine:${WINEDIR}/lib/wine"
        if [[ -d "${PATH_TO_GAME}" ]] ; then
            export WINEDLLPATH+=":${PATH_TO_GAME}"
        fi
    fi

    export WINE_LIBRARY_PATH="${WINEDIR}/lib64:${WINEDIR}/lib"
    export MEDIACONV_BLANK_VIDEO_FILE="${WINEDIR}"/share/media/blank.mkv
    export MEDIACONV_BLANK_AUDIO_FILE="${WINEDIR}"/share/media/blank.ptna
    return 0
}

pw_init_runtime () {
    check_flatpak && export PW_USE_RUNTIME="0"

    if [[ "$PW_USE_RUNTIME" != "1" ]] \
    || [[ "$PW_WINE_USE" == "USE_SYSTEM_WINE" ]]
    then
        export PW_LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${PW_PLUGINS_PATH}/portable/lib/lib64:${PW_PLUGINS_PATH}/portable/lib/lib32"
        export PW_VK_LAYER_PATH="${PW_PLUGINS_PATH}/portable/share/vulkan/implicit_layer.d"
        if check_flatpak \
        && check_gamescope_session
        then PW_TERM="xterm -fullscreen -bg black -fg white -e"
        elif check_flatpak
        then PW_TERM="xterm -bg black -fg white -e"
        else PW_TERM="env LD_LIBRARY_PATH=\"${PW_LD_LIBRARY_PATH}\" ${PW_PLUGINS_PATH}/portable/bin/xterm -bg black -fg white -e"
        fi
        export PW_TERM
        export pw_runtime="env"
        return 0
    fi

    if grep -i -E '(ROSA|RED OS)' "/etc/os-release" &>/dev/null ; then
        export LOCPATH="/run/host/usr/share/locale/"
    elif grep -i "altlinux" "/etc/os-release" &>/dev/null ; then
        export LIBGL_DRIVERS_PATH="/usr/lib/X11/modules/dri:/usr/lib64/X11/modules/dri"
        if [[ -f "/usr/share/vulkan/icd.d/nvidia_icd.json" ]] ; then
            VK_ADD_DRIVER_FILES="$(realpath /usr/share/vulkan/icd.d/nvidia_icd.json)"
            export VK_ADD_DRIVER_FILES
        fi
    fi

    PW_PV_OVERRIDES="/usr/lib/pressure-vessel/overrides/lib"

    export PW_LD_LIBRARY_PATH="\
${PW_PLUGINS_PATH}/portable/lib/lib64:\
${PW_PLUGINS_PATH}/portable/lib/lib32:\
${PW_PV_OVERRIDES}/x86_64-linux-gnu/aliases:\
${PW_PV_OVERRIDES}/i386-linux-gnu/aliases:\
/overrides/lib/x86_64-linux-gnu:\
/overrides/lib/i386-linux-gnu"

    if [ ! -z "${PATH}" ]
    then export PW_PATH="${PATH}:${PW_PLUGINS_PATH}/portable/bin"
    else export PW_PATH="${PW_PLUGINS_PATH}/portable/bin"
    fi

    if [[ ! -z "${PW_SANDBOX_HOME_PATH}" && -d "${PW_SANDBOX_HOME_PATH}" ]] ; then
        export pw_runtime="${PW_WINELIB}/pressure-vessel/bin/pressure-vessel-unruntime \
        --unshare-home \
        --home=${PW_SANDBOX_HOME_PATH} \
        --remove-game-overlay \
        --share-pid \
        ${PW_ADD_TO_ARGS_IN_RUNTIME} -- \
        env PATH=$PW_PATH "
    else
        export pw_runtime="${PW_WINELIB}/pressure-vessel/bin/pressure-vessel-unruntime \
        --share-home \
        --remove-game-overlay \
        --share-pid \
        ${PW_ADD_TO_ARGS_IN_RUNTIME} -- \
        env PATH=$PW_PATH "
    fi

    if [[ "$PW_USE_SYSTEM_VK_LAYERS" == "1" ]] ; then
        export PRESSURE_VESSEL_IMPORT_VULKAN_LAYERS=1
        unset PW_VK_LAYER_PATH
    else
        export PRESSURE_VESSEL_IMPORT_VULKAN_LAYERS=0
        export PW_VK_LAYER_PATH="${PW_PLUGINS_PATH}/portable/share/vulkan/implicit_layer.d"
    fi

    # export PRESSURE_VESSEL_GRAPHICS_PROVIDER="/"

    unset PRESSURE_VESSEL_RUNTIME_ARCHIVE
    export PRESSURE_VESSEL_COPY_RUNTIME=1
    export PRESSURE_VESSEL_GC_LEGACY_RUNTIMES=1
    export PRESSURE_VESSEL_RUNTIME=runtime
    export PRESSURE_VESSEL_RUNTIME_BASE="${PW_WINELIB}"
    export PRESSURE_VESSEL_VARIABLE_DIR="${PW_WINELIB}/var"
    export PRESSURE_VESSEL_PREFIX="${PW_WINELIB}/pressure-vessel"
    export FONTCONFIG_PATH=''

    unset PRESSURE_VESSEL_FILESYSTEMS_RO PRESSURE_VESSEL_FILESYSTEMS_RW
    for PWRTMRO in ${PW_RT_MOUNT_RO[*]} ; do
        if [ ! -z "${PRESSURE_VESSEL_FILESYSTEMS_RO}" ]
        then export PRESSURE_VESSEL_FILESYSTEMS_RO="${PRESSURE_VESSEL_FILESYSTEMS_RO}:${PWRTMRO}"
        else export PRESSURE_VESSEL_FILESYSTEMS_RO="${PWRTMRO}"
        fi
    done
    for PWRTMRW in ${PW_RT_MOUNT_RW[*]} ; do
        if [ ! -z "${PRESSURE_VESSEL_FILESYSTEMS_RW}" ]
        then export PRESSURE_VESSEL_FILESYSTEMS_RW="${PRESSURE_VESSEL_FILESYSTEMS_RW}:${PWRTMRW}"
        else export PRESSURE_VESSEL_FILESYSTEMS_RW="${PWRTMRW}"
        fi
    done

    export PW_TERM="env LD_LIBRARY_PATH=\"${PW_LD_LIBRARY_PATH}\" ${PW_PLUGINS_PATH}/portable/bin/xterm -bg black -fg white -e"

    print_info "RUNTIME is enabled"
}

pw_mangohud_check () {
    if [[ "${PW_MANGOHUD}" == "1" ]] \
    && ! check_gamescope_session
    then
        if [[ "$PW_USE_SYSTEM_VK_LAYERS" != "1" ]] ; then
            sed -i 's/: "VK_LAYER/: "PW_VK_LAYER/' "${PW_PLUGINS_PATH}/portable/share/vulkan/implicit_layer.d"/MangoHud*.json
            var_pw_vk_istance_layers_config_update "PW_VK_LAYER_MANGOHUD_overlay64:PW_VK_LAYER_MANGOHUD_overlay32"
            print_info "Portable MangoHud is enabled"
        else
            print_info "System MangoHud is enabled"
            export PW_MANGOHUD_SLR='MANGOHUD=1'
        fi
        MANGOHUD_LIB_NAME="libMangoHud.so"
        if [[ "${PW_VULKAN_USE}" = "0" ]] ; then
            MANGOHUD_LIB_NAME="libMangoHud_dlsym.so:libMangoHud_opengl.so:${MANGOHUD_LIB_NAME}"
        fi
        if [[ ! -z "${PW_LD_PRELOAD}" ]]
        then export PW_LD_PRELOAD="${PW_LD_PRELOAD}:${MANGOHUD_LIB_NAME}"
        else export PW_LD_PRELOAD="${MANGOHUD_LIB_NAME}"
        fi
    else
        export PW_MANGOHUD_SLR=''
        print_info "MangoHud is disabled"
    fi
    return 0
}

pw_vkbasalt_check () {
    if [[ "${PW_VKBASALT}" == "1" ]] ; then
        if [[ "$PW_USE_SYSTEM_VK_LAYERS" != "1" ]] ; then
            sed -i 's/: "VK_LAYER/: "PW_VK_LAYER/' "${PW_PLUGINS_PATH}/portable/share/vulkan/implicit_layer.d"/vkBasalt*.json
            var_pw_vk_istance_layers_config_update "PW_VK_LAYER_VKBASALT_post_processing64:PW_VK_LAYER_VKBASALT_post_processing32"
            print_info "Portable vkBasalt is enabled"
        else
            print_info "System vkBasalt is enabled"
            export ENABLE_VKBASALT="1"
        fi
        export VKBASALT_CONFIG_FILE="${PORT_WINE_PATH}/data/vkBasalt.conf"
        if ! grep "${PW_PLUGINS_PATH}/reshade" "${VKBASALT_CONFIG_FILE}" &>/dev/null
        then sed -ri "s|= .*/reshade|= \"${PW_PLUGINS_PATH}\"/reshade|g" "${VKBASALT_CONFIG_FILE}"
        fi
        if [[ ! -z "${PW_VKBASALT_EFFECTS}" ]] && [[ ! -z "${PW_VKBASALT_FFX_CAS}" ]] ; then
            sed -ri "s/effects = .*/effects = ${PW_VKBASALT_EFFECTS}/g" "${VKBASALT_CONFIG_FILE}"
            sed -ri "s/casSharpness .*/casSharpness = ${PW_VKBASALT_FFX_CAS}/g" "${VKBASALT_CONFIG_FILE}"
        fi
    else
        export DISABLE_VKBASALT="1"
        print_info "vkBasalt is disabled"
    fi
    return 0
}

regdlloverrides () {
    PW_DLL_EXE=$(echo $@ | awk -F: '{print $1}')
    PW_DLL_LIB=$(echo $@ | awk -F: '{print $2}' | awk -F= '{print $1}')
    PW_DLL_SET=$(echo $@ | awk -F= '{print $NF}')
    case "${PW_DLL_SET}" in
        "n,b") PW_DLL_IN_REG="native,builtin" ;;
        "b,n") PW_DLL_IN_REG="builtin,native" ;;
          "n") PW_DLL_IN_REG="native" ;;
          "b") PW_DLL_IN_REG="builtin" ;;
            *) PW_DLL_IN_REG="" ;;
    esac
    grep 'HKCU,Software\\Wine\\AppDefaults\\'"${PW_DLL_EXE}"'\\DllOverrides,'\"${PW_DLL_LIB}\",0x2,\"${PW_DLL_IN_REG}\" "${WINEDIR}/share/wine/wine.inf"
    if [[ "$?" != "0" ]] ; then
        grep "ProtonOverrides" "${WINEDIR}/share/wine/wine.inf"
        if [[ "$?" == "0" ]] ; then
            sed -i "/\[ProtonOverrides\]/a HKCU,Software\\\Wine\\\AppDefaults\\\\${PW_DLL_EXE}\\\DllOverrides,\"${PW_DLL_LIB}\",0x2,\"${PW_DLL_IN_REG}\"" "${WINEDIR}/share/wine/wine.inf"
        else
            sed -i "/\[SteamClient.ntamd64\]/a HKCU,Software\\\Wine\\\AppDefaults\\\\${PW_DLL_EXE}\\\DllOverrides,\"${PW_DLL_LIB}\",0x2,\"${PW_DLL_IN_REG}\"" "${WINEDIR}/share/wine/wine.inf"
        fi
    fi
}

wait_wineserver () {
    while ls -l /proc/*/exe >/dev/null 2>&1 | grep -ie PortProton | grep -E 'wine(64)?-preloader|wineserver'
    do
        sleep 1
    done
    "$WINESERVER" -w
    return 0
}
export -f wait_wineserver

kill_portwine () {
    if [[ "${PW_WINE_USE}" != "USE_SYSTEM_WINE" ]] ; then
        check_flatpak
        if [[ "$ALPINE_FP" == "1" ]] ; then
            wine_pids=$(ls -l /proc/*/exe >/dev/null 2>&1 | grep -ie PortProton | grep -E 'wine(64)?-preloader|wineserver' | awk -F/ '{print $3}')
            bwrap_pids=$(pgrep -f wrap | grep PortProton | head -n 1)
        else
            wine_pids=$(ls -l /proc/*/exe 2>/dev/null | grep -ie PortProton | grep -E 'wine(64)?-preloader|wineserver' | awk -F/ '{print $3}')
            bwrap_pids="$(pgrep -a wrap | grep PortProton | head -n 1 | awk '{print $1}')"
        fi
    else
        wine_pids=$(ls -l /proc/*/exe 2>/dev/null | grep -E 'wine(64)?-preloader|wineserver' | awk -F/ '{print $3}')
        unset bwrap_pids
    fi

    for pw_kill_pids in ${bwrap_pids} ${wine_pids}; do
        if ps cax | grep "${pw_kill_pids}" ; then
            kill -n 9 "${pw_kill_pids}" &>/dev/null
        fi
    done

    return 0
}
export -f kill_portwine

pw_kill_autostart () {
    if [[ -z "${2}" ]]
    then SWAIT=3
    else SWAIT="${2}"
    fi
    sleep 5
    while true ; do
        if  [[ -z "$(ps aux | grep -m 1 -i "$1" | grep -v grep | awk '{print $2}')" ]] \
        && [[ ! -z "$(ps aux | grep wrap | grep -v grep | grep -i "PortProton" | head -n 1)" ]]
        then
            echo -e "PID $1 not found"
            sleep "${SWAIT}"
        else
            sleep "${SWAIT}"
            if [[ "$3" == please ]]
            then kill -s SIGTERM $(ps aux | grep -m 1 -i "$1" | grep -v grep | awk '{print $2}')
            else kill_portwine
            fi
            break
        fi
    done

    if [[ ! -z "$(ls "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/drive_c/ | grep -m 1 ".tmp")" ]] ; then
        rm -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/drive_c/*.tmp
    fi

    return 0
}
export -f pw_kill_autostart

stop_portwine () {
    [[ "$int_xneur" == "1"  ]] && xneur &

    if [[ "$PW_USE_US_LAYOUT" == "1" ]] \
    && ! check_wayland_session \
    && command -v setxkbmap &>/dev/null
    then
        setxkbmap
    fi

    if ! check_wayland_session \
    && [[ "$(xrandr | sed -rn 's/^.*primary.* ([0-9]+x[0-9]+).*$/\1/p')" != "$PW_SCREEN_RESOLUTION" ]]
    then
        xrandr --output "$PW_SCREEN_PRIMARY" --mode "$PW_SCREEN_RESOLUTION"
    fi

    if [[ "${PW_DISABLE_COMPOSITING}" == "1" ]] \
    && ! check_gamescope_session
    then
        if [[ "${DESKTOP_SESSION}" =~ "plasma" ]] ; then
            qdbus org.kde.KWin /Compositor resume
        elif [[ "${DESKTOP_SESSION}" =~ "mate" ]] ; then
            gsettings set org.mate.Marco.general compositing-manager true
        elif [[ "${DESKTOP_SESSION}" =~ "xfce" ]] ; then
            xfconf-query -c xfwm4 -p /general/use_compositing -s true
        elif [[ "${DESKTOP_SESSION}" =~ "cinnamon" ]] ; then
            gsettings set org.cinnamon.muffin unredirect-fullscreen-windows false
        elif [[ "${DESKTOP_SESSION}" =~ "deepin" ]] ; then
            dbus-send --session --dest=com.deepin.WMSwitcher --type=method_call /com/deepin/WMSwitcher com.deepin.WMSwitcher.RequestSwitchWM
        fi
    fi

    pw_stop_progress_bar
    try_remove_file "${PORT_SCRIPTS_PATH}/0"
    try_remove_file "${PORT_SCRIPTS_PATH}/1"
    kill_portwine &&
    try_remove_dir "${PW_WINELIB}/var"
    find "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/" -maxdepth 1 -type f -name "*.tmp" -delete
    if [[ "$XDG_SESSION_TYPE" == "tty" ]] ; then
        if [ ! -z "$(pgrep -a yad_gui_pp | grep "\--notification" | awk '{print $1}')" ] ; then
            kill -s SIGUSR1 "$(pgrep -a yad_gui_pp | grep "\--notification" | awk '{print $1}')"
        fi
    else
        if [[ ! -z "$(pgrep -a tray_gui_pp)" ]] ; then
            kill -s SIGUSR1 $(pgrep -a tray_gui_pp) 2>/dev/null
        fi
    fi
    if [[ ! -z "$(pgrep -a yad_gui_pp)" ]] ; then
        kill -s SIGUSR1 $(pgrep -a yad_gui_pp) 2>/dev/null
    fi
    pw_auto_create_shortcut
    add_in_stop_portwine
    unset SKIP_CHECK_UPDATES
    exit 0
}
export -f stop_portwine

pw_download_libs () {
    if [[ ! -f "${PW_WINELIB}/runtime/files/bin/vkcube" ]] \
    || [[ ! -f "${PW_WINELIB}/pressure-vessel/bin/pressure-vessel-wrap" ]]
    then
        print_info "Download and install libraries..."
        if try_download "github.com/Castro-Fidel/PortWINE/releases/download/libs${PW_LIBS_VER}/libs${PW_LIBS_VER}.tar.xz" \
                        "${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}.tar.xz" ; then
            if unpack "${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}.tar.xz" "${PORT_WINE_TMP_PATH}/" ; then
                try_remove_file "${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}.tar.xz"
                if ls "${PORT_WINE_TMP_PATH}" | grep libs_v | grep -v libs"${PW_LIBS_VER}" ; then
                    for RM_LIBS in $(ls "${PORT_WINE_TMP_PATH}" | grep libs_v | grep -v libs"${PW_LIBS_VER}")
                    do try_remove_dir "${PORT_WINE_TMP_PATH}/${RM_LIBS}"
                    done
                fi
            else
                try_remove_dir "${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}"
                try_remove_file "${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}.tar.xz"
                if yad_error_download
                then pw_download_libs
                else
                    export PW_LIBS_VER="_v$((${PW_LIBS_VER//_v/} - 1))"
                    export PW_WINELIB="${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}"
                fi
            fi
        else
            if yad_error_download
            then pw_download_libs
            else
                export PW_LIBS_VER="_v$((${PW_LIBS_VER//_v/} - 1))"
                export PW_WINELIB="${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}"
            fi
        fi
    fi
    return 0
}

pw_check_and_download_wine () {
    [[ ! -d "${PORT_WINE_PATH}/data/dist/" ]] && create_new_dir "${PORT_WINE_PATH}/data/dist"
    if [[ "${PW_WINE_USE}" == "GET-OTHER-WINE" ]] ; then
        gui_proton_downloader
    fi
    if [[ "$PW_WINE_USE" == PROTON_LG ]]
    then export PW_WINE_USE="${PW_PROTON_LG_VER}"
    elif [[ "$PW_WINE_USE" == WINE_*_LG ]] || [[ "$PW_WINE_USE" == WINE_LG ]]
    then export PW_WINE_USE="${PW_WINE_LG_VER}"
    fi
    if [ ! -d "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}" ] ; then
        print_info "Download and install ${PW_WINE_USE}..."
        if try_download "github.com/Castro-Fidel/wine_builds/releases/download/${PW_WINE_USE}/${PW_WINE_USE}.tar.xz" \
                        "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz" ; then
            if unpack "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz" "${PORT_WINE_PATH}/data/dist/" ; then
                echo "${PW_WINE_USE}" > "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}/version"
                try_remove_file "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz"
                try_remove_dir "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}"
            else
                try_remove_file "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz"
                try_remove_dir "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}"
                try_remove_dir "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}"

                yad_error_download && pw_check_and_download_wine || exit 1
            fi
        else
            yad_error_download && pw_check_and_download_wine || exit 1
        fi
    fi
    return 0
}

pw_check_and_download_dxvk_and_vkd3d () {
    # Download stable and git version DXVK
    for DXVK_VAR_VER in "${DXVK_STABLE_VER}" "${DXVK_GIT_VER}" "${DXVK_LEGACY_VER}" ; do
        if [ ! -d "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}" ] ; then
            print_info "Download and install DXVK v.${DXVK_VAR_VER}"
            if try_download "https://github.com/Castro-Fidel/vulkan/releases/download/dxvk-${DXVK_VAR_VER}/dxvk-${DXVK_VAR_VER}.tar.xz" \
                                      "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.xz" ; then
                if unpack "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.xz" "${PW_VULKAN_DIR}" ; then
                    try_remove_file "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.xz"
                else
                    try_remove_file "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.xz"
                    try_remove_dir "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}"

                    yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
                fi
            elif try_download "https://github.com/doitsujin/dxvk/releases/download/v${DXVK_VAR_VER}/dxvk-${DXVK_VAR_VER}.tar.gz" \
                                       "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.gz"  no_mirror ; then
                if unpack "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.gz" "${PW_VULKAN_DIR}" ; then
                    try_remove_file "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.gz"
                else
                    try_remove_file "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}.tar.gz"
                    try_remove_dir "${PW_VULKAN_DIR}/dxvk-${DXVK_VAR_VER}"

                    yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
                fi
            else
                yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
            fi
        fi
    done

    # Download stable and git version VKD3D
    for VKD3D_VAR_VER in "${VKD3D_STABLE_VER}" "${VKD3D_GIT_VER}" ; do
        if [ ! -d "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}" ] ; then
            print_info "Download and install VKD3D-PROTON v.${VKD3D_VAR_VER}"
            if try_download "https://github.com/Castro-Fidel/vulkan/releases/download/vkd3d-proton-${VKD3D_VAR_VER}/vkd3d-proton-${VKD3D_VAR_VER}.tar.xz" \
                                      "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.xz" ; then
                if unpack "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.xz" "${PW_VULKAN_DIR}" ; then
                    try_remove_file "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.xz"
                else
                    try_remove_file "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.xz"
                    try_remove_dir "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}"

                    yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
                fi
            elif try_download "https://github.com/HansKristian-Work/vkd3d-proton/releases/download/v${VKD3D_VAR_VER}/vkd3d-proton-${VKD3D_VAR_VER}.tar.zst" \
                                        "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.zst" no_mirror ; then
                if unpack "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.zst" "${PW_VULKAN_DIR}" ; then
                    try_remove_file "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.zst"
                else
                    try_remove_file "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}.tar.zst"
                    try_remove_dir "${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_VAR_VER}"

                    yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
                fi
            else
                yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
            fi
        fi
    done

    # Download DGVOODOO2
    if [ ! -f "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}/dgVoodoo.conf" ] ; then
        print_info "Download and install DGVOODOO2 v.${DGV2_VER}"
        if try_download "https://github.com/Castro-Fidel/vulkan/releases/download/dgvoodoo2-${DGV2_VER}/dgvoodoo2-${DGV2_VER}.tar.xz" \
        "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}.tar.xz"
        then
            if unpack "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}.tar.xz" "${PW_VULKAN_DIR}" ; then
                try_remove_file "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}.tar.xz"
            else
                try_remove_file "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}.tar.xz"
                try_remove_dir "${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}"

                yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
            fi
        else
            yad_error_download && pw_check_and_download_dxvk_and_vkd3d || exit 1
        fi
    fi

    return 0
}

pw_check_and_download_plugins () {
    if [[ ! -f "${PW_PLUGINS_PATH}/portable/lib/lib64/libvkbasalt.so" ]] ; then
        print_info "Download and install plugins${PW_PLUGINS_VER}..."
        if command -v yad &>/dev/null ; then
            if try_download "github.com/Castro-Fidel/wine_builds/releases/download/plugins${PW_PLUGINS_VER}/plugins${PW_PLUGINS_VER}.tar.xz" "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz" ; then
                if unpack "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz" "${PORT_WINE_TMP_PATH}" ; then
                    try_remove_file "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz"
                    # TODO: drop clear prefix, and add update prefix from new plugins
                    pw_clear_pfx
                    if ls "${PORT_WINE_TMP_PATH}" | grep plugins_v | grep -v "plugins${PW_PLUGINS_VER}"
                    then
                        for RM_PLUGINS in $(ls "${PORT_WINE_TMP_PATH}" | grep plugins_v | grep -v "plugins${PW_PLUGINS_VER}")
                        do try_remove_dir "${PORT_WINE_TMP_PATH}/${RM_PLUGINS}"
                        done
                    fi
                else
                    try_remove_file "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz"
                    try_remove_dir "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}"
                    if yad_error_download
                    then pw_check_and_download_plugins
                    else
                        export PW_PLUGINS_VER="_v$((${PW_PLUGINS_VER//_v/} - 1))"
                        export PW_PLUGINS_PATH="${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}"
                    fi
                fi
            else
                if yad_error_download
                then pw_check_and_download_plugins
                else
                    export PW_PLUGINS_VER="_v$((${PW_PLUGINS_VER//_v/} - 1))"
                    export PW_PLUGINS_PATH="${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}"
                fi
            fi
        else
            pw_notify_send -i info \
            "$(gettext "Please wait for update PortProton")"
            if try_download "github.com/Castro-Fidel/wine_builds/releases/download/plugins${PW_PLUGINS_VER}/plugins${PW_PLUGINS_VER}.tar.xz" \
                                   "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz" silent ; then
                if unpack "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz" "${PORT_WINE_TMP_PATH}" ; then
                    try_remove_file "${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}.tar.xz"
                    # TODO: drop clear prefix, and add update prefix from new plugins
                    pw_clear_pfx
                    if ls "${PORT_WINE_TMP_PATH}" | grep plugins_v | grep -v "plugins${PW_PLUGINS_VER}"
                    then
                        for RM_PLUGINS in $(ls "${PORT_WINE_TMP_PATH}" | grep plugins_v | grep -v "plugins${PW_PLUGINS_VER}")
                        do try_remove_dir "${PORT_WINE_TMP_PATH}/${RM_PLUGINS}"
                        done
                    fi
                else
                    print_error "Failed updating PortProton!"
                    pw_notify_send -i error \
                    "$(gettext "Failed updating PortProton!")"
                fi
            fi
        fi
    fi
    if command -v 7z &>/dev/null
    then export pw_7z="7z"
    else export pw_7z="${PW_PLUGINS_PATH}/portable/lib/p7zip/7z"
    fi
    return 0
}

check_dirs_and_files_in_pfx () {
        create_new_dir "${WINEPREFIX}/dosdevices"
        create_new_dir "${WINEPREFIX}/drive_c/windows/syswow64/"
        create_new_dir "${WINEPREFIX}/drive_c/windows/system32/"
        create_new_dir "${WINEPREFIX}/drive_c/tmp/.private/$USER/"
        create_new_dir "${WINEPREFIX}/drive_c/tmp/.private/steamuser/"

        [ ! -d "${WINEPREFIX}/dosdevices/c:" ] && try_force_link_dir "${WINEPREFIX}/drive_c/" "${WINEPREFIX}/dosdevices/c:"
        [ ! -d "${WINEPREFIX}/dosdevices/z:" ] && try_force_link_dir "/" "${WINEPREFIX}/dosdevices/z:"
        [ ! -d "${WINEPREFIX}/dosdevices/h:" ] && try_force_link_dir "$HOME" "${WINEPREFIX}/dosdevices/h:"

        if [ ! -d "${WINEPREFIX}/dosdevices/s:" ] \
        && [ -d "$HOME/.local/share/Steam/steamapps/common/" ]
        then
            try_force_link_dir "$HOME/.local/share/Steam/steamapps/common/" "${WINEPREFIX}/dosdevices/s:"
        fi

        create_new_dir "${WINEPREFIX}/drive_c/windows/Fonts/"
        LN_FONTS="arialbd.ttf courbd.ttf georgia.ttf micross.ttf msyh.ttf simsun.ttc arial.ttf cour.ttf malgun.ttf msgothic.ttc nirmala.ttf times.ttf"
        for link_font in $LN_FONTS ; do
            if [[ ! -f "${WINEPREFIX}/drive_c/windows/Fonts/${link_font}" ]] ; then
                try_force_link_file "${WINEDIR}/share/fonts/${link_font}" "${WINEPREFIX}/drive_c/windows/Fonts/${link_font}"
            fi
        done

        create_new_dir "${WINEPREFIX}/drive_c/users/steamuser"
        if [[ ! -d "${WINEPREFIX}/drive_c/users/${USER}" ]]
        then try_force_link_dir "${WINEPREFIX}/drive_c/users/steamuser" "${WINEPREFIX}/drive_c/users/${USER}"
        fi

        create_new_dir "${WINEPREFIX}/drive_c/users/Public/Documents/Steam"
        create_new_dir "${WINEPREFIX}/drive_c/users/steamuser/Documents/Steam"
        if [[ ! -d "${WINEPREFIX}/drive_c/users/steamuser/My Documents" ]]
        then try_force_link_dir "${WINEPREFIX}/drive_c/users/steamuser/Documents" "${WINEPREFIX}/drive_c/users/steamuser/My Documents"
        fi

        create_new_dir "${WINEPREFIX}/drive_c/users/steamuser/AppData/Roaming"
        if [[ ! -d "${WINEPREFIX}/drive_c/users/steamuser/Application Data" ]]
        then try_force_link_dir "${WINEPREFIX}/drive_c/users/steamuser/AppData/Roaming" "${WINEPREFIX}/drive_c/users/steamuser/Application Data"
        fi

        create_new_dir "${WINEPREFIX}/drive_c/users/steamuser/AppData/Local"
        create_new_dir "${WINEPREFIX}/drive_c/users/steamuser/Local Settings"
        if [[ ! -d "${WINEPREFIX}/drive_c/users/steamuser/Local Settings/Application Data" ]]
        then try_force_link_dir "${WINEPREFIX}/drive_c/users/steamuser/AppData/Local" "${WINEPREFIX}/drive_c/users/steamuser/Local Settings/Application Data"
        fi

        DRIVES=(d e f g i j k l m n o p q r t u v w x y)
        DEF_MOUNT_DIRS=('/media' '/mnt' '/run/media')
        MOUNT_DIRS=($(find "${DEF_MOUNT_DIRS[@]}" -maxdepth 1 -mindepth 1  -type d 2>/dev/null | sort -u))
        find -L "$WINEPREFIX/dosdevices" -maxdepth 1 -type l -exec rm -rf {} \; 2>/dev/null ;

        for drive_dir in "${MOUNT_DIRS[@]}" ; do
            if  mountpoint -q "$drive_dir" ; then
                if ! realpath "${WINEPREFIX}/dosdevices"/*: | grep -w "$drive_dir$" &>/dev/null ; then
                    drive_num=0
                    while [[ "$drive_num" -lt "${#DRIVES[@]}" ]] ; do
                        drive_path="${WINEPREFIX}/dosdevices/${DRIVES[$drive_num]}:"
                        mount_point=$(df -P "$drive_dir" | awk 'NR==2 {print $1}')
                        mount_name=$(lsblk -no pkname "$mount_point" 2>/dev/null)

                        if [[ ! -L "$drive_path" ]] ; then
                            if [[ $(cat "/sys/class/block/$mount_name/removable") != "1" ]] 2>/dev/null ; then
                                if [[ ! $(echo "$mount_point" | grep "mmc") ]] 2>/dev/null ; then
                                    ln -sf "$drive_dir" "$drive_path"
                                    print_info "Mounted ${drive_dir} to ${drive_path}"
                                    break
                                else
                                    ((drive_num++))
                                fi
                            else
                                ((drive_num++))
                            fi
                        else
                            ((drive_num++))
                        fi
                    done
                fi
            fi
        done
    }

check_nvidia_rtx () {
    if [[ ! -z $(echo "$LSPCI_VGA" | grep -i "nvidia") ]]; then
        # Turing (without nvidia 16XX)
        [[ "$LSPCI_VGA" == *TU[0-9]* ]] && [[ "$LSPCI_VGA" != *TU11[6-7]* ]] && return 0
        # Ampere
        [[ "$LSPCI_VGA" == *GA[0-9]* ]] && return 0
        # Ada_Lovelace
        [[ "$LSPCI_VGA" == *AD[0-9]* ]] || [[ "$LSPCI_VGA" == *2[6-8][0-9]* ]] && return 0
    fi
    return 1
}

pw_init_db () {
    if [[ -f "${portwine_exe}" ]] ; then
        PORTWINE_DB="$(basename "${portwine_exe%.[Ee][Xx][Ee]}")"
        export PORTWINE_DB
        if echo "${PORTWINE_DB}" | grep -i "_vo_\|_gv_" &>/dev/null ; then
            print_warning "Force use gamesvoice.ppdb file!"
            PORTWINE_DB_FILE="${PORT_SCRIPTS_PATH}/portwine_db/gamesvoice.ppdb"
            # shellcheck source=/dev/null
            source "${PORTWINE_DB_FILE}"
        elif [[ -f "${portwine_exe}".ppdb ]] ; then
            PORTWINE_DB_FILE="${portwine_exe}".ppdb
            # shellcheck source=/dev/null
            source "${PORTWINE_DB_FILE}"
            if echo "${portwine_exe}" | grep "/prefixes/" &>/dev/null ; then
                if [[ -z $(ls "${PORT_WINE_PATH}/prefixes/" | grep -e "^${PW_PREFIX_NAME}$") ]] \
                || [[ -z $(grep -e "^export PW_PREFIX_NAME=" "${PORTWINE_DB_FILE}" 2>/dev/null) ]]
                then
                    PW_PREFIX_NAME=$(echo "${portwine_exe}" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
                    edit_db_from_gui PW_PREFIX_NAME
                fi
            fi
        else
            IFS=$'\n'
            PW_FIND_DB_FILE=$(grep -ilw "#${PORTWINE_DB}.exe" "${PORT_SCRIPTS_PATH}/portwine_db"/* | sed s/".exe"/""/gi)
            if [[ $(echo "$PW_FIND_DB_FILE" | wc -l) -gt 1 ]] ; then
                unset ADD_CB_DBFILE
                for PW_CHECK_DB_FILE in $PW_FIND_DB_FILE ; do
                    if [[ -z "${ADD_CB_DBFILE}" ]] ; then
                        ADD_CB_DBFILE="$(echo "${PW_CHECK_DB_FILE}" | awk -F'/' '{print $NF}')"
                    else
                        ADD_CB_DBFILE="$(echo "${PW_CHECK_DB_FILE}" | awk -F'/' '{print $NF}')!${ADD_CB_DBFILE}"
                    fi
                done
                PORTWINE_DB_FILE="${PORT_SCRIPTS_PATH}/portwine_db"/$("${pw_yad}" --text-align=center \
                --text "$(gettext "More than one DB file found for the application being launched.\nSelect the required one, or delete the extra DB file before the next run.\n")" --borders="${YAD_BORDERS}" --form \
                --title "Database file selection..." --window-icon="$PW_GUI_ICON_PATH/portproton.svg"  --form --separator="" \
                --field="DB FILE: :CB" "${ADD_CB_DBFILE}" 2>/dev/null)
                if [[ "$?" == "1" || "$?" == "252" ]] ; then exit 0 ; fi
            elif [[ $(echo "$PW_FIND_DB_FILE" | wc -l) -eq 1 ]] ; then
                PORTWINE_DB_FILE="${PW_FIND_DB_FILE}"
            fi
            IFS="$orig_IFS"
            
            if [[ -f "${PW_FIND_DB_FILE}" ]] ; then
                # shellcheck source=/dev/null
                source "${PORTWINE_DB_FILE}"
            fi

            if [[ -f "${PW_FIND_DB_FILE}" ]] \
            && [[ "${PW_DISABLED_CREATE_DB}" != 1 ]]
            then
                try_copy_file "${PW_FIND_DB_FILE}" "${portwine_exe}".ppdb
                PORTWINE_DB_FILE="${portwine_exe}".ppdb
                if echo "${portwine_exe}" | grep "/prefixes/" &>/dev/null ; then
                    if [[ -z $(ls "${PORT_WINE_PATH}/prefixes/" | grep -e "^${PW_PREFIX_NAME}$") ]] \
                    || [[ -z $(grep -e "^export PW_PREFIX_NAME=" "${PORTWINE_DB_FILE}" 2>/dev/null) ]]
                    then
                        PW_PREFIX_NAME=$(echo "${portwine_exe}" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
                        edit_db_from_gui PW_PREFIX_NAME
                    fi
                fi
            fi
        fi

        if [[ -f "${PORTWINE_DB_FILE}" ]] ; then
            # shellcheck source=/dev/null
            source "${PORTWINE_DB_FILE}"
            init_wine_ver &&
            print_info "Use $PORTWINE_DB_FILE db file."
        else
            if [[ "${PW_DISABLED_CREATE_DB}" != 1 ]] ; then
                if [[ ! -z "${PORTWINE_DB}" ]] ; then
                    export PORTWINE_DB_FILE=$(grep -il "#${PORTWINE_DB}.exe" "${PORT_SCRIPTS_PATH}/portwine_db"/*)
                    if [[ -z "${PORTWINE_DB_FILE}" ]] ; then
                        {
                            echo "#!/usr/bin/env bash"
                            echo "#Author: ${USER}"
                            echo "#${PORTWINE_DB}.exe"
                            echo "#Rating=1-5"
                        } > "${portwine_exe}".ppdb
                        export PORTWINE_DB_FILE="${portwine_exe}".ppdb
                    fi
                    if [[ ! -z $(echo "${portwine_exe}" | grep "/data/prefixes/") ]] && \
                        [[ -z $(echo "${portwine_exe}" | grep "/data/prefixes/DEFAULT/") ]]
                    then
                        PW_PREFIX_NAME=$(echo "${portwine_exe}" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
                    fi
                    # shellcheck source=/dev/null
                    source "${PORTWINE_DB_FILE}"
                fi
                edit_db_from_gui PW_VULKAN_USE PW_WINE_USE PW_PREFIX_NAME
            fi
        fi

        if [[ -z "${PATH_TO_GAME}" ]] || [[ ! -d "${PATH_TO_GAME}" ]]; then
            PATH_TO_GAME="$( cd "$( dirname "${portwine_exe}" )" >/dev/null 2>&1 && pwd )"
            export PATH_TO_GAME
        fi

        if [[ "${PW_WINE_CPU_TOPOLOGY}" == "disabled" ]] && [[ ! -z "${WINE_CPU_TOPOLOGY}" ]] ; then
            export PW_WINE_CPU_TOPOLOGY="${WINE_CPU_TOPOLOGY}"
        fi
        if ls "${PATH_TO_GAME}"/*_Data/Resources/ 2>/dev/null | grep "unity" &>/dev/null \
        && [[ "${PW_WINE_CPU_TOPOLOGY}" == "disabled" ]] \
        && [[ $(grep -c "^processor" /proc/cpuinfo) -gt "8" ]]
        then
                export PW_WINE_CPU_TOPOLOGY="8:0,1,2,3,4,5,6,7"
                print_info "Automatic added fix for unity games: WINE_CPU_TOPOLOGY=8:0,1,2,3,4,5,6,7"
        fi

        if check_nvidia_rtx ; then
            check_variables PW_USE_NVAPI_AND_DLSS "1"
            check_variables PW_USE_FAKE_DLSS "0"
            check_variables PW_USE_RAY_TRACING "1"
        fi
    fi

    [[ "${START_FROM_STEAM}" == 1 ]] && export PW_GUI_DISABLED_CS=1
    [[ "${START_FROM_PP_GUI}" == 1 ]] && export PW_GUI_DISABLED_CS=0 && unset START_FROM_PP_GUI

    return 0
}

# GUI UPDATE SCRIPTS
pw_port_update () {
    case "$BRANCH" in
        master|stable)
            BRANCH_VERSION_UPDATE="SCRIPTS_STABLE_VERSION"
            BRANCH="master"
            ;;
         devel|beta)
            BRANCH_VERSION_UPDATE="SCRIPTS_NEXT_VERSION"
            BRANCH="devel"
            ;;
    esac

    if [ ! -f "${PORT_WINE_TMP_PATH}/scripts_ver" ] ; then
        echo "2024" > "${PORT_WINE_TMP_PATH}/scripts_ver"
    fi
    scripts_install_ver=$(head -n 1 "${PORT_WINE_TMP_PATH}/scripts_ver")
    print_info "Check update..."
    if curl -f -s --list-only --connect-timeout 3 "https://gitlab.eterfund.ru/Castro-Fidel/PortWINE/raw/${BRANCH}/data_from_portwine/scripts/var" > "${PORT_WINE_TMP_PATH}/curent_var_ver"
    then
        URL_FOR_CHANGELOG="https://gitlab.eterfund.ru/Castro-Fidel/PortWINE/raw/${BRANCH}/data_from_portwine"
        URL_TO_PW_BRANCH="https://gitlab.eterfund.ru/Castro-Fidel/PortWINE/-/archive/${BRANCH}/PortWINE-${BRANCH}.tar.gz"
    else
        if curl -f -s --list-only --connect-timeout 3 "https://raw.githubusercontent.com/Castro-Fidel/PortWINE/${BRANCH}/data_from_portwine/scripts/var" > "${PORT_WINE_TMP_PATH}/curent_var_ver"
        then
            URL_FOR_CHANGELOG="https://raw.githubusercontent.com/Castro-Fidel/PortWINE/${BRANCH}/data_from_portwine"
            URL_TO_PW_BRANCH="https://github.com/Castro-Fidel/PortWINE/archive/refs/heads/${BRANCH}.tar.gz"
        else
            print_error "Unable to determine the version on the server. Skip it..."
            return 1
        fi
    fi
    [[ "$LANGUAGE" == ru ]] && local PW_CHANGELOG_FILE="changelog_ru" || local PW_CHANGELOG_FILE="changelog_en"
    [[ ! -f "${PORT_WINE_TMP_PATH}/scripts_update_notifier" ]] && echo "1" > "${PORT_WINE_TMP_PATH}/scripts_update_notifier"
    read "scripts_update_not" < "${PORT_WINE_TMP_PATH}/scripts_update_notifier"
    if [[ "${scripts_update_not}" == "1" ]] ; then
        scripts_current_ver=$(grep "$BRANCH_VERSION_UPDATE" "${PORT_WINE_TMP_PATH}/curent_var_ver" | awk -F "=" '{print $2}')
        print_info "Scripts version in git = ${scripts_current_ver}"
        print_info "Scripts version local = ${scripts_install_ver}"
        if [[ ! -z "${scripts_current_ver}" ]] &&  [[ "${scripts_current_ver}" -gt "${scripts_install_ver}" ]] ; then
            if [[ -f "${PW_GUI_THEMES_PATH}/gui/yad_gui_pp" ]]
            then export pw_yad="${PW_GUI_THEMES_PATH}/gui/yad_gui_pp"
            elif command -v yad &>/dev/null
            then local pw_yad="yad"
            else local pw_yad="not_found"
            fi
            if [[ "${pw_yad}" == "not_found" ]] \
            || [[ "$PW_CHANGE_BRANCH" == "1" ]] ; then
                xcsd="$(gettext "UPDATING NOW")"
                unset PW_CHANGE_BRANCH
            else
                curl -s --list-only "${URL_FOR_CHANGELOG}/${PW_CHANGELOG_FILE}" | tee "${PORT_WINE_TMP_PATH}/curent_var_ver"
                CHANGLOG_NEWS=$(sed "/Scripts version ${scripts_install_ver}/,$ d" "${PORT_WINE_TMP_PATH}/curent_var_ver" | sed '1,/---/ d' )
                xcsd=$("${pw_yad}" --title "$(gettext "Update scripts:") v.(${scripts_install_ver}${BRANCH_VERSION})" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
                --form --separator='%%%' --width=1000 --height=500 \
                --field=":TXT" "${CHANGLOG_NEWS}" \
                --button="$(gettext "DO NOT REMIND ME")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":16 \
                --button="$(gettext "REMIND ME LATER")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":18 \
                --button="$(gettext "UPDATING NOW")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":20 2>/dev/null)
                YAD_STATUS="$?"

                # --button="$(gettext "EXIT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":252 \
                
                case "${YAD_STATUS}" in
                    1|252) exit 0 ;;
                       16) xcsd="$(gettext "DO NOT REMIND ME")" ;;
                       18) xcsd="$(gettext "REMIND ME LATER")" ;;
                       20) xcsd="$(gettext "UPDATING NOW")" ;;
                esac
            fi
            case $xcsd in
                "$(gettext "UPDATING NOW")")
                    echo "######################################################"
                    print_info "Update scripts..."
                    try_remove_file "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}.tar.gz"
                    if try_download "${URL_TO_PW_BRANCH}" "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}.tar.gz" no_mirror ; then
                        if unpack "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}.tar.gz" "${PORT_WINE_TMP_PATH}"
                        then
                            create_new_dir "${PORT_WINE_TMP_PATH}/scripts_backup"
                            cd "${PORT_WINE_PATH}/data/" || fatal
                            tar -czpf "${PORT_WINE_TMP_PATH}/scripts_backup/scripts_v.${scripts_install_ver}.tar.gz" \
                            ./scripts ./img/gui ./locales ./themes ./changelog* ./dxvk.conf ./vkBasalt.conf ./tmp/scripts_ver
                            try_remove_dir "${PORT_WINE_PATH}/data/scripts/"
                            cp -fr "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}/data_from_portwine/"* "${PORT_WINE_PATH}/data/"
                            try_remove_file "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}.tar.gz"
                            try_remove_dir "${PORT_WINE_TMP_PATH}/PortWINE-${BRANCH}/"
                            echo "${scripts_current_ver}" > "${PORT_WINE_TMP_PATH}/scripts_ver"
                            echo "Restarting PP after update..."
                            unset SKIP_CHECK_UPDATES
                            /usr/bin/env bash -c ${pw_full_command_line[*]} &
                            exit 0
                        fi
                    else
                        yad_error_download && pw_port_update || exit 1
                    fi ;;
                "$(gettext "REMIND ME LATER")")
                    echo " " ;;
                "$(gettext "DO NOT REMIND ME")")
                    echo "0" > "${PORT_WINE_TMP_PATH}/scripts_update_notifier" ;;
            esac
        fi
    fi
    export scripts_install_ver
    try_remove_file "${PORT_WINE_TMP_PATH}/curent_var_ver"
    return 0
}

update_winetricks () {
    W_TRX_URL="https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks"
    W_TRX_EXT_VER="$(curl -s --list-only ${W_TRX_URL} | grep -i 'WINETRICKS_VERSION=' | sed 's/WINETRICKS_VERSION=//')"
    print_info "Version winetricks on server: ${W_TRX_EXT_VER}"
    W_TRX_INT_VER="$(grep -i 'WINETRICKS_VERSION=' "${PORT_WINE_TMP_PATH}/winetricks" | sed 's/WINETRICKS_VERSION=//')"
    print_info "Version winetricks in port: ${W_TRX_INT_VER}"
    if [[ ! -f "${PORT_WINE_TMP_PATH}/winetricks" && ! -z "$W_TRX_EXT_VER" ]] \
    || [[ "$W_TRX_INT_VER" != "$W_TRX_EXT_VER" && ! -z "$W_TRX_EXT_VER" ]]
    then
        try_remove_file "${PORT_WINE_TMP_PATH}/winetricks"
        if try_download "${W_TRX_URL}" "${PORT_WINE_TMP_PATH}/winetricks" no_mirror ; then
            W_TRX_INT_VER="$(grep -i 'WINETRICKS_VERSION=' "${PORT_WINE_TMP_PATH}/winetricks" | sed 's/WINETRICKS_VERSION=//')"
            print_info "Winetricks version in port has been updated (${W_TRX_INT_VER})"
            chmod u+x "${PORT_WINE_TMP_PATH}/winetricks"
        fi
    fi
    if  [ -f "${PORT_WINE_TMP_PATH}/winetricks" ] ; then
        sed -i 's/w_metadata vcrun2015 dlls \\/w_metadata !dont_use_2015! dlls \\/' "${PORT_WINE_TMP_PATH}/winetricks"
        sed -i 's/w_metadata vcrun2017 dlls \\/w_metadata !dont_use_2017! dlls \\/' "${PORT_WINE_TMP_PATH}/winetricks"
        sed -i 's/w_metadata vcrun2019 dlls \\/w_metadata !dont_use_2019! dlls \\/' "${PORT_WINE_TMP_PATH}/winetricks"
        sed -i 's/w_set_winver win2k3/w_set_winver win7/' "${PORT_WINE_TMP_PATH}/winetricks"
    fi
    return 0
}

edit_db_from_gui () {
    if [[ "${PW_DISABLED_CREATE_DB}" == "1" ]] ; then
        print_warning "Skipped edit_db_from_gui"
        return 0
    fi
    print_info "edit_db_from_gui PORTWINE_DB_FILE=$PORTWINE_DB_FILE"
    if [[ ! -z "$PORTWINE_DB_FILE" ]] \
    && [[ -f "$PORTWINE_DB_FILE" ]]
    then
        for mod_db in $@ ; do
            if ! grep "export ${mod_db}=" "${PORTWINE_DB_FILE}" &>/dev/null
            then echo "export ${mod_db}=\"${!mod_db}\"" >> "${PORTWINE_DB_FILE}"
            elif grep "export ${mod_db}=" "${PORTWINE_DB_FILE}" | grep -v "#" &>/dev/null
            then
                if [[ "$(grep "export ${mod_db}=" "${PORTWINE_DB_FILE}" | grep -v "#")" != "export ${mod_db}=\"${!mod_db}\"" ]]
                then sed -ri "s|^export ${mod_db}=.*|export ${mod_db}=\"${!mod_db}\"|" "${PORTWINE_DB_FILE}"
                fi
            elif [[ "$(grep "export ${mod_db}=" "${PORTWINE_DB_FILE}")" != "export ${mod_db}=${!mod_db}" ]]
            then sed -ri "s|.*export ${mod_db}=.*|export ${mod_db}=\"${!mod_db}\"|g" "${PORTWINE_DB_FILE}"
            fi
        done
    fi
    return 0
}

edit_user_conf_from_gui () {
    for mod_db in $@ ; do
        if ! grep "export ${mod_db}=" "${USER_CONF}" &>/dev/null
        then echo "export ${mod_db}=\"${!mod_db}\"" >> "${USER_CONF}"
        elif grep "export ${mod_db}=" "${USER_CONF}" | grep -v "#" &>/dev/null
        then
            if [[ "$(grep "export ${mod_db}=" "${USER_CONF}" | grep -v "#")" != "export ${mod_db}=\"${!mod_db}\"" ]]
            then sed -ri "s/^export ${mod_db}=.*/export ${mod_db}=\"${!mod_db}\"/" "${USER_CONF}"
            fi
        elif [[ "$(grep "export ${mod_db}=" "${USER_CONF}")" != "export ${mod_db}=${!mod_db}" ]]
        then sed -ri "s/.*export ${mod_db}=.*/export ${mod_db}=\"${!mod_db}\"/g" "${USER_CONF}"
        fi
    done
    return 0
}

pw_create_gui_png () {
    basename_portwine_exe="$(basename "${portwine_exe}")"
    if echo "$basename_portwine_exe" | grep -ie "setup\|install\|\.msi$" &>/dev/null ; then
        export PW_ICON_FOR_YAD="${PORT_WINE_PATH}/data/img/setup.png"
        export name_desktop_png="setup"
        return 0
    elif echo "$basename_portwine_exe" | grep -ie "\.reg$" &>/dev/null ; then
        export PW_ICON_FOR_YAD="${PORT_WINE_PATH}/data/img/reg.png"
        export name_desktop_png="reg"
        return 0
    elif echo "$basename_portwine_exe"| grep -ie "\.bat$" &>/dev/null ; then
        export PW_ICON_FOR_YAD="${PORT_WINE_PATH}/data/img/bat.png"
        export name_desktop_png="bat"
        return 0
    fi

    if [[ -z "$PORTPROTON_NAME" ]] \
    || [[ "$PW_NO_RESTART_PPDB" == "1" ]]
    then
        if [[ ! -z "${PORTWINE_CREATE_SHORTCUT_NAME}" ]] ; then
            PORTPROTON_NAME="${PORTWINE_CREATE_SHORTCUT_NAME}"
        else
            if command -v exiftool &>/dev/null ; then
                if ! PW_PRODUCTNAME=$(timeout 3 exiftool -ProductName "${portwine_exe}" 2>/dev/null | sed -n 's/^Product Name\s*:\s*//p') ; then
                    print_error "exiftool - broken!"
                    if [[ ! -z $PW_DEBUG ]] ; then
                        debug_timer --start
                        timeout 5 exiftool -ProductName "${portwine_exe}"
                        debug_timer --end "exiftool"
                    fi
                fi
            else
                print_warning "use portable exiftool"
                PW_PRODUCTNAME=$(env PERL5LIB="${PW_PLUGINS_PATH}/portable/lib/perl5" "${PW_PLUGINS_PATH}/portable/bin/exiftool" -ProductName "${portwine_exe}" | sed -n 's/^Product Name\s*:\s*//p')
            fi

            if [[ "$PW_PRODUCTNAME" =~ "Launcher" ]] \
            || [[ "$PW_PRODUCTNAME" =~ "RU" ]]
            then
                PW_PRODUCTNAME="$(echo "$PW_PRODUCTNAME" | sed 's/Launcher\|RU//g')"
            fi

            if [[ ! -z "$PW_PRODUCTNAME" ]] \
            && [[ "$PW_PRODUCTNAME" != Bootstrap* ]]
            then
                PORTPROTON_NAME="$PW_PRODUCTNAME"
            else
                PORTPROTON_NAME="$(basename "${portwine_exe%.[Ee][Xx][Ee]}")"
            fi
        fi

        PORTPROTON_NAME="$(echo "${PORTPROTON_NAME}" | sed "s/\`//g" | sed "s/\"//g" | sed "s/'//g" | sed "s/\!//g")"
        export PORTPROTON_NAME
        edit_db_from_gui PORTPROTON_NAME
    fi

    resize_png "$portwine_exe" "${PORTPROTON_NAME}" "128"

    PORTPROTON_NAME_PNG="${PORTPROTON_NAME// /_}"
    if [[ -z "${PW_ICON_FOR_YAD}" ]] ; then
        if [[ ! -z "$(file "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME_PNG}.png" | grep "${PW_RESIZE_TO} x ${PW_RESIZE_TO}")" ]] ; then
            export PW_ICON_FOR_YAD="${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME_PNG}.png"
        else
            export PW_ICON_FOR_YAD="${PW_GUI_ICON_PATH}/port_proton.png"
        fi
    fi
}

pw_find_exe () {
    unset PW_FIND_TIME
    case "$1" in
        "news") PW_FIND_TIME="-cmin -180" ;;
    esac

    PW_EXCLUDE_EXE_FIND='(windows|explorer|anticheat|programdata|redist|setup|.ppdb|unin|error|crash|handler|install|/jre/|steam|uploader|eac|netframe)'
    PW_PATH_FOR_FIND="${PORT_WINE_PATH}/data/prefixes/"

    pw_start_progress_bar_block "$(gettext "Searching for .exe files... Please wait.")"
    find "${PW_PATH_FOR_FIND}" -type f -name '*.exe' ${PW_FIND_TIME} | grep -viE ${PW_EXCLUDE_EXE_FIND} | \
    awk -F"/prefixes/" '{print $2}' > "${PW_TMPFS_PATH}/tmp_yad_find_exe"
    pw_stop_progress_bar

    unset FIND_TO_GUI
    while read line; do
        FIND_TO_GUI+="${line}%"
    done < "${PW_TMPFS_PATH}/tmp_yad_find_exe"

    IFS="%"
    PW_SET_FIND_EXE="$("${pw_yad}" --height="500" --width="800" --list --text-align=center \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(gettext "Create shortcut for...")" \
    --text="\n$(gettext "Choose the .exe file for which you need to create a shortcut and click OK.\n")" \
    --column="$(gettext "Choose path to .exe file:")" ${FIND_TO_GUI} \
    --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null)"
    YAD_STATUS="$?"
    if [[ "$YAD_STATUS" == "1" || "$YAD_STATUS" == "252" ]] ; then
        if [[ -z "${PW_SET_FIND_EXE}" ]] ; then
            export SKIP_CHECK_UPDATES="1"
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            print_info "Restarting..."
        fi
        exit 0
    fi
    IFS="$orig_IFS"

    if [[ ! -z "${PW_SET_FIND_EXE}" ]] ; then
        portwine_exe="${PW_PATH_FOR_FIND}$(echo "${PW_SET_FIND_EXE}" | awk -F'|' '{print $1}')"
        portwine_create_shortcut silent
        export SKIP_CHECK_UPDATES="1"
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    fi
}

pw_create_unique_exe () {
    BASEDIR_GAME="$(dirname "$portwine_exe")"
    if [[ -d "$BASEDIR_GAME" ]] ; then
        pushd "$BASEDIR_GAME" || fatal
        BASENAME_GAME_EXE="$(basename "$portwine_exe")"
        if [[ ! -z "$1" ]] ; then
            BASENAME_GAME="$(basename "$1" .exe).exe"
            ln -sf "$BASENAME_GAME_EXE" "$BASENAME_GAME"
            export portwine_exe="$BASEDIR_GAME/$BASENAME_GAME"
        elif [[ ! -z "$PORTWINE_CREATE_SHORTCUT_NAME" ]] ; then
            ln -sf "$BASENAME_GAME_EXE" "$PORTWINE_CREATE_SHORTCUT_NAME.exe"
            export portwine_exe="$BASEDIR_GAME/$PORTWINE_CREATE_SHORTCUT_NAME.exe"
        else
            print_warning "There are no arguments for creating a symbolic link! Skip it..."
        fi
        popd || fatal
    fi
}

start_portwine () {
    if [[ "${PW_LOCALE_SELECT}" != "disabled" ]] && [[ ! -z "${PW_LOCALE_SELECT}" ]] ; then
        export LC_ALL="${PW_LOCALE_SELECT}"
        if [[ "${PW_USE_RUNTIME}" == "1" ]] && [[ "${HOST_LC_ALL}" != "${LC_ALL}" ]] ; then
            export HOST_LC_ALL="${LC_ALL}"
        fi
    else
        export PW_LOCALE_SELECT="disabled"
    fi

    WINEPREFIX="$(readlink -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}")"
    export WINEPREFIX

    pw_get_pfx () {
        case "${PW_PREFIX_NAME}" in
            PROGRAMS) local FILE_NAME_PFX="progpfx${PROGPFX_VER}" ;;
              DOTNET) local FILE_NAME_PFX="dotpfx${DOTPFX_VER}" ;;
                   *) return 0 ;;
        esac

        local PFX_URL="github.com/Castro-Fidel/PortWINE/releases/download/${FILE_NAME_PFX}/${FILE_NAME_PFX}.tar.xz"
        local PFX_TMP="${PORT_WINE_PATH}/data/tmp/pfx"
        create_new_dir "${PFX_TMP}"

        # TODO: drop this fix legacy file .dotnet later
        if [[ -f "${WINEPREFIX}/.dotnet${DOTPFX_VER}" ]] ; then
            touch "${WINEPREFIX}/.${FILE_NAME_PFX}"
            try_remove_file "${WINEPREFIX}/.dotnet${DOTPFX_VER}"
        fi

        if [[ ! -f "${WINEPREFIX}/.${FILE_NAME_PFX}" ]] ; then
            print_info "Download and install ${FILE_NAME_PFX} pfx..."
            if [[ ! -f "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz" ]] ; then
                if try_download "${PFX_URL}" "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz" ; then
                    if unpack "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz" "${PORT_WINE_PATH}/data/prefixes/"
                    then touch "${WINEPREFIX}/.${FILE_NAME_PFX}"
                    fi
                else
                    try_remove_file "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz"
                    yad_error_download && exit 1
                fi
            else
                if unpack "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz" "${PORT_WINE_PATH}/data/prefixes/"
                then touch "${WINEPREFIX}/.${FILE_NAME_PFX}"
                else
                    try_remove_file "${PFX_TMP}/${FILE_NAME_PFX}.tar.xz"
                    yad_error_download && exit 1 
                fi
            fi
        fi
        return 0
    }

    pw_get_pfx

    var_winedlloverride_update "${PW_MUST_WINEDLLOVERRIDES}"
    pw_list_install_dll_in_prefix
    if [[ "${PW_WINE_USE}" != "USE_SYSTEM_WINE" ]]
    then pw_check_and_download_wine "${PW_WINE_USE}"
    fi
    init_wine_ver

    if [[ "$PW_USE_OBS_VKCAPTURE" == "1" ]] ; then
        export OBS_VKCAPTURE="1"
        export PW_USE_SYSTEM_VK_LAYERS="1"
        print_warning "System mangohud, vkBasalt, obs-vk capture and other applications using vulkan layers are forcibly used."
    fi

    pw_init_runtime

    if [[ "${PW_USE_GSTREAMER}" == "1" ]] ; then
        if [[ -d "${WINEDIR}/lib64/gstreamer-1.0" ]] \
        && ! check_flatpak
        then
            export GST_PLUGIN_SYSTEM_PATH_1_0="${WINEDIR}/lib64/gstreamer-1.0:${WINEDIR}/lib/gstreamer-1.0"
        fi
        export GST_GL_WINDOW="surfaceless"
        create_new_dir "${PORT_WINE_TMP_PATH}"/gstreamer_tmp/
        export WINE_GST_REGISTRY_DIR="${PORT_WINE_TMP_PATH}"/gstreamer_tmp/
        export MEDIACONV_AUDIO_DUMP_FILE="${PORT_WINE_TMP_PATH}"/gstreamer_tmp/audiov2.foz
        export MEDIACONV_AUDIO_TRANSCODED_FILE="${PORT_WINE_TMP_PATH}"/gstreamer_tmp/transcoded_audio.foz
        export MEDIACONV_VIDEO_DUMP_FILE="${PORT_WINE_TMP_PATH}"/gstreamer_tmp/video.foz
        export MEDIACONV_VIDEO_TRANSCODED_FILE="${PORT_WINE_TMP_PATH}"/gstreamer_tmp/transcoded_video.foz
    else
        var_winedlloverride_update "winegstreamer="
    fi

    check_variables VKD3D_DEBUG none
    check_variables DXVK_LOG_LEVEL none
    check_variables VK_LOADER_DEBUG none
    check_variables VKBASALT_LOG_LEVEL none
    check_variables DXVK_NVAPI_LOG_LEVEL none
    if [[ "${PW_LOG}" == 1 ]] \
    || [[ ! -z "$$PW_DEBUG" ]]
    then
        export WINEDEBUG="fixme-all,err+loaddll,err+dll,err+file,err+reg"
        export WINE_MONO_TRACE="E:System.NotImplementedException"
        export VKBASALT_LOG_LEVEL="error"
    else
        export WINEDEBUG="-all"
        unset WINE_MONO_TRACE
    fi

    pw_wineboot () {
        LIST_NATIVE_WINE_DLL_LINKS="winevulkan ir50_32 wined3d vulkan-1"
        for change_wine_dll in $LIST_NATIVE_WINE_DLL_LINKS ; do
            try_force_link_file "${WINEDIR}/lib64/wine/x86_64-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/${change_wine_dll}.dll"
            try_force_link_file "${WINEDIR}/lib/wine/i386-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${change_wine_dll}.dll"
        done

        LIST_NATIVE_WINE_DLL_COPY="atl100 ntdll shell32"
        for change_wine_dll in $LIST_NATIVE_WINE_DLL_COPY ; do
            try_copy_file "${WINEDIR}/lib64/wine/x86_64-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/${change_wine_dll}.dll"
            try_copy_file "${WINEDIR}/lib/wine/i386-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${change_wine_dll}.dll"
        done

        print_info "Used wineboot $@ for prefix: ${PW_PREFIX_NAME}"
        ${pw_runtime} GST_PLUGIN_SYSTEM_PATH_1_0="" LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" \
        WINEDEBUG="fixme-all,err+loaddll,err+dll,err+file,err+reg" \
        "${WINELOADER}" wineboot $@ &>>"${PW_TMPFS_PATH}/update_pfx_log"
        wait_wineserver
        print_info "The prefix has been updated."
    }

    if [[ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/drive_c/windows ]] \
    || [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/userdef.reg ]] \
    || [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/system.reg ]] \
    || [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/user.reg ]]
    then
        pw_clear_pfx
        check_dirs_and_files_in_pfx
        if [[ "${PW_PREFIX_NAME}" == "DEFAULT" ]] ; then
            pw_update_pfx_cover_gui
            unpack "${PW_PLUGINS_PATH}/default_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" silent
            pw_wineboot -r
        elif [[ "${PW_CHECK_AUTOINSTALL}" == 1 ]] \
        && [[ "${DISABLE_CP_DEFPFX}" != 1 ]] ; then
            pw_update_pfx_cover_gui
            unpack "${PW_PLUGINS_PATH}/default_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" silent
            pw_wineboot -r
        elif [[ "${PW_CHECK_AUTOINSTALL}" == 1 ]] \
        && [[ "${DISABLE_CP_DEFPFX}" == 1 ]] ; then
            pw_update_pfx_cover_gui
            unpack "${PW_PLUGINS_PATH}/blank_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" silent
            pw_wineboot -i
        elif yad_question "$(gettext "Do you want to installing recommended libraries in the new prefix:") ${PW_PREFIX_NAME}" ; then
            pw_update_pfx_cover_gui
            unpack "${PW_PLUGINS_PATH}/default_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" silent
            pw_wineboot -r
        else
            pw_update_pfx_cover_gui
            unpack "${PW_PLUGINS_PATH}/blank_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" silent
            pw_wineboot -i
        fi
    else
        check_dirs_and_files_in_pfx
        if [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver" ]] \
        || [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/shell32.dll" ]] \
        || ! grep "${PW_WINE_USE}" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver" &>/dev/null
        then
            pw_update_pfx_cover_gui
            # # HOTFIX - update prefix for vkPlay
            # if echo "$portwine_exe" | grep -i vkplay &>/dev/null
            # then pw_kill_autostart GameCe 5 please & :
            # fi
            pw_wineboot -r
        fi
    fi
    rm -f "${PORT_WINE_PATH}"/data/prefixes/*/drive_c/users/*/Desktop/*.lnk

    echo "${PW_WINE_USE}" > "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver"

    if [[ "${PW_NO_ESYNC}" == 1 ]]
    then export WINEESYNC="0"
    else export WINEESYNC="1"
    fi

    if [[ "${PW_NO_FSYNC}" == 1 ]] ; then
        export WINEFSYNC="0"
        export WINEFSYNC_FUTEX2="0"
    else
        export WINEFSYNC="1"
        export WINEFSYNC_FUTEX2="1"
        export WINEFSYNC_SPINCOUNT=100
    fi

    if [[ "${PW_FORCE_LARGE_ADDRESS_AWARE}" == 1 ]]
    then export WINE_LARGE_ADDRESS_AWARE="1"
    else export WINE_LARGE_ADDRESS_AWARE="0"
    fi

    if [[ "${PW_OLD_GL_STRING}" == 1 ]] ; then
        export MESA_EXTENSION_MAX_YEAR="2003" #mesa override
        export __GL_ExtensionStringVersion="17700" #nvidia override
    fi

    if [[ "${PW_NO_WRITE_WATCH}" == 1 ]] ; then
        export WINE_DISABLE_WRITE_WATCH="1"
    fi

    if [[ "${PW_VULKAN_NO_ASYNC}" == 1 ]] ; then
        export RADV_DEBUG="llvm"
        var_vkd3d_config_update single_queue
    fi

    enabled_fake_nvidia_videocard ()
    {
        case "$1" in
            old)
                sed -i /'dxgi.customDeviceDesc =/c # dxgi.customDeviceDesc =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customDeviceId =/c dxgi.customDeviceId = 222F' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customVendorId =/c dxgi.customVendorId = 10de' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideAmdGpu =/c # dxgi.hideAmdGpu =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideNvidiaGpu =/c # dxgi.hideNvidiaGpu =' "${DXVK_CONFIG_FILE}"
                unset WINE_HIDE_NVIDIA_GPU WINE_HIDE_AMD_GPU
                unset DXVK_NVAPI_DRIVER_VERSION DXVK_NVAPI_ALLOW_OTHER_DRIVERS
                ;;

            new)
                sed -i /'dxgi.customDeviceDesc =/c dxgi.customDeviceDesc = "NVIDIA GeForce RTX 4090"' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customDeviceId =/c dxgi.customDeviceId = 2684' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customVendorId =/c dxgi.customVendorId = 10de' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideAmdGpu =/c dxgi.hideAmdGpu = True' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideNvidiaGpu =/c dxgi.hideNvidiaGpu = False' "${DXVK_CONFIG_FILE}"
                export WINE_HIDE_NVIDIA_GPU="0"
                export WINE_HIDE_AMD_GPU="1"
                export DXVK_NVAPI_DRIVER_VERSION="53713"
                export DXVK_NVAPI_ALLOW_OTHER_DRIVERS="1"
                export WINEHAGS="1"
                ;;
                
            *)
                sed -i /'dxgi.customDeviceDesc =/c # dxgi.customDeviceDesc =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customDeviceId =/c # dxgi.customDeviceId =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.customVendorId =/c # dxgi.customVendorId =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideAmdGpu =/c # dxgi.hideAmdGpu =' "${DXVK_CONFIG_FILE}"
                sed -i /'dxgi.hideNvidiaGpu =/c # dxgi.hideNvidiaGpu =' "${DXVK_CONFIG_FILE}"
                unset WINE_HIDE_NVIDIA_GPU WINE_HIDE_AMD_GPU
                unset DXVK_NVAPI_DRIVER_VERSION DXVK_NVAPI_ALLOW_OTHER_DRIVERS
                ;;
        esac
    }

    # NVAPI, DLSS AND FAKE DLSS
    if [[ "${PW_USE_FAKE_DLSS_3}" == "1" ]] ; then
        FAKE_DLSS_3_FILES="amd_fidelityfx_dx12.dll amd_fidelityfx_vk.dll dlss-enabler.dll dlss-enabler-upscaler.dll 
        dlssg_to_fsr3_amd_is_better-3.0.dll dlssg_to_fsr3_amd_is_better.dll libxess.dll nvapi64-proxy.dll winmm.dll
        _nvngx.dll nvngx-wrapper.dll nvngx.ini"
        DXVK_ENABLE_NVAPI="1"
        for cp_fdlls3 in $FAKE_DLSS_3_FILES ; do
            try_copy_file_with_checksums "${PW_PLUGINS_PATH}/fake_dlss_3/$cp_fdlls3" "${WINEPREFIX}/drive_c/windows/system32/$cp_fdlls3"
            [[ "$cp_fdlls3" == *.dll ]] && var_winedlloverride_update "${cp_fdlls3//.dll}=n"
        done
        try_remove_file "${WINEPREFIX}/drive_c/windows/system32/nvngx.dll"
        # try_copy_file_with_checksums "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/nvngx.dll"
        enabled_fake_nvidia_videocard new
    elif [[ "${PW_USE_FAKE_DLSS}" == "1" ]] ; then
        DXVK_ENABLE_NVAPI="1"
        try_remove_file "${WINEPREFIX}/drive_c/windows/system32/_nvngx.dll"
        try_copy_file_with_checksums "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/nvngx.dll"
        try_copy_file_with_checksums "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.ini" "${WINEPREFIX}/drive_c/windows/system32/nvngx.ini"
        enabled_fake_nvidia_videocard old
        var_winedlloverride_update "nvapi,nvapi64,nvngx=n;_nvngx=;nvcuda=b"
    elif [[ "${PW_USE_NVAPI_AND_DLSS}" == 1 ]] ; then
        DXVK_ENABLE_NVAPI="1"
        try_remove_file "${WINEPREFIX}/drive_c/windows/system32/nvngx.ini"
        FIND_NVNGX="$(dirname $(find /usr/* -type f -name "nvngx.dll" 2>/dev/null | head -n 1 | awk '{print $1}'))"
        if [[ ! -z "$FIND_NVNGX" ]] ; then
            try_copy_file_with_checksums "${FIND_NVNGX}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/nvngx.dll"
            try_copy_file_with_checksums "${FIND_NVNGX}/_nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/_nvngx.dll"
        else
            try_copy_file_with_checksums "${PW_PLUGINS_PATH}/nvngx/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/nvngx.dll"
            try_copy_file_with_checksums "${PW_PLUGINS_PATH}/nvngx/_nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/_nvngx.dll"
        fi
        enabled_fake_nvidia_videocard 0
        var_winedlloverride_update "nvngx,_nvngx,nvapi,nvapi64=n;nvcuda=b"
    else
        DXVK_ENABLE_NVAPI="0"
        for rm_dll in "nvngx.ini" "nvngx.dll" "_nvngx.dll" ; do
            try_remove_file "${WINEPREFIX}/drive_c/windows/system32/$rm_dll"
        done
        enabled_fake_nvidia_videocard 0
        var_winedlloverride_update "nvngx,_nvngx="
    fi
    export DXVK_ENABLE_NVAPI

    if [[ "${PW_HEAP_DELAY_FREE}" == 1 ]]
    then export WINE_HEAP_DELAY_FREE="1"
    else export WINE_HEAP_DELAY_FREE="0"
    fi

    if [[ "${PW_WINE_ALLOW_XIM}" == 1 ]]
    then export WINE_ALLOW_XIM="1"
    else export WINE_ALLOW_XIM="0"
    fi

    if [[ "${PW_FIX_VIDEO_IN_GAME}" == 1 ]]
    then export WINE_DO_NOT_CREATE_DXGI_DEVICE_MANAGER="1"
    else export WINE_DO_NOT_CREATE_DXGI_DEVICE_MANAGER="0"
    fi

    [[ "${PW_MANGOHUD_USER_CONF}" == 1 ]] && unset MANGOHUD_CONFIG
    [[ "${PW_VKBASALT_USER_CONF}" == 1 ]] && unset PW_VKBASALT_EFFECTS PW_VKBASALT_FFX_CAS

    if [[ "${PW_USE_DXR11}" == 1 ]] \
    || [[ "${PW_USE_DXR10}" == 1 ]] \
    || [[ "${PW_USE_RAY_TRACING}" == 1 ]]
    then
        var_vkd3d_config_update dxr
        var_radv_perftest_config_update rt
    else
        var_vkd3d_config_update nodxr
    fi

    if [[ "${PW_GPU_USE}" != "disabled" ]] ; then
        export DXVK_FILTER_DEVICE_NAME="${PW_GPU_USE}"
        export VKD3D_FILTER_DEVICE_NAME="${PW_GPU_USE}"
    fi

    case "$PW_FORCE_USE_VSYNC" in  # 0-FORCE_OFF, 1-FORCE_ON, 2-BY_DEFAULT
        0)
            export vblank_mode=0
            export __GL_SYNC_TO_VBLANK=0
            ;;
        1)
            export vblank_mode=1
            export __GL_SYNC_TO_VBLANK=1
            ;;
        *)
            unset vblank_mode __GL_SYNC_TO_VBLANK
            ;;
    esac

    export int_xneur=0
    if command -v xneur &>/dev/null \
    && pgrep xneur
    then
        killall xneur
        export int_xneur=1
    fi

    if check_gamescope_session ; then
        export PW_GAMEMODERUN_SLR=""
    elif [[ "$PW_USE_GAMEMODE" = "1" ]] \
    && [[ ! -z "$DBUS_SESSION_BUS_ADDRESS"  ]]
    then
        if command -v gamemoded &>/dev/null ; then
            export GAMEMODERUN=1
            PW_GAMEMODERUN_SLR="gamemoderun"
            systemctl enable --now --user gamemoded &>/dev/null
            print_info "Gamemod will be launched."
        elif check_flatpak ; then
            export GAMEMODERUN=1
            PW_GAMEMODERUN_SLR="gamemoderun"
            print_info "Gamemod will be launched."
        elif [[ "$PW_USE_RUNTIME" == 1 ]] ; then
            export GAMEMODERUN=1
            if ! pidof gamemoded &>/dev/null ; then
                GAMEMODEAUTO_NAME="libgamemodeauto.so.0"
                if [[ ! -z "${PW_LD_PRELOAD}" ]]
                then export PW_LD_PRELOAD="${PW_LD_PRELOAD}:${GAMEMODEAUTO_NAME}"
                else export PW_LD_PRELOAD="${GAMEMODEAUTO_NAME}"
                fi

                env LD_LIBRARY_PATH="${PW_PLUGINS_PATH}/portable/lib/lib64:${PW_PLUGINS_PATH}/portable/lib/lib32" \
                "${PW_PLUGINS_PATH}/portable/bin/gamemoded" &>/dev/null &

                print_info "Gamemod will be launched."
                sleep 0.1
            fi
        else
            export GAMEMODERUN=0
            export PW_GAMEMODERUN_SLR=""
        fi
    else
        export GAMEMODERUN=0
        export PW_GAMEMODERUN_SLR=""
        print_info "Gamemod is not installed or disabled in vars script or db file: PW_USE_GAMEMODE=$PW_USE_GAMEMODE"
    fi
    pw_other_fixes

    # enabled BattleEye_Runtime and EasyAntiCheat_Runtime
    if [[ "${PW_USE_EAC_AND_BE}" == 1 ]] ; then
        export PROTON_BATTLEYE_RUNTIME="${PW_PLUGINS_PATH}/BattlEye_Runtime"
        export PROTON_EAC_RUNTIME="${PW_PLUGINS_PATH}/EasyAntiCheat_Runtime"
        var_winedlloverride_update "beclient,beclient_x64=b,n"
    else
        unset PROTON_BATTLEYE_RUNTIME PROTON_EAC_RUNTIME
    fi

    if [[ "${PW_REDUCE_PULSE_LATENCY}" == 1 ]] ; then
        export PULSE_LATENCY_MSEC=60
        # export PIPEWIRE_LATENCY=128/48000
    else
        unset PULSE_LATENCY_MSEC
    fi

    if [[ "$PW_USE_US_LAYOUT" == "1" ]] \
    && ! check_wayland_session \
    && command -v setxkbmap &>/dev/null
    then
        setxkbmap -model pc101 us -print | xkbcomp - $DISPLAY &>/dev/null
    else
        export PW_USE_US_LAYOUT="0"
    fi

    D3D_EXTRAS_LIBS="d3dcompiler_33 d3dcompiler_34 d3dcompiler_35 d3dcompiler_36 d3dcompiler_37
    d3dcompiler_38 d3dcompiler_39 d3dcompiler_40 d3dcompiler_41 d3dcompiler_42 d3dcompiler_43 d3dcompiler_46
    d3dcompiler_47 d3dx10_33 d3dx10_34 d3dx10_35 d3dx10_36 d3dx10_37 d3dx10_38 d3dx10_39 d3dx10_40 d3dx10_41
    d3dx10_42 d3dx10_43 d3dx10 d3dx11_42 d3dx11_43 d3dx9_24 d3dx9_25 d3dx9_26 d3dx9_27 d3dx9_28 d3dx9_29 d3dx9_30
    d3dx9_31 d3dx9_32 d3dx9_33 d3dx9_34 d3dx9_35 d3dx9_36 d3dx9_37 d3dx9_38 d3dx9_39 d3dx9_40 d3dx9_41 d3dx9_42 d3dx9_43"
    if [[ "${PW_USE_D3D_EXTRAS}" == 1 ]] ; then
        if [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ]] \
        || [[ -z "$(grep d3dx9 "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log")" ]]
        then
            echo "d3dx9" >> "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
        fi
        # try link d3d extras libs
        PATH_TO_D3DEXTRAS="${PW_PLUGINS_PATH}/d3d_extras"
        for d3d_extras_from_plugins in $D3D_EXTRAS_LIBS ; do
            try_force_link_file "${PATH_TO_D3DEXTRAS}/x64/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/system32/${d3d_extras_from_plugins}.dll"
            try_force_link_file "${PATH_TO_D3DEXTRAS}/x32/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${d3d_extras_from_plugins}.dll"
            var_winedlloverride_update "${d3d_extras_from_plugins}=n"
            if [[ -z "$(grep "${d3d_extras_from_plugins}" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log")" ]] ; then
                echo "${d3d_extras_from_plugins}" >> "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
            fi
        done
    else
        for d3d_extras_from_plugins in $D3D_EXTRAS_LIBS ; do
            try_force_link_file "${WINEDIR}/lib/wine/i386-windows/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${d3d_extras_from_plugins}.dll"
            try_force_link_file "${WINEDIR}/lib64/wine/x86_64-windows/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/system32/${d3d_extras_from_plugins}.dll"
        done
    fi

    # fix physx
    PHYSX_PATH="${WINEPREFIX}/drive_c/Program Files (x86)/NVIDIA Corporation/PhysX/Common"
    for physx_dll in PhysXDevice PhysXLoader PhysXUpdateLoader ; do
        try_force_link_file "${PHYSX_PATH}/${physx_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${physx_dll}.dll"
        try_force_link_file "${PHYSX_PATH}/${physx_dll}64.dll" "${WINEPREFIX}/drive_c/windows/system32/${physx_dll}64.dll"
    done
    try_force_link_file "${PHYSX_PATH}/cudart32_65.dll" "${WINEPREFIX}/drive_c/windows/syswow64/cudart32_65.dll"
    try_force_link_file "${PHYSX_PATH}/cudart64_65.dll" "${WINEPREFIX}/drive_c/windows/system32/cudart64_65.dll"

    # 3D API
    unset CP_VKD3D_FILES CP_DXVK_FILES CP_DGV2_FILES CP_WINE_FILES
    unset CP_GALLIUM_NINE_FILES PATH_TO_DXVK_FILES PATH_TO_VKD3D_FILES
    case "${PW_VULKAN_USE}" in
        0)
            # WINED3D OPENGL
            print_info "Use WINED3D OpenGL"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_VKD3D_FILES="libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12 d3d12core d3d11 d3d10core d3d10_1 d3d10 d3d9 d3d8 dxgi"
            export __GL_YIELD="NOTHING"
            export mesa_glthread="true"
            export WINE_D3D_CONFIG="renderer=gl"
            export PW_VKBASALT="0"
            ;;
        1)
            # STABLE DXVK AND VKD3D
            print_info "DXVK v.${DXVK_STABLE_VER} in use"
            print_info "VKD3D-PROTON v.${VKD3D_STABLE_VER} in use"
            PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_STABLE_VER}"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_STABLE_VER}"
            CP_DXVK_FILES="d3d11 d3d10core d3d10_1 d3d10 d3d9 dxgi" # openvr_api_dxvk
            CP_VKD3D_FILES="d3d12 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12core d3d8"
            ;;
        2)
            # NEWEST DXVK AND VKD3D
            print_info "DXVK v.${DXVK_GIT_VER} in use"
            print_info "VKD3D-PROTON v.${VKD3D_GIT_VER} in use"
            PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_GIT_VER}"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_DXVK_FILES="d3d11 d3d10core d3d9 d3d8 dxgi" # openvr_api_dxvk
            CP_VKD3D_FILES="d3d12 d3d12core libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d10_1 d3d10"
            ;;
        3)
            # GALLIUM NINE
            print_info "Use GALLIUM-NINE (Native DX9 on MESA drivers)"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_GALLIUM_NINE_FILES="d3d9"
            CP_VKD3D_FILES="libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12 d3d12core d3d11 d3d10core d3d10_1 d3d10 d3d8 dxgi"
            export __GL_YIELD="NOTHING"
            export mesa_glthread="true"
            export WINE_D3D_CONFIG="renderer=gl"
            export PW_VKBASALT="0"
            export PW_MANGOHUD="0"
            export PW_WINE_FULLSCREEN_FSR="0"
            if echo "$LSPCI_VGA" | grep -i "AMD" &>/dev/null \
            && ! check_wayland_session
            then
                if check_gamescope_session \
                || [[ "${PW_GAMESCOPE}" = "1" ]]
                then
                    export AMD_DEBUG="nodcc"
                fi
            fi
            ;;
        4)
            # GALLIUM ZINK
            print_info "Use GALLIUM-ZINK (OpenGL on MESA vulkan drivers)"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_VKD3D_FILES="libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12 d3d12core d3d11 d3d10core d3d10_1 d3d10 d3d9 d3d8 dxgi"
            export __GL_YIELD="NOTHING"
            export mesa_glthread="true"
            export WINE_D3D_CONFIG="renderer=gl"
            export  __GLX_VENDOR_LIBRARY_NAME="mesa"
            export MESA_LOADER_DRIVER_OVERRIDE="zink"
            export GALLIUM_DRIVER="zink"
            if ! check_wayland_session \
            && ! check_gamescope_session \
            && [[ "${PW_GAMESCOPE}" != "1" ]]
            then
                export LIBGL_KOPPER_DRI2="1"
            fi
            ;;
        5)
            # LEGACY DXVK
            print_info "DXVK v.${DXVK_LEGACY_VER} in use"
            PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_LEGACY_VER}"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_DXVK_FILES="d3d11 d3d10core d3d10_1 d3d10 d3d9 dxgi"
            CP_VKD3D_FILES="libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12 d3d12core d3d8"
            ;;
        6)
            # WINED3D VULKAN (DAMAVAND)
            print_info "Use DAMAVAND (DirectX to wined3d vulkan)"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            CP_VKD3D_FILES="libvkd3d-1 libvkd3d-shader-1"
            CP_WINE_FILES="d3d12 d3d12core d3d11 d3d10core d3d10_1 d3d10 d3d9 d3d8 dxgi"
            export WINE_D3D_CONFIG="renderer=vulkan"
            ;;
    esac

    # DGVOODOO2 enable:
    if [[ "${PW_DGVOODOO2}" == "1" ]] ; then
        print_info "Try create symlink DGVOODOO2 files..."
        PATH_TO_DGV2_FILES="${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}"
        CP_DGV2_FILES="d3d9 d3d8 d3dimm ddraw glide3x glide2x glide"
        try_copy_file "${PATH_TO_DGV2_FILES}/dgVoodoo.conf" "${WINEPREFIX}/drive_c/windows/syswow64/"
        DGV2CONF="${WINEPREFIX}/drive_c/windows/syswow64/dgVoodoo.conf"
        if [[ "${PW_DGV2_DISABLE_D3D}" == "1" ]] ; then
            rm_from_var CP_DGV2_FILES "d3d9 d3d8"
        else
            try_force_link_file "${PATH_TO_DGV2_FILES}/x64/d3d9.dll" "${WINEPREFIX}/drive_c/windows/system32/d3d9.dll"
            try_remove_file "${WINEPREFIX}/drive_c/windows/system32/dgVoodoo.conf"
            try_force_link_file "${DGV2CONF}" "${WINEPREFIX}/drive_c/windows/system32/dgVoodoo.conf"
            case "${PW_VULKAN_USE}" in
                0)
                    rm_from_var CP_WINE_FILES "d3d9 d3d8"
                    ;;
                1)
                    rm_from_var CP_DXVK_FILES "d3d9"
                    rm_from_var CP_WINE_FILES "d3d8"
                    ;;
                2)
                    rm_from_var CP_DXVK_FILES "d3d9 d3d8"
                    ;;
                3)
                    rm_from_var CP_GALLIUM_NINE_FILES "d3d9"
                    rm_from_var CP_WINE_FILES "d3d8"
                    ;;
                4)
                    rm_from_var CP_WINE_FILES "d3d9 d3d8"
                    ;;
                5)
                    rm_from_var CP_DXVK_FILES "d3d9"
                    rm_from_var CP_WINE_FILES "d3d8"
                    ;;
                6)
                    rm_from_var CP_WINE_FILES "d3d9 d3d8"
                    ;;
            esac
        fi

        if [[ "${PW_DGV2_GLIDE_NAPALM}" == "1" ]] ; then
            rm_from_var CP_DGV2_FILES "glide3x"
            try_force_link_file "${PATH_TO_DGV2_FILES}/x32/glide3x-napalm.dll" "${WINEPREFIX}/drive_c/windows/syswow64/glide3x.dll"
            var_winedlloverride_update "glide3x=n"
        fi

        for wine_dgv2_dll in $CP_DGV2_FILES ; do
            try_force_link_file "${PATH_TO_DGV2_FILES}/x32/${wine_dgv2_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${wine_dgv2_dll}.dll"
            var_winedlloverride_update "${wine_dgv2_dll}=n"
        done
        # DGVOODOO2 settings:
        if [[ "${PW_DGV2_USE_DX12}" == "1" ]] ; then
            if [[ "${PW_VULKAN_USE}" == "1" ]] || [[ "${PW_VULKAN_USE}" == "2" ]]
            then sed -i "s/OutputAPI = d3d11_fl11_0/OutputAPI = d3d12_fl12_0/" "$DGV2CONF"
            fi
        fi

        if [[ "${PW_DGV2_ENABLE_CRT}" == "1" ]] ; then
            sed -i "s/ScalingMode = unspecified/ScalingMode = stretched_ar_crt/" "$DGV2CONF"
        else
            if [[ "${PW_DGV2_RESAMPLING}" != "disabled" ]] ; then
                sed -i "s/ScalingMode = unspecified/ScalingMode = stretched_ar/" "$DGV2CONF"
            elif [[ "${PW_DGV2_DISPLAY_ROI}" != "disabled" ]] ; then
                sed -i "s/ScalingMode = unspecified/ScalingMode = stretched_ar/" "$DGV2CONF"
            elif [[ "${PW_DGV2_CURSOR_SCALE}" != "0" ]] ; then
                sed -i "s/ScalingMode = unspecified/ScalingMode = stretched_ar/" "$DGV2CONF"
            else
                sed -i "s/ScalingMode = unspecified/ScalingMode = stretched/" "$DGV2CONF"
            fi
        fi

        [[ "${PW_DGV2_FREE_MOUSE}" == "1" ]] && sed -i "s/FreeMouse = false/FreeMouse = true/" "$DGV2CONF"
        [[ "${PW_DGV2_FASTMEMORY}" == "1" ]] && sed -i "s/FastVideoMemoryAccess = false/FastVideoMemoryAccess = true/" "$DGV2CONF"

        if [[ "${PW_DGV2_FORCE_VSYNC}" == "1" ]] ; then
            sed -i "s/ForceVerticalSync = false/ForceVerticalSync = true/" "$DGV2CONF"
        fi

        [[ "${PW_DGV2_DISABLE_MIPMAPPING}" == "1" ]] && sed -i "s/DisableMipmapping = false/DisableMipmapping = true/g" "$DGV2CONF"
        [[ "${PW_DGV2_GLIDE_GAMMA_RAMP}" != "1" ]] && sed -i "s/EnableGlideGammaRamp = true/EnableGlideGammaRamp = false/" "$DGV2CONF"
        [[ "${PW_DGV2_BLIT_STRETCH}" == "1" ]] && sed -i "s/BilinearBlitStretch = false/BilinearBlitStretch = true/" "$DGV2CONF"
        [[ "${PW_DGV2_PHONG_SHADING}" == "1" ]] && sed -i "s/PhongShadingWhenPossible = false/PhongShadingWhenPossible = true/" "$DGV2CONF"
        [[ "${PW_DGV2_16_BIT_DEPTH_BUFFER}" == "1" ]] && sed -i "s/16BitDepthBuffer = false/16BitDepthBuffer = true/" "$DGV2CONF"
        [[ "${PW_DGV2_3DFX_SPLASH_SCREEN}" == "1" ]] && sed -i "s/3DfxSplashScreen = false/3DfxSplashScreen = true/" "$DGV2CONF"
        [[ "${PW_DGV2_3DFX_WATERMARK}" == "1" ]] && sed -i "s/3DfxWatermark = false/3DfxWatermark = true/" "$DGV2CONF"
        [[ "${PW_DGV2_FILTER_POINT_SAMPLED}" == "1" ]] && sed -i "s/KeepFilterIfPointSampled = false/KeepFilterIfPointSampled = true/" "$DGV2CONF"
        [[ "${PW_DGV2_DGVOODOO_WATERMARK}" == "1" ]] && sed -i "s/dgVoodooWatermark = false/dgVoodooWatermark = true/" "$DGV2CONF"
        [[ "${PW_DGV2_EMULATING_PCI_ACCESS}" == "1" ]] && sed -i "s/ForceEmulatingTruePCIAccess = false/ForceEmulatingTruePCIAccess = true/" "$DGV2CONF"
        [[ "${PW_DGV2_INACTIVE_APP_STATE}" == "1" ]] && sed -i "s/EnableInactiveAppState = false/EnableInactiveAppState = true/" "$DGV2CONF"

        if [[ "${PW_DGV2_FILTERING}" != "disabled" ]] ; then
            [[ "${PW_DGV2_FILTERING}" == "Point sampled" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = pointsampled/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Bilinear" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = bilinear/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Linear mip" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = linearmip/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Trilinear" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = trilinear/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Anisotropic 2x" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = 2/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Anisotropic 4x" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = 4/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Anisotropic 8x" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = 8/" "$DGV2CONF"
            [[ "${PW_DGV2_FILTERING}" == "Anisotropic 16x" ]] && sed -i "s/\<Filtering = appdriven\>/Filtering = 16/" "$DGV2CONF"
            case "${PW_DGV2_FILTERING}" in
            "Point sampled")
                sed -i "s/TMUFiltering = appdriven/TMUFiltering = pointsampled/" "$DGV2CONF" ;;
            "Bilinear"|"Linear mip"|"Trilinear"|"Anisotropic 2x"|"Anisotropic 4x"|"Anisotropic 8x"|"Anisotropic 16x")
                sed -i "s/TMUFiltering = appdriven/TMUFiltering = bilinear/" "$DGV2CONF" ;;
            esac
        fi

        if [[ "${PW_DGV2_ANTIALIASING}" != "disabled" ]] && [[ "${PW_DGV2_GLIDE_NAPALM}" != "1" ]] ; then
            sed -i "s/Antialiasing = appdriven/Antialiasing = ${PW_DGV2_ANTIALIASING}/g" "$DGV2CONF"
        elif [[ "${PW_DGV2_ANTIALIASING}" != "disabled" ]] && [[ "${PW_DGV2_GLIDE_NAPALM}" == "1" ]] ; then
            sed -i "86s/Antialiasing = appdriven/Antialiasing = ${PW_DGV2_ANTIALIASING}/" "$DGV2CONF"
        fi

        if [[ "${PW_DGV2_VRAM}" != "disabled" ]] ; then
            if [[ "${PW_DGV2_VIDEOCARD}" == "dgVoodoo2 SVGA 3D" ]] ; then
                if [[ "${PW_DGV2_VRAM}" -le "128" ]] ; then
                    sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
                else
                    sed -i "s/VRAM = 256/VRAM = 64/" "$DGV2CONF"
                fi
            fi
            [[ "${PW_DGV2_VIDEOCARD}" == "Geforce 4 Ti 4800" ]] && [[ "${PW_DGV2_VRAM}" -ge "64" ]] && [[ "${PW_DGV2_VRAM}" -le "256" ]] \
                && sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "ATI Radeon 8500" ]] && [[ "${PW_DGV2_VRAM}" -ge "64" ]] && [[ "${PW_DGV2_VRAM}" -le "256" ]] \
                && sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "Matrox Parhelia-512" ]] && [[ "${PW_DGV2_VRAM}" -ge "128" ]] && [[ "${PW_DGV2_VRAM}" -le "256" ]] \
                && sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "GeForce FX 5700 Ultra" ]] && [[ "${PW_DGV2_VRAM}" -ge "64" ]] && [[ "${PW_DGV2_VRAM}" -le "256" ]] \
                && sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
            if [[ "${PW_DGV2_VIDEOCARD}" == "GeForce 9800 GT" ]] ; then
                if [[ "${PW_DGV2_VRAM}" -ge "512" ]] && [[ "${PW_DGV2_VRAM}" -le "1024" ]] ; then
                    sed -i "s/VRAM = 256/VRAM = ${PW_DGV2_VRAM}/" "$DGV2CONF"
                else
                    sed -i "s/VRAM = 256/VRAM = 512/" "$DGV2CONF"
                fi
            fi
        else
            [[ "${PW_DGV2_VIDEOCARD}" == "dgVoodoo2 SVGA 3D" ]] && sed -i "s/VRAM = 256/VRAM = 64/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "GeForce 9800 GT" ]] && sed -i "s/VRAM = 256/VRAM = 512/" "$DGV2CONF"
        fi

        if [[ "${PW_DGV2_RESOLUTION}" != "disabled" ]] ; then
            if [[ "${PW_DGV2_RESOLUTION}" == "2x app resolution" ]] ; then
                sed -i "s/Resolution = unforced/Resolution = 2x/g" "$DGV2CONF"
            elif [[ "${PW_DGV2_RESOLUTION}" == "3x app resolution" ]] ; then
                sed -i "s/Resolution = unforced/Resolution = 3x/g" "$DGV2CONF"
            elif [[ "${PW_DGV2_RESOLUTION}" == "4x app resolution" ]] ; then
                sed -i "s/Resolution = unforced/Resolution = 4x/g" "$DGV2CONF"
            else
                H_RES_DGV2="${PW_DGV2_RESOLUTION:0:4}"
                V_RES_DGV2="${PW_DGV2_RESOLUTION:5:9}"
                sed -i "s/Resolution = unforced/Resolution = h:${H_RES_DGV2}, v:${V_RES_DGV2}/g" "$DGV2CONF"
            fi
        fi

        if [[ "${PW_DGV2_DISPLAY_ROI}" != "disabled" ]] ; then
            sed -i "s/DisplayROI =/DisplayROI = ${PW_DGV2_DISPLAY_ROI}, pos:centered/" "$DGV2CONF"
        fi

        [[ "${PW_DGV2_FPS_LIMIT}" != "disabled" ]] && sed -i "s/FPSLimit = 0/FPSLimit = ${PW_DGV2_FPS_LIMIT}/" "$DGV2CONF"
        [[ "${PW_DGV2_BIT_DEPTH}" != "disabled" ]]  && sed -i "s/DesktopBitDepth =/DesktopBitDepth = ${PW_DGV2_BIT_DEPTH}/" "$DGV2CONF"
        [[ "${PW_DGV2_CURSOR_SCALE}" != "0" ]]  && sed -i "s/CursorScaleFactor = 0/CursorScaleFactor = ${PW_DGV2_CURSOR_SCALE}/" "$DGV2CONF"

        if [[ "${PW_DGV2_RESAMPLING}" != "disabled" ]] ; then
            [[ "${PW_DGV2_RESAMPLING}" == "Point sampled" ]] && sed -i "s/Resampling = bilinear/Resampling = pointsampled/" "$DGV2CONF"
            [[ "${PW_DGV2_RESAMPLING}" == "Lanczos-2" ]] && sed -i "s/Resampling = bilinear/Resampling = lanczos-2" "$DGV2CONF"
            [[ "${PW_DGV2_RESAMPLING}" == "Bicubic" ]] && sed -i "s/Resampling = bilinear/Resampling = bicubic" "$DGV2CONF"
            [[ "${PW_DGV2_RESAMPLING}" == "Lanczos-3" ]] && sed -i "s/Resampling = bilinear/Resampling = lanczos-3" "$DGV2CONF"
        fi

        if [[ "${PW_DGV2_VIDEOCARD}" != "dgVoodoo2 Virtual 3D" ]] ; then
            [[ "${PW_DGV2_VIDEOCARD}" == "dgVoodoo2 SVGA 3D" ]] && sed -i "s/VideoCard = internal3D/VideoCard = svga/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "Geforce 4 Ti 4800" ]] && sed -i "s/VideoCard = internal3D/VideoCard = geforce_ti_4800/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "ATI Radeon 8500" ]] && sed -i "s/VideoCard = internal3D/VideoCard = ati_radeon_8500/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "Matrox Parhelia-512" ]] && sed -i "s/VideoCard = internal3D/VideoCard = matrox_parhelia-512/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "GeForce FX 5700 Ultra" ]] && sed -i "s/VideoCard = internal3D/VideoCard = geforce_fx_5700_ultra/" "$DGV2CONF"
            [[ "${PW_DGV2_VIDEOCARD}" == "GeForce 9800 GT" ]] && sed -i "s/VideoCard = internal3D/VideoCard = geforce_9800_gt/" "$DGV2CONF"
            if [[ "${PW_DGV2_VIDEOCARD}" == "Voodoo Graphics" ]] ; then
                sed -i "s/VideoCard = voodoo_2/VideoCard = voodoo_graphics/" "$DGV2CONF"
                sed -i "s/NumberOfTMUs = 2/NumberOfTMUs = 1/" "$DGV2CONF"
                if [[ "${PW_DGV2_ONBOARD_RAM}" == "1" ]] ; then
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 4/" "$DGV2CONF"
                else
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 2/" "$DGV2CONF"
                fi
            fi
            if [[ "${PW_DGV2_VIDEOCARD}" == "Voodoo Rush" ]] ; then
                sed -i "s/VideoCard = voodoo_2/VideoCard = voodoo_rush/" "$DGV2CONF"
                sed -i "s/NumberOfTMUs = 2/NumberOfTMUs = 1/" "$DGV2CONF"
                if [[ "${PW_DGV2_ONBOARD_RAM}" == "1" ]] ; then
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 4/" "$DGV2CONF"
                else
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 2/" "$DGV2CONF"
                fi
            fi
            if [[ "${PW_DGV2_VIDEOCARD}" == "Voodoo 2" ]] ; then
                if [[ "${PW_DGV2_ONBOARD_RAM}" == "1" ]] ; then
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 12/" "$DGV2CONF"
                fi
            fi
            if [[ "${PW_DGV2_VIDEOCARD}" == "Voodoo Banshee" ]] ; then
                sed -i "s/VideoCard = voodoo_2/VideoCard = voodoo_banshee/" "$DGV2CONF"
                sed -i "s/NumberOfTMUs = 2/NumberOfTMUs = 1/" "$DGV2CONF"
                if [[ "${PW_DGV2_ONBOARD_RAM}" == "1" ]] ; then
                    sed -i "s/OnboardRAM = 8/OnboardRAM = 16/" "$DGV2CONF"
                fi
            fi
        fi

        [[ "${PW_DGV2_BRIGHTNESS}" != "100" ]] && sed -i "s/Brightness = 100/Brightness = ${PW_DGV2_BRIGHTNESS}/" "$DGV2CONF"
        [[ "${PW_DGV2_COLOR}" != "100" ]] && sed -i "s/Color = 100/Color = ${PW_DGV2_COLOR}/" "$DGV2CONF"
        [[ "${PW_DGV2_CONTRAST}" != "100" ]] && sed -i "s/Contrast = 100/Contrast = ${PW_DGV2_CONTRAST}/" "$DGV2CONF"
    else
        for rm_dgv2_dll in d3dimm glide glide2x glide3x ; do
            try_remove_file "${WINEPREFIX}/drive_c/windows/syswow64/${rm_dgv2_dll}.dll"
        done
        add_to_var CP_WINE_FILES "ddraw"
    fi

    # force use dxgi.dll from wine
    if [[ "${PW_USE_WINE_DXGI}" == "1" ]] ; then
        rm_from_var CP_DXVK_FILES "dxgi"
        add_to_var CP_WINE_FILES "dxgi"
    fi

    print_info "Try create symlink WINE files..."
    for copy_wine_dll in $CP_WINE_FILES ; do
        try_force_link_file "${WINEDIR}/lib64/wine/x86_64-windows/${copy_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/${copy_wine_dll}.dll"
        try_force_link_file "${WINEDIR}/lib/wine/i386-windows/${copy_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${copy_wine_dll}.dll"
    done

    if [[ ! -z "$CP_DXVK_FILES" ]] ; then
        print_info "Try create symlink DXVK files..."
        for wine_dxvk_dll in $CP_DXVK_FILES ; do
            if [[ -f "${PATH_TO_DXVK_FILES}/x64/${wine_dxvk_dll}.dll"  ]] ; then
                try_force_link_file "${PATH_TO_DXVK_FILES}/x32/${wine_dxvk_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${wine_dxvk_dll}.dll"
                try_force_link_file "${PATH_TO_DXVK_FILES}/x64/${wine_dxvk_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/${wine_dxvk_dll}.dll"
                var_winedlloverride_update "${wine_dxvk_dll}=n"
            fi
        done
        create_new_dir "${PATH_TO_DXVK_FILES}/dxvk_cache"
        export DXVK_STATE_CACHE_PATH="${PATH_TO_DXVK_FILES}"/dxvk_cache
        export DXVK_STATE_CACHE="1"
    fi

    if [[ "$DXVK_ENABLE_NVAPI" == "1" ]] ; then
        print_info "Try create symlink NVAPI files..."
        try_force_link_file "${PATH_TO_DXVK_FILES}/x32/nvapi.dll" "${WINEPREFIX}/drive_c/windows/syswow64/nvapi.dll"
        try_force_link_file "${PATH_TO_DXVK_FILES}/x64/nvapi64.dll" "${WINEPREFIX}/drive_c/windows/system32/nvapi64.dll"
    fi

    if [[ ! -z "$CP_VKD3D_FILES" ]] ; then
        print_info "Try create symlink VKD3D files..."
        for wine_vkd3d_dll in $CP_VKD3D_FILES ; do
            if [[ -f "${PATH_TO_VKD3D_FILES}/x64/${wine_vkd3d_dll}.dll" ]] ; then
                try_force_link_file "${PATH_TO_VKD3D_FILES}/x86/${wine_vkd3d_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/${wine_vkd3d_dll}.dll"
                try_force_link_file "${PATH_TO_VKD3D_FILES}/x64/${wine_vkd3d_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/${wine_vkd3d_dll}.dll"
                var_winedlloverride_update "${wine_vkd3d_dll}=n"
            fi
        done
        create_new_dir "${PATH_TO_VKD3D_FILES}/vkd3d_cache"
        export VKD3D_SHADER_CACHE_PATH="${PATH_TO_VKD3D_FILES}/vkd3d_cache"
    fi

    if [[ "$CP_GALLIUM_NINE_FILES" == "d3d9" ]] ; then
        print_info "Try create symlink GALLIUM_NINE files..."
        PATH_TO_GALLIUM_NINE_FILES="${PW_PLUGINS_PATH}/gallium_nine_v.${PW_GALLIUM_NINE_VER}"
        try_force_link_file "${PATH_TO_GALLIUM_NINE_FILES}/lib32/d3d9-nine.dll.so" "${WINEPREFIX}/drive_c/windows/syswow64/d3d9.dll"
        try_force_link_file "${PATH_TO_GALLIUM_NINE_FILES}/lib64/d3d9-nine.dll.so" "${WINEPREFIX}/drive_c/windows/system32/d3d9.dll"
        unset FIND_D3D_MODULE D3D_MODULE_PATH
        if ! check_flatpak ; then
            FIND_D3D_MODULE=$(dirname $(find /usr/ -maxdepth 4 -type f -name "d3dadapter9.so.*") 2>/dev/null)
            if [[ ! -z "$FIND_D3D_MODULE" ]] ; then
                IFS=$'\n'
                for D3D_MP in $FIND_D3D_MODULE ; do
                    if [[ ! -z "$D3D_MODULE_PATH" ]]
                    then export D3D_MODULE_PATH="$D3D_MODULE_PATH:/run/host${D3D_MP}"
                    else export D3D_MODULE_PATH="/run/host${D3D_MP}"
                    fi
                done
                IFS="$orig_IFS"
                print_info "D3D_MODULE_PATH=${D3D_MODULE_PATH}"
            else
                pw_stop_progress_bar
                yad_error "$(gettext "d3dadapter9.so.1.0.0 - Not found in the system.\nInstall the missing package: <b>d3dadapter9</b>")"
                stop_portwine
            fi
        else
            export D3D_MODULE_PATH="/app/lib/i386-linux-gnu/GL/default/lib/d3d:/usr/lib/x86_64-linux-gnu/GL/default/lib/d3d"
            print_info "D3D_MODULE_PATH=${D3D_MODULE_PATH}"
        fi
        var_winedlloverride_update "d3d9=n"
    fi

    if [[ ! -d "${WINEPREFIX}/drive_c/vrclient/bin" ]] ; then
        create_new_dir "${WINEPREFIX}/drive_c/vrclient/bin"
        try_force_link_file "${WINEDIR}"/lib/wine/i386-windows/vrclient.dll "${WINEPREFIX}/drive_c/vrclient/bin/vrclient.dll"
        try_force_link_file "${WINEDIR}"/lib64/wine/x86_64-windows/vrclient_x64.dll "${WINEPREFIX}/drive_c/vrclient/bin/vrclient_x64.dll"
    fi

    if [[ "${PW_USE_SHADER_CACHE}" == "1" ]] ; then
        create_new_dir "${PW_VULKAN_DIR}/gl_shader_cache"
        export __GL_SHADER_DISK_CACHE="1"
        export __GL_SHADER_DISK_CACHE_PATH="${PW_VULKAN_DIR}/gl_shader_cache"
        export __GL_SHADER_DISK_CACHE_SIZE="50000000000"
        export __GL_SHADER_DISK_CACHE_SKIP_CLEANUP="1"
    else
        export __GL_SHADER_DISK_CACHE="0"
        export DXVK_STATE_CACHE="disable"
        export VKD3D_SHADER_CACHE_PATH="0"
    fi

    if [[ "${PW_WINE_FULLSCREEN_FSR}" == "1" ]] \
    && ! check_gamescope_session
    then
        export WINE_FULLSCREEN_FSR="1"
        export WINE_FULLSCREEN_FSR_STRENGTH="2"
        export WINE_FULLSCREEN_INTEGER_SCALING="0"
    else
        export WINE_FULLSCREEN_FSR="0"
        unset WINE_FULLSCREEN_FAKE_CURRENT_RES WINE_FULLSCREEN_FSR_STRENGTH WINE_FULLSCREEN_INTEGER_SCALING
    fi

    if [[ "${PW_WINE_CPU_TOPOLOGY}" != "disabled" ]] ; then
        export WINE_CPU_TOPOLOGY="${PW_WINE_CPU_TOPOLOGY}"
    fi

    if [[ "${PW_MESA_GL_VERSION_OVERRIDE}" != "disabled" ]] ; then
        export MESA_GL_VERSION_OVERRIDE="${PW_MESA_GL_VERSION_OVERRIDE}"
    fi

    if [[ "${PW_VKD3D_FEATURE_LEVEL}" != "disabled" ]] ; then
        export VKD3D_FEATURE_LEVEL="${PW_VKD3D_FEATURE_LEVEL}"
    fi

    #run_winetricks_from_db
    if [[ ! -z "${PW_MUST_HAVE_DLL}" ]]
    then export PW_DLL_INSTALL="$(echo "${PW_MUST_HAVE_DLL} ${PW_DLL_INSTALL}" | awk '{ for(i=1;i<=NF;i++){a[$i]++} }END{ for(i in a){printf("%s ",i)} }' )"
    fi

    if [[ ! -z "${PW_DLL_INSTALL}" ]] ; then
        export PW_DLL_NEED_INSTALL=""
        export USE_WT_FROM_DB=0
        if [ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ] ; then
            touch "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
        fi
        for need_install_dll_to_pfx in ${PW_DLL_INSTALL} ; do
            if [[ "${need_install_dll_to_pfx}" == vcrun201[5-9] ]] \
            || [[ "${need_install_dll_to_pfx}" == vcrun2022 ]] ; then
                need_install_dll_to_pfx="vcrun2022"
                sed -i '/vcrun2015/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
                sed -i '/vcrun2017/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
                sed -i '/vcrun2019/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
            fi
            grep "${need_install_dll_to_pfx}" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" &>/dev/null
            if [[ "$?" == "1" ]] ; then
                if [[ -z "${PW_DLL_NEED_INSTALL}" ]]
                then export PW_DLL_NEED_INSTALL="${need_install_dll_to_pfx}"
                else export PW_DLL_NEED_INSTALL="${need_install_dll_to_pfx} ${PW_DLL_NEED_INSTALL}"
                fi
                export USE_WT_FROM_DB=1
            fi
        done
        if [[ "${USE_WT_FROM_DB}" == "1" ]] ; then
            pw_update_pfx_cover_gui
            update_winetricks
            echo "START WINETRICKS..." >> "${PW_TMPFS_PATH}/update_pfx_log"
            echo "Try to install DLL in prefix: ${PW_DLL_NEED_INSTALL}" >> "${PW_TMPFS_PATH}/update_pfx_log"
            print_info "Try to install DLL in prefix: ${PW_DLL_NEED_INSTALL}"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${PORT_WINE_TMP_PATH}/winetricks" -q -r -f ${PW_DLL_NEED_INSTALL} | tee -a "${PW_TMPFS_PATH}/update_pfx_log"
            wait_wineserver
            kill_portwine
        fi
    fi

    if [[ -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg" ]] \
    && [[ -z $(grep "Windows $PW_WINDOWS_VER" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg") ]]
    then
        if [[ ! -z "${PW_WINDOWS_VER}" ]] \
        && [[ $(echo "$PW_WINDOWS_VER" | sed 's/.*/\L&/') == "xp" ]]
        then
            export PW_WINDOWS_VER="xp64"
        fi
        ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
        "${WINELOADER}" winecfg -v $(echo "win${PW_WINDOWS_VER}" | sed 's/.*/\L&/')
        wait_wineserver
        kill_portwine
        echo "Set to win${PW_WINDOWS_VER}"
    fi

    if [[ "${portwine_exe}" == *-Shipping.exe ]] ; then
        echo "Disable EAC"
        [[ -z "${LAUNCH_PARAMETERS}" ]] && export LAUNCH_PARAMETERS+=" -eac-nop-loaded "
    fi

    HIDRAW_VAR="$(grep "DisableHidraw" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg")"
    SDL_VAR="$(grep "Enable SDL" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg")"
    if [[ "${PW_DINPUT_PROTOCOL}" == "1" ]] ; then
        if [[ "$HIDRAW_VAR" ]] \
        && [[ "$SDL_VAR" ]]
        then
            if [[ "${HIDRAW_VAR: -1}" != "0" ]] ; then
                sed -i "s/${HIDRAW_VAR}/${HIDRAW_VAR%?}0/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg"
            fi

            if [[ "${SDL_VAR: -1}" != "0" ]] ; then
                sed -i "s/${SDL_VAR}/${SDL_VAR%?}0/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg"
            fi
        else
            print_info "Added DisableHidraw = 0 to reg file"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\winebus" /v "DisableHidraw" /t REG_DWORD /d 0 /f
            print_info "Added Enable SDL = 0 to reg file"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\winebus" /v "Enable SDL" /t REG_DWORD /d 0 /f
        fi
    else
        # var_winedlloverride_update "xinput1_1,xinput1_2,xinput1_3,xinput9_1_0=b"

        if [[ "$HIDRAW_VAR" ]] \
        && [[ "$SDL_VAR" ]]
        then
            if [[ "${HIDRAW_VAR: -1}" != "1" ]] ; then
                sed -i "s/${HIDRAW_VAR}/${HIDRAW_VAR%?}1/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg"
            fi

            if [[ "${SDL_VAR: -1}" != "1" ]] ; then
                sed -i "s/${SDL_VAR}/${SDL_VAR%?}1/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg"
            fi
        else
            print_info "Added DisableHidraw = 1 to reg file"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\winebus" /v "DisableHidraw" /t REG_DWORD /d 1 /f
            print_info "Added Enable SDL = 1 to reg file"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\winebus" /v "Enable SDL" /t REG_DWORD /d 1 /f
        fi
    fi

    WINE_WAYLAND_VAR="$(grep "x11,wayland" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg")"
    if [[ "${PW_USE_NATIVE_WAYLAND}" == "1" ]] ; then
        if [[ ! "$WINE_WAYLAND_VAR" ]] ; then
            print_info "Enable experimental Wayland support"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg add "HKEY_CURRENT_USER\Software\Wine\Drivers" /v "Graphics" /t REG_SZ /d "x11,wayland" /f
        fi
        export PW_DISPLAY="env DISPLAY="
        export PW_USE_RUNTIME="0"
    else
        unset PW_DISPLAY
        if [[ "$WINE_WAYLAND_VAR" ]] ; then
            print_info "Disable experimental Wayland support"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${WINELOADER}" reg delete "HKEY_CURRENT_USER\Software\Wine\Drivers" /v Graphics /f
        fi
    fi

    if [[ "${PW_SOUND_DRIVER_USE}" != "disabled" ]] \
    && grep -v "\"Audio\"=\"$PW_SOUND_DRIVER_USE\"" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null
    then
        if grep "\"Audio\"=" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null ; then
            sed -i 's/"Audio"=".*"/"Audio"="'${PW_SOUND_DRIVER_USE}'"/' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
        else
            echo -e '\n[Software\\Wine\\Drivers]\n"Audio"="'${PW_SOUND_DRIVER_USE}'"' >> "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
        fi
        kill_portwine
    elif [[ "${PW_SOUND_DRIVER_USE}" == "disabled" ]] \
    && grep -v "\"Audio\"=" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null
    then
        sed -i '/\[Software\\Wine\\Drivers\]/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null
        sed -i "/\"Audio\"=/d" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null
    fi

    pw_stop_progress_bar &&
    if ! check_start_from_steam ; then
        pw_tray_icon
        sleep 0.5
        if [[ "${PW_CHECK_AUTOINSTALL}" != "1" ]] ; then
            pw_start_progress_bar_cover "${COVERS_PATH}/loading_${LANGUAGE_GIF}.gif"
        fi
    fi
    add_in_start_portwine

    if [[ -f "${PATH_TO_GAME}/dxvk.conf" ]] ; then
        export DXVK_CONFIG_FILE="${PATH_TO_GAME}/dxvk.conf"
    fi

    for chk_lang_and_uname in "steam_emu.ini" "steam_api.ini" "steam_api64.ini" "SteamConfig.ini" ; do
        if [[ -f "${PATH_TO_GAME}/${chk_lang_and_uname}" ]] ; then
            sed -i "/^Language/c\Language=${FULL_LN}" "${PATH_TO_GAME}/${chk_lang_and_uname}"
            sed -i "/^UserName/c\UserName=${USER}" "${PATH_TO_GAME}/${chk_lang_and_uname}"
            sed -i "/^PlayerName/c\PlayerName=${USER}" "${PATH_TO_GAME}/${chk_lang_and_uname}"
        fi
    done

    if [[ "${PW_DISABLE_COMPOSITING}" == "1" ]] \
    && ! check_gamescope_session
    then
        if [[ "${DESKTOP_SESSION}" =~ "plasma" ]] ; then
            qdbus org.kde.KWin /Compositor suspend
        elif [[ "${DESKTOP_SESSION}" =~ "mate" ]] ; then
            gsettings set org.mate.Marco.general compositing-manager false
        elif [[ "${DESKTOP_SESSION}" =~ "xfce" ]] ; then
            xfconf-query -c xfwm4 -p /general/use_compositing -s false
        elif [[ "${DESKTOP_SESSION}" =~ "cinnamon" ]] ; then
            gsettings set org.cinnamon.muffin unredirect-fullscreen-windows true
        elif [[ "${DESKTOP_SESSION}" =~ "deepin" ]] ; then
            dbus-send --session --dest=com.deepin.WMSwitcher --type=method_call /com/deepin/WMSwitcher com.deepin.WMSwitcher.RequestSwitchWM
        fi
    fi

    # GAMESCOPE enable:
    if [[ "${PW_GAMESCOPE}" == 1 ]] \
    && [[ "${GAMESCOPE_INSTALLED}" == 1 ]] \
    && ! check_gamescope_session
    then
        if [[ "${PW_GPU_USE}" != "disabled" ]] ; then
            PW_vendorID="$(cat "${PW_TMPFS_PATH}/vulkaninfo.tmp" | grep -B3 "${PW_GPU_USE}" | grep vendorID | sort -u | awk -F'0x' '{print $2}')"
            PW_deviceID="$(cat "${PW_TMPFS_PATH}/vulkaninfo.tmp" | grep -B3 "${PW_GPU_USE}" | grep deviceID | sort -u | awk -F'0x' '{print $2}')"
            PW_ID_VIDEO=" --prefer-vk-device ${PW_vendorID}:${PW_deviceID}"
        else
            PW_ID_VIDEO=""
        fi
        if ! check_wayland_session ; then
            export vk_xwayland_wait_ready="false"
        fi
        #checkbox
        local PW_GAMESCOPE_ARGS_NEW=""
        [[ "${PW_GS_FULLSCREEN}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" -f"
        [[ "${PW_GS_FORCE_FULLSCREEN}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --force-windows-fullscreen"
        [[ "${PW_GS_BORDERLESS_WINDOW}" == "1" ]] &&  PW_GAMESCOPE_ARGS_NEW+=" -b"
        [[ "${PW_GS_FORCE_GRAB_CURSOR}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --force-grab-cursor"
        [[ "${PW_GS_FORCE_GRAB_KEYBOARD}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" -g"

        if [[ "${PW_GS_HDR_ENABLE}" == "1" ]] ; then
            PW_GAMESCOPE_ARGS_NEW+=" --hdr-enabled"
            export DXVK_HDR="1"
        fi

        [[ "${PW_GS_ENABLE_GAMESCOPE_WSI}" == "1" ]] && export ENABLE_GAMESCOPE_WSI="1"

        [[ "${PW_GS_HDR_ITM_ENABLE}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-itm-enable"
        [[ "${PW_GS_SDR_GAMMUT_WIDENESS}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --sdr-gamut-wideness"
        [[ "${PW_GS_FORCE_COMPOSITION}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --force-composition"
        [[ "${PW_GS_HDR_FORCE_SUPPORT}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-debug-force-support"
        [[ "${PW_GS_HDR_FORCE_OUTPUT}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-debug-force-output"
        [[ "${PW_GS_HDR_FORCE_HEATMAP}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-debug-heatmap"
        [[ "${PW_GS_EXPOSE_WAYLAND}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --expose-wayland"
        [[ "${PW_GS_REALTIME_SCHEDULING}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --rt"

        if [[ "${PW_GS_FIX_AMD_AND_INTEL}" == "1" ]] ; then
            export RADV_DEBUG="nodcc"
            export R600_DEBUG="nodcc"
            export INTEL_DEBUG="norbc"
        fi

        if [[ "${PW_GS_MANGOAPP}" == "1" ]] ; then
            PW_GAMESCOPE_ARGS_NEW+=" --mangoapp"
            export PW_MANGOHUD="0"
        fi

        [[ "${PW_GS_BACKEND_SDL}" == "1" ]] && PW_GAMESCOPE_ARGS_NEW+=" --backend sdl"
        [[ "${PW_GS_SDL_VIDEODRIVER_X11}" == "1" ]] && export SDL_VIDEODRIVER="x11"

        #combobox
        if [[ "${PW_GS_SHOW_RESOLUTION}" != "disabled" ]] ; then
            PWGSRESSW1="${PW_GS_SHOW_RESOLUTION%x*}"
            PWGSRESSW="${PWGSRESSW1%%-*}"
            PWGSRESSH1="${PW_GS_SHOW_RESOLUTION#*x}"
            PWGSRESSH="${PWGSRESSH1%%-*}"
            PW_GAMESCOPE_ARGS_NEW+=" -W ${PWGSRESSW} -H ${PWGSRESSH}"

            if [[ "${PW_GS_INTERNAL_RESOLUTION}" != "0.0" ]] ; then
                GS_RES_W=${PW_GS_SHOW_RESOLUTION:0:4}
                GS_RES_H=${PW_GS_SHOW_RESOLUTION:5:8}
                PWGSRESIW=$(echo ${GS_RES_W} ${PW_GS_INTERNAL_RESOLUTION} | awk '{print $1*$2}')
                PWGSRESIH=$(echo ${GS_RES_H} ${PW_GS_INTERNAL_RESOLUTION} | awk '{print $1*$2}')
                PW_GAMESCOPE_ARGS_NEW+=" -w ${PWGSRESIW} -h ${PWGSRESIH}"
            fi
        fi

        [[ "${PW_GS_FRAME_LIMIT}" != "disabled" ]] && PW_GAMESCOPE_ARGS_NEW+=" -r ${PW_GS_FRAME_LIMIT}"

        if [[ "${PW_GS_MESA_VK_WSI_PRESENT_MODE}" != "disabled" ]] ; then
            export MESA_VK_WSI_PRESENT_MODE=${PW_GS_MESA_VK_WSI_PRESENT_MODE}
        fi

        [[ "${PW_GS_MAX_SCALE_FACTOR}" != "0.0" ]] && PW_GAMESCOPE_ARGS_NEW+=" -m ${PW_GS_MAX_SCALE_FACTOR}"
        [[ "${PW_GS_SCALER_MODE}" != "disabled" ]] && PW_GAMESCOPE_ARGS_NEW+=" -S ${PW_GS_SCALER_MODE}"

        if [[ "${PW_GS_FILTER_MODE}" != "disabled" ]] ; then
            export PW_WINE_FULLSCREEN_FSR="0"
            if [[ "${PW_GS_FILTER_MODE_OLD}" != "true" ]] ; then
                PW_GAMESCOPE_ARGS_NEW+=" -F ${PW_GS_FILTER_MODE}"
            else
                if [[ "${PW_GS_FILTER_MODE}" == "fsr" ]] ; then
                    PW_GAMESCOPE_ARGS_NEW+=" -U"
                elif [[ "${PW_GS_FILTER_MODE}" == "nis" ]] ; then
                    PW_GAMESCOPE_ARGS_NEW+=" -Y"
                fi
            fi
            PW_GAMESCOPE_ARGS_NEW+=" --sharpness ${PW_GS_UPSCALE_SHARPNESS}"
        fi

        [[ "${PW_GS_MOUSE_SENSITIVITY}" != "0.0" ]] && PW_GAMESCOPE_ARGS_NEW+=" -s ${PW_GS_MOUSE_SENSITIVITY}"
        [[ "${PW_GS_SDR_CONTENT_NITS}" != "400" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-sdr-content-nits ${PW_GS_SDR_CONTENT_NITS}"
        [[ "${PW_GS_ITM_SDR_NITS}" != "0" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-itm-sdr-nits ${PW_GS_ITM_SDR_NITS}"
        [[ "${PW_GS_ITM_TARGET_NITS}" != "0" ]] && PW_GAMESCOPE_ARGS_NEW+=" --hdr-itm-target-nits ${PW_GS_ITM_TARGET_NITS}"

        edit_db_from_gui PW_GAMESCOPE_ARGS_NEW
        export PW_RUN_GAMESCOPE="gamescope${PW_ID_VIDEO}${PW_GAMESCOPE_ARGS_NEW} --"
    fi

    pw_mangohud_check
    pw_vkbasalt_check
}

pw_run () {
    unset GDK_BACKEND
    if [[ ! -z "${PATH_TO_GAME}" ]] \
    && [[ -d "${PATH_TO_GAME}" ]]
    then
        cd "${PATH_TO_GAME}" || fatal
    elif [[ -f "$portwine_exe" ]]
    then
        PATH_TO_GAME="$( cd "$( dirname "${portwine_exe}" )" >/dev/null 2>&1 && pwd )"
        cd "${PATH_TO_GAME}" || fatal
    else
        cd "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c" || fatal
    fi
    PW_LOG_TO_FILE="${PORT_WINE_PATH}/PortProton.log"
    PW_LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}:${WINE_LIBRARY_PATH}"
    print_var "WINEDIR" "WINEPREFIX" "WINEDLLOVERRIDES" "PATH_TO_GAME" "PW_WINE_USE" "PW_VULKAN_USE" "VULKAN_DRIVER_NAME"
    print_var "VKD3D_CONFIG" "PW_LD_LIBRARY_PATH" "PATH" "WINEESYNC" "WINEFSYNC" "WINEFSYNC_FUTEX2"
    print_var "WINEDLLPATH" "WINE_CPU_TOPOLOGY" "PW_RUN_GAMESCOPE" "LD_LIBRARY_PATH"
    kill -s SIGUSR1 "$PW_YAD_PID_PFX_COVER_UI" &>/dev/null
    if [[ "$PW_USE_RUNTIME" == 1 ]] \
    && [[ "$PW_WINE_USE" != "USE_SYSTEM_WINE" ]]
    then
        if [[ "${PW_LOG}" == 1 ]] ; then
            echo "WINEDLLOVERRIDES=${WINEDLLOVERRIDES}" >> "${PW_LOG_TO_FILE}"
            echo "------------------------------------" >> "${PW_LOG_TO_FILE}"
            [[ -f "${PW_TMPFS_PATH}/update_pfx_log" ]] && cat "${PW_TMPFS_PATH}/update_pfx_log" >> "${PW_LOG_TO_FILE}"
            echo "-" >> "${PW_LOG_TO_FILE}"
            echo "Log WINE:" >> "${PW_LOG_TO_FILE}"
            echo ""
            print_info "Log from RUNTIME and WINE:"
            ${PW_RUN_GAMESCOPE} \
            ${pw_runtime} \
            LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" \
            LD_PRELOAD="${PW_LD_PRELOAD}" \
            VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
            VK_INSTANCE_LAYERS=${PW_VK_INSTANCE_LAYERS} \
            ${PW_GAMEMODERUN_SLR} \
            ${PW_MANGOHUD_SLR} \
            ${PW_DISPLAY} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
        else
            if [[ "$PW_USE_TERMINAL" == 1 ]] \
            && [[ "$PW_USE_RUNTIME" == 1 ]]
            then
                export PW_ADD_TO_ARGS_IN_RUNTIME="--xterm"
                pw_init_runtime
            fi
            echo ""
            echo "Log WINE:" > "${PW_LOG_TO_FILE}"
            ${PW_RUN_GAMESCOPE} \
            ${pw_runtime} \
            LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" \
            LD_PRELOAD="${PW_LD_PRELOAD}" \
            VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
            VK_INSTANCE_LAYERS="${PW_VK_INSTANCE_LAYERS}" \
            ${PW_GAMEMODERUN_SLR} \
            ${PW_MANGOHUD_SLR} \
            ${PW_DISPLAY} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
            print_info "Update prefix log:"
            [[ -f "${PW_TMPFS_PATH}/update_pfx_log" ]] && cat "${PW_TMPFS_PATH}/update_pfx_log"
            echo
            print_info "Log from RUNTIME and WINE:"
            awk '! a[$0]++' "${PW_LOG_TO_FILE}"
            echo
        fi
    else
        if [[ "${PW_LOG}" == 1 ]] ; then
            echo "WINEDLLOVERRIDES=${WINEDLLOVERRIDES}" >> "${PW_LOG_TO_FILE}"
            echo "------------------------------------" >> "${PW_LOG_TO_FILE}"
            [[ -f "${PW_TMPFS_PATH}/update_pfx_log" ]] && cat "${PW_TMPFS_PATH}/update_pfx_log" >> "${PW_LOG_TO_FILE}"
            echo "-" >> "${PW_LOG_TO_FILE}"
            echo "Log WINE:" >> "${PW_LOG_TO_FILE}"
            echo ""
            print_info "Log WINE:"
            env FAKE_VAR= \
            LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" \
            LD_PRELOAD="${PW_LD_PRELOAD}" \
            VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
            VK_INSTANCE_LAYERS="${PW_VK_INSTANCE_LAYERS}" \
            ${PW_RUN_GAMESCOPE} \
            ${PW_GAMEMODERUN_SLR} \
            ${PW_MANGOHUD_SLR} \
            ${PW_DISPLAY} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
            wait_wineserver
        else
            print_info "Update prefix log:"
            [[ -f "${PW_TMPFS_PATH}/update_pfx_log" ]] && cat "${PW_TMPFS_PATH}/update_pfx_log"
            echo ""
            echo "Log WINE:" > "${PW_LOG_TO_FILE}"
            print_info "Log WINE:"
            env FAKE_VAR= \
            LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" \
            LD_PRELOAD="${PW_LD_PRELOAD}" \
            VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
            VK_INSTANCE_LAYERS="${PW_VK_INSTANCE_LAYERS}" \
            ${PW_RUN_GAMESCOPE} \
            ${PW_GAMEMODERUN_SLR} \
            ${PW_MANGOHUD_SLR} \
            ${PW_DISPLAY} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]}
            wait_wineserver
        fi
    fi
}
export -f pw_run

pw_yad_set_form () {
    if [[ $(<"${PW_TMPFS_PATH}/tmp_yad_form") != "" ]]; then
        PW_YAD_SET=$(head -n 1 "${PW_TMPFS_PATH}/tmp_yad_form" | awk '{print $1}')
        export PW_YAD_SET
    fi
}

pw_yad_form_vulkan () {
    if [[ "$(<"${PW_TMPFS_PATH}/tmp_yad_form_vulkan")" != "" ]] ; then
        if [[ ! -z "${KEY_START}" ]] ; then
            YAD_FORM_VULKAN=$(sed 's/$/\;/' "${PW_TMPFS_PATH}/tmp_yad_form_vulkan")
            VULKAN_MOD=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $1}')
            PW_WINE_VER=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $2}')
            PW_PREFIX_NAME=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $3}' | sed -e s/[[:blank:]]/_/g)
        else
            YAD_FORM_VULKAN=$(<"${PW_TMPFS_PATH}/tmp_yad_form_vulkan")
            VULKAN_MOD=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $1}')
            PW_PREFIX_NAME=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $2}' | sed -e s/[[:blank:]]/_/g)
            PW_WINE_VER=$(echo "${YAD_FORM_VULKAN}" | grep \;\; | awk -F";" '{print $3}')
        fi
        if [[ -z "${PW_PREFIX_NAME}" ]] \
        || [[ ! -z "$(echo "${PW_PREFIX_NAME}" | grep -E '^_.*' )" ]]
        then
            PW_PREFIX_NAME="DEFAULT"
        else
            PW_PREFIX_NAME="${PW_PREFIX_NAME^^}"
        fi
        export PW_PREFIX_NAME PW_WINE_VER VULKAN_MOD
    fi
}

portwine_launch () {
    start_portwine
    unset PW_VD_TMP
    if [[ "${PW_VIRTUAL_DESKTOP}" == "1" ]] ; then
        PW_VD_TMP=(explorer "/desktop=PortProton,${PW_SCREEN_RESOLUTION}")
    fi

    case "$portwine_exe" in
        *.[Ee][Xx][Ee])
            pw_run ${PW_VD_TMP[@]} ${WINE_WIN_START} "$portwine_exe"
        ;;
        *.[Bb][Aa][Tt])
            PW_USE_TERMINAL=1
            pw_run ${PW_VD_TMP[@]} "$portwine_exe"
        ;;
        *.[Mm][Ss][Ii])
            pw_run ${PW_VD_TMP[@]} msiexec /i "$portwine_exe"
        ;;
        *.[Rr][Ee][Gg])
            pw_run ${PW_VD_TMP[@]} regedit "$portwine_exe"
        ;;
        *)
            pw_run ${PW_VD_TMP[@]} winefile
        ;;
    esac
}

pw_winecfg () {
    start_portwine
    export GST_PLUGIN_SYSTEM_PATH_1_0=""
    pw_run winecfg
}

pw_winefile () {
    start_portwine
    pw_run winefile
}

pw_winecmd () {
    export PW_USE_TERMINAL=1
    start_portwine
    cd "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c" || fatal
    pw_run cmd
    stop_portwine
}

pw_winereg () {
    start_portwine
    export GST_PLUGIN_SYSTEM_PATH_1_0=""
    pw_run regedit
}

pw_start_cont_xterm () {
    cd "$HOME" || fatal
    unset PW_SANDBOX_HOME_PATH
    pw_init_runtime
    ${pw_runtime} \
    LD_PRELOAD="${PW_LD_PRELOAD}" \
    VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
    VK_INSTANCE_LAYERS="${PW_VK_INSTANCE_LAYERS}" \
    ${PW_GAMEMODERUN_SLR} \
    ${PW_MANGOHUD_SLR} \
    ${PW_TERM} bash
}

# GUI INFO
yad_info () {
    print_info "$@"
    if [[ ! -f "${pw_yad}" ]] ; then
        local pw_yad="yad"
        command -v "${pw_yad}" &>/dev/null || fatal "yad not found"
    fi
    "${pw_yad}" --no-wrap --text "$@" --width=400 --height=150 --borders=15 --title "INFO" \
    --gui-type-layout=${YAD_INFO_GUI_TYPE_LAYOUT} \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --image="$PW_GUI_ICON_PATH/info.svg" \
    --text-align=center --fixed \
    --button="$(gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png" 2>/dev/null
}
export -f yad_info

# GUI ERROR
yad_error () {
    print_error "$@"
    if [[ ! -f "${pw_yad}" ]] ; then
        local pw_yad="yad"
        command -v "${pw_yad}" &>/dev/null || fatal "yad not found"
    fi
    "${pw_yad}" --no-wrap --text "$@" --width=400 --height=150 --borders=15 --title "ERROR" \
    --gui-type-layout=${YAD_INFO_GUI_TYPE_LAYOUT} \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --image="$PW_GUI_ICON_PATH/error.svg" \
    --text-align=center --fixed \
    --button="$(gettext "EXIT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png" 2>/dev/null
}
export -f yad_error

yad_error_download () {
    if [[ ! -f "${pw_yad}" ]] ; then
        local pw_yad="yad"
        command -v "${pw_yad}" &>/dev/null || fatal "yad not found"
    fi
    "${pw_yad}" --text "$(gettext "You will need to check internet connection,and\npress Repeat for repeat download")" \
    --width=400 --borders=15 --title "$(gettext "Error")" \
    --gui-type-layout=${YAD_INFO_GUI_TYPE_LAYOUT} \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --image="$PW_GUI_ICON_PATH/download.svg" \
    --no-wrap --text-align=center \
    --button="$(gettext "SKIP")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(gettext "REPEAT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null
    [[ "$?" != 0 ]] && return 1 || return 0
}

# GUI QUESTIONS
yad_question () {
    if [[ ! -f "${pw_yad}" ]] ; then
        local pw_yad="yad"
        command -v "${pw_yad}" &>/dev/null || fatal "yad not found"
    fi
    "${pw_yad}" --text "${1}" --width=400 --height=150 --borders=15 --title "$(gettext "Choices")" \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --image="$PW_GUI_ICON_PATH/question.svg" \
    --gui-type-layout=${YAD_INFO_GUI_TYPE_LAYOUT} \
    --no-wrap --text-align=center --fixed \
    --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null
    [[ "$?" != 0 ]] && return 1 || return 0
}

pw_start_progress_bar_cover () {
    if ! check_start_from_steam ; then
        PW_GIF_FILE="$1"
        PW_GIF_SIZE_X=$(file "${PW_GIF_FILE}" | awk '{print $7 + 8}')
        PW_GIF_SIZE_Y=$(file "${PW_GIF_FILE}" | awk '{print $9 + 15}')
        "${pw_yad}" --picture --filename="${PW_GIF_FILE}" --close-on-unfocus --no-buttons --undecorated \
        --skip-taskbar --width="$PW_GIF_SIZE_X" --height="$PW_GIF_SIZE_Y" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" > /dev/null 2>&1 &
        export PW_YAD_PID_PROGRESS_BAR_COVER="$!"
        return 0
    fi
}

pw_start_progress_bar_cover_block () {
    if ! check_start_from_steam ; then
        [[ ! -f "${pw_yad}" ]] && local pw_yad="yad"
        PW_GIF_FILE="$1"
        PW_GIF_SIZE_X=$(file "${PW_GIF_FILE}" | awk '{print $7 + 8}')
        PW_GIF_SIZE_Y=$(file "${PW_GIF_FILE}" | awk '{print $9 + 15}')
        "${pw_yad}" --picture --filename="${PW_GIF_FILE}" --close-on-unfocus --no-buttons --undecorated \
        --skip-taskbar --width="$PW_GIF_SIZE_X" --height="$PW_GIF_SIZE_Y" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" > /dev/null 2>&1 &
        export PW_YAD_PID_PROGRESS_BAR_COVER_BLOCK="$!"
        return 0
    fi
}

pw_update_pfx_cover_gui () {
    if [[ "$1" == "winetricks" ]] ; then
        TAB_PLACE="--tab=$(gettext "TERMINAL")!$PW_GUI_ICON_PATH/$TAB_SIZE.png --tab=$(gettext "LOGO")!$PW_GUI_ICON_PATH/$TAB_SIZE.png"
        TAB_N1=2
        TAB_N2=1
        YAD_UNDECORATED=""
    else
        TAB_PLACE="--tab=$(gettext "LOGO")!$PW_GUI_ICON_PATH/$TAB_SIZE.png --tab=$(gettext "TERMINAL")!$PW_GUI_ICON_PATH/$TAB_SIZE.png"
        TAB_N1=1
        TAB_N2=2
        YAD_UNDECORATED="--undecorated"
    fi

    if ! check_start_from_steam \
    && ! pgrep -a yad | grep "yad_gui_pp --notebook --key=$PW_KEY_PROGRESS_BAR_UP" &>/dev/null
    then
        PW_KEY_PROGRESS_BAR_UP=$RANDOM
        PW_GIF_FILE="${COVERS_PATH}/update_prefix_${LANGUAGE_GIF}.gif"
        PW_GIF_SIZE_X=$(file "${PW_GIF_FILE}" | awk '{print $7 + 20}')
        PW_GIF_SIZE_Y=$(file "${PW_GIF_FILE}" | awk '{print $9 + 65}')
        echo "UPDATE PREFIX..." > "${PW_TMPFS_PATH}/update_pfx_log"
        export PW_TIMER=0
        while read -r line || [[ ! -z $(pgrep -a yad | grep "yad_gui_pp --notebook --key=$PW_KEY_PROGRESS_BAR_UP" | awk '{print $1}') ]] ; do
                sleep 0.005
                if [[ ! -z "${line}" ]] && [[ -z "$(echo "${line}" | grep -i "gstreamer")" ]] \
                                        && [[ -z "$(echo "${line}" | grep -i "kerberos")" ]] \
                                        && [[ -z "$(echo "${line}" | grep -i "ntlm")" ]]
                then
                    echo "# ${line}"
                fi
                if [[ "${PW_TIMER}" != 1 ]] ; then
                    sleep 3
                    PW_TIMER=1
                fi
        done < "${PW_TMPFS_PATH}/update_pfx_log" | "${pw_yad}" --plug=$PW_KEY_PROGRESS_BAR_UP --tabnum=$TAB_N2 --text-info --tail  2>/dev/null &
        "${pw_yad}" --plug=$PW_KEY_PROGRESS_BAR_UP --tabnum=$TAB_N1 --picture --filename="${PW_GIF_FILE}" 2>/dev/null &

        "${pw_yad}" --notebook --key="$PW_KEY_PROGRESS_BAR_UP" $TAB_PLACE --no-buttons \
        --skip-taskbar --width="$PW_GIF_SIZE_X" --height="$PW_GIF_SIZE_Y" $YAD_UNDECORATED \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "PortProton" --tab-pos="bottom" --expand 2>/dev/null &
        export PW_YAD_PID_PFX_COVER_UI="$!"
        return 0
    fi
}

pw_start_progress_bar_cs () {
    if ! check_start_from_steam ; then
        "${pw_yad}" --progress-old --text="$@
        " --pulsate --hide-text --close-on-unfocus --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --skip-taskbar \
        --width="$PROGRESS_BAR_WIDTH_SIZE" \
        --wrap-width="$PROGRESS_BAR_WIDTH_SIZE" \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" &>/dev/null &
        export PW_YAD_PID_PROGRESS_BAR_CS="$!"
        return 0
    fi
}

pw_start_progress_bar_block () {
    if ! check_start_from_steam ; then
        "${pw_yad}" --progress-old --text="$@
        " --pulsate --hide-text --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --skip-taskbar \
        --no-escape --text-align="center" --height=90 --fixed \
        --width="$PROGRESS_BAR_WIDTH_SIZE" \
        --wrap-width="$PROGRESS_BAR_WIDTH_SIZE" \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" &>/dev/null &
        export PW_YAD_PID_PROGRESS_BAR_BLOCK="$!"
        return 0
    fi
}

pw_start_progress_bar_install_game () {
    if ! check_start_from_steam ; then
        "${pw_yad}" --progress-old --text="$(gettext "Please wait. Installing the") $@
        " --pulsate --hide-text --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --skip-taskbar \
        --no-escape --text-align="center" --height=90 --fixed \
        --width="$PROGRESS_BAR_WIDTH_SIZE" \
        --wrap-width="$PROGRESS_BAR_WIDTH_SIZE" \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" &>/dev/null &
        export PW_YAD_PID_PROGRESS_BAR_BLOCK="$!"
        return 0
    fi
}

pw_stop_progress_bar () {
    sleep 0.1
    for PW_KILL_YAD_PID in "$PW_YAD_PID_PROGRESS_BAR_BLOCK" "$PW_YAD_PID_PROGRESS_BAR_CS" \
                           "$PW_YAD_PID_PFX_COVER_UI" "$PW_YAD_PID_PROGRESS_BAR_COVER"
    do
        kill -s SIGUSR1 "$PW_KILL_YAD_PID" &>/dev/null
    done
    unset PW_YAD_PID_PROGRESS_BAR_BLOCK PW_YAD_PID_PROGRESS_BAR_CS \
    PW_YAD_PID_PFX_COVER_UI PW_YAD_PID_PROGRESS_BAR_COVER
    return 0
}
export -f pw_stop_progress_bar

pw_stop_progress_bar_cover () {
    kill -s KILL "$PW_YAD_PID_PROGRESS_BAR_COVER" &>/dev/null
    return 0
}
export -f pw_stop_progress_bar_cover

pw_stop_progress_bar_cover_block () {
    kill -s KILL "$PW_YAD_PID_PROGRESS_BAR_COVER_BLOCK" &>/dev/null
    return 0
}
export -f pw_stop_progress_bar_cover_block

open_changelog () {
    [[ "$LANGUAGE" == ru ]] && local PW_CHANGELOG_FILE="changelog_ru" || local PW_CHANGELOG_FILE="changelog_en"
    "${pw_yad}" --title="$(gettext "CHANGELOG")" --no-buttons \
    --text-info --show-uri --wrap --width=1200 --height=700 --uri-color=red \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --filename="${PORT_WINE_PATH}/data/${PW_CHANGELOG_FILE}" &>/dev/null
    YAD_STATUS="$?"
    if [[ "$YAD_STATUS" == "1" || "$YAD_STATUS" == "252" ]] && [[ -z "$WINEPREFIX" ]] ; then
        export SKIP_CHECK_UPDATES="1"
        if [[ -z "${PW_ALL_DF}" ]] ; then
            export TAB_MAIN_MENU="4"
        else
            export TAB_MAIN_MENU="5"
        fi
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        print_info "Restarting..."
        exit 0
    else
        exit 0
    fi
}
export -f open_changelog

pw_tray_icon () {
    if [[ "$XDG_SESSION_TYPE" == "tty" ]] ; then
        if [ ! -z "$(pgrep -a yad_gui_pp | grep "\--notification" | awk '{print $1}')" ] ; then
            kill -s SIGUSR1 "$(pgrep -a yad_gui_pp | grep "\--notification" | awk '{print $1}')"
        fi
    else
        if [[ ! -z "$(pgrep -a tray_gui_pp)" ]] ; then
            kill -s SIGUSR1 $(pgrep -a tray_gui_pp) 2>/dev/null
        fi
    fi

    pw_tray_winefile () {
        pw_run winefile "C:\\users\\${USER}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\"
    }
    export -f pw_tray_winefile

    pw_tray_taskmgr () {
        pw_run taskmgr
    }
    export -f pw_tray_taskmgr

    tray_icon_click_exit () {
        pw_stop_progress_bar
        stop_portwine
    }
    export -f tray_icon_click_exit

    if [[ "$XDG_SESSION_TYPE" == "tty" ]] ; then
        tray_icon_click () {
            echo ""
        }
        export -f tray_icon_click
        "${pw_yad}" --notification --no-middle \
        --image="$PW_GUI_ICON_PATH/portproton_tray_flatpak.svg" \
        --command="bash -c tray_icon_click" \
        --tooltip="PortProton" \
        --icon-size=32 --menu="| \
        $(gettext "WINEFILE")!bash -c pw_tray_winefile!"$PW_GUI_ICON_PATH/wine_file.svg"|| \
        $(gettext "TASKMGR")!bash -c pw_tray_taskmgr!"$PW_GUI_ICON_PATH/wine_system.svg"|| \
        $(gettext "CHANGELOG")!bash -c open_changelog!"$PW_GUI_ICON_PATH/history.svg"|| \
        $(gettext "FORCE EXIT")     !bash -c tray_icon_click_exit!"$PW_GUI_ICON_PATH/close.svg"||" 2>/dev/null &
    else
        PW_GUI_TRAY_PATH="${PW_GUI_THEMES_PATH}/tray"
        env LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" "${PW_GUI_TRAY_PATH}/tray_gui_pp" &
    fi

    return 0
}

# GUI GET OTHER WINE
gui_proton_downloader () {
    unset VERSION_WINE_GIT VERSION_INSTALLED_WINE URL_VERSION_PROTON_GIT GIVE_WINE_URL
    try_remove_file "${PW_TMPFS_PATH}/tmp_proton_pw_git"
    try_remove_file "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
    try_remove_file "${PW_TMPFS_PATH}/tmp_proton_ge_git"
    try_remove_file "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git"
    try_remove_file "${PW_TMPFS_PATH}/tmp_installed_wine"
    try_remove_file "${PW_TMPFS_PATH}/tmp_set_wine"

    if [[ "$PW_WINE_USE" == PROTON_LG ]]
    then PW_WINE_USE="${PW_PROTON_LG_VER}"
    elif [[ "$PW_WINE_USE" == WINE_*_LG ]] || [[ "$PW_WINE_USE" == WINE_LG ]]
    then PW_WINE_USE="${PW_WINE_LG_VER}"
    elif [[ "$1" == "silent" ]] && [[ ! -z "$2" ]]
    then PW_WINE_USE="$2"
    fi
    export PW_WINE_USE

    if [[ "$1" == "silent" ]] \
    && [[ -d "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}" ]] ; then
        return 0
    fi

    pw_start_progress_bar_block "$(gettext "Check new version WINE...")"

    # PROTON_GE
    export PROTON_GE_GIT=($(curl -s "https://api.github.com/repos/GloriousEggroll/proton-ge-custom/releases" | grep "browser_download_url.*\.tar\.gz" | cut -d \" -f 4))
    if [[ ! -z "${PROTON_GE_GIT}" ]] ; then
        for PGEGIT in ${PROTON_GE_GIT[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.gz//' >> "${PW_TMPFS_PATH}/tmp_proton_ge_git"
        done
        sed -i '/Proton-6.5-GE-2/,$d' "${PW_TMPFS_PATH}/tmp_proton_ge_git"
        sed -i '/github-action/d' "${PW_TMPFS_PATH}/tmp_proton_ge_git"
        sed -i '/^$/d' "${PW_TMPFS_PATH}/tmp_proton_ge_git"
    fi

    # WINE_KRON4EK
    export WINE_KRON4EK=($(curl -s "https://api.github.com/repos/Kron4ek/Wine-Builds/releases" | grep "browser_download_url.*\.tar\.xz" | cut -d \" -f 4))
    if [[ ! -z "${WINE_KRON4EK}" ]] ; then
        for PGEGIT in ${WINE_KRON4EK[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
        done
        sed -i '/6.3/,$d' "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
        sed -i '/-x86/d' "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
        sed -i '/^$/d' "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
    fi

    # WINE_GE_CUSTOM
    export WINE_GE_CUSTOM=($(curl -s "https://api.github.com/repos/GloriousEggroll/wine-ge-custom/releases" | grep "browser_download_url.*\.tar\.xz" | cut -d \" -f 4))
    if [[ ! -z "${WINE_GE_CUSTOM}" ]] ; then
        for PGEGIT in ${WINE_GE_CUSTOM[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git"
        done
        sed -i '/6.23/,$d' "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git"
        sed -i '/^$/d' "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git"
    fi

    # PROTON_LG
    export PROTON_PW_GIT=($(curl -s "https://api.github.com/repos/Castro-Fidel/wine_builds/releases" | grep "browser_download_url.*\.tar\.xz" | cut -d \" -f 4 | sort -r))
    if [[ ! -z "${PROTON_PW_GIT}" ]] ; then
        for PPWGIT in ${PROTON_PW_GIT[@]} ; do
            echo ${PPWGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PW_TMPFS_PATH}/tmp_proton_pw_git"
        done
        sed -i /${PW_WINE_LG_VER}/d "${PW_TMPFS_PATH}/tmp_proton_pw_git"
        sed -i '/plugins/d' "${PW_TMPFS_PATH}/tmp_proton_pw_git"
        sed -i '/^$/d' "${PW_TMPFS_PATH}/tmp_proton_pw_git"
    fi
    pw_stop_progress_bar

    if [[ -z "${PROTON_PW_GIT}" ]] ; then
        yad_error "$(gettext "Error: check wine.")"
        export SKIP_CHECK_UPDATES="1"
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    fi

    pushd "${PORT_WINE_PATH}/data/dist/" || fatal
    for INSTALLING_VERSION_IN_DIST in * ; do
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PW_TMPFS_PATH}/tmp_proton_pw_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PW_TMPFS_PATH}/tmp_proton_ge_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git"
    done
    popd 1>/dev/null || fatal

    TMP_PROTON_PW_GIT="$(sed 's/^/FALSE /' "${PW_TMPFS_PATH}/tmp_proton_pw_git" | tr '\n' ' ')"
    TMP_PROTON_GE_GIT="$(sed 's/^/FALSE /' "${PW_TMPFS_PATH}/tmp_proton_ge_git" | tr '\n' ' ')"
    TMP_PROTON_KR_GIT="$(sed 's/^/FALSE /' "${PW_TMPFS_PATH}/tmp_wine_kron4ek_git" | tr '\n' ' ')"
    TMP_PROTON_CU_GIT="$(sed 's/^/FALSE /' "${PW_TMPFS_PATH}/tmp_wine_ge_custom_git" | tr '\n' ' ')"

    ls -l ${PORT_WINE_PATH}/data/dist | awk '{print $9}' | sed "/$PW_PROTON_LG_VER/d" | sed "/$PW_WINE_LG_VER/d" | sed '/^$/d' 1>${PW_TMPFS_PATH}/tmp_installed_wine
    TMP_INSTALLED_WINE="$(sed 's/^/FALSE /' "${PW_TMPFS_PATH}/tmp_installed_wine" | tr '\n' ' ')"

    if [[ "$1" != "silent" ]] ; then
        # GUI
        export KEY_WINE=$RANDOM

        "${pw_yad}" --plug=$KEY_WINE --tabnum=1 --list --checklist --separator="" \
        --gui-type-layout=${WINE_DOWNLOAD_GUI_TYPE_LAYOUT} --gui-type-text=${WINE_DOWNLOAD_GUI_TYPE_TEXT} \
        --column "$(gettext "Set")" \
        --column "$(gettext "Select WINE for download:")" \
        $TMP_PROTON_PW_GIT \
        1>> "${PW_TMPFS_PATH}/tmp_set_wine" \
        2>/dev/null &

        "${pw_yad}" --plug=$KEY_WINE --tabnum=2 --list --checklist --separator="" \
        --gui-type-layout=${WINE_DOWNLOAD_GUI_TYPE_LAYOUT} --gui-type-text=${WINE_DOWNLOAD_GUI_TYPE_TEXT} \
        --column "$(gettext "Set")" \
        --column "$(gettext "Select WINE for download:")" \
        $TMP_PROTON_KR_GIT \
        1>> "${PW_TMPFS_PATH}/tmp_set_wine" \
        2>/dev/null &

        "${pw_yad}" --plug=$KEY_WINE --tabnum=3 --list --checklist --separator="" \
        --gui-type-layout=${WINE_DOWNLOAD_GUI_TYPE_LAYOUT} --gui-type-text=${WINE_DOWNLOAD_GUI_TYPE_TEXT} \
        --column "$(gettext "Set")" \
        --column "$(gettext "Select WINE for download:")" \
        $TMP_PROTON_GE_GIT \
        1>> "${PW_TMPFS_PATH}/tmp_set_wine" \
        2>/dev/null &

        "${pw_yad}" --plug=$KEY_WINE --tabnum=4 --list --checklist --separator="" \
        --gui-type-layout=${WINE_DOWNLOAD_GUI_TYPE_LAYOUT} --gui-type-text=${WINE_DOWNLOAD_GUI_TYPE_TEXT} \
        --column "$(gettext "Set")" \
        --column "$(gettext "Select WINE for download:")" \
        $TMP_PROTON_CU_GIT \
        1>> "${PW_TMPFS_PATH}/tmp_set_wine" \
        2>/dev/null &

        "${pw_yad}" --plug=$KEY_WINE --tabnum=5 --list --checklist --separator="" \
        --gui-type-layout=${WINE_DOWNLOAD_GUI_TYPE_LAYOUT} --gui-type-text=${WINE_DOWNLOAD_GUI_TYPE_TEXT} \
        --column "$(gettext "Set")" \
        --column "$(gettext "Select installed WINE for delete:")" \
        $TMP_INSTALLED_WINE \
        1> "${PW_TMPFS_PATH}/tmp_installed_wine_set" \
        2>/dev/null &

        "${pw_yad}" --key=$KEY_WINE --notebook --width=500 --height=600 --text-align=center \
        --gui-type="settings-notebook" \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(gettext "WINE MANAGER")" --separator="" --expand \
        --tab-pos="top" \
        --tab="PROTON-LG"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="WINE-KRON4EK"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="PROTON-GE"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="WINE-GE-CUSTOM"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="$(gettext "INSTALLED")"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
        --button="$(gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":0 2>/dev/null
        YAD_WINE_STATUS="$?"
        if [[ "$YAD_WINE_STATUS" == "1" || "$YAD_WINE_STATUS" == "252" ]] ; then
            export SKIP_CHECK_UPDATES="1"
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi
        if [[ ! -z $(<"${PW_TMPFS_PATH}/tmp_set_wine") ]] ; then
            VERSION_WINE_GIT="$(sed 's/TRUE//' "${PW_TMPFS_PATH}/tmp_set_wine")"
        fi
        if [[ ! -z $(<"${PW_TMPFS_PATH}/tmp_installed_wine_set") ]] ; then
            VERSION_INSTALLED_WINE="$(sed 's/TRUE//' "${PW_TMPFS_PATH}/tmp_installed_wine_set")"
        fi

        if [[ -z "${VERSION_WINE_GIT}" ]] \
        && [[ -z "${VERSION_INSTALLED_WINE}" ]]
        then
            print_info "Nothing to do."
            export SKIP_CHECK_UPDATES="1"
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi

    elif [[ "$1" == "silent" ]] && [[ ! -z "$2" ]] ; then
        VERSION_WINE_GIT="$2"
    fi

    pw_download_get_wine () {
        print_info "Download and install ${VERSION_WINE_GIT}..."
        FILENAME="$(basename "${URL_VERSION_PROTON_GIT}")"
        if try_download "${URL_VERSION_PROTON_GIT}" "${PORT_WINE_PATH}/data/tmp/$FILENAME" ; then
            if unpack "${PORT_WINE_PATH}/data/tmp/${FILENAME}" "${PORT_WINE_PATH}/data/dist/" ; then
                try_remove_file "${PORT_WINE_PATH}/data/tmp/${FILENAME}"
                if [ ! -z "${portwine_exe}" ]; then
                    PW_WINE_USE="$(echo "${VERSION_WINE_GIT}" | tr [[:lower:]] [[:upper:]])"
                    edit_db_from_gui PW_WINE_USE
                fi
            else
                try_remove_file "${PORT_WINE_PATH}/data/tmp/${FILENAME}"
                try_remove_dir "${PORT_WINE_PATH}/data/dist/${FILENAME}"
                if yad_error_download
                then gui_proton_downloader $@
                else
                    export PW_WINE_VER="$PW_WINE_USE"
                    init_wine_ver
                fi
            fi
        else
            if yad_error_download
            then gui_proton_downloader $@
            else
                export PW_WINE_VER="$PW_WINE_USE"
                init_wine_ver
            fi
        fi
    }

    if [[ ! -z "${VERSION_INSTALLED_WINE}" ]] ; then
        for REMOVE_WINE in ${VERSION_INSTALLED_WINE} ; do
            try_remove_dir "${PORT_WINE_PATH}/data/dist/${REMOVE_WINE}"
        done
        if [[ -z "${VERSION_WINE_GIT}" ]] ; then
            export SKIP_CHECK_UPDATES="1"
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi
    fi

    VERSION_WINE_GIT=$(echo ${VERSION_WINE_GIT})
    if [[ "$1" != "silent" ]] ; then
        for GIVE_ALL_WINE in ${VERSION_WINE_GIT} ; do
            for GIVE_WINE_URL in ${WINE_GE_CUSTOM[@]} ${PROTON_GE_GIT[@]} ${WINE_KRON4EK[@]} ${PROTON_PW_GIT[@]} ; do
                if [ ! -z $(echo ${GIVE_WINE_URL} | grep -i "${GIVE_ALL_WINE}") ] ; then
                    export URL_VERSION_PROTON_GIT="${GIVE_WINE_URL}"
                fi
            done
            pw_download_get_wine
        done
        export SKIP_CHECK_UPDATES="1"
        edit_db_from_gui
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    else
        # init_wine_ver
        print_error "$PW_WINE_USE"
        for GIVE_WINE_URL in ${WINE_GE_CUSTOM[@]} ${PROTON_GE_GIT[@]} ${WINE_KRON4EK[@]} ${PROTON_PW_GIT[@]} ; do
            if [ ! -z $(echo ${GIVE_WINE_URL} | grep -i "${PW_WINE_USE}") ] ; then
                export URL_VERSION_PROTON_GIT="${GIVE_WINE_URL}"
            fi
        done
        pw_download_get_wine
    fi
}
export -f gui_proton_downloader

# GUI EDIT_DB
gui_edit_db () {
    KEY_EDIT_DB_GUI=$RANDOM
    PW_EDIT_DB_LIST="PW_MANGOHUD PW_MANGOHUD_USER_CONF PW_VKBASALT PW_VKBASALT_USER_CONF PW_DGVOODOO2 PW_GAMESCOPE
    PW_NO_ESYNC PW_NO_FSYNC PW_USE_RAY_TRACING PW_USE_NVAPI_AND_DLSS PW_USE_FAKE_DLSS PW_WINE_FULLSCREEN_FSR
    PW_HIDE_NVIDIA_GPU PW_VIRTUAL_DESKTOP PW_USE_TERMINAL PW_GUI_DISABLED_CS PW_USE_GAMEMODE PW_USE_D3D_EXTRAS
    PW_FIX_VIDEO_IN_GAME PW_REDUCE_PULSE_LATENCY PW_USE_US_LAYOUT PW_USE_GSTREAMER PW_FORCE_LARGE_ADDRESS_AWARE
    PW_USE_SHADER_CACHE PW_USE_WINE_DXGI PW_USE_EAC_AND_BE PW_USE_SYSTEM_VK_LAYERS PW_USE_OBS_VKCAPTURE
    PW_DISABLE_COMPOSITING PW_USE_RUNTIME PW_DINPUT_PROTOCOL PW_USE_NATIVE_WAYLAND
    "
    # PW_USE_FAKE_DLSS_3

    if check_wayland_session ; then
        rm_from_var PW_EDIT_DB_LIST "PW_USE_US_LAYOUT"
    else
        rm_from_var PW_EDIT_DB_LIST "PW_USE_NATIVE_WAYLAND"
    fi

    if check_flatpak ; then
        rm_from_var PW_EDIT_DB_LIST "PW_USE_RUNTIME"
    fi

    # PW_FORCE_USE_VSYNC PW_HEAP_DELAY_FREE

if [[ "${SKIP_EDIT_DB_TEXT_INFO}" != "1" ]] ; then
    export PW_DGVOODOO2_INFO=$(gettext 'Enable dgVoodoo2. Forced use all dgVoodoo2 libs (Glide 2.11-3.1, DirectDraw 1-7, Direct3D 2-9) on all 3D API. For WineD3D OpenGL need use WineLG (For Gallium Nine and Zink use too)')
    export PW_DINPUT_PROTOCOL_INFO=$(gettext 'Force use DirectInput protocol instead of XInput')
    export PW_USE_NATIVE_WAYLAND_INFO=$(gettext 'Enable experemental native Wayland support (need special wine build to work)')
    export PW_VKBASALT_INFO=$(gettext 'Enable vkBasalt by default to improve graphics in games running on Vulkan. (The HOME hotkey disables vkbasalt)')
    export PW_NO_ESYNC_INFO=$(gettext 'Do not use in-process synchronization primitives based on eventfd. (It is recommended not to change the value.)')
    export PW_NO_FSYNC_INFO=$(gettext 'Do not use futex-based in-process synchronization primitives. (Automatically disabled on systems without FUTEX_WAIT_MULTIPLE support) (It is recommended not to change the value)')
    export PW_USE_RAY_TRACING_INFO=$(gettext 'Enable vkd3d support - Ray Tracing')
    export PW_FIX_VIDEO_IN_GAME_INFO=$(gettext 'Required for video playback in some games so that it is not distorted (usually colored pink)')
    export PW_VULKAN_NO_ASYNC_INFO=$(gettext 'Disable asynchronous calls for VULKAN and DXVK modes')
    export PW_USE_NVAPI_AND_DLSS_INFO=$(gettext 'Enable DLSS on supported NVIDIA graphics cards')
    export PW_OLD_GL_STRING_INFO=$(gettext 'Forced use of older versions of OpenGL')
    export PW_HIDE_NVIDIA_GPU_INFO=$(gettext 'Disguise all features used for NVIDIA graphics cards')
    export PW_FORCE_USE_VSYNC_INFO=$(gettext 'Forced activation of vertical sync')
    export PW_VIRTUAL_DESKTOP_INFO=$(gettext 'Enable the application to run in the WINE virtual desktop')
    export PW_USE_TERMINAL_INFO=$(gettext 'Run the application in the terminal')
    export PW_HEAP_DELAY_FREE_INFO=$(gettext 'Include a delay in releasing some memory to bypass errors associated with using the application after the memory is released')
    export PW_NO_WRITE_WATCH_INFO=$(gettext 'A very dangerous way to hack the memory write timer in ntdll. This improves the performance of some very specific games. (It is recommended not to change the value.)')
    export PW_GUI_DISABLED_CS_INFO=$(gettext 'Disable the window for selecting startup modes and WINE versions')
    export PW_USE_GSTREAMER_INFO=$(gettext 'Use Gstreamer to output clips in games (WMF support)')
    export PW_FORCE_LARGE_ADDRESS_AWARE_INFO=$(gettext 'Increases RAM usage limits for 32-bit applications from two to four gigabytes')
    export PW_USE_RUNTIME_INFO=$(gettext 'Use container launch mode (It is recommended not to change the value)')
    export PW_MANGOHUD_INFO=$(gettext 'Using FPS and system load monitoring (Turns on and off by the key combination - right Shift + F12)')
    export PW_USE_GAMEMODE_INFO=$(gettext 'Using automatic system optimization to improve performance in games (provided the gamemode package is installed on the system)')
    export PW_USE_WINE_DXGI_INFO=$(gettext 'Forced use of built-in DXGI library (in rare cases it solves problems with DX12 games)')
    export PW_MANGOHUD_USER_CONF_INFO=$(gettext 'Forced use of MANGOHUD system settings (GOverlay, etc.)')
    export PW_VKBASALT_USER_CONF_INFO=$(gettext 'Forced use of VKBASALT system settings (GOverlay, etc.)')
    export PW_USE_D3D_EXTRAS_INFO=$(gettext 'Enable forced use of third-party DirectX libraries')
    export PW_USE_WINDOWS_7_INFO=$(gettext 'Change the version of WINDOWS 10 to WINDOWS 7 in the prefix')
    export PW_USE_SHADER_CACHE_INFO=$(gettext 'Use WINE shader caching (disable only if there are microfreezes in the game)')
    export PW_WINE_FULLSCREEN_FSR_INFO=$(gettext 'Works while using any version of ProtonGE in full screen mode at a resolution below the standard screen')
    export PW_USE_FAKE_DLSS_INFO=$(gettext 'Enable DLSS translator in FSR 2 in DirectX 12 games (CyberFSR2)')
    export PW_USE_EAC_AND_BE_INFO=$(gettext 'Enable Easy Anti-Cheat and BattlEye Anti-Cheat runtimes (required if game used this anti-cheats)')
    export PW_REDUCE_PULSE_LATENCY_INFO=$(gettext 'Reduce pulseaudio latency to fix intermittent sound')
    export PW_USE_US_LAYOUT_INFO=$(gettext 'Forced use of the us layout (useful for games in which the control works correctly only on the us layout)')
    export PW_RESTORE_RESOLUTION_INFO=$(gettext 'Remember the screen resolution when starting the game and return it when closing (useful for games that change the screen resolution when closing)')
    export PW_USE_SYSTEM_VK_LAYERS_INFO=$(gettext 'Use system mangohud, vkBasalt, obs-vkcapture and other applications using vulkan layers')
    export PW_USE_OBS_VKCAPTURE_INFO=$(gettext 'Enable the ability to write to OBS Studio using obs-vkcapture (ATTENTION: the forced use of system mangohud, vkBasalt, obs-vkcapture and other applications using vulkan layers will be enabled)')
    export PW_DISABLE_COMPOSITING_INFO=$(gettext 'Disable desktop compositing (effects). It often improves performance.')
    export PW_GAMESCOPE_INFO=$(gettext '<b>Super + F :</b> Toggle fullscreen
<b>Super + N :</b> Toggle nearest neighbour filtering
<b>Super + U :</b> Toggle FSR upscaling
<b>Super + Y :</b> Toggle NIS upscaling
<b>Super + I :</b> Increase FSR sharpness by 1
<b>Super + O :</b> Decrease FSR sharpness by 1
<b>Super + S :</b> Take screenshot (currently goes to /tmp/gamescope_DATE.png)
<b>Super + G :</b> Toggle keyboard grab
<b>Super + C :</b> Update clipboard')

    export SKIP_EDIT_DB_TEXT_INFO="1"
fi

    case "${PW_VULKAN_USE}" in
        0)
            # WineD3D OpenGL
            VKD3D_CB="DCB"
            DISABLE_EDIT_DB_LIST="PW_VKBASALT PW_VKBASALT_USER_CONF PW_USE_RAY_TRACING PW_USE_OBS_VKCAPTURE
            "
            ;;
        1|2)
            # Stable, Newest
            VKD3D_CB="CB"
            ;;
        3)
            # Gallium Nine
            VKD3D_CB="DCB"
            DISABLE_EDIT_DB_LIST="PW_MANGOHUD PW_MANGOHUD_USER_CONF PW_VKBASALT PW_VKBASALT_USER_CONF PW_USE_RAY_TRACING PW_WINE_FULLSCREEN_FSR
            PW_USE_SYSTEM_VK_LAYERS PW_USE_OBS_VKCAPTURE
            "
            ;;
        4|5|6)
            # Galliun Zink, Legacy, WineD3D Vulkan
            VKD3D_CB="DCB"
            DISABLE_EDIT_DB_LIST="PW_USE_RAY_TRACING
            "
            ;;
    esac

    unset ADD_CHK_BOX_EDIT_DB
    for int_to_boole in ${PW_EDIT_DB_LIST} ; do
        if [ "${!int_to_boole}" == "1" ]
        then export ${int_to_boole}="TRUE"
        else export ${int_to_boole}="FALSE"
        fi
        TMP_HELP_FOR_GUI="${int_to_boole}_INFO"
        int_to_boole_non_pw="$(echo ${int_to_boole//PW_/} | sed 's/_/ /g')"
        if [[ ! "${PW_VULKAN_USE}" == "1" ]] && [[ ! "${PW_VULKAN_USE}" == "2" ]] \
        && grep -wo "${int_to_boole}" <<<"${DISABLE_EDIT_DB_LIST}" &>/dev/null
        then ADD_CHK_BOX_EDIT_DB+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:D${THEME_CHKBOX}%${!int_to_boole}%"
        else ADD_CHK_BOX_EDIT_DB+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:${THEME_CHKBOX}%${!int_to_boole}%"
        fi
    done

    IFS="%"
    "${pw_yad}" --plug=$KEY_EDIT_DB_GUI --tabnum="1" --form --separator=" " --columns=3 ${ADD_CHK_BOX_EDIT_DB} \
    1> "${PW_TMPFS_PATH}/tmp_output_yad_edit_db" 2>/dev/null &
    IFS="$orig_IFS"

    case ${PW_WINDOWS_VER} in
         "7") ADD_WINVER_EDIT_DB="7!10!11!XP" ;;
        "XP") ADD_WINVER_EDIT_DB="XP!7!10!11" ;;
        "10") ADD_WINVER_EDIT_DB="10!11!7!XP" ;;
           *) ADD_WINVER_EDIT_DB="11!10!7!XP" ;;
    esac

    if [[ ! -z "${PW_SOUND_DRIVER_USE}" ]] \
    && [[ "${PW_SOUND_DRIVER_USE}" != "disabled" ]]
    then
        SOUND_DRIVER_VAR="${PW_SOUND_DRIVER_USE}"
    else
        SOUND_DRIVER_VAR="disabled"
    fi

    if [[ ! -z "${PW_GPU_USE}" ]] \
    && [[ "${PW_GPU_USE}" != "disabled" ]]
    then
        GPU_VAR="${PW_GPU_USE}"
    else
        GPU_VAR="disabled"
    fi

    if [[ "${PW_WINE_CPU_TOPOLOGY}" == *[0-9]:* ]] \
    && [[ "${PW_WINE_CPU_TOPOLOGY}" != "disabled" ]]
    then
        CPU_LIMIT_VAR="$(echo ${PW_WINE_CPU_TOPOLOGY%%:*})"
    else
        CPU_LIMIT_VAR="disabled"
    fi

    "${pw_yad}" --plug=$KEY_EDIT_DB_GUI --tabnum="2" --form --separator="%" --columns=1 \
    --field="$(gettext "Change the version of <b>WINDOWS</b> emulation")!$(gettext "Changing the <b>WINDOWS</b> emulation version may be required to run older games. <b>WINDOWS</b> versions below 10 do not support new games with DirectX 12") :CB" "${ADD_WINVER_EDIT_DB}" \
    --field="$(gettext "AUTOINSTALL WITH <b>WINETRICKS</b>")!$(gettext "Automatically install with <b>WINETRICKS</b> additional libraries required to run the game/program. List of libraries separated by spaces") :CBE" "$(combobox_fix --empty "${PW_DLL_INSTALL}" "vcrun2019 corefonts lucida")" \
    --field="$(gettext "Forced to use/disable libraries")!$(gettext "Forced to use/disable the library only for the given application. (There are examples in the drop-down list)

A brief instruction:
    * libraries are written <b>WITHOUT</b> the .dll file extension
    * libraries are separated by semicolons - <b>;</b>
    * library=n - use the <b>WINDOWS</b> (third-party) library
    * library=b - use <b>WINE</b> (built-in) library
    * library=n,b - use <b>WINDOWS</b> library and then <b>WINE</b>
    * library=b,n - use <b>WINE</b> library and then <b>WINDOWS</b>
    * library= - disable the use of this library") :CBE" "$(combobox_fix --empty "${WINEDLLOVERRIDES}" "libglesv2=!d3dx9_36,d3dx9_42=n,b;mfc120=b,n")" \
    --field=":LBLH" "" \
    --field="$(gettext "ADD ARGUMENTS FOR .EXE FILE")!$(gettext "Adding an argument after the <b>.exe</b> file, just like you would add an argument in a shortcut on a <b>WINDOWS </b> system") :CBE" "$(combobox_fix --empty "\\${LAUNCH_PARAMETERS[@]}" "-dx11 -skipintro 1")" \
    --field=":LBLH" "" \
    --field="$(gettext "Limit the use of processor cores")!$(gettext "Limiting the number of CPU cores is useful for Unity games (It is recommended to set the value equal to 8)") :CB" "$(combobox_fix --disabled "${CPU_LIMIT_VAR}" "${GET_LOGICAL_CORE}")" \
    --field="$(gettext "Choose a graphics card to run the game (in user.conf)")!$(gettext "Select which video card will be used to run the game (used for all running games and programs in PortProton)") :CB" "$(combobox_fix --disabled "${GPU_VAR}" "${GET_GPU_NAMES}")" \
    --field="$(gettext "Forcibly select the OpenGL version for the game")!$(gettext "You can select the required OpenGL version, some games require a forced Compatibility Profile (COMPAT). (Examples are in the drop-down list)") :CB" "$(combobox_fix --disabled "${PW_MESA_GL_VERSION_OVERRIDE}" "4.6COMPAT!4.6!4.5COMPAT!4.5!3.3COMPAT!3.3")" \
    --field="$(gettext "Forcibly select the VKD3D feature level")!$(gettext "You can set a forced feature level VKD3D for games on DirectX12") :${VKD3D_CB}" "$(combobox_fix --disabled "${PW_VKD3D_FEATURE_LEVEL}" "12_2!12_1!12_0!11_1!11_0")" \
    --field="$(gettext "Force certain locale for an app:")!$(gettext "Fixes encoding issues in legacy software") :CB" "$(combobox_fix --disabled "${PW_LOCALE_SELECT}" "$LOCALE_LIST")" \
    --field="$(gettext "Choice wine audio driver")!$(gettext "Fixes sound popling if choice alsa") :CB" "$(combobox_fix --disabled "${SOUND_DRIVER_VAR}" "alsa!oss!pulse")" \
    1> "${PW_TMPFS_PATH}/tmp_output_yad_fps_limit" 2>/dev/null &

    "${pw_yad}" --notebook --key="$KEY_EDIT_DB_GUI" --title "$(gettext "EDIT DB")" --text-align=center \
    --text "$(gettext "Change settings in database file for") <b>${PORTWINE_DB}</b>\n $(gettext "<b>NOTE:</b> To display help for each item, simply hover your mouse over the text")" \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --separator=" " --expand \
    --gui-type="settings-base" \
    --gui-type-text=${SETTINGS_BASE_GUI_TYPE_TEXT} --gui-type-layout=${SETTINGS_BASE_GUI_TYPE_LAYOUT} \
    --tab="$(gettext "MAIN")"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
    --tab="$(gettext "ADVANCED")"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
    --button="$(gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(gettext "RESET SETTINGS")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Restore default settings")":2 \
    --button="$(gettext "OPEN THE SETTINGS FILE")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Open the <b>.ppdb</b> settings file in a system text editor to view and change variables manually")":150 \
    --button="$(gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Save the current changes, and go to the previous menu")":0 2>/dev/null
    YAD_STATUS="$?"
    export SKIP_CHECK_UPDATES="1"

    case "$YAD_STATUS" in
        1|252)
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
          2)
                try_remove_file "$portwine_exe".ppdb
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
    esac

    output_yad_edit_db=($(<"${PW_TMPFS_PATH}/tmp_output_yad_edit_db"))
    bool_from_yad=0
    for boole_to_int in ${PW_EDIT_DB_LIST} ; do
        export ${boole_to_int}=${output_yad_edit_db[$bool_from_yad]}
        if [ "${!boole_to_int}" == "TRUE" ]
        then export ${boole_to_int}="1"
        else export ${boole_to_int}="0"
        fi
        export bool_from_yad=$(( ${bool_from_yad} + 1 ))
    done

    PW_ADD_SETTINGS=$(head -n 1 "${PW_TMPFS_PATH}/tmp_output_yad_fps_limit")

    PW_WINDOWS_VER="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $1}')"
    PW_DLL_INSTALL="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $2}')"
    WINEDLLOVERRIDES="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $3}')"
    LAUNCH_PARAMETERS="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $5}')"
    CPU_LIMIT="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $7}')"
    PW_GPU_USE="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $8}')"
    PW_MESA_GL_VERSION_OVERRIDE="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $9}')"
    PW_VKD3D_FEATURE_LEVEL="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $10}')"
    PW_LOCALE_SELECT="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $11}')"
    PW_SOUND_DRIVER_USE="$(echo ${PW_ADD_SETTINGS} | awk -F"%" '{print $12}')"

    if [[ "${CPU_LIMIT}" != "disabled" ]] ; then
        export PW_WINE_CPU_TOPOLOGY="${CPU_LIMIT}:$(seq -s, 0 $((${CPU_LIMIT} - 1)))"
    else
        export PW_WINE_CPU_TOPOLOGY="disabled"
    fi

    edit_db_from_gui ${PW_EDIT_DB_LIST} LAUNCH_PARAMETERS PW_WINDOWS_VER PW_DLL_INSTALL WINEDLLOVERRIDES PW_WINE_CPU_TOPOLOGY \
    PW_MESA_GL_VERSION_OVERRIDE PW_VKD3D_FEATURE_LEVEL PW_LOCALE_SELECT

    if [[ -z "$MANGOHUD_CONFIG" ]] ; then
        MONITOR_HEIGHT="$(echo "$PW_SCREEN_RESOLUTION" | awk -F'x' '{print $2}')"
        MH_FONT_SIZE="font_size=$(( MONITOR_HEIGHT / 45 ))"

        if [[ ! -z "$MH_FONT_SIZE" ]]
        then MANGOHUD_CONFIG="$DEFAULT_MANGOHUD_CONFIG,$MH_FONT_SIZE"
        else MANGOHUD_CONFIG="$DEFAULT_MANGOHUD_CONFIG"
        fi

        edit_db_from_gui MANGOHUD_CONFIG
    fi
    edit_user_conf_from_gui PW_GPU_USE PW_SOUND_DRIVER_USE

    case "$YAD_STATUS" in
          0)
                print_info "Restarting PP after update ppdb file..."
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
        150)
                print_info "Restarting PP after update ppdb file..."
                gui_edit_db_file
                ;;
    esac
}

# GUI VKBASALT
gui_vkbasalt () {
    KEY_FX_GUI=$RANDOM
    FILE_VKBASALT_CONF="${PORT_WINE_PATH}/data/vkBasalt.conf"
    LIST_FX=($(grep -E '.fx$|.fxh$' "${FILE_VKBASALT_CONF}" | awk '{print $1}'))
    GET_FX_IN_FILE=($(echo ${PW_VKBASALT_EFFECTS} | sed s/"cas:"// | sed s/":"/" "/g))

if [[ "${SKIP_VKBASALT_TEXT_INFO}" != "1" ]] ; then
    export PW_3DFX_INFO=$(gettext 'Simulation of an old 3dfx graphics accelerator (adds horizontal stripes)')
    export PW_AdaptiveSharpen_INFO=$(gettext 'Sharpness increase, can be used with CAS')
    export PW_Bloom_INFO=$(gettext 'Adds glow from bright light sources in the game')
    export PW_Border_INFO=$(gettext 'Cropping the image from the top and bottom (like in a movie =)')
    export PW_Cartoon_INFO=$(gettext 'Strong sharpening of texture edges')
    export PW_ChromaticAberration_INFO=$(gettext 'Adds chromatic aberration to an image')
    export PW_Clarity_INFO=$(gettext 'Sharpening textures with slight modifications of image contrast (similar in effect to CAS)')
    export PW_ColorMatrix_INFO=$(gettext 'Adding cool colors')
    export PW_Colourfulness_INFO=$(gettext 'Increasing color saturation')
    export PW_FakeHDR_INFO=$(gettext 'Add fake HDR')
    export PW_FilmGrain2_INFO=$(gettext 'Adding the film grain effect')
    export PW_Curves_INFO=$(gettext 'Increases the contrast of the image without affecting the bright and dark areas, so that the detail in shadows and sky is not lost')
    export PW_Denoise_INFO=$(gettext 'Reducing noise by blurring the image')
    export PW_Emphasize_INFO=$(gettext 'Reducing the bit depth of color')
    export PW_FakeMotionBlur_INFO=$(gettext 'Strong blurring of objects in motion')
    export PW_FilmicAnamorphSharpen_INFO=$(gettext 'Sharpening (recommended for cartoon games)')
    export PW_FilmicPass_INFO=$(gettext 'Adding cold colors')
    export PW_SMAA_INFO=$(gettext 'Alternative antialiasing option (less effect, but with less consumption of GPU resources, relative to the standard SMAA)')
    export PW_GaussianBlur_INFO=$(gettext 'Slight blur effect on bright objects (such as flames)')
    export PW_Glitch_INFO=$(gettext 'Glitch effect')
    export PW_HighPassSharpen_INFO=$(gettext 'Another method of sharpening using highpass frequencies')
    export PW_HQ4X_INFO=$(gettext 'Smoothing of nearby pixels with the drawing of missing parts (in normal games the picture is blurred)')
    export PW_HSLShift_INFO=$(gettext 'Advanced HSL shift (for each shade)')
    export PW_Layer_INFO=$(gettext "Image overlay on the game")
    export PW_Levels_INFO=$(gettext 'Increases contrast, but some detail in shadows or highlights may be lost')
    export PW_LevelsPlus_INFO=$(gettext 'Same as Levels, but with less loss of detail')
    export PW_LiftGammaGain_INFO=$(gettext 'Gamma correction by individual color channels: red, green and blue')
    export PW_LightDoF_INFO=$(gettext 'Increases depth of field (greatly affects FPS)')
    export PW_LumaSharpen_INFO=$(gettext 'Enhances image sharpness for improved detail')
    export PW_LUT_INFO=$(gettext 'Image color correction')
    export PW_MagicBloom_INFO=$(gettext 'Same as Bloom, but more natural')
    export PW_Monochrome_INFO=$(gettext 'Converts the image into monochrome')
    export PW_MultiLUT_INFO=$(gettext 'Extended version of the LUT (Hollywood loot is pre-installed)')
    export PW_MXAO_INFO=$(gettext 'SSAO algorithm (Greatly affects FPS)')
    export PW_NightVision_INFO=$(gettext 'Night vision effect')
    export PW_Nostalgia_INFO=$(gettext 'Makes the picture look like old photos')
    export PW_PerfectPerspective_INFO=$(gettext 'Fisheye effect (suitable for VR)')
    export PW_PPFX_Godrays_INFO=$(gettext 'Add God rays (also called 3D light rays) effect')
    export PW_Prism_INFO=$(gettext 'The prismatic lens effect')
    export PW_ReflectiveBumpMapping_INFO=$(gettext 'Creates the illusion of depth in textures')
    export PW_Sepia_INFO=$(gettext 'Adds a sepia effect like in old photos')
    export PW_Splitscreen_INFO=$(gettext 'Splits the image into raw and processed output for comparison')
    export PW_SurfaceBlur_INFO=$(gettext 'Reducing object detail without blurring contrast contours')
    export PW_Technicolor_INFO=$(gettext 'Makes the picture look like films from the 1930s')
    export PW_Technicolor2_INFO=$(gettext 'The updated version of Technicolor, gives a different picture by changing colors more aggressively')
    export PW_TiltShift_INFO=$(gettext 'Blur on the edges of the screen to create a Tilt Shift effect')
    export PW_Tonemap_INFO=$(gettext 'Changes the saturation of the picture')
    export PW_TriDither_INFO=$(gettext 'Reduces the amount of noise')
    export PW_UIDetect_INFO=$(gettext 'Automatically switches effects depending on the visibility of the UI')
    export PW_Vibrance_INFO=$(gettext 'Saturates faded colors without touching the bright ones')
    export PW_Vignette_INFO=$(gettext 'Adds a vignette to the image')
    export PW_AspectRatio_INFO=$(gettext 'An effect for adjusting the aspect ratio (for games that do not support widescreen monitors)')
    export PW_UIMask_INFO=$(gettext 'Part of the overall ReShade shader . It uses a mask with gradients from black to white to determine where the effects will be applied')
    export PW_StageDepth_INFO=$(gettext 'Allows you to add new elements to the 3d space inside the game and apply custom textures')
    export PW_PPFX_Bloom_INFO=$(gettext 'Adds a Bloom effect')

    export SKIP_VKBASALT_TEXT_INFO="1"
fi

    unset ADD_GUI_FX GUI_FX_RESULT
    for add_list_fx in ${LIST_FX[@]} ; do
        PW_VKBASALT_GUI_HELP="PW_${add_list_fx}_INFO"
        if echo " ${GET_FX_IN_FILE[@]} " | grep " ${add_list_fx} " &>/dev/null ; then
            ADD_GUI_FX+="--field=${CHKBOX_SPACE}${add_list_fx}!${!PW_VKBASALT_GUI_HELP}:${THEME_CHKBOX}%TRUE%"
        else
            ADD_GUI_FX+="--field=${CHKBOX_SPACE}${add_list_fx}!${!PW_VKBASALT_GUI_HELP}:${THEME_CHKBOX}%FALSE%"
        fi
    done
    if [[ ! -z "${PW_VKBASALT_FFX_CAS}" ]] ; then
        if [[ "$PW_VKBASALT_FFX_CAS" == "0" ]] \
        || [[ "$PW_VKBASALT_FFX_CAS" == "-1" ]]
        then export VKBASALT_FFX_CAS_GUI="0"
        elif [[ "$PW_VKBASALT_FFX_CAS" == "1" ]]
        then export VKBASALT_FFX_CAS_GUI="100"
        elif [[ "$PW_VKBASALT_FFX_CAS" == 0.0* ]]
        then export VKBASALT_FFX_CAS_GUI="$(echo "$PW_VKBASALT_FFX_CAS" | awk -F'0.0' '{print $2}')"
        else export VKBASALT_FFX_CAS_GUI="$(echo "$PW_VKBASALT_FFX_CAS" | awk -F'0.' '{print $2}')"
        fi
    else
        VKBASALT_FFX_CAS_GUI=66
    fi
    IFS="%"
    "${pw_yad}" --plug=$KEY_FX_GUI --tabnum="1" --form --columns=4 --separator=" " \
    --text-align=center --text="$(gettext "VkBasalt settings for adding effects to games running under Vulkan. (The <b>HOME</b> key disables vkbasalt)\n<b>NOTE:</b> To display help for each item, simply hover over the text")" \
    --gui-type-text=${VKBASALT_GUI_TYPE_TEXT_UP} --gui-type-layout=${VKBASALT_GUI_TYPE_LAYOUT_UP} \
    ${ADD_GUI_FX} 1> "${PW_TMPFS_PATH}/tmp_yad_basalt_set" 2>/dev/null &
    IFS="$orig_IFS"

    "${pw_yad}" --plug=$KEY_FX_GUI --tabnum="2" --separator=" " --form \
    --gui-type-layout=${VKBASALT_GUI_TYPE_LAYOUT_DOWN} \
    --field="AMD FidelityFX - Contrast Adaptive Sharpening"!"$(gettext "AMD FidelityFX - CAS is designed to dramatically improve texture sharpness without additional modification settings for games, with minimal loss of performance. (For older games it is recommended to set value = 100)")":SCL "${VKBASALT_FFX_CAS_GUI}" \
    1> "${PW_TMPFS_PATH}/tmp_yad_cas_set" 2>/dev/null &

    "${pw_yad}" --paned --key="$KEY_FX_GUI" --sensitive --title="vkBasalt" \
    --gui-type="settings-paned" \
    --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --button="$(gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(gettext "RESET") VKBASALT"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Restore default settings for") vkBasalt":178 \
    --button="$(gettext "DISABLE") VKBASALT"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Disable vkBasalt and go to the previous menu")":180 \
    --button="$(gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Save the current changes, and go to the previous menu")":182 2>/dev/null
    YAD_VKBASALT_STATUS="$?"
    export SKIP_CHECK_UPDATES="1"

    case "${YAD_VKBASALT_STATUS}" in
        1|252)
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0 ;;
        178)
            sed -i '/VKBASALT/d' "$portwine_exe".ppdb
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0 ;;
        180)
            export PW_VKBASALT="0" ;;
        182)
            export PW_VKBASALT="1" ;;
    esac
    YAD_BASALT_SET=$(<"${PW_TMPFS_PATH}/tmp_yad_basalt_set")
    YAD_CAS_SET=$(cat "${PW_TMPFS_PATH}/tmp_yad_cas_set" | sed s/" "//g)

    INT_COUNT_FX=0
    for read_list_fx in ${YAD_BASALT_SET} ; do
        if [[ "$read_list_fx" == "TRUE" ]] ; then
            export GUI_FX_RESULT+="${LIST_FX[$INT_COUNT_FX]}:"
        fi
        ((INT_COUNT_FX++))
    done
    GUI_FX_RESULT="${GUI_FX_RESULT}cas"
    export PW_VKBASALT_EFFECTS="${GUI_FX_RESULT}"

    if [[ "$YAD_CAS_SET" == "0" ]]
    then export PW_VKBASALT_FFX_CAS="-1"
    elif [[ "$YAD_CAS_SET" == "100" ]]
    then export PW_VKBASALT_FFX_CAS="1"
    elif [[ "$YAD_CAS_SET" -lt 10 ]]
    then export PW_VKBASALT_FFX_CAS="0.0$YAD_CAS_SET"
    else export PW_VKBASALT_FFX_CAS="0.$YAD_CAS_SET"
    fi

    edit_db_from_gui PW_VKBASALT_EFFECTS PW_VKBASALT_FFX_CAS PW_VKBASALT
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

# GUI MANGOHUD
gui_mangohud () {
    KEY_MH_GUI=$RANDOM
    LIST_MH=(
        arch battery battery_icon battery_time battery_watt core_bars core_load
        cpu_mhz cpu_power cpu_temp device_battery_icon engine_short_names
        engine_version exec_name fcat fps_metrics frame_count frametime full
        gamemode gpu_core_clock gpu_fan gpu_junction_temp gpu_mem_clock
        gpu_mem_temp gpu_name gpu_power gpu_temp gpu_voltage histogram horizontal
        horizontal_stretch hud_compact hud_no_margin io_read io_write no_display
        no_small_font procmem procmem_shared procmem_virt ram resolution
        show_fps_limit swap temp_fahrenheit throttling_status throttling_status_graph
        time version vkbasalt vram vulkan_driver wine
    )

if [[ "${SKIP_MANGOHUD_TEXT_INFO}" != "1" ]] ; then
    export PW_MH_arch_INFO=$(gettext 'Show if the application is 32- or 64-bit')
    export PW_MH_battery_INFO=$(gettext 'Display current battery percent and energy consumption')
    export PW_MH_battery_icon_INFO=$(gettext 'Display battery icon instead of percent')
    export PW_MH_battery_time_INFO=$(gettext 'Display remaining time for battery option')
    export PW_MH_battery_watt_INFO=$(gettext 'Display wattage for the battery option')
    export PW_MH_core_bars_INFO=$(gettext 'Change the display of core_load from numbers to vertical bars')
    export PW_MH_core_load_INFO=$(gettext 'Display load & frequency per core')
    export PW_MH_cpu_mhz_INFO=$(gettext 'Show the CPUs current MHz')
    export PW_MH_cpu_power_INFO=$(gettext 'Display CPU draw in watts')
    export PW_MH_cpu_temp_INFO=$(gettext 'Display current CPU temperature')
    export PW_MH_device_battery_icon_INFO=$(gettext 'Display wirless device battery icon.')
    export PW_MH_engine_short_names_INFO=$(gettext 'Display a short version of the used engine (e.g. OGL instead of OpenGL)')
    export PW_MH_engine_version_INFO=$(gettext 'Display OpenGL or vulkan and vulkan-based render engines version')
    export PW_MH_exec_name_INFO=$(gettext 'Display current exec name')
    export PW_MH_fcat_INFO=$(gettext 'Enables frame capture analysis')
    export PW_MH_fps_metrics_INFO=$(gettext 'Takes a list of decimal values or the value avg, e.g avg,0.001')
    export PW_MH_frame_count_INFO=$(gettext 'Display frame count')
    export PW_MH_full_INFO=$(gettext 'Enable most of the toggleable parameters (currently excludes histogram)')
    export PW_MH_gamemode_INFO=$(gettext 'Show if GameMode is on')
    export PW_MH_gpu_core_clock_INFO=$(gettext 'Display GPU core frequency')
    export PW_MH_gpu_fan_INFO=$(gettext 'GPU fan in rpm on AMD, FAN in percent on NVIDIA')
    export PW_MH_gpu_junction_temp_INFO=$(gettext 'Display current GPU temperature')
    export PW_MH_gpu_mem_clock_INFO=$(gettext 'Display GPU memory frequency')
    export PW_MH_gpu_mem_temp_INFO=$(gettext 'Display current GPU temperature')
    export PW_MH_gpu_name_INFO=$(gettext 'Display GPU name from pci.ids')
    export PW_MH_gpu_power_INFO=$(gettext 'Display GPU draw in watts')
    export PW_MH_gpu_temp_INFO=$(gettext 'Display current GPU temperature')
    export PW_MH_gpu_voltage_INFO=$(gettext 'Display GPU voltage (only works on AMD GPUs)')
    export PW_MH_histogram_INFO=$(gettext 'Change FPS graph to histogram')
    export PW_MH_horizontal_INFO=$(gettext 'Display Mangohud in a horizontal position')
    export PW_MH_horizontal_stretch_INFO=$(gettext 'Stretches the background to the screens width in horizontal mode')
    export PW_MH_hud_compact_INFO=$(gettext 'Display compact version of MangoHud')
    export PW_MH_hud_no_margin_INFO=$(gettext 'Remove margins around MangoHud')
    export PW_MH_io_read_INFO=$(gettext 'Show non-cached IO read, in MiB/s')
    export PW_MH_io_write_INFO=$(gettext 'Show non-cached IO write, in MiB/s')
    export PW_MH_no_display_INFO=$(gettext 'Hide the HUD by default')
    export PW_MH_no_small_font_INFO=$(gettext 'Use primary font size for smaller text like units')
    export PW_MH_procmem_INFO=$(gettext 'Displays process memory usage: resident procmem (resident) also toggles others off if disabled')
    export PW_MH_procmem_shared_INFO=$(gettext 'Displays process memory usage: shared')
    export PW_MH_procmem_virt_INFO=$(gettext 'Displays process memory usage: virtual')
    export PW_MH_ram_INFO=$(gettext 'Display system RAM usage')
    export PW_MH_resolution_INFO=$(gettext 'Display the current resolution')
    export PW_MH_show_fps_limit_INFO=$(gettext 'Display the current FPS limit')
    export PW_MH_swap_INFO=$(gettext 'Display swap space usage next to system RAM usage')
    export PW_MH_temp_fahrenheit_INFO=$(gettext 'Show temperature in Fahrenheit')
    export PW_MH_throttling_status_INFO=$(gettext 'Show if GPU is throttling based on Power, current, temp or "other" (Only shows if throttling is currently happening). Currently disabled by default for Nvidia as it causes lag on 3000 series')
    export PW_MH_throttling_status_graph_INFO=$(gettext 'Same as throttling_status but displays throttling in the frametime graph and only power and temp throttling')
    export PW_MH_time_INFO=$(gettext 'Display time')
    export PW_MH_version_INFO=$(gettext 'Show current MangoHud version')
    export PW_MH_vkbasalt_INFO=$(gettext 'Show if vkBasalt is on')
    export PW_MH_vram_INFO=$(gettext 'Display system VRAM usage')
    export PW_MH_vulkan_driver_INFO=$(gettext 'Display used Vulkan driver (radv/amdgpu-pro/amdvlk)')
    export PW_MH_frametime_INFO=$(gettext 'Display frametime next to FPS text')
    export PW_MH_wine_INFO=$(gettext 'Show current Wine or Proton version in use')

    export SKIP_MANGOHUD_TEXT_INFO="1"
fi

    unset ADD_GUI_MH GUI_MH_RESULT ADD_GUI_MH_FPS GUI_MH_FPS_RESULT PW_FPS_LIMIT_VAR PW_MANGOHUD_CONFIG
    try_remove_file "${PW_TMPFS_PATH}/tmp_yad_mh_set"
    try_remove_file "${PW_TMPFS_PATH}/tmp_yad_mh_fps_limit"

    GET_REFRESH_RATE=(30 40 45 48 60 75 90 120 144 165 175 240)

    if [ ! -z "$MANGOHUD_CONFIG" ]; then
        PW_MANGOHUD_CONFIG=($(echo "$MANGOHUD_CONFIG" | tr ',' '\n' | grep -v '='))
    else
        PW_MANGOHUD_CONFIG=($(echo "$DEFAULT_MANGOHUD_CONFIG" | tr ',' '\n' | grep -v '='))
    fi

    if [ ! -z "$FPS_LIMIT" ] ; then
        PW_FPS_LIMIT_VAR=($(echo "$FPS_LIMIT" | tr '' '\n' | grep -v '='))
    fi

    for add_list_mh in "${LIST_MH[@]}"; do
        PW_MH_GUI_HELP="PW_MH_${add_list_mh}_INFO"
        if grep -wo "$add_list_mh" <<<"${PW_MANGOHUD_CONFIG[@]}" &>/dev/null ; then
            ADD_GUI_MH+="--field=${CHKBOX_SPACE}$(echo ${add_list_mh} | sed 's/_/ /g' | tr [[:lower:]] [[:upper:]])!${!PW_MH_GUI_HELP}:${THEME_CHKBOX}%TRUE%"
        else
            ADD_GUI_MH+="--field=${CHKBOX_SPACE}$(echo ${add_list_mh} | sed 's/_/ /g' | tr [[:lower:]] [[:upper:]])!${!PW_MH_GUI_HELP}:${THEME_CHKBOX}%FALSE%"
        fi
    done

    for add_list_mh_fps in "${GET_REFRESH_RATE[@]}"; do
        if grep -wo "$add_list_mh_fps" <<<"${PW_FPS_LIMIT_VAR[@]}" &>/dev/null ; then
            ADD_GUI_MH_FPS+="--field=${CHKBOX_SPACE}$add_list_mh_fps:${THEME_CHKBOX}%TRUE%"
        else
            ADD_GUI_MH_FPS+="--field=${CHKBOX_SPACE}$add_list_mh_fps:${THEME_CHKBOX}%FALSE%"
        fi
    done

    IFS="%"
    "${pw_yad}" --plug=$KEY_MH_GUI --tabnum="1" --form --columns=5 --separator=" " --text-align=center \
        --text="$(gettext "MangoHud settings (Keys <b>R_SHIFT + F12</b> disable MangoHud)\n<b>note:</b> To display help for each item, just hover the mouse cursor over the text.")" \
        --gui-type-text=${MANGOHUD_GUI_TYPE_TEXT_UP} --gui-type-layout=${MANGOHUD_GUI_TYPE_LAYOUT_UP} \
        ${ADD_GUI_MH} 1> "${PW_TMPFS_PATH}/tmp_yad_mh_set" 2>/dev/null &
    "${pw_yad}" --plug=$KEY_MH_GUI --tabnum="2" --form --columns=10 --separator=" " --text-align=center \
        --text="$(gettext "Enabling fps limitation with MANGOHUD tools built into PortProton (Keys <b>L_SHIFT + F1</b> Toggles fps limitation)")" \
        --gui-type-text=${MANGOHUD_GUI_TYPE_TEXT_DOWN} --gui-type-layout=${MANGOHUD_GUI_TYPE_LAYOUT_DOWN} \
        ${ADD_GUI_MH_FPS} 1> "${PW_TMPFS_PATH}/tmp_yad_mh_fps_limit" 2>/dev/null &
    IFS="$orig_IFS"

    "${pw_yad}" --paned --key="$KEY_MH_GUI" --title="MangoHud" \
        --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --sensitive \
        --gui-type="settings-paned" \
        --button="$(gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Cancel the current changes and return to the previous menu")":1 \
        --button="$(gettext "RESET") MANGOHUD"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Restore default settings for") MangoHud":180 \
        --button="$(gettext "PREVIEW CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Start vkcube for preview changes")":184 \
        --button="$(gettext "DISABLE") MANGOHUD"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Disable MangoHud and go to the previous menu")":182 \
        --button="$(gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Save the current changes, and go to the previous menu")":186  2>/dev/null
    YAD_MANGOHUD_STATUS="$?"
    export SKIP_CHECK_UPDATES="1"

    case "${YAD_MANGOHUD_STATUS}" in
        1|252)
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
            ;;
        180)
            sed -i '/FPS_LIMIT/d' "$portwine_exe".ppdb
            sed -i '/MANGOHUD/d' "$portwine_exe".ppdb
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0 ;;
        182)
            export PW_MANGOHUD="0"
            ;;
        186)
            export PW_MANGOHUD="1"
            ;;
    esac

    YAD_MH_SET=$(<"${PW_TMPFS_PATH}/tmp_yad_mh_set")
    YAD_MH_FPS_LIMIT=$(<"${PW_TMPFS_PATH}/tmp_yad_mh_fps_limit")

    MONITOR_HEIGHT="$(echo $PW_SCREEN_RESOLUTION | awk -F'x' '{print $2}')"
    if [[ ! -z "$MONITOR_HEIGHT" ]]
    then MH_FONT_SIZE="font_size=$(( MONITOR_HEIGHT / 45 ))"
    fi

    local INT_COUNT_MH=0
    for read_list_mh in ${YAD_MH_SET} ; do
        if [[ "$read_list_mh" == "TRUE" ]]
        then local GUI_MH_RESULT+="$(echo "${LIST_MH[$INT_COUNT_MH]}," | sed 's/ /_/g' | tr [[:upper:]] [[:lower:]] )"
        fi
        ((INT_COUNT_MH++))
    done

    local INT_COUNT_MH_FPS=0
    for read_list_mh_fps in ${YAD_MH_FPS_LIMIT} ; do
        if [[ "$read_list_mh_fps" == "TRUE" ]]
        then local GUI_MH_FPS_RESULT+="${GET_REFRESH_RATE[$INT_COUNT_MH_FPS]}+"
        fi
        ((INT_COUNT_MH_FPS++))
    done

    GUI_MH_FPS_RESULT="${GUI_MH_FPS_RESULT}"
    if [ "$(echo "${GUI_MH_FPS_RESULT}" | awk -F'+' '{print $2}')" ] ; then
        export FPS_LIMIT="${GUI_MH_FPS_RESULT%+}"
        if [[ ! "${GUI_MH_RESULT}" =~ "show_fps_limit" ]] ; then
            GUI_MH_RESULT="${GUI_MH_RESULT}show_fps_limit,"
        fi
    else
        export FPS_LIMIT="${GUI_MH_FPS_RESULT%+}"
    fi

    if [[ ! -z "$MH_FONT_SIZE" ]]
    then GUI_MH_RESULT="${GUI_MH_RESULT}${MH_FONT_SIZE}"
    else GUI_MH_RESULT="${GUI_MH_RESULT%,}"
    fi

    if [[ ! -z "$FPS_LIMIT" ]] ; then
        GUI_MH_RESULT="${GUI_MH_RESULT},fps_limit=$FPS_LIMIT"
    elif [[ "$FPS_LIMIT" == "disabled" ]] ; then
        GUI_MH_RESULT="${GUI_MH_RESULT},fps_limit=0"
    fi

    if [[ "${GUI_MH_RESULT}" =~ "fps_only" ]] ; then
        local GUI_MH_RESULT="fps_only"
        print_info "In the MangoHud config fps_only is detected all settings are reseted"
    fi

    export MANGOHUD_CONFIG="${GUI_MH_RESULT}"
    export PW_MANGOHUD FPS_LIMIT
    edit_db_from_gui MANGOHUD_CONFIG PW_MANGOHUD FPS_LIMIT

    if [[  "${YAD_MANGOHUD_STATUS}" == "184" ]] ; then
        PW_MANGOHUD="1"
        pw_init_runtime
        pw_mangohud_check
        ${pw_runtime} \
        LD_PRELOAD="${PW_LD_PRELOAD}" \
        VK_LAYER_PATH="${PW_VK_LAYER_PATH}" \
        VK_INSTANCE_LAYERS="${PW_VK_INSTANCE_LAYERS}" \
        "${PW_PLUGINS_PATH}/portable/bin/vkcube"
        gui_mangohud
    fi

    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

# GUI DGVOODOO2
gui_dgvoodoo2 () {
    KEY_DGV2_GUI=$RANDOM
    PW_DGV2_LIST="PW_DGV2_USE_DX12 PW_DGV2_DISABLE_MIPMAPPING PW_DGV2_FREE_MOUSE PW_DGV2_ENABLE_CRT PW_DGV2_DISABLE_D3D PW_DGV2_FILTER_POINT_SAMPLED
    PW_DGV2_BLIT_STRETCH PW_DGV2_FORCE_VSYNC PW_DGV2_FASTMEMORY PW_DGV2_PHONG_SHADING PW_DGV2_DGVOODOO_WATERMARK PW_DGV2_GLIDE_NAPALM PW_DGV2_ONBOARD_RAM
    PW_DGV2_GLIDE_GAMMA_RAMP PW_DGV2_EMULATING_PCI_ACCESS PW_DGV2_16_BIT_DEPTH_BUFFER PW_DGV2_3DFX_WATERMARK PW_DGV2_3DFX_SPLASH_SCREEN
    PW_DGV2_INACTIVE_APP_STATE
    "

if [[ "${SKIP_DGVOODOO2_TEXT_INFO}" != "1" ]] ; then
    export PW_DGV2_USE_DX12_INFO=$(gettext 'Use DirectX12 for dgVoodoo2. Doesnt always work better. (Working only on newest and stable dxvk/vkd3d) (Direct3D and Glide settings)')
    export PW_DGV2_FORCE_VSYNC_INFO=$(gettext 'Force the vertical sync to avoid tearing or prevent the GPU from rendering at crazy high speed. Keep in mind that some application need unforced vSync because of synchronization reasons. (Direct3D settings)')
    export PW_DGV2_DGVOODOO_WATERMARK_INFO=$(gettext 'Shows the dgVoodoo watermark in-game when enabled. (Direct3D settings)')
    export PW_DGV2_ENABLE_CRT_INFO=$(gettext 'CRT-like blurred appearance. (Direct3D and Glide settings)')
    export PW_DGV2_DISABLE_MIPMAPPING_INFO=$(gettext 'Disabling mipmapping can be used to make textured surfaces sharper. (Direct3D and Glide settings)')
    export PW_DGV2_FREE_MOUSE_INFO=$(gettext 'If enabled then physical mouse is free to move inside the game window when using emulated scaling and/or application and forced resolution differs. Can be useful when a game relies and the physical window size. (Direct3D and Glide settings)')
    export PW_DGV2_DISABLE_D3D_INFO=$(gettext 'Do not use d3d8 and d3d9 dgVoodoo2 libraries. For some old games, using the example of Space Rangers, a bug with a black screen is corrected. (Direct3D settings)')
    export PW_DGV2_FILTER_POINT_SAMPLED_INFO=$(gettext 'if enabled then forced filtering affects only non-point sampled textures. (Direct3D settings)')
    export PW_DGV2_FASTMEMORY_INFO=$(gettext 'Enable fast memory access. (Enable if games have poor performance, using Unreal 2 as an example) (Direct3D settings)')
    export PW_DGV2_PHONG_SHADING_INFO=$(gettext 'If this option is enabled then Phong shading is applied in place of Gouraud when it is possible. It works only when an application uses the fixed function vertex/pixel pipline and pushes all the work of the vertex transforming and lighting to Direct3D when rendering primitives. Keep in mind that it requires much more GPU power than default shading. (Direct3D settings)')
    export PW_DGV2_BLIT_STRETCH_INFO=$(gettext 'If this options enabled then linear filtering is applied for stretched copying between 2D surfaces (DirectDraw only). Early hardware did not support or apply point sampled blitting which can end up very pixelated results. Linear filtering is much nicer in general but can cause artifacts especially with colorkeyed blitting. (Direct3D settings)')
    export PW_DGV2_GLIDE_NAPALM_INFO=$(gettext 'Use 3dfx glide napalm, uses antialiasing provided by the application. (Disables antialiasing setting) (Glide settings)')
    export PW_DGV2_GLIDE_GAMMA_RAMP_INFO=$(gettext 'Enable gamma ramp coming for Glide. (Glide settings)')
    export PW_DGV2_ONBOARD_RAM_INFO=$(gettext 'Increases memory for videocard Voodoo Graphics, Voodoo Rush, Voodoo 2, Voodoo Banshee. You can set the game resolution higher. (Glide settings)')
    export PW_DGV2_EMULATING_PCI_ACCESS_INFO=$(gettext 'When enabled, the virtual 3Dfx card will have PCI bus speeds emulated as closely as possible. Emulation of true PCI access should theoretically never be disabled but there can be possible degradation of performance when left enabled. Only some games require accurate emulation of the PCI bus, so in most cases PCI bus emulation should be left disabled. (Glide settings)')
    export PW_DGV2_16_BIT_DEPTH_BUFFER_INFO=$(gettext 'Enabling 16-bit depth buffers can prevent Z-fighting in games, but also has the disadvantage of possibly causing artifacting. (Glide settings)')
    export PW_DGV2_3DFX_WATERMARK_INFO=$(gettext 'Shows the 3Dfx watermark in-game when enabled. (Glide settings)')
    export PW_DGV2_3DFX_SPLASH_SCREEN_INFO=$(gettext 'The 3Dfx splash screen is seen when an application starts, disabling this option prevents this animation from playing. (Glide settings)')
    export PW_DGV2_INACTIVE_APP_STATE_INFO=$(gettext 'Enabling inactive state prevents the application from detecting losing the application focus. Most of the Glide applications close or deactivate themself when losing focus, so this can be used to let the application run in the background. (Glide settings)')

    export SKIP_DGVOODOO2_TEXT_INFO="1"
fi

    # fix
    [[ "${PW_DGV2_FILTERING}" == "0" ]] && export PW_DGV2_FILTERING="disabled" && edit_db_from_gui PW_DGV2_FILTERING
    [[ "${PW_DGV2_ANTIALIASING}" == "0" ]] && export PW_DGV2_ANTIALIASING="disabled" && edit_db_from_gui PW_DGV2_ANTIALIASING

    case "${PW_VULKAN_USE}" in
        0|3|4|5|6)
            DISABLE_DGV2_LIST="PW_DGV2_USE_DX12
            "
            ;;
    esac

    unset ADD_CHK_BOX_DGV2
    for int_to_boole in ${PW_DGV2_LIST} ; do
        if [ "${!int_to_boole}" == "1" ]
        then export ${int_to_boole}="TRUE"
        else export ${int_to_boole}="FALSE"
        fi
        TMP_HELP_FOR_GUI="${int_to_boole}_INFO"
        int_to_boole_non_pw="$(echo ${int_to_boole//PW_DGV2/} | sed 's/_/ /g' )"
        if [[ ! "${PW_VULKAN_USE}" == "1" ]] && [[ ! "${PW_VULKAN_USE}" == "2" ]] \
        && grep -wo "${int_to_boole}" <<<"${DISABLE_DGV2_LIST}" &>/dev/null
        then ADD_CHK_BOX_DGV2+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:D${THEME_CHKBOX}%${!int_to_boole}%"
        else ADD_CHK_BOX_DGV2+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:${THEME_CHKBOX}%${!int_to_boole}%"
        fi
    done

    IFS="%"
    "${pw_yad}" --plug=$KEY_DGV2_GUI --tabnum="1" --form --separator=" " --columns=4 ${ADD_CHK_BOX_DGV2} --text-align=center \
    --text="$(gettext "dgVoodoo2 settings\n<b>NOTE:</b> To display help for each item, simply hover over the text")" \
    --gui-type-text=${DGVOODOO2_GUI_TYPE_TEXT_UP} --gui-type-layout=${DGVOODOO2_GUI_TYPE_LAYOUT_UP}  \
    1> "${PW_TMPFS_PATH}/tmp_yad_dgv2_set" 2>/dev/null &
    IFS="$orig_IFS"

    DGVOODOO2_XRANDR="$(grep -e '[0-9]x' "${PW_TMPFS_PATH}/xrandr.tmp" | awk '{print $1}' | sort -rVu | grep "^[0-9]" | tr '\n' ' ')"
    for drop_low in ${DGVOODOO2_XRANDR} ; do
        if (( "${drop_low//x/}" > "1000000" )) ; then
            DGVOODOO2_XRANDR_NEW+="${drop_low}!"
        fi
    done

    DGVOODOO2_FILTERING="Point sampled!Bilinear!Linear mip!Trilinear!Anisotropic 2x!Anisotropic 4x!Anisotropic 8x!Anisotropic 16x"
    DGVOODOO2_RESAMPLING="Point sampled!Bilinear!Lanczos-2!Bicubic!Lanczos-3"
    DGVOODOO2_VIDEOCARD="dgVoodoo2 SVGA 3D!dgVoodoo2 Virtual 3D!Geforce 4 Ti 4800!ATI Radeon 8500!Matrox Parhelia-512!GeForce FX 5700 Ultra!GeForce 9800 GT!Voodoo Graphics!Voodoo Rush!Voodoo 2!Voodoo Banshee"

    "${pw_yad}" --plug=$KEY_DGV2_GUI --tabnum="2" --form --columns=3 --separator="%" \
    --gui-type-layout=${DGVOODOO2_GUI_TYPE_LAYOUT_DOWN} \
    --field="${CHKBOX_SPACE}RESOLUTION!$(gettext "Set the resolution used. (Direct3D and Glide settings)") :CBE" "$(combobox_fix --disabled "${PW_DGV2_RESOLUTION}" "4x app resolution!3x app resolution!2x app resolution!${DGVOODOO2_XRANDR_NEW::-1}")" \
    --field="${CHKBOX_SPACE}FPS LIMIT!$(gettext "You can configure fps limit. (Direct3D and Glide settings)") :CBE" "$(combobox_fix --disabled "${PW_DGV2_FPS_LIMIT}" "30!40!45!48!60!75!90!120!144!165!175!240")" \
    --field="${CHKBOX_SPACE}FILTERING!$(gettext "You can select from various texture filtering modes here. Forcing other than the app default can result in glitches or break some rendering effects. (Glide have max filtering bilinear) (Direct3D and Glide settings)") :CB" "$(combobox_fix --disabled "${PW_DGV2_FILTERING}" "${DGVOODOO2_FILTERING}")" \
    --field="${CHKBOX_SPACE}ANTIALIASING!$(gettext "Forcing antialiasing. This can cause artifacts. (Direct3D and Glide settings)") :CB" "$(combobox_fix --disabled "${PW_DGV2_ANTIALIASING}" "2x!4x!8x")" \
    --field="${CHKBOX_SPACE}BIT DEPTH!$(gettext "You can define what screen bit depth should be reported through dgVoodoo. (For game MOTO need 16 bit depth) (Direct3D and Glide settings)") :CB" "$(combobox_fix --disabled "${PW_DGV2_BIT_DEPTH}" "8!16!32")" \
    --field="${CHKBOX_SPACE}VIDEOCARD!$(gettext "You can select a virtual video card. (For game Quake 2 need Voodoo Graphics or Voodoo Rush card) (Direct3D and Glide settings)") :CB" "$(combobox_fix "${PW_DGV2_VIDEOCARD}" "${DGVOODOO2_VIDEOCARD}")" \
    --field="${CHKBOX_SPACE}VRAM!$(gettext "Increase the amount of vram for dgVoodoo2. (Direct3D settings)
- dgVoodoo2 SVGA 3D: 16 - 128
- dgVoodoo2 Virtual 3D: 16 - 4096
- Geforce 4 Ti 4800: 64 - 256
- ATI Radeon 8500: 64 - 256
- Matrox Parhelia-512: 128 - 256
- GeForce FX 5700 Ultra: 64 - 256
- GeForce 9800 GT: 512 - 1024") :CB" "$(combobox_fix "${PW_DGV2_VRAM}" "16!32!64!128!256!512!1024!2048!4096")" \
    --field="${CHKBOX_SPACE}BRIGHTNESS!$(gettext "Brightness can be finetuned here. (Direct3D and Glide settings)") :NUM" "${PW_DGV2_BRIGHTNESS}:!0..400" \
    --field="${CHKBOX_SPACE}COLOR!$(gettext "Color intensity. Finetued it to make colors or less vital compared to the application default, or even Black and White, according to your taste. (Direct3D and Glide settings)") :NUM" "${PW_DGV2_COLOR}:!0..400" \
    --field="${CHKBOX_SPACE}CONTRAST!$(gettext "Contrast is the intensity difference between dark and bright regions. Only static contrast is implemented and you can finetune it here. (Direct3D and Glide settings)") :NUM" "${PW_DGV2_CONTRAST}:!0..400" \
    --field="${CHKBOX_SPACE}DISPLAY ROI!$(gettext "Useful for applications rendering into a widescreen subrectangle inside a 4:3 resolution - the widescreen subrectangle can be defined as display ROI, the input of the scaling process. (Direct3D and Glide settings)") :CBE" "$(combobox_fix --disabled "${PW_DGV2_DISPLAY_ROI}" "3_2!4_3!5_4!16_9!16_10!21_9")" \
    --field="${CHKBOX_SPACE}RESAMPLING!$(gettext "If scaling is done by the dgVoodoo than you can choose which resampling method to use. The more complex filter the more computationally expensive. Available ones in order of complexty. (Direct3D and Glide settings)
- Point sampled (unblurred pixels)
- Bilinear (smoothed)
- Lanczos-2 (smoothed but sharper)
- Bicubic (smoothed but sharper)
- Lanczos-3 (smoothed, sharpest)") :CB" "$(combobox_fix --disabled "${PW_DGV2_RESAMPLING}" "${DGVOODOO2_RESAMPLING}")" \
    --field="${CHKBOX_SPACE}CURSOR SCALE!$(gettext "Integer scale factor of the emulated hardware mouse cursor. 0: calculated from the application and forced resolution. (Direct3D and Glide settings)") :NUM" "${PW_DGV2_CURSOR_SCALE}:!0..5" \
    1> "${PW_TMPFS_PATH}/tmp_yad_dgv2_set_cb" 2>/dev/null &

    "${pw_yad}" --paned --key=$KEY_DGV2_GUI --height="350" --title="dgVoodoo2" \
    --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --gui-type="settings-paned" \
    --button="$(gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(gettext "RESET") DGVOODOO2"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Restore default settings for") dgVoodoo2":162 \
    --button="$(gettext "DISABLE") DGVOODOO2"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Disable dgVoodoo2 and go to the previous menu")":164 \
    --button="$(gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Save the current changes, and go to the previous menu")":166 \
    2>/dev/null
    YAD_DGV2_STATUS="$?"
    export SKIP_CHECK_UPDATES="1"

    case "${YAD_DGV2_STATUS}" in
        1|252)
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
            ;;
        162)
            sed -i '/PW_DGV2/d' "$portwine_exe".ppdb
            sed -i '/DGVOODOO2/d' "$portwine_exe".ppdb
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
            ;;
        164)
            export PW_DGVOODOO2="0"
            ;;
        166)
            export PW_DGVOODOO2="1"
            ;;
    esac

    output_yad_dgv2=($(<"${PW_TMPFS_PATH}/tmp_yad_dgv2_set"))
    bool_from_yad=0
    for boole_to_int in ${PW_DGV2_LIST} ; do
        export ${boole_to_int}=${output_yad_dgv2[$bool_from_yad]}
        if [ "${!boole_to_int}" == "TRUE" ]
        then export ${boole_to_int}="1"
        else export ${boole_to_int}="0"
        fi
        export bool_from_yad=$(( ${bool_from_yad} + 1 ))
    done

    PW_ADD_SETTINGS_DGV2=$(head -n 1 "${PW_TMPFS_PATH}/tmp_yad_dgv2_set_cb")

    PW_DGV2_RESOLUTION="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $1}')"
    PW_DGV2_FPS_LIMIT="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $2}')"
    PW_DGV2_FILTERING="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $3}')"
    PW_DGV2_ANTIALIASING="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $4}')"
    PW_DGV2_BIT_DEPTH="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $5}')"
    PW_DGV2_VIDEOCARD="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $6}')"
    PW_DGV2_VRAM="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $7}')"
    PW_DGV2_BRIGHTNESS="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $8}')"
    PW_DGV2_COLOR="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $9}')"
    PW_DGV2_CONTRAST="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $10}')"
    PW_DGV2_DISPLAY_ROI="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $11}')"
    PW_DGV2_RESAMPLING="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $12}')"
    PW_DGV2_CURSOR_SCALE="$(echo ${PW_ADD_SETTINGS_DGV2} | awk -F"%" '{print $13}')"

    edit_db_from_gui ${PW_DGV2_LIST} PW_DGVOODOO2 PW_DGV2_FILTERING PW_DGV2_ANTIALIASING PW_DGV2_VRAM PW_DGV2_RESOLUTION \
    PW_DGV2_FPS_LIMIT PW_DGV2_BIT_DEPTH PW_DGV2_BRIGHTNESS PW_DGV2_COLOR PW_DGV2_CONTRAST PW_DGV2_VIDEOCARD PW_DGV2_DISPLAY_ROI \
    PW_DGV2_CURSOR_SCALE PW_DGV2_RESAMPLING

    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

# GUI GAMESCOPE
gui_gamescope () {
    KEY_GS_GUI=$RANDOM
    PW_GS_LIST="PW_GS_FULLSCREEN PW_GS_FORCE_FULLSCREEN PW_GS_BORDERLESS_WINDOW PW_GS_FORCE_GRAB_CURSOR
    PW_GS_FORCE_GRAB_KEYBOARD PW_GS_HDR_ENABLE PW_GS_ENABLE_GAMESCOPE_WSI PW_GS_HDR_ITM_ENABLE PW_GS_SDR_GAMMUT_WIDENESS
    PW_GS_FORCE_COMPOSITION PW_GS_HDR_FORCE_SUPPORT PW_GS_HDR_FORCE_OUTPUT PW_GS_HDR_FORCE_HEATMAP
    PW_GS_EXPOSE_WAYLAND PW_GS_REALTIME_SCHEDULING PW_GS_FIX_AMD_AND_INTEL
    "

    grep -e '--mangoapp' "${PW_TMPFS_PATH}/gamescope.tmp" &>/dev/null && add_to_var PW_GS_LIST "PW_GS_MANGOAPP"

    if check_wayland_session ; then
        grep -e '--backend' "${PW_TMPFS_PATH}/gamescope.tmp" &>/dev/null \
        && add_to_var PW_GS_LIST "PW_GS_BACKEND_SDL" && add_to_var PW_GS_LIST "PW_GS_SDL_VIDEODRIVER_X11"
    else
        export PW_GS_BACKEND_SDL="0"
        export PW_GS_SDL_VIDEODRIVER_X11="0"
    fi

    GS_FILTER_CB="linear!nearest!fsr!nis!pixel"
    #debian bookworm fix
    if grep -e '-U, --fsr-upscaling' "${PW_TMPFS_PATH}/gamescope.tmp" &>/dev/null ; then
        GS_FILTER_CB="fsr!nis"
        export PW_GS_FILTER_MODE_OLD="true"
        rm_from_var PW_GS_LIST "PW_GS_HDR_ENABLE"
        rm_from_var PW_GS_LIST "PW_GS_HDR_FORCE_SUPPORT"
        rm_from_var PW_GS_LIST "PW_GS_HDR_FORCE_OUTPUT"
        rm_from_var PW_GS_LIST "PW_GS_HDR_FORCE_SUPPORT"
        rm_from_var PW_GS_LIST "PW_GS_FORCE_GRAB_CURSOR"
        rm_from_var PW_GS_LIST "PW_GS_FORCE_GRAB_KEYBOARD"
    fi

if [[ "${SKIP_GAMESCOPE_TEXT_INFO}" != "1" ]] ; then
    export PW_GS_FULLSCREEN_INFO=$(gettext 'Make the window fullscreen')
    export PW_GS_FORCE_FULLSCREEN_INFO=$(gettext 'Force windows inside of gamescope to be the size of the nested display (fullscreen)')
    export PW_GS_BORDERLESS_WINDOW_INFO=$(gettext 'Make the window borderless. Working only with backend sdl or X11 session.')
    export PW_GS_FORCE_GRAB_KEYBOARD_INFO=$(gettext 'Grab the keyboard')
    export PW_GS_FORCE_GRAB_CURSOR_INFO=$(gettext 'Always use relative mouse mode instead of flipping dependent on cursor visibility.')
    export PW_GS_HDR_ENABLE_INFO=$(gettext 'Enable HDR output (needs Gamescope WSI layer enabled for support from clients). If this is not set, and there is a HDR client, it will be tonemapped SDR'.)
    export PW_GS_ENABLE_GAMESCOPE_WSI_INFO=$(gettext 'This can be useful for some HDR options and for some DXVK v2.3 vsync optimisations')
    export PW_GS_SDR_GAMMUT_WIDENESS_INFO=$(gettext "Set the 'wideness' of the gamut for SDR comment. 0 - 1.")
    export PW_GS_HDR_ITM_ENABLE_INFO=$(gettext 'Enable SDR->HDR inverse tone mapping. only works for SDR input.')
    export PW_GS_FORCE_COMPOSITION_INFO=$(gettext 'Disables direct scan-out')
    export PW_GS_HDR_FORCE_SUPPORT_INFO=$(gettext 'Forces support for HDR and associated opptions even if the current display does not support it')
    export PW_GS_HDR_FORCE_OUTPUT_INFO=$(gettext "Forces support and output to HDR10 PQ even if the output does not support it (will look very wrong if it doesn't)")
    export PW_GS_HDR_FORCE_HEATMAP_INFO=$(gettext 'Displays a heatmap-style debug view of HDR luminence across the scene in nits.')
    export PW_GS_EXPOSE_WAYLAND_INFO=$(gettext 'Expose Wayland clients using xdg-shell')
    export PW_GS_REALTIME_SCHEDULING_INFO=$(gettext 'Enable GameScope realtime scheduling')
    export PW_GS_MANGOAPP_INFO=$(gettext 'Launch with the mangoapp (mangohud) performance overlay enabled. You should use this instead of using mangohud on the game or gamescope.')
    export PW_GS_BACKEND_SDL_INFO=$(gettext 'Use sdl backend. (by default wayland backend is used).')
    export PW_GS_SDL_VIDEODRIVER_X11_INFO=$(gettext 'Force use sdl videodriver x11, works with BACKEND SDL. (Default is wayland)')
    export PW_GS_FIX_AMD_AND_INTEL_INFO=$(gettext 'Fixes artifacts on AMD and Intel video cards. (Use only when necessary).')

    export SKIP_GAMESCOPE_TEXT_INFO="1"
fi

    unset ADD_CHK_BOX_GS
    if [[ "${GAMESCOPE_INSTALLED}" == 1 ]] ; then
        GAMESCOPE_NEED_INSTALL="$(gettext "Change settings gamescope for") <b>${PORTWINE_DB}</b>\n $(gettext "<b>NOTE:</b> To display help for each item, simply hover your mouse over the text")"
        GS_CB="CB" && GS_CBE="CBE" && GS_NUM="NUM" && GS_NUMN="NUMN"
        for int_to_boole in ${PW_GS_LIST} ; do
            if [ "${!int_to_boole}" == "1" ]
            then export ${int_to_boole}="TRUE"
            else export ${int_to_boole}="FALSE"
            fi
            TMP_HELP_FOR_GUI="${int_to_boole}_INFO"
            int_to_boole_non_pw="$(echo ${int_to_boole//PW_GS/} | sed 's/_/ /g' )"
            ADD_CHK_BOX_GS+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:${THEME_CHKBOX}%${!int_to_boole}%"
        done
    else
        if check_flatpak ; then
            GAMESCOPE_NEED_INSTALL="$(gettext "<b>Gamescope is not detected on the system, to install it, run the command in the terminal:\n flatpak install -y runtime/org.freedesktop.Platform.VulkanLayer.gamescope/x86_64/23.08\n (Can be selected and copied)</b>")"
            YAD_TEXT_SELECTION="--selectable-labels"
        else
            GAMESCOPE_NEED_INSTALL="$(gettext "<b>Gamescope is not detected on the system, please contact the manufacturer of your distribution\n or search the Internet for information on how to install gamescope on your system.</b>")"
        fi
        GS_CB="DCB" && GS_CBE="DCBE" && GS_NUM="DNUM" && GS_NUMN="DNUMN"
        PW_GS_SHOW_RESOLUTION="disabled"
        PW_GS_INTERNAL_RESOLUTION="0.0"
        PW_GS_FRAME_LIMIT="disabled"
        PW_GS_MESA_VK_WSI_PRESENT_MODE="disabled"
        PW_GS_SCALER_MODE="disabled"
        PW_GS_FILTER_MODE="disabled"
        PW_GS_UPSCALE_SHARPNESS="0"
        PW_GS_MAX_SCALE_FACTOR="0.0"
        PW_GS_MOUSE_SENSITIVITY="0.0"
        PW_GS_SDR_CONTENT_NITS="0"
        PW_GS_ITM_SDR_NITS="0"
        PW_GS_ITM_TARGET_NITS="0"
        for int_to_boole in ${PW_GS_LIST} ; do
            if [ "${!int_to_boole}" == "1" ]
            then export ${int_to_boole}="TRUE"
            else export ${int_to_boole}="FALSE"
            fi
            TMP_HELP_FOR_GUI="${int_to_boole}_INFO"
            int_to_boole_non_pw="$(echo ${int_to_boole//PW_GS/} | sed 's/_/ /g' )"
            ADD_CHK_BOX_GS+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:D${THEME_CHKBOX}%${!int_to_boole}%"
        done
    fi

    IFS="%"
    "${pw_yad}" --plug=$KEY_GS_GUI --tabnum="1" --form --columns=4 --separator=" " --text-align=center --text "$GAMESCOPE_NEED_INSTALL" \
    --gui-type-text=${GAMESCOPE_GUI_TYPE_TEXT_UP} --gui-type-layout=${GAMESCOPE_GUI_TYPE_LAYOUT_UP} $YAD_TEXT_SELECTION \
    ${ADD_CHK_BOX_GS} \
    1> "${PW_TMPFS_PATH}/tmp_yad_gs_set" 2>/dev/null &
    IFS="$orig_IFS"

    GAMESCOPE_XRANDR="$(grep -e '[0-9]x' "${PW_TMPFS_PATH}/xrandr.tmp" | awk '{print $1}' | sort -rVu | grep "^[0-9]" | tr '\n' ' ')"
    for drop_low in ${GAMESCOPE_XRANDR} ; do
        if (( "${drop_low//x/}" > "1000000" )) ; then
            GAMESCOPE_XRANDR_NEW+="${drop_low}!"
        fi
    done

    "${pw_yad}" --plug=$KEY_GS_GUI --tabnum="2" --form --columns=2 --separator="%" --homogeneous-row \
    --gui-type-text=${GAMESCOPE_GUI_TYPE_TEXT_DOWN} --gui-type-layout=${GAMESCOPE_GUI_TYPE_LAYOUT_DOWN} \
    --field="${CHKBOX_SPACE}$(gettext "Show resolution")!$(gettext "Set the resolution used by gamescope. Resizing the gamescope window will update these settings. Defaults to 1280x720.") :${GS_CBE}" "$(combobox_fix --disabled "${PW_GS_SHOW_RESOLUTION}" "${GAMESCOPE_XRANDR_NEW::-1}")" \
    --field="${CHKBOX_SPACE}$(gettext "Internal resolution")!$(gettext "Scale internal resolution (0.0 is unused)") :${GS_NUMN}" "${PW_GS_INTERNAL_RESOLUTION}:!0..2" \
    --field="${CHKBOX_SPACE}$(gettext "Frame limit")!$(gettext "Set a framerate limit. Specified in frames per second") :${GS_CBE}" "$(combobox_fix --disabled "${PW_GS_FRAME_LIMIT}" "30!40!45!48!60!75!90!120!144!165!175!240")" \
    --field="${CHKBOX_SPACE}$(gettext "Mesa vulkan WSI")!$(gettext "Overrides the WSI present mode:
fifo - First in, first out. Limits the frame rate + no tearing. (VSync)
immediate - Unlimited frame rate + tearing.
mailbox - Triple buffering. Unlimited frame rate + no tearing.
relaxed - Same as fifo but allows tearing when below the monitors refresh rate.") :${GS_CB}" "$(combobox_fix --disabled "${PW_GS_MESA_VK_WSI_PRESENT_MODE}" "fifo!immediate!mailbox!relaxed")" \
    --field="${CHKBOX_SPACE}$(gettext "Scaler mode")!$(gettext "Specify how to scale the GameScope window content") :${GS_CB}" "$(combobox_fix --disabled "${PW_GS_SCALER_MODE}" "auto!integer!fit!fill!stretch")" \
    --field="${CHKBOX_SPACE}$(gettext "Filter mode")!$(gettext "Use a filter such as AMD FidelityFX SuperResolution 1.0 (FSR) or NVIDIA Image Scaling v1.0.3 (NIS)") :${GS_CB}" "$(combobox_fix --disabled "${PW_GS_FILTER_MODE}" "${GS_FILTER_CB}")" \
    --field="${CHKBOX_SPACE}$(gettext "Upscale sharpness")!$(gettext "Sharpening value for FidelityFX SuperResolution 1.0 (FSR) and NVIDIA Image Scaling v1.0.3 (NIS). 0 is max sharpening, 20 is min sharpening.") :${GS_NUM}" "${PW_GS_UPSCALE_SHARPNESS}:!0..20" \
    --field="${CHKBOX_SPACE}$(gettext "Max scale factor")!$(gettext "Maximum Scale Factor. Working only on X11 or backend sdl (0.0 is unused)") :${GS_NUMN}" "${PW_GS_MAX_SCALE_FACTOR}:!0..100" \
    --field="${CHKBOX_SPACE}$(gettext "Mouse sensitivity")!$(gettext "Configurable mouse sensitivity, multiply mouse movement by specified decimal number amount") :${GS_NUMN}" "${PW_GS_MOUSE_SENSITIVITY}:!0..100" \
    --field="${CHKBOX_SPACE}$(gettext "HDR SDR nits")!$(gettext "Set the luminance of SDR content in nits. Default: 400 nits.") :${GS_NUM}" "${PW_GS_SDR_CONTENT_NITS}:!0..10000" \
    --field="${CHKBOX_SPACE}$(gettext "HDR inverse SDR")!$(gettext "Set the luminance of SDR content in nets used as the input for the inverse tone mapping process - Maximum is 1000 nits") :${GS_NUM}" "${PW_GS_ITM_SDR_NITS}:!0..1000" \
    --field="${CHKBOX_SPACE}$(gettext "HDR inverse target")!$(gettext "Set the target luninance of the inverse tone mapping process - Max is 10000 nits") :${GS_NUM}" "${PW_GS_ITM_TARGET_NITS}:!0..10000" \
    1> "${PW_TMPFS_PATH}/tmp_yad_gs_set_cb" 2>/dev/null &

    "${pw_yad}" --paned --key="$KEY_GS_GUI" --title="GameScope" \
    --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --gui-type="settings-paned" \
    --button="$(gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(gettext "RESET") GAMESCOPE"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Restore default settings for") GameScope":162 \
    --button="$(gettext "DISABLE") GAMESCOPE"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Disable GameScope and go to the previous menu")":164 \
    --button="$(gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Save the current changes, and go to the previous menu")":166 \
    2>/dev/null
    YAD_GAMESCOPE_STATUS="$?"
    export SKIP_CHECK_UPDATES="1"

    case "${YAD_GAMESCOPE_STATUS}" in
        1|252)
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
            ;;
        162)
            sed -i '/PW_GS/d' "$portwine_exe".ppdb
            sed -i '/GAMESCOPE/d' "$portwine_exe".ppdb
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
            ;;
        164)
            export PW_GAMESCOPE="0"
            ;;
        166)
            export PW_GAMESCOPE="1"
            ;;
    esac

    output_yad_gs=($(<"${PW_TMPFS_PATH}/tmp_yad_gs_set"))
    bool_from_yad=0
    for boole_to_int in ${PW_GS_LIST} ; do
        export ${boole_to_int}=${output_yad_gs[$bool_from_yad]}
        if [ "${!boole_to_int}" == "TRUE" ]
        then export ${boole_to_int}="1"
        else export ${boole_to_int}="0"
        fi
        export bool_from_yad=$(( ${bool_from_yad} + 1 ))
    done

    PW_ADD_SETTINGS_GS=$(head -n 1 "${PW_TMPFS_PATH}/tmp_yad_gs_set_cb")

    PW_GS_SHOW_RESOLUTION="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $1}')"
    PW_GS_INTERNAL_RESOLUTION="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $2}' | tr ',' '.')"
    PW_GS_FRAME_LIMIT="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $3}')"
    PW_GS_MESA_VK_WSI_PRESENT_MODE="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $4}')"
    PW_GS_SCALER_MODE="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $5}')"
    PW_GS_FILTER_MODE="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $6}')"
    PW_GS_UPSCALE_SHARPNESS="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $7}')"
    PW_GS_MAX_SCALE_FACTOR="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $8}' | tr ',' '.')"
    PW_GS_MOUSE_SENSITIVITY="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $9}' | tr ',' '.')"
    PW_GS_SDR_CONTENT_NITS="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $10}')"
    PW_GS_ITM_SDR_NITS="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $11}')"
    PW_GS_ITM_TARGET_NITS="$(echo ${PW_ADD_SETTINGS_GS} | awk -F"%" '{print $12}')"

    edit_db_from_gui ${PW_GS_LIST} PW_GAMESCOPE PW_GS_SHOW_RESOLUTION PW_GS_INTERNAL_RESOLUTION \
    PW_GS_FRAME_LIMIT PW_GS_MESA_VK_WSI_PRESENT_MODE PW_GS_SCALER_MODE  PW_GS_FILTER_MODE \
    PW_GS_UPSCALE_SHARPNESS PW_GS_MAX_SCALE_FACTOR PW_GS_MOUSE_SENSITIVITY \
    PW_GS_SDR_CONTENT_NITS PW_GS_ITM_SDR_NITS PW_GS_ITM_TARGET_NITS

    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

# RESIZE PNG FILE
resize_png () {
    if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] ; then
        print_error "no argument specified for resize_png"
        return 1
    else
        local RESIZE_FILE="$1"
        local RESIZE_NAME_PNG="${2// /_}"
        local RESIZE_TO="$3"
    fi
    
    [[ -f "${PORT_WINE_PATH}/data/img/${RESIZE_NAME_PNG}.png" ]] && return 0

    try_remove_file "${PORT_WINE_PATH}/data/img/launcher.png"
    try_remove_file "${PORT_WINE_PATH}/data/img/Launcher.png"

    if check_flatpak ; then
        if ! timeout 3 \
        exe-thumbnailer --force-resize -s "$RESIZE_TO" "$(readlink -f "${RESIZE_FILE}")" "${PORT_WINE_PATH}/data/img/${RESIZE_NAME_PNG}.png" \
        && [[ "$ALPINE_FP" != "1" ]]
        then
            print_error "exe-thumbnailer - broken!"
            if [[ ! -z $PW_DEBUG ]] ; then
                debug_timer --start
                timeout 5 exe-thumbnailer --force-resize -s "$RESIZE_TO" "$(readlink -f "${RESIZE_FILE}")" "${PORT_WINE_PATH}/data/img/${RESIZE_NAME_PNG}.png"
                debug_timer --end "exe-thumbnailer"
            fi
        fi
    else
        print_warning "use portable exe-thumbnailer"
        env PYTHONPATH="${PW_PLUGINS_PATH}/portable/lib/python3.9/site-packages/" \
        LD_LIBRARY_PATH="${PW_PLUGINS_PATH}/portable/lib/lib64" \
        "${PW_WINELIB}/runtime/files/bin/python3.9"  \
        "${PW_PLUGINS_PATH}/portable/bin/exe-thumbnailer" --force-resize -s "$RESIZE_TO" "$(readlink -f "${RESIZE_FILE}")" "${PORT_WINE_PATH}/data/img/${RESIZE_NAME_PNG}.png"
    fi
    return 0
}

# GUI CREATE SHORTCUT
portwine_create_shortcut () {
    export GDK_BACKEND="x11"
    pw_stop_progress_bar
    [[ ! -e ${portwine_exe} ]] && return 1
    unset PORTPROTON_NAME name_desktop_png
    pw_create_gui_png

    [[ -z "${PW_SHORTCUT_MENU}" ]] && PW_SHORTCUT_MENU="TRUE"
    [[ -z "${PW_SHORTCUT_DESKTOP}" ]] && PW_SHORTCUT_DESKTOP="TRUE"
    [[ -z "${PW_SHORTCUT_STEAM}" ]] && PW_SHORTCUT_STEAM="FALSE"

    if [[ -z "${PORTPROTON_NAME}" ]] ; then
        export name_desktop="$(basename "$portwine_exe")"
    else
        export name_desktop="${PORTPROTON_NAME}"
    fi

    [[ -z "${name_desktop_png}" ]] && name_desktop_png="${PORTPROTON_NAME// /_}"

    if [[ "$1" == "block_name" ]] ; then
        OUTPUT=$("${pw_yad}" --title="$(gettext "Choices")" --form \
        --gui-type="settings-shortcut" \
        --gui-type-box=${SETTINGS_SHORTCUT_GUI_TYPE_BOX} --gui-type-layout=${SETTINGS_SHORTCUT_GUI_TYPE_LAYOUT} \
        --gui-type-text=${SETTINGS_SHORTCUT_GUI_TYPE_TEXT} --gui-type-images=${SETTINGS_SHORTCUT_GUI_TYPE_IMAGE} \
        --window-icon "$PW_GUI_ICON_PATH/portproton.svg" \
        --image "${PORT_WINE_PATH}/data/img/${name_desktop_png}.png" \
        --text-align="center" --text "$(gettext "The shortcut will be created in the PortProton directory.")" \
        --field="  ${name_desktop}":LBL "" \
        --field="  $(gettext "Add shortcut to MENU -> GAMES")":CHK "$PW_SHORTCUT_MENU" \
        --field="  $(gettext "Add shortcut to Desktop")":CHK "$PW_SHORTCUT_DESKTOP" \
        --field="  $(gettext "Add shortcut to STEAM library")":CHK "$PW_SHORTCUT_STEAM" \
        --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":2 \
        --button="$(gettext "CREATE SHORTCUT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null)
        PW_YAD_OUT=$?
    else
        OUTPUT=$("${pw_yad}" --title="$(gettext "Choices")" --form \
        --gui-type="settings-shortcut" \
        --gui-type-box=${SETTINGS_SHORTCUT_GUI_TYPE_BOX} --gui-type-layout=${SETTINGS_SHORTCUT_GUI_TYPE_LAYOUT} \
        --gui-type-text=${SETTINGS_SHORTCUT_GUI_TYPE_TEXT} --gui-type-images=${SETTINGS_SHORTCUT_GUI_TYPE_IMAGE} \
        --window-icon "$PW_GUI_ICON_PATH/portproton.svg" \
        --image "${PORT_WINE_PATH}/data/img/${name_desktop_png}.png" \
        --text-align="center" --text "$(gettext "The shortcut will be created in the PortProton directory.")" \
        --field="$(gettext "Name")" "${name_desktop}" \
        --field="  $(gettext "Add shortcut to MENU -> GAMES")":CHK "$PW_SHORTCUT_MENU" \
        --field="  $(gettext "Add shortcut to Desktop")":CHK "$PW_SHORTCUT_DESKTOP" \
        --field="  $(gettext "Add shortcut to STEAM library")":CHK "$PW_SHORTCUT_STEAM" \
        --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":2 \
        --button="$(gettext "CREATE SHORTCUT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null)
        PW_YAD_OUT=$?
    fi

    if [[ "$PW_YAD_OUT" == "0" ]] ; then
        if [[ "$1" == "block_name" ]]
        then name_desktop="$PORTWINE_CREATE_SHORTCUT_NAME"
        else name_desktop=$(echo "$OUTPUT" | awk -F'|' '{print $1}')
        fi
        name_desktop="$(echo "${name_desktop}" | sed "s/\`//g" | sed "s/\"//g" | sed "s/'//g" | sed "s/\!//g")"
        PW_SHORTCUT_MENU=$(echo "$OUTPUT" | awk -F'|' '{print $2}')
        PW_SHORTCUT_DESKTOP=$(echo "$OUTPUT" | awk -F'|' '{print $3}')
        PW_SHORTCUT_STEAM=$(echo "$OUTPUT" | awk -F'|' '{print $4}')

        edit_user_conf_from_gui PW_SHORTCUT_MENU PW_SHORTCUT_DESKTOP PW_SHORTCUT_STEAM

        try_remove_file "${PORT_WINE_PATH}/${name_desktop}.desktop"

        echo "[Desktop Entry]" > "${PORT_WINE_PATH}/${name_desktop}.desktop"
        echo "Name=${name_desktop}" >> "${PORT_WINE_PATH}/${name_desktop}.desktop"
        if check_flatpak
        then echo "Exec=flatpak run ru.linux_gaming.PortProton \"${portwine_exe}\"" >> "${PORT_WINE_PATH}/${name_desktop}.desktop"
        else echo "Exec=env \"${PORT_SCRIPTS_PATH}/start.sh\" \"${portwine_exe}\"" >> "${PORT_WINE_PATH}/${name_desktop}.desktop"
        fi
        {
            echo "Type=Application"
            echo "Categories=Game"
            echo "StartupNotify=true"
            echo "Path=${PORT_SCRIPTS_PATH}/"
            echo "Icon=${PORT_WINE_PATH}/data/img/${name_desktop_png}.png"
        } >> "${PORT_WINE_PATH}/${name_desktop}.desktop"
        chmod u+x "${PORT_WINE_PATH}/${name_desktop}.desktop"

        if [[ "${PW_SHORTCUT_MENU}" == "TRUE" ]] ; then
            try_remove_file "${HOME}/.local/share/applications/${name_desktop}.desktop"
            cp -f "${PORT_WINE_PATH}/${name_desktop}.desktop" "${HOME}/.local/share/applications/"
        fi

        if [[ "${PW_SHORTCUT_DESKTOP}" == "TRUE" ]] ; then
            if [[ -d "${HOME}/Desktop" ]] ; then
                cp -f "${PORT_WINE_PATH}/${name_desktop}.desktop" "${HOME}/Desktop/"
            elif [[ -d "${HOME}/Рабочий стол" ]] ; then
                cp -f "${PORT_WINE_PATH}/${name_desktop}.desktop" "${HOME}/Рабочий стол/"
            elif [[ $(xdg-user-dir DESKTOP) ]] ; then
                cp -f "${PORT_WINE_PATH}/${name_desktop}.desktop" "$(xdg-user-dir DESKTOP)"
            fi
        fi

        if [[ "${PW_SHORTCUT_STEAM}" == "TRUE" ]] ; then
            export STEAM_SCRIPTS="${PORT_WINE_PATH}/steam_scripts"
            create_new_dir "${STEAM_SCRIPTS}"
            echo "#!/usr/bin/env bash" > "${STEAM_SCRIPTS}/${name_desktop}.sh"
            echo "export START_FROM_STEAM=1" >> "${STEAM_SCRIPTS}/${name_desktop}.sh"
            echo "export LD_PRELOAD=" >> "${STEAM_SCRIPTS}/${name_desktop}.sh"
            if check_flatpak
            then echo "flatpak run ru.linux_gaming.PortProton \"${portwine_exe}\" " >> "${STEAM_SCRIPTS}/${name_desktop}.sh"
            else echo "\"${PORT_SCRIPTS_PATH}/start.sh\" \"${portwine_exe}\" " >> "${STEAM_SCRIPTS}/${name_desktop}.sh"
            fi
            chmod u+x "${STEAM_SCRIPTS}/${name_desktop}.sh"
            export SCVDF="shortcuts.vdf"
            for STUIDPATH in "${HOME}"/.local/share/Steam/userdata/*/ ; do
                create_new_dir "${STUIDPATH}/config/"
                create_new_dir "${STUIDPATH}/config/grid"
                export SCPATH="${STUIDPATH}/config/$SCVDF"
                export SGGRIDDIR="${STUIDPATH}/config/grid"
                "${PORT_SCRIPTS_PATH}/add_in_steam.sh"
            done
            if [[ "${PW_SKIP_RESTART_STEAM}" != 1 ]] && pgrep -i steam &>/dev/null ; then
                if yad_question "$(gettext "For adding shortcut to STEAM, needed restart.\n\nRestart STEAM now?")" ; then
                    pw_start_progress_bar_block "$(gettext "Restarting STEAM... Please wait.")"
                    kill -s SIGTERM $(pgrep -a steam) &>/dev/null
                    while pgrep -i steam &>/dev/null ; do
                        sleep 0.5
                    done
                    steam &
                    sleep 5
                    pw_stop_progress_bar
                    exit 0
                fi
            fi
            unset PW_SKIP_RESTART_STEAM
        fi

        if [[ "$PW_NO_RESTART_PPDB" != "1" ]] ; then
            print_info "Restarting PP..."
            unset PW_YAD_FORM_TAB
            export SKIP_CHECK_UPDATES="1"
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi
    fi
}

pw_auto_create_shortcut () {
    if [[ "${PW_CHECK_AUTOINSTALL}" == 1 ]] \
    || [[ -z "${portwine_exe}" ]] \
    || echo "${portwine_exe}" | grep -i "gc_api.exe\|vkplay\|pulseonline" &>/dev/null
    then
        print_warning "Skipping auto create shortcut"
        return 0
    fi

    unset LINKS
    for link_file in "${PORT_WINE_PATH}"/data/prefixes/*/drive_c/users/*/Desktop/*.lnk
    do
        if ! echo "$link_file" | grep "\*.lnk" &>/dev/null ; then
            link_file=$(readlink -f "${link_file}")
            LINKS+=("${link_file// /@_@}")
        else
            continue
        fi
    done
    [[ -z "$LINKS" ]] && return 0
    SORTED_LINKS=($(echo ${LINKS[@]} | tr ' ' '\n' | sort -u))

    for link_file in "${SORTED_LINKS[@]//@_@/ }" ; do
        if command -v exiftool &>/dev/null ; then
            if timeout 3 exiftool "$link_file" &> "${PW_TMPFS_PATH}/exiftool.tmp" ; then
                prefix_name=$(echo "$link_file" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
                if fix_icon_name=$(grep -i "Icon File Name" "${PW_TMPFS_PATH}/exiftool.tmp" 2>/dev/null) \
                && [[ "${fix_icon_name//*.exe/true}" == "true" ]] ; then
                    link_drive=$(sed -n 's/^Icon File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | cut -d: -f1 | awk '{print tolower($0)}')
                    link_path=$(sed -n 's/^Icon File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|%ProgramFiles(x86)%|C:\\Program Files (x86)|gi;s|%ProgramFiles%|C:\\Program Files|gi' | sed "s|^.|${PORT_WINE_PATH}/data/prefixes/$prefix_name/dosdevices/$link_drive|g" | sed 's/\\/\//g')
                else
                    link_drive=$(sed -n 's/^Local Base Path\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | cut -d: -f1 | awk '{print tolower($0)}')
                    link_path=$(sed -n 's/^Local Base Path\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|%ProgramFiles(x86)%|C:\\Program Files (x86)|gi;s|%ProgramFiles%|C:\\Program Files|gi' | sed "s|^.|${PORT_WINE_PATH}/data/prefixes/$prefix_name/dosdevices/$link_drive|g" | sed 's/\\/\//g')
                fi
                link_name=$(sed -n 's/^File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|\.lnk||')
                link_cmd=$(sed -n 's/^Command Line Arguments\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp")
            else
                print_error "exiftool - broken!"
                if [[ ! -z $PW_DEBUG ]] ; then
                    debug_timer --start
                    timeout 5 exiftool "$link_file"
                    debug_timer --end "exiftool"
                fi
            fi
        else
            print_warning "use portable exiftool"
            env PERL5LIB="${PW_PLUGINS_PATH}/portable/lib/perl5" "${PW_PLUGINS_PATH}/portable/bin/exiftool" "$link_file" &> "${PW_TMPFS_PATH}/exiftool.tmp"
            prefix_name=$(echo "$link_file" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
            if fix_icon_name=$(grep -i "Icon File Name" "${PW_TMPFS_PATH}/exiftool.tmp" 2>/dev/null) \
            && [[ "${fix_icon_name//*.exe/true}" == "true" ]] ; then
                link_drive=$(sed -n 's/^Icon File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | cut -d: -f1 | awk '{print tolower($0)}')
                link_path=$(sed -n 's/^Icon File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|%ProgramFiles(x86)%|C:\\Program Files (x86)|gi;s|%ProgramFiles%|C:\\Program Files|gi' | sed "s|^.|${PORT_WINE_PATH}/data/prefixes/$prefix_name/dosdevices/$link_drive|g" | sed 's/\\/\//g')
            else
                link_drive=$(sed -n 's/^Local Base Path\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | cut -d: -f1 | awk '{print tolower($0)}')
                link_path=$(sed -n 's/^Local Base Path\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|%ProgramFiles(x86)%|C:\\Program Files (x86)|gi;s|%ProgramFiles%|C:\\Program Files|gi' | sed "s|^.|${PORT_WINE_PATH}/data/prefixes/$prefix_name/dosdevices/$link_drive|g" | sed 's/\\/\//g')
            fi
            link_name=$(sed -n 's/^File Name\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp" | sed 's|\.lnk||')
            link_cmd=$(sed -n 's/^Command Line Arguments\s*:\s*//p' "${PW_TMPFS_PATH}/exiftool.tmp")
        fi
        
        try_remove_file "$link_file"

        if ! exe_path="$(realpath "${link_path}" 2>/dev/null)" ; then
            print_warning "Removed broken link for: $link_name"
        else
            print_info "Created link for: $link_name"
            PORTPROTON_NAME="$link_name"
            export portwine_exe="$exe_path"
            resize_png "$portwine_exe" "${PORTPROTON_NAME}" "128"
            export PW_NO_RESTART_PPDB=1
            portwine_create_shortcut
        fi
    done
}
export -f pw_auto_create_shortcut

portwine_delete_shortcut () {
    PW_DELETE_MENU="$(grep -il "${portwine_exe}" "${HOME}/.local/share/applications"/*.desktop 2>/dev/null)"
    PW_DELETE_SHORTCUT+=(${PW_DELETE_MENU// /@_@})

    PW_DELETE_PP="$(grep -il "${portwine_exe}" "${PORT_WINE_PATH}"/*.desktop 2>/dev/null)"
    PW_DELETE_SHORTCUT+=(${PW_DELETE_PP// /@_@})

    if [[ -d "${HOME}/Desktop" ]] ; then
        PW_DELETE_DESKTOP="$(grep -il "${portwine_exe}" "${HOME}/Desktop"/*.desktop 2>/dev/null)"
        PW_DELETE_SHORTCUT+=(${PW_DELETE_DESKTOP// /@_@})
    fi
    if [[ -d "${HOME}/Рабочий стол" ]] ; then
        PW_DELETE_DESKTOP="$(grep -il "${portwine_exe}" "${HOME}/Рабочий стол"/*.desktop 2>/dev/null)"
        PW_DELETE_SHORTCUT+=(${PW_DELETE_DESKTOP// /@_@})
    fi
    if [[ $(xdg-user-dir DESKTOP) ]] ; then
        PW_DELETE_DESKTOP="$(grep -il "${portwine_exe}" "$(xdg-user-dir DESKTOP)"/*.desktop 2>/dev/null)"
        PW_DELETE_SHORTCUT+=(${PW_DELETE_DESKTOP// /@_@})
    fi

    for delete_shortcut in ${PW_DELETE_SHORTCUT[@]} ; do
        rm -f "${delete_shortcut//@_@/ }"
    done
}

portwine_missing_shortcut () {
    "${pw_yad}" --title="$(gettext "Error")" --form \
    --window-icon "$PW_GUI_ICON_PATH/portproton.svg" --fixed \
    --image "$PW_GUI_ICON_PATH/error.svg" \
    --text "\n$(gettext "Could not find the file:")\n${portwine_exe}\n\n$(gettext "ATTENTION:\nIf you forgot to mount the disk with the running application, click CANCEL!")\n" \
    --button="$(gettext "DELETE SHORTCUT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 \
    --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1
    if [ $? -eq "0" ] ; then
        portwine_delete_shortcut
    fi
    exit 0
}

# GUI WINETRICKS | GUI PREFIX MANAGER
pw_prefix_manager () {
    update_winetricks
    start_portwine
    if [ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ] ; then
        touch "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
    fi

    pw_start_progress_bar_block "$(gettext "Starting prefix manager:") $PW_PREFIX_NAME"
    "${PORT_WINE_TMP_PATH}/winetricks" dlls list | awk -F'(' '{print $1}' 1> "${PW_TMPFS_PATH}/dll_list"
    "${PORT_WINE_TMP_PATH}/winetricks" fonts list | awk -F'(' '{print $1}' 1> "${PW_TMPFS_PATH}/fonts_list"
    "${PORT_WINE_TMP_PATH}/winetricks" settings list | awk -F'(' '{print $1}' 1> "${PW_TMPFS_PATH}/settings_list"

    gui_prefix_manager () {
        unset SET_FROM_PFX_MANAGER_TMP SET_FROM_PFX_MANAGER
        IFS=$'\n'
        try_remove_file  "${PW_TMPFS_PATH}/dll_list_tmp"
        while read PW_BOOL_IN_DLL_LIST ; do
            if [[ -z $(echo "${PW_BOOL_IN_DLL_LIST}" | grep -E 'd3d|directx9|dont_use|dxvk|vkd3d|galliumnine|faudio1|Foundation') ]] ; then
                if grep "^$(echo "${PW_BOOL_IN_DLL_LIST}" | awk '{print $1}')$" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ; then
                    echo -e "true\n$(echo "${PW_BOOL_IN_DLL_LIST}" | awk '{print $1}')\n$(echo ${PW_BOOL_IN_DLL_LIST} | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/dll_list_tmp"
                else
                    echo -e "false\n$(echo "${PW_BOOL_IN_DLL_LIST}" | awk '{print $1}')\n$(echo ${PW_BOOL_IN_DLL_LIST} | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/dll_list_tmp"
                fi
            fi
        done < "${PW_TMPFS_PATH}/dll_list"
        try_remove_file  "${PW_TMPFS_PATH}/fonts_list_tmp"
        while read PW_BOOL_IN_FONTS_LIST ; do
            if [[ -z $(echo "${PW_BOOL_IN_FONTS_LIST}" | grep -E 'dont_use') ]] ; then
                if grep "^$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')$" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ; then
                    echo -e "true\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/fonts_list_tmp"
                else
                    echo -e "false\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/fonts_list_tmp"
                fi
            fi
        done < "${PW_TMPFS_PATH}/fonts_list"
        try_remove_file  "${PW_TMPFS_PATH}/settings_list_tmp"
        while read PW_BOOL_IN_FONTS_LIST ; do
            if [[ -z $(echo "${PW_BOOL_IN_FONTS_LIST}" | grep -E 'vista|alldlls|autostart_|bad|good|win|videomemory|vd=|isolate_home') ]] ; then
                if grep "^$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')$" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" ; then
                    echo -e "true\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/settings_list_tmp"
                else
                    echo -e "false\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{print $1}')\n$(echo "${PW_BOOL_IN_FONTS_LIST}" | awk '{ $1 = ""; print substr($0, 2) }')" >> "${PW_TMPFS_PATH}/settings_list_tmp"
                fi
            fi
        done < "${PW_TMPFS_PATH}/settings_list"
        pw_stop_progress_bar

        KEY_EDIT_MANAGER_GUI=$RANDOM
        "${pw_yad}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=1 --list --checklist \
        --gui-type-layout=${WINETRICKS_GUI_TYPE_LAYOUT} --gui-type-text=${WINETRICKS_GUI_TYPE_TEXT} \
        --text="$(gettext 'Select components to install in prefix:') <b>\"${PW_PREFIX_NAME}\"</b>, $(gettext 'using wine:') <b>\"${PW_WINE_USE}\"</b>" \
        --column="$(gettext "Set")" --column="$(gettext "Libraries")" \
        --column="$(gettext "Information")" < "${PW_TMPFS_PATH}/dll_list_tmp" 1>> "${PW_TMPFS_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=2 --list --checklist \
        --gui-type-layout=${WINETRICKS_GUI_TYPE_LAYOUT} --gui-type-text=${WINETRICKS_GUI_TYPE_TEXT} \
        --text="$(gettext 'Select fonts to install in prefix:') <b>\"${PW_PREFIX_NAME}\"</b>, $(gettext 'using wine:') <b>\"${PW_WINE_USE}\"</b>" \
        --column="$(gettext "Set")" --column="$(gettext "Libraries")" \
        --column="$(gettext "Information")" < "${PW_TMPFS_PATH}/fonts_list_tmp" 1>> "${PW_TMPFS_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=3 --list --checklist \
        --gui-type-layout=${WINETRICKS_GUI_TYPE_LAYOUT} --gui-type-text=${WINETRICKS_GUI_TYPE_TEXT} \
        --text="$(gettext 'Change config for prefix:') <b>\"${PW_PREFIX_NAME}\"</b>" \
        --column="$(gettext "Set")" --column="$(gettext "Libraries")" \
        --column="$(gettext "Information")" < "${PW_TMPFS_PATH}/settings_list_tmp" 1>> "${PW_TMPFS_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad}" --key=$KEY_EDIT_MANAGER_GUI --notebook \
        --width=700 --height=700 --expand \
        --gui-type="settings-notebook" \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(gettext 'PREFIX MANAGER')" \
        --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
        --button="$(gettext "FORCE INSTALL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Installation with forced redownload of libraries")":2 \
        --button="$(gettext "INSTALL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(gettext "Standard installation.")":0 \
        --tab-pos="top" \
        --tab="$(gettext 'DLLS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="$(gettext 'FONTS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="$(gettext 'SETTINGS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" 2>/dev/null
        YAD_STATUS="$?"

        try_remove_file  "${PW_TMPFS_PATH}/dll_list_tmp"
        try_remove_file  "${PW_TMPFS_PATH}/fonts_list_tmp"
        try_remove_file  "${PW_TMPFS_PATH}/settings_list_tmp"

        case "$YAD_STATUS" in
            1|252)
                print_info "Nothing to do. Restarting PortProton..."
                stop_portwine &
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
            0)
                WT_FORCE=""
                ;;
            2)
                WT_FORCE="-r -f"
                ;;
        esac

        for STPFXMNG in $(<"${PW_TMPFS_PATH}/to_winetricks") ; do
            grep $(echo ${STPFXMNG} | awk -F'|' '{print $2}') "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" &>/dev/null
            if [ "$?" == "1" ] ; then
                [[ ! -z "${STPFXMNG}" ]] && SET_FROM_PFX_MANAGER+="$(echo "${STPFXMNG}" | awk -F'|' '{print $2}') "
            fi
        done
        IFS="$orig_IFS"
        try_remove_file "${PW_TMPFS_PATH}/to_winetricks"

        if [[ ! -z ${SET_FROM_PFX_MANAGER} ]] ; then
            pw_update_pfx_cover_gui "winetricks"
            echo "START WINETRICKS..." >> "${PW_TMPFS_PATH}/update_pfx_log"
            echo "Try to install DLL in prefix: ${SET_FROM_PFX_MANAGER}" >> "${PW_TMPFS_PATH}/update_pfx_log"
            print_info "Try to install DLL in prefix: ${SET_FROM_PFX_MANAGER}"
            ${pw_runtime} LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${PORT_WINE_TMP_PATH}/winetricks" -q ${WT_FORCE} ${SET_FROM_PFX_MANAGER} | tee -a "${PW_TMPFS_PATH}/update_pfx_log"
            wait_wineserver
            kill_portwine
            pw_stop_progress_bar
            gui_prefix_manager
        else
            print_info "Nothing to do. Restarting PortProton..."
            stop_portwine &
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi
    }
    gui_prefix_manager
}

portwine_start_debug () {
    kill_portwine
    export PW_LOG=1
    if [[ -z "$VULKAN_DRIVER_NAME" ]] || [[ "$VULKAN_DRIVER_NAME" == "llvmpipe" ]] ; then
    pw_notify_send -i warning \
    "$(gettext "Attention working version of vulkan not detected!")" \
    "$(gettext "It is recommended to run games in OpenGL (low performance possible)!")"
    fi
    echo "$(gettext "PortProton was launched in creation mode PortProton.log and it is successfully stored in the root directory of the port")" > "${PORT_WINE_PATH}/PortProton.log"
    echo "$(gettext "To diagnose the problem, copy ALL of the log to discord server: https://discord.gg/FTaheP99wE")" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "PortWINE version:" >> "${PORT_WINE_PATH}/PortProton.log"
    read install_ver < "${PORT_WINE_TMP_PATH}/PortProton_ver"
    echo "PortProton-${install_ver}" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Scripts version:" >> "${PORT_WINE_PATH}/PortProton.log"
    cat "${PORT_WINE_TMP_PATH}/scripts_ver" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-----------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if check_flatpak ; then
        echo "FLATPAK in used" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [ "${PW_USE_RUNTIME}" = 0 ] ; then
        echo "RUNTIME is disabled" >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "RUNTIME is enabled" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "----------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if [ ! -z "${portwine_exe}" ] ; then
        echo "Debug for programm:" >> "${PORT_WINE_PATH}/PortProton.log"
        echo "${portwine_exe}" >> "${PORT_WINE_PATH}/PortProton.log"
        echo "---------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "GLIBC version:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo $(ldd --version | grep -m1 ldd | awk '{print $NF}') >> "${PORT_WINE_PATH}/PortProton.log"
    echo "--------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    case "$PW_VULKAN_USE" in
        0) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DirectX to wined3d opengl" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        1) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DXVK v.${DXVK_STABLE_VER}, VKD3D-PROTON v.${VKD3D_STABLE_VER}" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        2) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DXVK v.${DXVK_GIT_VER}, VKD3D-PROTON v.${VKD3D_GIT_VER}" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        3) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - Native DX9 on MESA drivers" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        4) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - OpenGL on MESA vulkan drivers" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        5) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DXVK v.${DXVK_LEGACY_VER}" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        6) echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DirectX to wined3d vulkan" >> "${PORT_WINE_PATH}/PortProton.log" ;;
        *) echo "PW_VULKAN_USE=${PW_VULKAN_USE}" >> "${PORT_WINE_PATH}/PortProton.log" ;;
    esac
    echo "--------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Version WINE in use:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "$PW_WINE_USE" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Program bit depth:" >> "${PORT_WINE_PATH}/PortProton.log"
    if [[ $(file "$portwine_exe") =~ x86-64 ]]; then
        echo "64 bit" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-----------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "32 bit" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "--------------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Date and time of start debug for PortProton:" >> "${PORT_WINE_PATH}/PortProton.log"
    date >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-----------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "The installation path of the PortProton:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "$PORT_WINE_PATH" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if check_flatpak ; then
        echo "Flatpak runtime:" >> "${PORT_WINE_PATH}/PortProton.log"
        grep -oP 'PRETTY_NAME="\K[^"]+' /etc/os-release  >> "${PORT_WINE_PATH}/PortProton.log"
        echo "Operating system:" >> "${PORT_WINE_PATH}/PortProton.log"
        grep -oP 'PRETTY_NAME="\K[^"]+' /run/host/etc/os-release  >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "Operating system:" >> "${PORT_WINE_PATH}/PortProton.log"
        grep -oP 'PRETTY_NAME="\K[^"]+' /etc/os-release  >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "--------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Desktop environment:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Desktop session: ${DESKTOP_SESSION}" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Current desktop: ${XDG_CURRENT_DESKTOP}" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Session type: ${XDG_SESSION_TYPE}" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "---------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Kernel:" >> "${PORT_WINE_PATH}/PortProton.log"
    uname -r >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU physical cores:$(grep 'cpu cores' /proc/cpuinfo | sort -u | cut -d':' -f2)" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU logical cores: $(grep 'processor' /proc/cpuinfo | wc -l)" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU model name:$(grep 'model name' /proc/cpuinfo | sort -u | cut -d':' -f2)" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "RAM:" >> "${PORT_WINE_PATH}/PortProton.log"
    free -m >> "${PORT_WINE_PATH}/PortProton.log"
    echo "--------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Filesystem "${PATH_TO_GAME}" - $(stat -f -c %T "${PATH_TO_GAME}")" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Filesystem "${PORT_WINE_PATH}" - $(stat -f -c %T "${PORT_WINE_PATH}")" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Filesystem "${PW_TMPFS_PATH}" - $(stat -f -c %T "${PW_TMPFS_PATH}")" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "---------------------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Graphic cards and drivers:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo 'lspci -k | grep -EA3 VGA|3D|Display :' >> "${PORT_WINE_PATH}/PortProton.log"
    lspci -k | grep -EA3 'VGA|3D|Display' >> "${PORT_WINE_PATH}/PortProton.log"
    echo "----" >> "${PORT_WINE_PATH}/PortProton.log"
    command -v glxinfo &>/dev/null && glxinfo -B >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-----" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "inxi -G:" >> "${PORT_WINE_PATH}/PortProton.log"
    if ! check_flatpak ; then
        "${PW_PLUGINS_PATH}/portable/bin/inxi" -Gc0 >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    if echo "$LSPCI_VGA" | grep -i nvidia &>/dev/null ; then
        if command -v ldconfig &>/dev/null ; then
            echo "------" >> "${PORT_WINE_PATH}/PortProton.log"
            echo "ldconfig -p | grep libGLX_nvidia" >> "${PORT_WINE_PATH}/PortProton.log"
            ldconfig -p | grep libGLX_nvidia >> "${PORT_WINE_PATH}/PortProton.log"
        fi
    fi
    echo "PW_SCREEN_RESOLUTION=$PW_SCREEN_RESOLUTION" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "PW_SCREEN_PRIMARY=$PW_SCREEN_PRIMARY" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "----------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "Vulkan info device name:" >> "${PORT_WINE_PATH}/PortProton.log"
    grep -E '^GPU|deviceName|driverName' "${PW_TMPFS_PATH}/vulkaninfo.tmp" >> "${PORT_WINE_PATH}/PortProton.log"
    "${PW_PLUGINS_PATH}/portable/bin/vkcube" --c 50
    if [ $? -eq 0 ]; then
        echo "Vulkan cube test passed successfully" >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "Vulkan cube test completed with error" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "----------------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo 'locale:' >> "${PORT_WINE_PATH}/PortProton.log"
    locale >> "${PORT_WINE_PATH}/PortProton.log"
    echo "---" >> "${PORT_WINE_PATH}/PortProton.log"
    echo 'locale -a | grep -i "$(locale | grep -e '^LANG=' | sed 's/LANG=//' | sed  's/\-8//')" :' >> "${PORT_WINE_PATH}/PortProton.log"
    locale -a | grep -i "$(locale | grep -e '^LANG=' | sed 's/LANG=//' | sed  's/\-8//')" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if [[ "${PW_USE_D3D_EXTRAS}" != 1 ]]
    then echo "D3D_EXTRAS - disabled" >> "${PORT_WINE_PATH}/PortProton.log"
    else echo "D3D_EXTRAS - enabled" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "------------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "winetricks.log:" >> "${PORT_WINE_PATH}/PortProton.log"
    cat "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log" | sed -e /"^d3dcomp*"/d -e /"^d3dx*"/d >> "${PORT_WINE_PATH}/PortProton.log"
    echo "-----------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if [ ! -z "${PORTWINE_DB_FILE}" ]; then
        echo "Use ${PORTWINE_DB_FILE} db file:" >> "${PORT_WINE_PATH}/PortProton.log"
        cat "${PORTWINE_DB_FILE}" | sed '/##/d' >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "Use ${PORT_SCRIPTS_PATH}/portwine_db/default db file:" >> "${PORT_WINE_PATH}/PortProton.log"
        cat "${PORT_SCRIPTS_PATH}/portwine_db/default" | sed '/##/d' >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "----------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"
    if [ -f "${USER_CONF}" ]; then
        cat "${USER_CONF}" | sed '/bash/d' >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    echo "---------------------------------------" >> "${PORT_WINE_PATH}/PortProton.log"

    export DXVK_HUD="full"

    portwine_launch &
    sleep 3
    pw_stop_progress_bar_cover
    unset PW_TIMER
    while read -r line || [[ ! -z $(pgrep -a yad | grep "yad_gui_pp --text-info --tail --button="STOP"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 --title="DEBUG"" | awk '{print $1}') ]] ; do
            sleep 0.005
            if [[ ! -z "${line}" ]] && [[ -z "$(echo "${line}" | grep -i "kerberos")" ]] \
                                    && [[ -z "$(echo "${line}" | grep -i "ntlm")" ]]
            then
                echo "# ${line}"
            fi
            if [[ "${PW_TIMER}" != 1 ]] ; then
                sleep 3
                PW_TIMER=1
            fi
    done < "${PORT_WINE_PATH}/PortProton.log" | "${pw_yad}" --text-info --tail --button="STOP"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 --title="DEBUG" \
    --skip-taskbar --width=800 --height=400 --text "$(gettext "Please wait! After finishing the PortProton, click STOP.")" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" 2>/dev/null &&
    kill_portwine
    sed -i '/.fx$/d' "${PORT_WINE_PATH}/PortProton.log"
    sed -i '/HACK_does_openvr_work/d' "${PORT_WINE_PATH}/PortProton.log"
    sed -i '/Uploading is disabled/d' "${PORT_WINE_PATH}/PortProton.log"
    sed -i '/wine: RLIMIT_NICE is <= 20/d' "${PORT_WINE_PATH}/PortProton.log"
    sed -i '/ALT_2.24/d' "${PORT_WINE_PATH}/PortProton.log"
    sed -i '/UDEV monitor/d' "${PORT_WINE_PATH}/PortProton.log"
    deb_text=$(awk '! a[$0]++' "${PORT_WINE_PATH}/PortProton.log")
    echo "$deb_text" > "${PORT_WINE_PATH}/PortProton.log"
    "$pw_yad" --title="PortProton.log" --no-buttons --text-align=center \
    --text-info --show-uri --wrap --width=1200 --height=550  --uri-color=red \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --filename="${PORT_WINE_PATH}/PortProton.log"
    stop_portwine
}

pw_create_prefix_backup () {
    cd "$HOME"
    PW_PREFIX_TO_BACKUP=$("${pw_yad}" --file --directory --width=800 --height=500 \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(gettext "BACKUP PREFIX TO...")" \
    --button="$(gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 2>/dev/null)
    YAD_STATUS="$?"
    if [[ "$YAD_STATUS" == "1" || "$YAD_STATUS" == "252" ]] ; then exit 0 ; fi
    if [[ ! -z "$(grep "/${PW_PREFIX_NAME}/" "${PORT_WINE_PATH}"/*.desktop )" ]] ; then
        try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.create_shortcut"
        grep "/${PW_PREFIX_NAME}/" "${PORT_WINE_PATH}"/*.desktop | awk -F"/${PW_PREFIX_NAME}/" '{print $2}' \
        | awk -F\" '{print $1}' > "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.create_shortcut"
    fi
    unset PW_SANDBOX_HOME_PATH
    pw_init_runtime
    if check_flatpak
    then TMP_ALL_PATH=""
    else TMP_ALL_PATH="LD_LIBRARY_PATH=\"${PW_LD_LIBRARY_PATH}\""
    fi
    chmod -R u+w "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"
cat << EOF > "${PORT_WINE_TMP_PATH}"/pp_pfx_backup.sh
    #!/usr/bin/env bash
    ${TMP_ALL_PATH} mksquashfs "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}" \
    "${PW_PREFIX_TO_BACKUP}/${PW_PREFIX_NAME}.ppack.part" -comp zstd \
    || rm -f "${PW_PREFIX_TO_BACKUP}/${PW_PREFIX_NAME}.ppack.part"
EOF
    chmod u+x "${PORT_WINE_TMP_PATH}"/pp_pfx_backup.sh
    ${pw_runtime} ${PW_TERM} "${PORT_WINE_TMP_PATH}"/pp_pfx_backup.sh
    try_remove_file "${PORT_WINE_TMP_PATH}"/pp_pfx_backup.sh
    if [[ -f "${PW_PREFIX_TO_BACKUP}/${PW_PREFIX_NAME}.ppack.part" ]] ; then
        mv -f "${PW_PREFIX_TO_BACKUP}/${PW_PREFIX_NAME}.ppack.part" "${PW_PREFIX_TO_BACKUP}/${PW_PREFIX_NAME}.ppack"
        yad_info "$(gettext "Backup has been successfully created for prefix:") $PW_PREFIX_NAME"
        if [[ ! -f "${PORT_WINE_TMP_PATH}/pfx_backup_info" ]] ; then
            yad_info "$(gettext "Attention! Do not pass the resev copy to third parties, there is a risk of losing accounts!")"
            echo "1" > "${PORT_WINE_TMP_PATH}/pfx_backup_info"
        fi
    else
        yad_error "$(gettext "An error occurred while backing up the prefix:") $PW_PREFIX_NAME"
    fi
    return 0
}

pw_autoinstall_from_db () {
    export PW_USER_TEMP="${PORT_WINE_TMP_PATH}"
    export PW_FORCE_LARGE_ADDRESS_AWARE=1
    export PW_USE_GAMEMODE=0
    export PW_CHECK_AUTOINSTALL=1
    export PW_GUI_DISABLED_CS=1
    export PW_NO_WRITE_WATCH=0
    export PW_VULKAN_USE=1
    export PW_USE_EAC_AND_BE=0
    export PW_NO_FSYNC=1
    export PW_NO_ESYNC=1
    unset PORTWINE_CREATE_SHORTCUT_NAME
    export PW_DISABLED_CREATE_DB=1
    export PW_MANGOHUD=0
    export PW_VKBASALT=0
    export PW_USE_D3D_EXTRAS=1
    . "${PORT_SCRIPTS_PATH}/pw_autoinstall/${PW_YAD_SET}"
}

button_click () {
    [[ ! -z "$1" ]] && echo "$1" > "${PW_TMPFS_PATH}/tmp_yad_form"
    if [[ ! -z $(pidof -s yad) ]] || [[ ! -z $(pidof -s yad_gui_pp) ]] ; then
        kill -s SIGUSR1 $(pgrep -a yad | grep "\--key=${KEY} \--notebook" | awk '{print $1}') > /dev/null 2>&1
    fi
}
export -f button_click

button_click_start () {
    [[ ! -z "$1" ]] && echo "$1" > "${PW_TMPFS_PATH}/tmp_yad_form"
    if [[ ! -z $(pidof -s yad) ]] || [[ ! -z $(pidof -s yad_gui_pp) ]] ; then
        kill -s SIGUSR1 $(pgrep -a yad | grep "\--key=${KEY_START} \--notebook" | awk '{print $1}') > /dev/null 2>&1 \
        || kill -s SIGUSR1 $(pgrep -a yad | grep "\--key=${KEY_START} \--paned" | awk '{print $1}') > /dev/null 2>&1
    fi
}
export -f button_click_start

run_desktop_b_click () {
    [[ ! -z "$1" ]] && echo "$1" > "${PW_TMPFS_PATH}/tmp_yad_form"

    if [[ ! -z $(pidof -s yad) ]] || [[ ! -z $(pidof -s yad_gui_pp) ]] ; then
        kill -s SIGUSR1 $(pgrep -a yad | grep "\--key=${KEY} \--notebook" | awk '{print $1}') > /dev/null 2>&1
    fi
    if check_flatpak
    then PW_EXEC_FROM_DESKTOP="$(grep Exec "${PORT_WINE_PATH}/${PW_YAD_SET//@_@/ }" | head -n 1 | sed 's|flatpak run ru.linux_gaming.PortProton|\"${PORT_SCRIPTS_PATH}/start.sh\"|' | awk -F'=' '{print $2}')"
    else PW_EXEC_FROM_DESKTOP="$(grep Exec "${PORT_WINE_PATH}/${PW_YAD_SET//@_@/ }" | head -n 1 | awk -F"=env " '{print $2}')"
    fi

    print_info "Restarting PP after choose desktop file..."
    # stop_portwine
    export SKIP_CHECK_UPDATES="1"
    /usr/bin/env bash -c "${PW_EXEC_FROM_DESKTOP}" &
    exit 0
}
export -f run_desktop_b_click

gui_clear_pfx () {
    if yad_question "$(gettext "Do you want to clear prefix in PortProton?")" ; then
        pw_clear_pfx
        print_info "Restarting PP after clearing prefix..."
        export SKIP_CHECK_UPDATES="1"
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    fi
}
export -f gui_clear_pfx

gui_rm_portproton () {
    if yad_question "$(gettext "Do you really want to remove PortProton?")" ; then
        rm -fr "${PORT_WINE_PATH}"
        rm -fr "${PORT_WINE_TMP_PATH}"
        rm -fr "${HOME}/PortWINE"
        rm -f "$(grep -il PortProton "${HOME}/.local/share/applications"/*.desktop)"
        update-desktop-database -q "${HOME}/.local/share/applications"
    fi
    exit 0
}
export -f gui_rm_portproton

gui_pw_update () {
    try_remove_file "${PORT_WINE_TMP_PATH}/scripts_update_notifier"
    print_info "Restarting PP for check update..."
    export SKIP_CHECK_UPDATES="0"
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f gui_pw_update

change_loc () {
    try_remove_file "${PORT_WINE_TMP_PATH}/PortProton_loc"
    print_info "Restarting PP for change language..."
    export SKIP_CHECK_UPDATES="1"
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_loc

change_mirror () {
    if [[ "$MIRROR" == "CLOUD" ]] ; then
        sed -i 's/="CLOUD"/="GITHUB"/g' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Mirror changed to:")" "<b>https://github.com/Castro-Fidel</b>"
    else
        sed -i 's/="GITHUB"/="CLOUD"/g' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Mirror changed to:")" "<b>https://cloud.linux-gaming.ru</b>"
    fi
    print_info "Restarting PP for change mirror..."
    export SKIP_CHECK_UPDATES="1"
    if [[ -z "${PW_ALL_DF}" ]] ; then
        export TAB_MAIN_MENU="4"
    else
        export TAB_MAIN_MENU="5"
    fi
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_mirror

change_branch () {
    if [[ "$BRANCH" == "master" ]] ; then
        sed -i 's/="master"/="devel"/g' "$USER_CONF"
        export MIRROR="GITHUB"
        yad_info "$(gettext "Branch changed to:")  <b>DEVEL</b>"
    else
        sed -i 's/="devel"/="master"/g' "$USER_CONF"
        echo "2024" > "${PORT_WINE_TMP_PATH}/scripts_ver"
        export PW_CHANGE_BRANCH="1"
        yad_info "$(gettext "Branch changed to:")  <b>STABLE</b>"
    fi
    print_info "Restarting PP for change branch..."
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_branch

change_download_grid () {
    if [[ "$DOWNLOAD_STEAM_GRID" == "1" ]] ; then
        sed -i 's/DOWNLOAD_STEAM_GRID=.*/DOWNLOAD_STEAM_GRID="0"/' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Cover downloads are disabled")"
    else
        sed -i 's/DOWNLOAD_STEAM_GRID=.*/DOWNLOAD_STEAM_GRID="1"/' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Cover downloads are enabled")"
    fi
    print_info "Restarting PP for change cover download behavior..."
    export SKIP_CHECK_UPDATES="1"
    if [[ -z "${PW_ALL_DF}" ]] ; then
        export TAB_MAIN_MENU="4"
    else
        export TAB_MAIN_MENU="5"
    fi
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_download_grid

change_gui_start () {
    if [[ "$PW_GUI_START" == "NOTEBOOK" ]] ; then
        sed -i 's/="NOTEBOOK"/="PANED"/g' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Gui start changed to:")" "<b>PANED</b>"
    else
        sed -i 's/="PANED"/="NOTEBOOK"/g' "$USER_CONF"
        pw_notify_send -i info \
        "$(gettext "Gui start changed to:")" "<b>NOTEBOOK</b>"
    fi
    print_info "Restarting PP for change gui start..."
    export SKIP_CHECK_UPDATES="1"
    if [[ -z "${PW_ALL_DF}" ]] ; then
        export TAB_MAIN_MENU="4"
    else
        export TAB_MAIN_MENU="5"
    fi
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_gui_start

gui_wine_uninstaller () {
    start_portwine
    pw_run uninstaller
}
export -f gui_wine_uninstaller

gui_open_user_conf () {
    PW_USERCONF_GUI="$("${pw_yad}" --title="$(gettext "EDIT USER CONFIG")" \
    --text-info --editable --width=800 --height=600 \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --button="$(gettext "MAIN MENU")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
    --button="$(gettext "SAVE")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":2 \
    --filename="${PORT_WINE_PATH}/data/user.conf" 2>/dev/null)"
    YAD_STATUS="$?"

    case "${YAD_STATUS}" in
        1|252)
                export SKIP_CHECK_UPDATES="1"
                if [[ -z "${PW_ALL_DF}" ]] ; then
                    export TAB_MAIN_MENU="4"
                else
                    export TAB_MAIN_MENU="5"
                fi
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                print_info "Restarting..."
                exit 0
                ;;
            2)
                echo "${PW_USERCONF_GUI}" > "${PORT_WINE_PATH}/data/user.conf"
                export SKIP_CHECK_UPDATES="1"
                if [[ -z "${PW_ALL_DF}" ]] ; then
                    export TAB_MAIN_MENU="4"
                else
                    export TAB_MAIN_MENU="5"
                fi
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                print_info "Restarting..."
                exit 0
                ;;
    esac
}
export -f gui_open_user_conf

gui_edit_db_file () {
    GUI_EDIT_DB_FILE="$("${pw_yad}" --title="$(gettext "EDIT DB")" \
    --text-info --editable --width=800 --height=600 \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --button="$(gettext "SETTINGS")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
    --button="$(gettext "SAVE")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":2 \
    --filename="${PORTWINE_DB_FILE}" 2>/dev/null)"
    YAD_STATUS="$?"

    case "$YAD_STATUS" in
        1|252)
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
          2)
                echo "${GUI_EDIT_DB_FILE}" > "${PORTWINE_DB_FILE}"
                /usr/bin/env bash -c ${pw_full_command_line[*]} &
                exit 0
                ;;
    esac
}

gui_open_scripts_from_backup () {
    cd "${PORT_WINE_TMP_PATH}/scripts_backup/" || fatal
    PW_SCRIPT_FROM_BACKUP=$("${pw_yad}" --file --width=650 --height=500 \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "SCRIPTS FROM BACKUP" --file-filter="backup_scripts|scripts_v*.tar.gz" 2>/dev/null )
    YAD_STATUS="$?"
    if [[ "$YAD_STATUS" == "1" || "$YAD_STATUS" == "252" ]] ; then exit 0 ; fi
    unpack "$PW_SCRIPT_FROM_BACKUP" "${PORT_WINE_PATH}/data/"
    echo "0" > "${PORT_WINE_TMP_PATH}/scripts_update_notifier"
    print_info "Restarting PP after backup..."
    export SKIP_CHECK_UPDATES="1"
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f gui_open_scripts_from_backup

#GUI CREDITS AND ABOUT
gui_credits () {
    ########################################################################
    # grep ';subscription;' stat-subscriptions.csv | awk -F';' '{print $1}' | sort -u | sed "s/\"//g" > thanks
    ########################################################################
    KEY_CREDITS=$RANDOM
    DATE=$(date +'%Y')

    . "${PORT_WINE_PATH}/data/scripts/thanks"

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=1 --show-uri --scroll \
    --text-align=center --text="$(eval_gettext "PortProton v. \${install_ver}
    scripts v. \${scripts_install_ver}")" --text-info <<< "$(eval_gettext "https://linux-gaming.ru

Copyright © \$DATE Castro-Fidel (linux-gaming.ru)

This program comes with absolutely no warranty.
See the License for details.
")" 2>/dev/null &

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=2 --scroll \
    --text-info <<< "$(gettext "Author:   Casro-Fidel (Mikhail Tergoev)

Developer assistants and testers:")
$(credits_devel)
" 2>/dev/null &

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=3 --text-info --scroll <<< "$(eval_gettext "MIT License

Copyright © \$DATE Castro-Fidel (linux-gaming.ru)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the \"Software\"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED (AS IS), WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
")" 2>/dev/null &

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=4 \
    --text-info --show-uri --scroll \
    <<< "$(gettext "The project was supported:
https://boosty.to/portwine-linux.ru")
$(credits_boosty)

$(gettext "The list will be updated...
")" 2>/dev/null &

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=5 \
    --text="$(gettext "Do you want to join and help in the development of the project:")" \
    --image-path="$PW_GUI_ICON_PATH" --image="port_proton" \
    --uri-color=red --show-uri --text-info --scroll <<< "$(gettext "Website:  http://linux-gaming.ru
Discord:  http://discord.gg/yJSEFjF
VK:       https://vk.com/linuxgamingru
-------------------------------------------
Become a paid subscriber:
boosty:  https://boosty.to/portwine-linux.ru
Yandex wallet:   410012267513818
-------------------------------------------")" 2>/dev/null &

    "${pw_yad}" --plug="${KEY_CREDITS}" --tabnum=6 --show-uri --title="THIRD PARTY LIBRARIES" \
    --text-info <<< "$(gettext "                               Third-party libraries that are used in PortProton

            PortProton is built on the basis of the following free software libraries:

WINE-PROTON: https://github.com/ValveSoftware/Proton
WINE-PROTON-GE: https://github.com/GloriousEggroll/proton-ge-custom/
Kron4ek/Wine: https://github.com/Kron4ek/Wine-Builds
YAD: https://github.com/v1cont/yad
DGVOODOO2: https://dege.fw.hu/"
)" 2>/dev/null &

    "${pw_yad}" --title "$(gettext "ABOUT US")" --key="${KEY_CREDITS}" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --notebook --no-buttons  --tab-pos=bottom --expand --width=750 --height=400 \
    --tab="$(gettext "ABOUT PORTPROTON")" --tab="$(gettext "AUTORS")" --tab="$(gettext "LICENSE")" \
    --tab="$(gettext "SPONSORS")" --tab="$(gettext "JOIN")" --tab="$(gettext "THIRD PARTY LIBRARIES")"

    if [[ -z "${PW_ALL_DF}" ]] ; then
        export TAB_MAIN_MENU="4"
    else
        export TAB_MAIN_MENU="5"
    fi

    echo "Exit from credits"
    /usr/bin/env bash -c "${pw_full_command_line[*]}" &
    exit 0
}
