#!/usr/bin/env bash
# Author: Castro-Fidel (linux-gaming.ru)
# shellcheck disable=SC2140,SC2034
########################################################################

ping_test () {
    ping -w3 -c1 $@ &> /dev/null
    [ "$?" == "0" ] && return 0 || return 1
}

print_error () {
    printf "\E[31m%s Error: $@ %s\e[0m\n"
}
export -f print_error

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

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

change_locale () {
    if [[ -f "${PORT_WINE_TMP_PATH}/PortProton_loc" ]] ; then
        LANGUAGE=$(cat "${PORT_WINE_TMP_PATH}/PortProton_loc")
    fi

    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

    if [[ -z "${LANG}" ]] ; then
        export LANG=C
        LANGUAGE="en"
    else
        if [[ ! -z "${LANGUAGE}" ]] \
        && [[ -f "${PORT_WINE_TMP_PATH}/PortProton_loc" ]] ; then
            echo ""
        else
            [[ ! -f "${pw_yad_v13_0}" ]] && pw_yad_v13_0="yad"
            SET_LANG=$("${pw_yad_v13_0}" --title  "LANGUAGE" --text "Select the language:" --list --radiolist \
            --column="Set:" --column "Choose language:" TRUE "ru" FALSE "en" \
            --window-icon "$PW_GUI_ICON_PATH/portproton.svg" --width=300 --height=150)
            SET_LANG="$(echo "${SET_LANG}" | awk -F'|' '{print $2}')"
            echo "${SET_LANG}" > "${PORT_WINE_TMP_PATH}/PortProton_loc"
            if [[ ! -z "${SET_LANG}" ]]
            then LANGUAGE=${SET_LANG}
            else exit 1
            fi
        fi
    fi
    export LANGUAGE
}
export -f change_locale

generate_pot () {
    for lang in $LANGUAGES_LIST
    do
        echo "${lang}"
        mkdir -p "${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"
        xgettext --from-code=UTF-8 --language shell -i \
        "${PORT_WINE_PATH}/data/scripts/start.sh" \
        "${PORT_WINE_PATH}/data/scripts/setup.sh" \
        "${PORT_WINE_PATH}/data/scripts/functions_helper" \
        -o "$TEMPLATE_POT"
        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

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

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
    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_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

try_force_link_file () {
    if [ ! -f "$1" ] ; then :
    elif [ -z "$2" ] ; then	print_error "no way to link file $1"
    else
        ln -s -f -r "$1" "$2"
        [ "$?" != 0 ] && print_error "failed to link file $1 to $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_download () {
    local filename="$(basename "$1")"
    local dest="$2"
    local silent="false"
    local no_mirror="false"

    case "$3" in
           silent)
                silent=true ;;
        no_mirror)
                no_mirror=true
    esac

    if [[ "${MIRROR}" == CDN ]] \
    && [[ "$no_mirror" != "true" ]]
    then
        FIRST_URL="$url_cdn/$filename"
        SECOND_URL="$1"
    else
        FIRST_URL="$1"
        SECOND_URL="$url_cdn/$filename"
    fi

    [[ ! -f "${pw_yad_v13_0}" ]] && local pw_yad_v13_0="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..."
    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_v13_0" --progress --percentage=0 --text="$(eval_gettext "Downloading") $filename" --auto-close --no-escape \
        --auto-kill --center --text-align="center" --fixed --no-buttons --title "PortProton" --width=500 --height=90 \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --borders=15
    fi

    if [[ "${PIPESTATUS[0]}" != 0 ]] ; then
        if [[ "$no_mirror" != "true" ]] ; then
            print_warning "Failed download $filename from $FIRST_URL, trying mirror..."
            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_v13_0" --progress --percentage=0 --text="$(eval_gettext "Downloading") $filename" --auto-close --no-escape \
            --auto-kill --center --text-align="center" --fixed --no-buttons --title "PortProton" --width=500 --height=90 \
            --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --borders=15
        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"
            return 0
        fi
    fi
    print_ok "File downloaded successfully: $filename from $FIRST_URL"
    return 0
}

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

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

var_vkd3d_config_update () {
    if [ ! -z "$VKD3D_CONFIG" ]
    then export VKD3D_CONFIG="$1;$VKD3D_CONFIG"
    else export VKD3D_CONFIG="$1"
    fi
    return 0
}

var_radv_perftest_config_update () {
    if [ ! -z "$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 [ ! -z "$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 [[ ! -z "$LD_LIBRARY_PATH" ]]
    then export LD_LIBRARY_PATH="$1:$LD_LIBRARY_PATH"
    else export LD_LIBRARY_PATH="$1"
    fi
    return 0
}

pw_notify_send () {
    if command -v notify-send &>/dev/null ; then
        if [[ -f "$PW_GUI_ICON_PATH/portproton.svg" ]]
        then notify-send -a PortProton -i "$PW_GUI_ICON_PATH/portproton.svg" $@ "${PW_NOTIFY_TEXT}"
        else notify-send -a PortProton $@ "${PW_NOTIFY_TEXT}"
        fi
    fi
}

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_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
        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"
        $command "$1" -C "$2"
        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
}

pw_clear_pfx () {
    try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.dotnet48"
    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 "$(eval_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
    elif grep "optirun_on" "${USER_CONF}" &>/dev/null ; then
        sed -i '/optirun_on/d' "${USER_CONF}"
    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
        orig_IFS="$IFS"
        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"
                    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

            grep 'Global,"{41FCC608-8496-4DEF-B43E-7D9BD675A6FF}",0x10001,0x00000001' "${WINEDIR}/share/wine/wine.inf" &> /dev/null
            if [[ "$?" != "0" ]] ; 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"
            fi
        fi

        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

        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"

    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
        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/audio.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

    return 0
}

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

    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=""
        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"
    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 "${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} --"
    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} --"
    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
        export PW_MANGOHUD_SLR='MANGOHUD=1'
        MANGOHUD_LIB_NAME="libMangoHud.so"
        if [[ "${PW_VULKAN_USE}" = "0" ]] ; then
	        MANGOHUD_LIB_NAME="libMangoHud_dlsym.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
        var_pw_vk_istance_layers_config_update "VK_LAYER_MANGOHUD_overlay64:VK_LAYER_MANGOHUD_overlay32"
        print_info "Portable MANGOHUD is enabled"
    else
        export PW_MANGOHUD_SLR=""
        print_info "Portable MANGOHUD is disabled"
    fi
    return 0
}

pw_vkbasalt_check () {
    if [[ "${ENABLE_VKBASALT}" == 1 ]] && [[ "${PW_VULKAN_USE}" != "0" ]] && [[ "${PW_VULKAN_USE}" != "3" ]] ; then
        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
        var_pw_vk_istance_layers_config_update "VK_LAYER_VKBASALT_post_processing64:VK_LAYER_VKBASALT_post_processing32"
        print_info "vkBasalt is enabled"
    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 2>/dev/null | 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
        wine_pids="$(ls -l /proc/*/exe 2>/dev/null | grep -ie PortProton | grep -E 'wine(64)?-preloader|wineserver' | awk -F/ '{print $3}')"
        for pw_kill_pids in ${wine_pids} ; do
            if ps cax | grep "${pw_kill_pids}" ; then
                kill -n 9 "${pw_kill_pids}" &>/dev/null
            fi
        done

        bwrap_pids="$(pgrep -a wrap | grep PortProton | head -n 1 | awk '{print $1}')"
        for pw_kill_pids in ${bwrap_pids} ; do
            if ps cax | grep "${pw_kill_pids}" ; then
                kill -n 9 "${pw_kill_pids}" &>/dev/null
            fi
        done
    else
        wine_pids="$(ls -l /proc/*/exe 2>/dev/null | grep -E 'wine(64)?-preloader|wineserver' | awk -F/ '{print $3}')"
        for pw_kill_pids in ${wine_pids} ; do
            if ps cax | grep "${pw_kill_pids}" ; then
                kill -n 9 "${pw_kill_pids}" &>/dev/null
            fi
        done
    fi
    return 0
}
export -f kill_portwine

pw_kill_autostart () {
    if [[ -z "${2}" ]]
    then SWAIT=5
    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" ]] \
    && [[ "${XDG_SESSION_TYPE}" != "wayland" ]] \
    && command -v setxkbmap &>/dev/null
    then
        setxkbmap
    fi

    if [[ "${XDG_SESSION_TYPE}" != "wayland" ]] \
    && [[ "$(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 [[ ! -z "$(pgrep -a yad_v13_0 | grep "\--notification" | awk '{print $1}')" ]] ; then
        kill -s SIGUSR1 "$(pgrep -a yad_v13_0 | grep "\--notification" | awk '{print $1}')" 2>/dev/null
    fi
    add_in_stop_portwine
    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}" ; 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 D8VK
    if [ ! -d "${PW_VULKAN_DIR}/d8vk-${D8VK_VER}" ] ; then
        print_info "Download and install D8VK v.${D8VK_VER}"
        if try_download "https://github.com/Castro-Fidel/vulkan/releases/download/d8vk-${D8VK_VER}/d8vk-${D8VK_VER}.tar.xz" \
        "${PW_VULKAN_DIR}/d8vk-${D8VK_VER}.tar.xz"
        then
            if unpack "${PW_VULKAN_DIR}/d8vk-${D8VK_VER}.tar.xz" "${PW_VULKAN_DIR}" ; then
                try_remove_file "${PW_VULKAN_DIR}/d8vk-${D8VK_VER}.tar.xz"
            else
                try_remove_file "${PW_VULKAN_DIR}/d8vk-${D8VK_VER}.tar.xz"
                try_remove_dir "${PW_VULKAN_DIR}/d8vk-${D8VK_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

    #Download DGVOODOO2
    if [ ! -d "${PW_VULKAN_DIR}/dgvoodoo2-v${DGV2_VER}" ] ; then
        print_info "Download and install DGVOODOO2 v.${DGV2_VER}"
        if try_download "https://github.com/lutris/dgvoodoo2/releases/download/v${DGV2_VER}/dgvoodoo2-v${DGV2_VER}.tar.xz" \
        "${PW_VULKAN_DIR}/dgvoodoo2-v${DGV2_VER}.tar.xz"
        then
            if unpack "${PW_VULKAN_DIR}/dgvoodoo2-v${DGV2_VER}.tar.xz" "${PW_VULKAN_DIR}" ; then
                try_remove_file "${PW_VULKAN_DIR}/dgvoodoo2-v${DGV2_VER}.tar.xz"
            else
                try_remove_file "${PW_VULKAN_DIR}/dgvoodoo2-v${DGV2_VER}.tar.xz"
                try_remove_dir "${PW_VULKAN_DIR}/dgvoodoo2-v${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/bin/yad_new" ]] \
    || [[ ! -f "${PW_PLUGINS_PATH}/portable/bin/yad_v13_0" ]]
    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_TEXT="Please wait for update PortProton"
            pw_notify_send -u normal
            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
                    PW_NOTIFY_TEXT="Failed updating PortProton!"
                    pw_notify_send -u critical
                    print_error "Failed updating PortProton!"
                fi
            fi
        fi
    fi
    export pw_yad="${PW_PLUGINS_PATH}/portable/bin/yad"
    export pw_yad_v13_0="${PW_PLUGINS_PATH}/portable/bin/yad_v13_0"
    export pw_zstd="${PW_PLUGINS_PATH}/portable/bin/zstd"
    export pw_7z="${PW_PLUGINS_PATH}/portable/lib/p7zip/7z"
    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:"

        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/"
            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

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

        for drive_dir in "${MOUNT_DIRS[@]}" ; do
            if [[ ! "${DEF_MOUNT_DIRS[@]}" =~ "$drive_dir" ]] \
            && ! realpath "${WINEPREFIX}/dosdevices"/*: | grep "$drive_dir" &>/dev/null ; then
                drive_num=0
                while [[ "$drive_num" -lt "${#DRIVES[@]}" ]] ; do
                    drive_path="${WINEPREFIX}/dosdevices/${DRIVES[$drive_num]}:"
                    if [ ! -d "$drive_path" ] ; then
                        ln -sf "$drive_dir" "$drive_path"
                        break
                    else
                        ((drive_num++))
                    fi
                done
            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}" .exe)"
        export PORTWINE_DB
        if [[ -f "${portwine_exe}".ppdb ]] && [[ "${PORTWINE_DB}" != "RiotClientServices" ]] ; then
            PORTWINE_DB_FILE="${portwine_exe}".ppdb
            # shellcheck source=/dev/null
            source "${PORTWINE_DB_FILE}"
            if [[ -z "$(ls -p "${PORT_WINE_PATH}/prefixes/" | grep -e "^${PW_PREFIX_NAME}/")" ]] ; then
                PW_PREFIX_NAME=$(echo "${portwine_exe}" | awk -F"/prefixes/" '{print $2}' | awk -F"/" '{print $1}')
                edit_db_from_gui PW_PREFIX_NAME
            fi
        else
            orig_IFS="$IFS"
            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_v13_0}" --text-align=center \
                --text "$(eval_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}" ]] && [[ -z $(grep "^export PW_DISABLED_CREATE_DB=1" "${PW_FIND_DB_FILE}") ]] ; then
                try_copy_file "${PW_FIND_DB_FILE}" "${portwine_exe}".ppdb
                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
        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."
        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

        if grep "RUS" "${PORT_WINE_TMP_PATH}/PortProton_loc" &>/dev/null ; then
            for chk_lang_and_uname in "steam_emu.ini" "steam_api.ini" "steam_api64.ini" ; do
                if [[ -f "${PATH_TO_GAME}/${chk_lang_and_uname}" ]] ; then
                    sed -i "s/Language=english/Language=russian/" "${PATH_TO_GAME}/${chk_lang_and_uname}"
                    sed -i "/UserName=/c\UserName=${USER}" "${PATH_TO_GAME}/${chk_lang_and_uname}"
                fi
            done
        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
}

pw_port_update () {
    ###SCRIPTS UPDATE###
    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/master/data_from_portwine/scripts/var" > "${PORT_WINE_TMP_PATH}/curent_var_ver"
    then
        URL_FOR_CHANGELOG="https://gitlab.eterfund.ru/Castro-Fidel/PortWINE/raw/master/data_from_portwine"
        URL_TO_PW_MASTER="https://gitlab.eterfund.ru/Castro-Fidel/PortWINE/-/archive/master/PortWINE-master.tar.gz"
    else
        if curl -f -s --list-only --connect-timeout 3 "https://raw.githubusercontent.com/Castro-Fidel/PortWINE/master/data_from_portwine/scripts/var" > "${PORT_WINE_TMP_PATH}/curent_var_ver"
        then
            URL_FOR_CHANGELOG="https://raw.githubusercontent.com/Castro-Fidel/PortWINE/master/data_from_portwine"
            URL_TO_PW_MASTER="https://github.com/Castro-Fidel/PortWINE/archive/refs/heads/master.tar.gz"
        else
            print_error "Unable to determine the version on the server. Skip it..."
            return 1
        fi
    fi
    local PW_CHANGELOG_FILE="changelog_${LANGUAGE}"
    [[ ! -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 "SCRIPTS_NEXT_VERSION" "${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_PLUGINS_PATH}/portable/bin/yad_v13_0" ]]
            then export pw_yad_v13_0="${PW_PLUGINS_PATH}/portable/bin/yad_v13_0"
            elif command -v yad &>/dev/null
            then local pw_yad_v13_0="yad"
            else local pw_yad_v13_0="not_found"
            fi
            if [[ "${pw_yad_v13_0}" == "not_found" ]] ; then
                xcsd="$(eval_gettext "UPDATING NOW")"
            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_v13_0}" --title "$(eval_gettext "Update scripts:") v.${scripts_current_ver}" --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
                --form --separator='%%%' --width=1200 --height=600 \
                --field=":TXT" "${CHANGLOG_NEWS}" \
                --button="$(eval_gettext "EXIT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":252 \
                --button="$(eval_gettext "DO NOT REMIND ME")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":16 \
                --button="$(eval_gettext "REMIND ME LATER")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":18 \
                --button="$(eval_gettext "UPDATING NOW")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":20 2>/dev/null)
                YAD_STATUS="$?"
                case "${YAD_STATUS}" in
                    1|252) exit 0 ;;
                       16) xcsd="$(eval_gettext "DO NOT REMIND ME")" ;;
                       18) xcsd="$(eval_gettext "REMIND ME LATER")" ;;
                       20) xcsd="$(eval_gettext "UPDATING NOW")" ;;
                esac
            fi
            case $xcsd in
                "$(eval_gettext "UPDATING NOW")")
                    echo "######################################################"
                    print_info "Update scripts..."
                    try_remove_file "${PORT_WINE_TMP_PATH}/PortWINE-master.tar.gz"
                    if try_download "${URL_TO_PW_MASTER}" "${PORT_WINE_TMP_PATH}/PortWINE-master.tar.gz" no_mirror ; then
                        if unpack "${PORT_WINE_TMP_PATH}/PortWINE-master.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 \
                            ./changelog* ./dxvk.conf ./vkBasalt.conf ./tmp/scripts_ver
                            try_remove_dir "${PORT_WINE_PATH}/data/scripts/portwine_db"
                            cp -fr "${PORT_WINE_TMP_PATH}/PortWINE-master/data_from_portwine/"* "${PORT_WINE_PATH}/data/"
                            try_remove_file "${PORT_WINE_TMP_PATH}/PortWINE-master.tar.gz"
                            try_remove_dir "${PORT_WINE_TMP_PATH}/PortWINE-master/"
                            echo "${scripts_current_ver}" > "${PORT_WINE_TMP_PATH}/scripts_ver"
                            echo "Restarting PP after update..."
                            export SKIP_CHECK_UPDATES=1
                            /usr/bin/env bash -c ${pw_full_command_line[*]} &
                            exit 0
                        fi
                    else
                        yad_error_download && pw_port_update || exit 1
                    fi ;;
                "$(eval_gettext "REMIND ME LATER")")
                    echo " " ;;
                "$(eval_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 () {
    print_info "edit_db_from_gui PORTWINE_DB_FILE=$PORTWINE_DB_FILE"
    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
    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 () {
    if [ ! -z "${PORTWINE_CREATE_SHORTCUT_NAME}" ] ; then
        PORTPROTON_NAME="${PORTWINE_CREATE_SHORTCUT_NAME}"
    else
        PORTPROTON_NAME="$(basename "${portwine_exe}" | sed s/".exe"/""/gi )"
    fi
    export PORTPROTON_NAME
    PW_RESIZE_TO=128
    try_remove_file "${PORT_WINE_PATH}/data/img/setup.png"
    try_remove_file "${PORT_WINE_PATH}/data/img/Setup.png"
    try_remove_file "${PORT_WINE_PATH}/data/img/launcher.png"
    try_remove_file "${PORT_WINE_PATH}/data/img/Launcher.png"
    if [[ ! -f "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" ]] ; then
        try_remove_dir "${PORT_WINE_TMP_PATH}/tmp_img"
        create_new_dir "${PORT_WINE_TMP_PATH}/tmp_img"
        if command -v wrestool &>/dev/null ; then
            wrestool -x --output="${PORT_WINE_TMP_PATH}/tmp_img/" -t14 "$(readlink -f "${portwine_exe}")"
        else
            env LD_LIBRARY_PATH="${PW_PLUGINS_PATH}/portable/lib/lib64" "${PW_PLUGINS_PATH}/portable/bin/wrestool" \
            -x --output="${PORT_WINE_TMP_PATH}/tmp_img/" -t14 "$(readlink -f "${portwine_exe}")"
        fi
        cp "$(ls -S -1 "${PORT_WINE_TMP_PATH}/tmp_img/"*".ico" | head -n 1)" "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.ico"

        if command -v icotool &>/dev/null ; then
            icotool -x --width=${PW_RESIZE_TO} --height=${PW_RESIZE_TO} --output="${PORT_WINE_TMP_PATH}/tmp_img/" \
            "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.ico"
        else
            env LD_LIBRARY_PATH="${PW_PLUGINS_PATH}/portable/lib/lib64" "${PW_PLUGINS_PATH}/portable/bin/icotool" \
            -x --width=${PW_RESIZE_TO} --height=${PW_RESIZE_TO} --output="${PORT_WINE_TMP_PATH}/tmp_img/" \
            "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.ico"
        fi
        if [[ ! -f "$(ls -S -1 "${PORT_WINE_TMP_PATH}/tmp_img/"*".png" | grep "${PW_RESIZE_TO}x${PW_RESIZE_TO}")" ]] ; then
            if command -v icotool &>/dev/null ; then
                icotool -x --output="${PORT_WINE_TMP_PATH}/tmp_img/" "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.ico"
            else
                env LD_LIBRARY_PATH="${PW_PLUGINS_PATH}/portable/lib/lib64" "${PW_PLUGINS_PATH}/portable/bin/icotool" \
                -x --output="${PORT_WINE_TMP_PATH}/tmp_img/" "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.ico"
            fi
            cp "$(ls -S -1 "${PORT_WINE_TMP_PATH}/tmp_img/"*".png" | head -n 1)" "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.png"
        else
            cp "$(ls -S -1 "${PORT_WINE_TMP_PATH}/tmp_img/"*".png" | grep "${PW_RESIZE_TO}x${PW_RESIZE_TO}" | head -n 1)" \
            "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.png"
        fi
        cp -f "${PORT_WINE_TMP_PATH}/tmp_img/${PORTPROTON_NAME}.png" "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png"
    fi
    if [[ -z "$(file "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" | grep "${PW_RESIZE_TO} x ${PW_RESIZE_TO}")" ]] \
    && command -v convert &>/dev/null \
    && [[ -f "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" ]]
    then
        convert "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" -resize ${PW_RESIZE_TO}x${PW_RESIZE_TO} "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png"
    fi
    if [[ -z "${PW_ICON_FOR_YAD}" ]] ; then
        if [[ ! -z "$(file "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" | grep "${PW_RESIZE_TO} x ${PW_RESIZE_TO}")" ]] ; then
            export PW_ICON_FOR_YAD="${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.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 "$(eval_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}' > "${PORT_WINE_TMP_PATH}/tmp_yad_find_exe"
    pw_stop_progress_bar

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

    OrigIFS="$IFS" && IFS=%
    PW_SET_FIND_EXE="$("${pw_yad_v13_0}" --height="500" --width="800" --list --text-align=center \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(eval_gettext "Create shortcut for...")" \
    --text="\n$(eval_gettext "Choose the .exe file for which you need to create a shortcut and click OK.\n")" \
    --column="$(eval_gettext "Choose path to .exe file:")" ${FIND_TO_GUI} \
    --button="$(eval_gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(eval_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=$OrigIFS

    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 () {
    WINEPREFIX="$(readlink -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}")"
    export WINEPREFIX

    PROGPFX_URL="github.com/Castro-Fidel/PortWINE/releases/download/progpfx${PROGPFX_VER}/progpfx${PROGPFX_VER}.tar.xz"
    if [[ "${PW_PREFIX_NAME}" == "PROGRAMS" && ! -f "${WINEPREFIX}"/.progpfx ]] ; then
        print_info "Download and install PROGRAMS pfx..."
        if try_download "${PROGPFX_URL}" "${PORT_WINE_PATH}/data/tmp/progpfx.tar.xz" ; then
            unpack "${PORT_WINE_PATH}/data/tmp/progpfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/"
            try_remove_file "${PORT_WINE_PATH}/data/tmp/progpfx.tar.xz"
        else
            try_remove_file "${PORT_WINE_PATH}/data/tmp/progpfx.tar.xz"
            yad_error_download && exit 1
        fi
    fi

    DOTPFX_URL="github.com/Castro-Fidel/PortWINE/releases/download/dotpfx${DOTPFX_VER}/dotpfx${DOTPFX_VER}.tar.xz"
    if [[ "${PW_PREFIX_NAME}" == "DOTNET" ]] && [[ ! -f "${WINEPREFIX}"/.dotnet${DOTPFX_VER} ]] ; then
        print_info "Download and install DOTNET pfx..."
        if try_download "${DOTPFX_URL}" "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz" ; then
            if unpack "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/"
            then touch "${WINEPREFIX}/.dotnet${DOTPFX_VER}"
            fi
            try_remove_file "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz"
        else
            try_remove_file "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz"
            yad_error_download && exit 1
        fi
    fi

    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
    pw_init_runtime

    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 ]] ; 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="winevulkan ir50_32 atl100 ntdll wined3d vulkan-1 shell32 dxgi d3d8 d3d9 d3d10core d3d11 d3d12 d3d12core"
        for change_wine_dll in $LIST_NATIVE_WINE_DLL ; do
            try_copy_file "${WINEDIR}/lib64/wine/x86_64-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/"
            try_copy_file "${WINEDIR}/lib/wine/i386-windows/${change_wine_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
        done

        print_info "Used wineboot $@ for prefix: ${PW_PREFIX_NAME}"
        ${pw_runtime} env 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 $@ &>>"${PORT_WINE_TMP_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_AUTOINSTAL}" == 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_AUTOINSTAL}" == 1 ]] \
        && [[ "${DISABLE_CP_DEFPFX}" == 1 ]] ; then
            pw_update_pfx_cover_gui
            pw_wineboot -i
        elif yad_question "$(eval_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
            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
    if grep 'xapofx1_' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg" &>/dev/null ; then
        sed -i '/xapofx1_/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
        sed -i '/x3daudio/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
        sed -i '/xaudio2/d' "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/user.reg"
    fi
    echo "${PW_WINE_USE}" > "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.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

    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

    if [[ "${PW_USE_GAMESCOPE}" == 1 ]] \
    && command -v gamescope &>/dev/null \
    && ! check_gamescope_session
    then
        # Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/6029
        if [[ -d "/sys/module/i915" ]] ; then
            export INTEL_DEBUG="norbc"
        fi
        if [[ "${PW_GPU_USE}" != "disabled" ]] ; then
            PW_vendorID="$("$PW_VULKANINFO_PORTABLE" 2>/dev/null | grep -B3 "${PW_GPU_USE}" | grep vendorID | sort -u | awk -F'0x' '{print $2}')"
            PW_deviceID="$("$PW_VULKANINFO_PORTABLE" 2>/dev/null | 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
            unset PW_ID_VIDEO
        fi
        export PW_RUN_GAMESCOPE="gamescope -o 30 ${PW_ID_VIDEO} ${GAMESCOPE_ARGS} --"
        export PW_WINE_FULLSCREEN_FSR="0"
        export vk_xwayland_wait_ready="false"
    else
        export PW_USE_GAMESCOPE="0"
        export PW_RUN_GAMESCOPE=""
    fi

    enabled_fake_nvidia_videocard ()
    {
        if [[ "${1}" == old ]] ; then
            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
        elif [[ "${1}" == new ]] ; then
            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
        elif [[ "${1}" == 0 ]] ; then
            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
        fi
    }

    for rm_dll in "nvml.dll" "nvngx.ini" "nvngx.dll" "_nvngx.dll" ; do
        try_remove_file "${WINEPREFIX}/drive_c/windows/syswow64/$rm_dll"
        try_remove_file "${WINEPREFIX}/drive_c/windows/system32/$rm_dll"
    done

    export DXVK_ENABLE_NVAPI=1
    if [[ "${PW_USE_FAKE_DLSS_3}" == 1 ]] ; then
        try_copy_file "${PW_PLUGINS_PATH}/fake_dlss_3/${PW_FAKE_DLSS_3_VER}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        try_copy_file "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_3_VER}/dlssg_to_fsr3_amd_is_better.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        try_copy_file "${PW_PLUGINS_PATH}/nvngx/_nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        var_winedlloverride_update "nvapi,nvapi64,nvngx,_nvngx,dlssg_to_fsr3_amd_is_better=n;nvcuda=b"
        enabled_fake_nvidia_videocard new
        export WINEHAGS=1
    elif [[ "${PW_USE_FAKE_DLSS}" == 1 ]] && [[ -d "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/" ]] ; then
        try_copy_file "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        try_copy_file "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.ini" "${WINEPREFIX}/drive_c/windows/system32/"
        enabled_fake_nvidia_videocard old
        var_winedlloverride_update "nvapi,nvapi64,nvngx=n;_nvngx=;nvcuda=b"
    elif [[ "${PW_USE_NVAPI_AND_DLSS}" == 1 ]] ; then
        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 "${FIND_NVNGX}/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
            try_copy_file "${FIND_NVNGX}/_nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        else
            try_copy_file "${PW_PLUGINS_PATH}/nvngx/nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
            try_copy_file "${PW_PLUGINS_PATH}/nvngx/_nvngx.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        fi
        enabled_fake_nvidia_videocard 0
        var_winedlloverride_update "nvngx,_nvngx,nvapi,nvapi64=n;nvcuda=b"
    else
        enabled_fake_nvidia_videocard 0
        export DXVK_ENABLE_NVAPI=0
        var_winedlloverride_update "nvngx,_nvngx="
    fi

    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
        export VKD3D_FEATURE_LEVEL="12_1"
        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}"
        if echo "${PW_GPU_USE}" | grep -i nvidia &>/dev/null ; then
            if [[ -f "/usr/share/vulkan/icd.d/nvidia_icd.json" ]] ; then
                export VK_ICD_FILENAMES="/usr/share/vulkan/icd.d/nvidia_icd.json"
            fi
        fi
    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" ]] ; 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" ]] \
    && [[ "${XDG_SESSION_TYPE}" != "wayland" ]] \
    && 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/"
            try_force_link_file "${PATH_TO_D3DEXTRAS}/x32/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
            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/"
            try_force_link_file "${WINEDIR}/lib64/wine/x86_64-windows/${d3d_extras_from_plugins}.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        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/"
        try_force_link_file "${PHYSX_PATH}/${physx_dll}64.dll" "${WINEPREFIX}/drive_c/windows/system32/"
    done
    try_force_link_file "${PHYSX_PATH}/cudart32_65.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
    try_force_link_file "${PHYSX_PATH}/cudart64_65.dll" "${WINEPREFIX}/drive_c/windows/system32/"

    create_new_dir "${PW_VULKAN_DIR}/opengl"
    PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/opengl"
    PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/opengl"

    if [[ "${PW_VULKAN_USE}" == "0" ]] ; then
        print_info "Use OpenGL"
        export GL_YIELD="NOTHING"
        export mesa_glthread=true
        export WINE_D3D_CONFIG='renderer=gl'
    elif [[ "${PW_VULKAN_USE}" == "3" ]] ; then
        print_info "Use GALLIUM-NINE (Native DX9 on MESA drivers)"
        export PW_GALLIUM_NINE_PATH="${PW_PLUGINS_PATH}/gallium_nine_v.${PW_GALLIUM_NINE_VER}"
        try_force_link_file "${PW_GALLIUM_NINE_PATH}/lib32/d3d9-nine.dll.so" "${WINEPREFIX}/drive_c/windows/syswow64/d3d9.dll"
        try_force_link_file "${PW_GALLIUM_NINE_PATH}/lib64/d3d9-nine.dll.so" "${WINEPREFIX}/drive_c/windows/system32/d3d9.dll"
        var_winedlloverride_update "d3d9=n;wined3d=b;dxvk_config,vulkan-1,winevulkan="
        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
                old_IFS=$IFS
                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=$old_IFS
                print_info "D3D_MODULE_PATH=${D3D_MODULE_PATH}"
            else
                pw_stop_progress_bar
                yad_error "$(eval_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
        export mesa_glthread=true
        export ENABLE_VKBASALT=0
        export PW_MANGOHUD=0
        export WINE_FULLSCREEN_FSR=0
    elif [[ "${PW_VULKAN_USE}" == "4" ]] ; then
        print_info "Use GALLIUM-ZINK (OpenGL on MESA vulkan drivers)"
        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
        if [[ "${XDG_SESSION_TYPE}" != "wayland" ]] || [[ "${PW_USE_GAMESCOPE}" != 1 ]] ; then
            export LIBGL_KOPPER_DRI2=1
        fi
    elif [[ "${PW_VULKAN_USE}" == "6" ]] ; then
        print_info "Use DAMAVAND (DirectX to wined3d vulkan)"
        export WINE_D3D_CONFIG='renderer=vulkan'
    else
        print_info "Use VKD3D-PROTON, DXVK and D8VK (DirectX 8-12 to vulkan)"
        if [[ "${PW_USE_WINE_DXGI}" == 1 ]] ; then
            export CP_DXVK_FILES="d3d11 d3d10core d3d9 dxvk_config" # openvr_api_dxvk
            export CP_WINE_FILES="d3d10_1 d3d10 dxgi"
        elif [[ "${USE_ALL_DXVK_DLLS}" == 1 ]] ; then
            export CP_DXVK_FILES="d3d11 d3d10core d3d10_1 d3d10 d3d9 dxgi dxvk_config" # openvr_api_dxvk
        else
            export CP_DXVK_FILES="d3d11 d3d10core d3d9 dxgi dxvk_config" # openvr_api_dxvk
            export CP_WINE_FILES="d3d10_1 d3d10"
        fi

        check_variables VKD3D_FEATURE_LEVEL "12_1"
        check_variables DXVK_HDR

        print_info "D8VK v.${D8VK_VER} in use"

        if [[ "${PW_VULKAN_USE}" == "1" ]] ; then
            export PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_STABLE_VER}"
            export PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_STABLE_VER}"
            print_info "DXVK v.${DXVK_STABLE_VER} in use"
            print_info "VKD3D-PROTON v.${VKD3D_STABLE_VER} in use"
        elif [[ "${PW_VULKAN_USE}" == "2" ]] ; then
            export PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_GIT_VER}"
            export PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            print_info "DXVK v.${DXVK_GIT_VER} in use"
            print_info "VKD3D-PROTON v.${VKD3D_GIT_VER} in use"
        elif [[ "${PW_VULKAN_USE}" == "5" ]] ; then
            export PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_STABLE_VER}"
            export PATH_TO_DGV2_FILES="${PW_VULKAN_DIR}/dgvoodoo2-${DGV2_VER}"
            print_info "DXVK v.${DXVK_STABLE_VER} in use"
            print_info "DGVOODOO2 ${DGV2_VER} in use"
            for DGV2_DLL in d3dimm ddraw glide glide2x glide3x ; do
                try_copy_file "$PATH_TO_DGV2_FILES/x32/${DGV2_DLL}.dll" "${PATH_TO_GAME}"
            done

            if [[ ! -f "${PATH_TO_GAME}/dgVoodoo.conf" ]] ; then
            echo "[General]
OutputAPI                            = d3d11_fl11_0
[Glide]
3DfxWatermark                       = false
3DfxSplashScreen                    = false
[DirectX]
dgVoodooWatermark                   = false" > "${PATH_TO_GAME}/dgVoodoo.conf"
            fi
            var_winedlloverride_update ddraw=n
        fi

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

        print_info "Try copy native DXVK files..."
        for wine_dxvk_dll in ${CP_DXVK_FILES} ; do
            if [[ -f "${PATH_TO_DXVK_FILES}/x64/${wine_dxvk_dll}.dll"  ]] ; then
                try_copy_file "${PATH_TO_DXVK_FILES}/x32/${wine_dxvk_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
                try_copy_file "${PATH_TO_DXVK_FILES}/x64/${wine_dxvk_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/"
                var_winedlloverride_update "${wine_dxvk_dll}=n"
            fi
        done

        print_info "Try copy NVAPI files..."
        try_copy_file "${PATH_TO_DXVK_FILES}/x32/nvapi.dll" "${WINEPREFIX}/drive_c/windows/syswow64/" || \
        try_copy_file "${PW_PLUGINS_PATH}/nvapi/x32/nvapi.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
        try_copy_file "${PATH_TO_DXVK_FILES}/x64/nvapi64.dll" "${WINEPREFIX}/drive_c/windows/system32/" || \
        try_copy_file "${PW_PLUGINS_PATH}/nvapi/x64/nvapi64.dll" "${WINEPREFIX}/drive_c/windows/system32/"

        print_info "Try copy native VKD3D files..."
        for wine_vkd3d_dll in d3d12 d3d12core libvkd3d-1 libvkd3d-shader-1 ; do
            if [[ -f "${PATH_TO_VKD3D_FILES}/x64/${wine_vkd3d_dll}.dll" ]] ; then
                try_copy_file "${PATH_TO_VKD3D_FILES}/x86/${wine_vkd3d_dll}.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
                try_copy_file "${PATH_TO_VKD3D_FILES}/x64/${wine_vkd3d_dll}.dll" "${WINEPREFIX}/drive_c/windows/system32/"
                var_winedlloverride_update "${wine_vkd3d_dll}=n"
            fi
        done

        PATH_TO_D8VK_FILES="${PW_VULKAN_DIR}/d8vk-${D8VK_VER}"
        print_info "Try link native D8VK file..."
        try_force_link_file "${PATH_TO_D8VK_FILES}/x32/d3d8.dll" "${WINEPREFIX}/drive_c/windows/syswow64/"
        try_force_link_file "${PATH_TO_D8VK_FILES}/x64/d3d8.dll" "${WINEPREFIX}/drive_c/windows/system32/"
        var_winedlloverride_update d3d8=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/"
        try_force_link_file "${WINEDIR}"/lib64/wine/x86_64-windows/vrclient_x64.dll "${WINEPREFIX}/drive_c/vrclient/bin/"
    fi

    if [[ "${PW_USE_SHADER_CACHE}" == 1 ]] ; then
        create_new_dir "${PATH_TO_DXVK_FILES}/gl_shader_cache"
        export __GL_SHADER_DISK_CACHE=1
        export __GL_SHADER_DISK_CACHE_PATH="${PATH_TO_DXVK_FILES}/gl_shader_cache"
        export __GL_SHADER_DISK_CACHE_SIZE=50000000000
        export __GL_SHADER_DISK_CACHE_SKIP_CLEANUP=1
        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
        create_new_dir "${PATH_TO_VKD3D_FILES}/vkd3d_cache"
        export VKD3D_SHADER_CACHE_PATH="${PATH_TO_VKD3D_FILES}/vkd3d_cache"
    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 ]] || [[ "${WINE_FULLSCREEN_FSR}" == 1 ]] ; 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

    #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..." >> "${PORT_WINE_TMP_PATH}/update_pfx_log"
            echo "Try to install DLL in prefix: ${PW_DLL_NEED_INSTALL}" >> "${PORT_WINE_TMP_PATH}/update_pfx_log"
            print_info "Try to install DLL in prefix: ${PW_DLL_NEED_INSTALL}"
            ${pw_runtime} env PATH="${PATH}" 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 "${PORT_WINE_TMP_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} env PATH="${PATH}" 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

    pw_stop_progress_bar &&
    if ! check_start_from_steam ; then
        pw_tray_icon
        sleep 0.5
        if [[ "${PW_CHECK_AUTOINSTAL}" != "1" ]] ; then
            pw_start_progress_bar_cover "${COVERS_PATH}/loading_${LANGUAGE}.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

    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

    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 "${PORT_WINE_TMP_PATH}/update_pfx_log" ]] && cat "${PORT_WINE_TMP_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} \
            env PATH="${PATH}" \
            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} \
            "${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} \
            env PATH="${PATH}" \
            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} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
            print_info "Update prefix log:"
            [[ -f "${PORT_WINE_TMP_PATH}/update_pfx_log" ]] && cat "${PORT_WINE_TMP_PATH}/update_pfx_log"
            echo
            print_info "Log from RUNTIME and WINE:"
            cat "${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 "${PORT_WINE_TMP_PATH}/update_pfx_log" ]] && cat "${PORT_WINE_TMP_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} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
            wait_wineserver
        else
            print_info "Update prefix log:"
            [[ -f "${PORT_WINE_TMP_PATH}/update_pfx_log" ]] && cat "${PORT_WINE_TMP_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} \
            "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]}
            wait_wineserver
        fi
    fi
}
export -f pw_run

portwine_launch () {
    start_portwine
    PORTWINE_MSI=$(basename "${portwine_exe}" | grep .msi)
    PORTWINE_BAT=$(basename "${portwine_exe}" | grep .bat)
    if [[ ! -z "${PW_VIRTUAL_DESKTOP}" && "${PW_VIRTUAL_DESKTOP}" == "1" ]] ; then
        pw_run explorer "/desktop=PortProton,${PW_SCREEN_RESOLUTION}" ${WINE_WIN_START} "$portwine_exe"
    elif [ ! -z "${PORTWINE_MSI}" ]; then
        pw_run msiexec /i "$portwine_exe"
    elif [[ ! -z "${PORTWINE_BAT}" || -n "${portwine_exe}" ]] ; then
        pw_run ${WINE_WIN_START} "$portwine_exe"
    else
        pw_run winefile
    fi
}

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_runtime} ${PW_TERM} "${WINELOADER}" 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} \
    env PATH="${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_TERM} bash
}

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

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

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

yad_question () {
    if [[ ! -f "${pw_yad_v13_0}" ]] ; then
        local pw_yad_v13_0="yad"
        command -v "${pw_yad_v13_0}" &>/dev/null || fatal "yad not found"
    fi
    "${pw_yad_v13_0}" --text "${1}" --width=400 --borders=15 --title "$(eval_gettext "Choices")" \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --image="$PW_GUI_ICON_PATH/question.svg" \
    --no-wrap --center --text-align=center \
    --button="$(eval_gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(eval_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_v13_0}" --picture --filename="${PW_GIF_FILE}" --close-on-unfocus --no-buttons --undecorated --center \
        --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_v13_0}" ]] && local pw_yad_v13_0="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_v13_0}" --picture --filename="${PW_GIF_FILE}" --close-on-unfocus --no-buttons --undecorated --center \
        --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=$(eval_gettext "TERMINAL")!$PW_GUI_ICON_PATH/$TAB_SIZE.png --tab=$(eval_gettext "LOGO")!$PW_GUI_ICON_PATH/$TAB_SIZE.png"
        TAB_N1=2
        TAB_N2=1
        YAD_UNDECORATED=""
    else
        TAB_PLACE="--tab=$(eval_gettext "LOGO")!$PW_GUI_ICON_PATH/$TAB_SIZE.png --tab=$(eval_gettext "TERMINAL")!$PW_GUI_ICON_PATH/$TAB_SIZE.png"
        TAB_N1=1
        TAB_N2=2
        YAD_UNDECORATED="--undecorated --center"
    fi

    if ! check_start_from_steam && ! pgrep -a yad | grep "yad_v13_0 --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"
        PW_GIF_SIZE_X=$(file "${PW_GIF_FILE}" | awk '{print $7 + 20}')
        PW_GIF_SIZE_Y=$(file "${PW_GIF_FILE}" | awk '{print $9 + 60}')
        echo "UPDATE PREFIX..." > "${PORT_WINE_TMP_PATH}/update_pfx_log"
        export PW_TIMER=0
        while read -r line || [[ ! -z $(pgrep -a yad | grep "yad_v13_0 --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 < "${PORT_WINE_TMP_PATH}/update_pfx_log" | "${pw_yad_v13_0}" --plug=$PW_KEY_PROGRESS_BAR_UP --tabnum=$TAB_N2 --text-info --tail  2>/dev/null &
        "${pw_yad_v13_0}" --plug=$PW_KEY_PROGRESS_BAR_UP --tabnum=$TAB_N1 --picture --filename="${PW_GIF_FILE}" --image-on-top 2>/dev/null &

        "${pw_yad_v13_0}" --notebook --key="$PW_KEY_PROGRESS_BAR_UP" $TAB_PLACE --no-buttons --expand \
        --auto-close --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 --progress-text="$@
        " --pulsate --close-on-unfocus --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --center --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 --progress-text="$@
        " --pulsate --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --center --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_BLOCK="$!"
        return 0
    fi
}

pw_start_progress_bar_install_game () {
    if ! check_start_from_steam ; then
        "${pw_yad}" --progress --progress-text="$(eval_gettext "Please wait. Installing the") $@
        " --pulsate --borders="$PROGRESS_BAR_BORDERS_SIZE" \
        --no-buttons --undecorated --center --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_BLOCK="$!"
        return 0
    fi
}

pw_stop_progress_bar () {
    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
    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 () {
    local PW_CHANGELOG_FILE="changelog_${LANGUAGE}"
    "${pw_yad_v13_0}" --title="$(eval_gettext "CHANGELOG")" --no-buttons --text-align=center \
    --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
}
export -f open_changelog

pw_tray_icon () {
    if [ ! -z "$(pgrep -a yad_v13_0 | grep "\--notification" | awk '{print $1}')" ] ; then
        kill -s SIGUSR1 "$(pgrep -a yad_v13_0 | grep "\--notification" | awk '{print $1}')"
    fi

    tray_icon_click () {
        echo ""
    }
    export -f tray_icon_click

    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 check_flatpak || [[ "$PW_DESKTOP_THEME" == "universal" ]]
    then TRAY_ICON="$PW_GUI_ICON_PATH/portproton_tray_flatpak.svg"
    elif [[ "$PW_DESKTOP_THEME" == "dark" ]]
    then TRAY_ICON="$PW_GUI_ICON_PATH/portproton_tray_dark.svg"
    else TRAY_ICON="$PW_GUI_ICON_PATH/portproton_tray_light.svg"
    fi
    "${pw_yad_v13_0}" --notification --no-middle \
    --image="$TRAY_ICON" \
    --command="bash -c tray_icon_click" \
    --tooltip="PortProton" \
    --icon-size=32 --menu="| \
    $(eval_gettext "WINEFILE")!bash -c pw_tray_winefile!"$PW_GUI_ICON_PATH/wine_file.svg"|| \
    $(eval_gettext "TASKMGR")!bash -c pw_tray_taskmgr!"$PW_GUI_ICON_PATH/wine_system.svg"|| \
    $(eval_gettext "CHANGELOG")!bash -c open_changelog!"$PW_GUI_ICON_PATH/history.svg"|| \
    $(eval_gettext "FORCE EXIT")     !bash -c tray_icon_click_exit!"$PW_GUI_ICON_PATH/close.svg"||" 2>/dev/null &

    return 0
}

pw_gui_for_edit_db () {
    KEY_EDIT_DB_GUI=$RANDOM
    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
    # if [[ ! -z "${PW_AMD_VULKAN_USE}" ]] && [[ "${PW_AMD_VULKAN_USE}" != "disabled" ]] ; then
    #     AMD_VULKAN_VAR="${PW_AMD_VULKAN_USE}"
    # else
    #     AMD_VULKAN_VAR="disabled"
    # fi

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

    GET_GPU_NAMES=$("$PW_VULKANINFO_PORTABLE" 2>/dev/null | awk -F '=' '/deviceName/{print $2}' | sed '/llvm/d'| sort -u | sed 's/^ //' | paste -sd '!')

    logical_cores=$(grep -c "^processor" /proc/cpuinfo)
    if [[ "${logical_cores}" -le "4" ]] ; then
        GET_LOGICAL_CORE="1!$(seq -s! 1 $((${logical_cores} - 1)))"
    else
        GET_LOGICAL_CORE="1!2!$(seq -s! 4 4 $((${logical_cores} - 1)))"
    fi

    unset ADD_CHK_BOX_EDIT_DB

    ENABLE_VKBASALT_INFO=$(eval_gettext 'Enable vkBasalt by default to improve graphics in games running on Vulkan. (The HOME hotkey disables vkbasalt)')
    PW_NO_ESYNC_INFO=$(eval_gettext 'Do not use in-process synchronization primitives based on eventfd. (It is recommended not to change the value.)')
    PW_NO_FSYNC_INFO=$(eval_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)')
    PW_USE_RAY_TRACING_INFO=$(eval_gettext 'Enable vkd3d support - Ray Tracing')
    PW_FIX_VIDEO_IN_GAME_INFO=$(eval_gettext 'Required for video playback in some games so that it is not distorted (usually colored pink)')
    PW_VULKAN_NO_ASYNC_INFO=$(eval_gettext 'Disable asynchronous calls for VULKAN and DXVK modes')
    PW_USE_NVAPI_AND_DLSS_INFO=$(eval_gettext 'Enable DLSS on supported NVIDIA graphics cards')
    PW_OLD_GL_STRING_INFO=$(eval_gettext 'Forced use of older versions of OpenGL')
    PW_HIDE_NVIDIA_GPU_INFO=$(eval_gettext 'Disguise all features used for NVIDIA graphics cards')
    PW_FORCE_USE_VSYNC_INFO=$(eval_gettext 'Forced activation of vertical sync')
    PW_VIRTUAL_DESKTOP_INFO=$(eval_gettext 'Enable the application to run in the WINE virtual desktop')
    PW_USE_TERMINAL_INFO=$(eval_gettext 'Run the application in the terminal')
    PW_HEAP_DELAY_FREE_INFO=$(eval_gettext 'Include a delay in releasing some memory to bypass errors associated with using the application after the memory is released')
    PW_NO_WRITE_WATCH_INFO=$(eval_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.)')
    PW_GUI_DISABLED_CS_INFO=$(eval_gettext 'Disable the window for selecting startup modes and WINE versions')
    PW_USE_GSTREAMER_INFO=$(eval_gettext 'Use Gstreamer to output clips in games (WMF support)')
    PW_FORCE_LARGE_ADDRESS_AWARE_INFO=$(eval_gettext 'Increases RAM usage limits for 32-bit applications from two to four gigabytes')
    PW_USE_RUNTIME_INFO=$(eval_gettext 'Use container launch mode (It is recommended not to change the value)')
    PW_MANGOHUD_INFO=$(eval_gettext 'Using FPS and system load monitoring (Turns on and off by the key combination - right Shift + F12)')
    PW_USE_GAMEMODE_INFO=$(eval_gettext 'Using automatic system optimization to improve performance in games (provided the gamemode package is installed on the system)')
    PW_USE_WINE_DXGI_INFO=$(eval_gettext 'Forced use of built-in DXGI library (in rare cases it solves problems with DX12 games)')
    PW_MANGOHUD_USER_CONF_INFO=$(eval_gettext 'Forced use of MANGOHUD system settings (GOverlay, etc.)')
    PW_VKBASALT_USER_CONF_INFO=$(eval_gettext 'Forced use of VKBASALT system settings (GOverlay, etc.)')
    PW_USE_D3D_EXTRAS_INFO=$(eval_gettext 'Enable forced use of third-party DirectX libraries')
    PW_USE_WINDOWS_7_INFO=$(eval_gettext 'Change the version of WINDOWS 10 to WINDOWS 7 in the prefix')
    PW_USE_SHADER_CACHE_INFO=$(eval_gettext 'Use WINE shader caching (disable only if there are microfreezes in the game)')
    PW_WINE_FULLSCREEN_FSR_INFO=$(eval_gettext 'Works while using any version of ProtonGE in full screen mode at a resolution below the standard screen')
    PW_USE_FAKE_DLSS_INFO=$(eval_gettext 'Enable DLSS translator in FSR 2 in DirectX 12 games (CyberFSR2)')
    PW_USE_FAKE_DLSS_3_INFO=$(eval_gettext 'Enable DLSS translator in FSR 3 in DirectX 12 games (Experimental project dlssg-to-fsr3)')
    PW_USE_EAC_AND_BE_INFO=$(eval_gettext 'Enable Easy Anti-Cheat and BattlEye Anti-Cheat runtimes (required if game used this anti-cheats)')
    PW_REDUCE_PULSE_LATENCY_INFO=$(eval_gettext 'Reduce pulseaudio latency to fix intermittent sound')
    PW_USE_US_LAYOUT_INFO=$(eval_gettext 'Forced use of the us layout (useful for games in which the control works correctly only on the us layout)')
    PW_RESTORE_RESOLUTION_INFO=$(eval_gettext 'Remember the screen resolution when starting the game and return it when closing (useful for games that change the screen resolution when closing)')
    PW_USE_SYSTEM_VK_LAYERS_INFO=$(eval_gettext 'Use system mangohud, vkBasalt, obs-vkcapture and other applications using vulkan layers')
    PW_USE_OBS_VKCAPTURE_INFO=$(eval_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)')
    PW_DISABLE_COMPOSITING_INFO=$(eval_gettext 'Disable desktop compositing (effects). It often improves performance.')
    PW_USE_GAMESCOPE_INFO=$(eval_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")

    edit_db_field_add () {
        for int_to_boole in $@ ; 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} | sed 's/^PW_//' | sed 's/_/ /g' )"
            ADD_CHK_BOX_EDIT_DB+="--field=${CHKBOX_SPACE}${int_to_boole_non_pw}!${!TMP_HELP_FOR_GUI}:${THEME_CHKBOX}%${!int_to_boole}%"
        done
    }
    edit_db_field_add $@
    old_IFS=$IFS
    IFS="%"
    "${pw_yad_v13_0}" --plug=$KEY_EDIT_DB_GUI --tabnum="1" --form --separator=" " --columns=3 ${ADD_CHK_BOX_EDIT_DB} \
    1> "${PORT_WINE_TMP_PATH}/tmp_output_yad_edit_db" 2>/dev/null &
    IFS=$old_IFS

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

    "${pw_yad_v13_0}" --plug=$KEY_EDIT_DB_GUI --tabnum="2" --form --separator="%" --columns=1 --scroll \
    --field="$(eval_gettext "Change the version of <b>WINDOWS</b> emulation")!$(eval_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="$(eval_gettext "AUTOINSTALL WITH <b>WINETRICKS</b>")!$(eval_gettext "Automatically install with <b>WINETRICKS</b> additional libraries required to run the game/program. List of libraries separated by spaces") :CBE" "${PW_DLL_INSTALL}!vcrun2019 corefonts lucida" \
    --field="$(eval_gettext "Forced to use/disable libraries")!$(eval_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" "${WINEDLLOVERRIDES}!libglesv2=!d3dx9_36,d3dx9_42=n,b;mfc120=b,n" \
    --field=":LBL" "" \
    --field="$(eval_gettext "ADD ARGUMENTS FOR .EXE FILE")!$(eval_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" "\\${LAUNCH_PARAMETERS[@]}!-dx11 -skipintro 1" \
    --field=":LBL" "" \
    --field="$(eval_gettext "Limit the use of processor cores")!$(eval_gettext "Limiting the number of CPU cores is useful for Unity games (It is recommended to set the value equal to 8)") :CB" "${CPU_LIMIT_VAR}!disabled!${GET_LOGICAL_CORE}" \
    --field="$(eval_gettext "Choose a graphics card to run the game (in user.conf)")!$(eval_gettext "Select which video card will be used to run the game (used for all running games and programs in PortProton)") :CB" "${GPU_VAR}!disabled!${GET_GPU_NAMES}" \
    --field="$(eval_gettext "Add arguments for GAMESCOPE:")!$(eval_gettext "
<b>-W, -H:</b> set the resolution used by gamescope. Resizing the gamescope window will update these settings. Ignored in embedded mode. If -H is specified but -W isn't, a 16:9 aspect ratio is assumed. Defaults to 1280×720.
<b>-w, -h:</b> set the resolution used by the game. If -h is specified but -w isn't, a 16:9 aspect ratio is assumed. Defaults to the values specified in -W and -H.
<b>-r:</b> set a frame-rate limit for the game. Specified in frames per second. Defaults to unlimited.
<b>-o:</b> set a frame-rate limit for the game when unfocused. Specified in frames per second. Defaults to unlimited.
<b>-F fsr:</b> use AMD FidelityFX™ Super Resolution 1.0 for upscaling.
<b>-F nis:</b> use NVIDIA Image Scaling v1.0.3 for upscaling.
<b>-S integer:</b> use integer scaling.
<b>-S stretch:</b> use stretch scaling, the game will fill the window. (e.g. 4:3 to 16:9).
<b>-b:</b> create a border-less window.
<b>-f:</b> create a full-screen window.") :CBE" "\\${GAMESCOPE_ARGS}!-r 60 -F fsr!" \
    --field="$(eval_gettext "Forcibly select the OpenGL version for the game")!$(eval_gettext "You can select the required OpenGL version, some games require a forced Compatibility Profile (COMPAT). (Examples are in the drop-down list)") :CB" "${PW_MESA_GL_VERSION_OVERRIDE}!disabled!4.6COMPAT!4.6!4.5COMPAT!4.5!3.3COMPAT!3.3" \
    2>/dev/null 1> "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" 2>/dev/null &

    "${pw_yad_v13_0}" --paned --key="$KEY_EDIT_DB_GUI" --title "EDIT_DB" --text-align=center --height="750" \
    --text "$(eval_gettext "Change settings in database file for") <b>${PORTWINE_DB}</b>\n $(eval_gettext "<b>NOTE:</b> To display help for each item, simply hover your mouse over the text")" --separator=" " \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --button="$(eval_gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(eval_gettext "RESET SETTINGS")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Restore default settings")":2 \
    --button="$(eval_gettext "OPEN THE SETTINGS FILE")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Open the <b>.ppdb</b> settings file in a system text editor to view and change variables manually")":150 \
    --button="$(eval_gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_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)
                /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
                ;;
        252)
                exit 0
                ;;
    esac
    output_yad_edit_db=($(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_edit_db"))
    bool_from_yad=0
    edit_db_field_read () {
        for boole_to_int in $@ ; 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
    }
    edit_db_field_read $@
    PW_WINDOWS_VER="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $1}')"
    PW_DLL_INSTALL="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $2}')"
    WINEDLLOVERRIDES="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $3}')"
    LAUNCH_PARAMETERS="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $5}')"
    CPU_LIMIT="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $7}')"
    PW_GPU_USE="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $8}')"
    GAMESCOPE_ARGS="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $9}')"
    PW_MESA_GL_VERSION_OVERRIDE="$(cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $10}')"
    # PW_AMD_VULKAN_USE="`cat "${PORT_WINE_TMP_PATH}/tmp_output_yad_fps_limit" | awk -F"%" '{print $10}'`"

    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
    echo "pw_gui_for_edit_db PORTWINE_DB_FILE=$PORTWINE_DB_FILE"
    edit_db_from_gui $@ LAUNCH_PARAMETERS PW_WINDOWS_VER PW_DLL_INSTALL WINEDLLOVERRIDES PW_WINE_CPU_TOPOLOGY GAMESCOPE_ARGS PW_MESA_GL_VERSION_OVERRIDE
    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_AMD_VULKAN_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)
                xdg-open "${PORTWINE_DB_FILE}" &
                exit 0
                ;;
    esac
}

gui_proton_downloader () {
    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}"
    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 "$(eval_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))
    try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
    if [[ ! -z "${PROTON_GE_GIT}" ]] ; then
        for PGEGIT in ${PROTON_GE_GIT[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.gz//' >> "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
        done
        sed -i '/Proton-6.5-GE-2/,$d' "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
        sed -i '/github-action/d' "${PORT_WINE_TMP_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))
    try_remove_file "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
    if [[ ! -z "${WINE_KRON4EK}" ]] ; then
        for PGEGIT in ${WINE_KRON4EK[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
        done
        sed -i '/6.3/,$d' "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
        sed -i '/-x86/d' "${PORT_WINE_TMP_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))
    try_remove_file "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"
    if [[ ! -z "${WINE_GE_CUSTOM}" ]] ; then
        for PGEGIT in ${WINE_GE_CUSTOM[@]} ; do
            echo ${PGEGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"
        done
        sed -i '/6.23/,$d' "${PORT_WINE_TMP_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))
    try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
    if [[ ! -z "${PROTON_PW_GIT}" ]] ; then
        for PPWGIT in ${PROTON_PW_GIT[@]} ; do
            echo ${PPWGIT} | awk -F/ '{print $NF}' | sed 's/.tar.xz//' >> "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
        done
        sed -i /${PW_WINE_LG_VER}/d "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
        sed -i '/plugins/d' "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
    fi
    pw_stop_progress_bar
    if [[ -z "${PROTON_PW_GIT}" ]] ; then
        yad_error "$(eval_gettext "Error: check wine.")"
        export SKIP_CHECK_UPDATES=1
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    fi
    for INSTALLING_VERSION_IN_DIST in $(ls "${PORT_WINE_PATH}/data/dist/") ; do
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
        sed -i "/${INSTALLING_VERSION_IN_DIST}$/Id" "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"
        [[ -z "$(cat "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git")" ]] && echo "nothing" > "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"
    done

    [[ -z "$(cat "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git")" ]] && echo "nothing" >"${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
    [[ -z "$(cat "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git")" ]] && echo "nothing" >"${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
    [[ -z "$(cat "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git")" ]] && echo "nothing" >"${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
    [[ -z "$(cat "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git")" ]] && echo "nothing" >"${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"

	# Installed wine
	ls -l ${PORT_WINE_PATH}/data/dist | awk '{print $9}' | sed '/^$/d' 1> ${PORT_WINE_TMP_PATH}/tmp_installed_wine
    if [[ "$1" != "silent" ]] ; then
        # GUI
        export KEY_WINE=$RANDOM
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_pw_set"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_kron4ek_set"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_set"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_ge_custom_set"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_installed_wine_set"

        "${pw_yad_v13_0}" --plug=$KEY_WINE --tabnum=1 --list --separator="" --listen \
        --column "$(eval_gettext "Select WINE for download:")" < "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git" 1> "${PORT_WINE_TMP_PATH}/tmp_proton_pw_set" 2>/dev/null &
        "${pw_yad_v13_0}" --plug=$KEY_WINE --tabnum=2 --list --separator="" --listen \
        --column "$(eval_gettext "Select WINE for download:")" < "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git" 1> "${PORT_WINE_TMP_PATH}/tmp_kron4ek_set" 2>/dev/null &
        "${pw_yad_v13_0}" --plug=$KEY_WINE --tabnum=3 --list --separator="" --listen \
        --column "$(eval_gettext "Select WINE for download:")" < "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git" 1> "${PORT_WINE_TMP_PATH}/tmp_proton_set" 2>/dev/null &
        "${pw_yad_v13_0}" --plug=$KEY_WINE --tabnum=4 --list --separator="" --listen \
        --column "$(eval_gettext "Select WINE for download:")" < "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git" 1> "${PORT_WINE_TMP_PATH}/tmp_ge_custom_set" 2>/dev/null &
        "${pw_yad_v13_0}" --plug=$KEY_WINE --tabnum=5 --list --separator="" --listen \
        --column "$(eval_gettext "Select installed WINE for delete:")" < "${PORT_WINE_TMP_PATH}/tmp_installed_wine" 1> "${PORT_WINE_TMP_PATH}/tmp_installed_wine_set" 2>/dev/null &
        "${pw_yad_v13_0}" --key=$KEY_WINE --notebook --width=500 --height=600 --text-align=center --no-button \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(eval_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="$(eval_gettext "INSTALLED")"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --button="$(eval_gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
        --button="$(eval_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 $(awk '{print $1}' "${PORT_WINE_TMP_PATH}/tmp_proton_set") ]] ; then
            VERSION_WINE_GIT="$(cat "${PORT_WINE_TMP_PATH}/tmp_proton_set")"
        elif [[ ! -z $(awk '{print $1}' "${PORT_WINE_TMP_PATH}/tmp_proton_pw_set") ]] ; then
            VERSION_WINE_GIT="$(cat "${PORT_WINE_TMP_PATH}/tmp_proton_pw_set")"
        elif [[ ! -z $(awk '{print $1}' "${PORT_WINE_TMP_PATH}/tmp_kron4ek_set") ]] ; then
            VERSION_WINE_GIT="$(cat "${PORT_WINE_TMP_PATH}/tmp_kron4ek_set")"
        elif [[ ! -z $(awk '{print $1}' "${PORT_WINE_TMP_PATH}/tmp_ge_custom_set") ]] ; then
            VERSION_WINE_GIT="$(cat "${PORT_WINE_TMP_PATH}/tmp_ge_custom_set")"
        elif [[ ! -z $(awk '{print $1}' "${PORT_WINE_TMP_PATH}/tmp_installed_wine_set") ]] ; then
            VERSION_INSTALLED_WINE="$(cat "${PORT_WINE_TMP_PATH}/tmp_installed_wine_set")"
        else
            print_info "Nothing to do."
            export SKIP_CHECK_UPDATES=1
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi

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

        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_pw_git"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_wine_kron4ek_git"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_proton_ge_git"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_wine_ge_custom_git"
        try_remove_file "${PORT_WINE_TMP_PATH}/tmp_installed_wine"

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

    for GIVE_WINE_URL in ${PROTON_GE_GIT[@]} ${PROTON_PW_GIT[@]} ${WINE_KRON4EK[@]} ${WINE_GE_CUSTOM[@]} ; do
        if [ ! -z $(echo ${GIVE_WINE_URL} | grep -i "${VERSION_WINE_GIT}") ] ; then
            export URL_VERSION_PROTON_GIT="${GIVE_WINE_URL}"
            break
        fi
    done

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

    print_info "Download and install ${VERSION_WINE_GIT}..."
    PW_WINE_ARCHIVE="$(echo "${URL_VERSION_PROTON_GIT}" | awk -F'/' '{print $NF}' | awk -F'.tar' '{print $1}')"
    FILENAME="$(basename "${URL_VERSION_PROTON_GIT}")"
    if try_download "${URL_VERSION_PROTON_GIT}" "${PORT_WINE_PATH}/data/tmp/$FILENAME" ; then
        for archive_type in "gz" "xz" ; do
            if [ -f "${PORT_WINE_PATH}/data/tmp/${PW_WINE_ARCHIVE}.tar.${archive_type}" ] ; then
                if unpack "${PORT_WINE_PATH}/data/tmp/${PW_WINE_ARCHIVE}.tar.${archive_type}" "${PORT_WINE_PATH}/data/dist/" ; then
                    try_remove_file "${PORT_WINE_PATH}/data/tmp/${PW_WINE_ARCHIVE}.tar.${archive_type}"
                    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/${PW_WINE_ARCHIVE}.tar.${archive_type}"
                    try_remove_dir "${PORT_WINE_PATH}/data/dist/${PW_WINE_ARCHIVE}"
                    if yad_error_download
                    then gui_proton_downloader $@
                    else
                        export PW_WINE_VER="$PW_WINE_USE"
                        init_wine_ver
                    fi
                fi
            fi
        done
    else
        if yad_error_download
        then gui_proton_downloader $@
        else
            export PW_WINE_VER="$PW_WINE_USE"
            init_wine_ver
        fi
    fi

    if [[ "$1" != "silent" ]] ; then
        export SKIP_CHECK_UPDATES=1
        edit_db_from_gui
        /usr/bin/env bash -c ${pw_full_command_line[*]} &
        exit 0
    else
        init_wine_ver
    fi
}
export -f gui_proton_downloader

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))

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

    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 [[ ! -z $(echo " ${GET_FX_IN_FILE[@]} " | grep " ${add_list_fx} ") ]] ; 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" ]]
        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
    export old_IFS=$IFS
    export IFS="%"
    "${pw_yad_v13_0}" --plug=$KEY_FX_GUI --tabnum="1" --form --columns=4 --separator=" " \
    --text-align=center --text="$(eval_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\n")" \
    ${ADD_GUI_FX} 1> "${PORT_WINE_TMP_PATH}/tmp_yad_basalt_set" 2>/dev/null &
    export IFS="${old_IFS}"

    "${pw_yad_v13_0}" --plug=$KEY_FX_GUI --tabnum="2" --separator=" " --form \
    --field="AMD FidelityFX - Contrast Adaptive Sharpening"!"$(eval_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> "${PORT_WINE_TMP_PATH}/tmp_yad_cas_set" 2>/dev/null &

    "${pw_yad_v13_0}" --paned --key="$KEY_FX_GUI" --height="625" --title="vkBasalt" --center \
    --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
    --button="$(eval_gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Cancel the current changes and return to the previous menu")":1 \
    --button="$(eval_gettext "DISABLE VKBASALT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Disable vkBasalt and go to the previous menu")":180 \
    --button="$(eval_gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Save the current changes, and go to the previous menu")":182 2>/dev/null
    YAD_VKBASALT_STATUS="$?"
    case "${YAD_VKBASALT_STATUS}" in
        1|252)
            export SKIP_CHECK_UPDATES=1
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0 ;;
        180)
            export ENABLE_VKBASALT=0 ;;
        182)
            export ENABLE_VKBASALT=1 ;;
    esac
    YAD_BASALT_SET=$(cat "${PORT_WINE_TMP_PATH}/tmp_yad_basalt_set")
    YAD_CAS_SET=$(cat "${PORT_WINE_TMP_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="0"
    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 ENABLE_VKBASALT
    export SKIP_CHECK_UPDATES=1
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

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
    )

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

    unset ADD_GUI_MH GUI_MH_RESULT

    GET_REFRESH_RATE="30!45!60!75!120!144!165!240"

    if [[ -z "$FPS_LIMIT" ]]; then
        FPS_LIMIT_VAR="disabled"
    else
        FPS_LIMIT_VAR="${FPS_LIMIT}"
    fi

    if [ -n "$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

    for add_list_mh in "${LIST_MH[@]}"; do
        PW_MH_GUI_HELP="PW_MH_${add_list_mh}_INFO"
        if [[ -n "$(grep -wo "$add_list_mh" <<<"${PW_MANGOHUD_CONFIG[@]}")" ]] ; 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

    export old_IFS=$IFS
    export IFS="%"
    "${pw_yad_v13_0}" --plug=$KEY_MH_GUI --tabnum="1" --form --columns=4 --separator=" " \
        --text-align=center --text="$(eval_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.\n")" \
        ${ADD_GUI_MH} 1> "${PORT_WINE_TMP_PATH}/tmp_yad_mh_set" 2>/dev/null &
    export IFS="${old_IFS}"

    "${pw_yad_v13_0}" --plug=$KEY_MH_GUI --tabnum="2" --separator=" " --form \
        --field="$(eval_gettext "MANGOHUD FPS LIMIT")!$(eval_gettext "Enabling FPS limits using MANGOHUD built into PortProton") :CB" "${FPS_LIMIT_VAR}!disabled!${GET_REFRESH_RATE}" \
        1> "${PORT_WINE_TMP_PATH}/tmp_yad_mh_fps_limit" 2>/dev/null &

    "${pw_yad_v13_0}" --paned --key="$KEY_MH_GUI" --height="575" --title="MangoHud" --center \
        --separator=" " --window-icon="$PW_GUI_ICON_PATH/portproton.svg" \
        --button="$(eval_gettext "CANCEL THE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Cancel the current changes and return to the previous menu")":1 \
        --button="$(eval_gettext "DISABLE MANGOHUD")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Disable MangoHud and go to the previous menu")":182 \
        --button="$(eval_gettext "SAVE CHANGES")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"$(eval_gettext "Save the current changes, and go to the previous menu")":184 2>/dev/null
    YAD_MANGOHUD_STATUS="$?"

    case "${YAD_MANGOHUD_STATUS}" in
        1 | 252)
            export SKIP_CHECK_UPDATES=1
            /usr/bin/env bash -c "${pw_full_command_line[*]}" &
            exit 0
            ;;
        182)
            export PW_MANGOHUD=0
            export FPS_LIMIT="disabled"
            edit_db_from_gui PW_MANGOHUD FPS_LIMIT
            export SKIP_CHECK_UPDATES=1
            /usr/bin/env bash -c "${pw_full_command_line[*]}" &
            exit 0
            ;;
        184)
            export PW_MANGOHUD=1
            ;;
    esac

    YAD_MH_SET=$(<"${PORT_WINE_TMP_PATH}/tmp_yad_mh_set")
    FPS_LIMIT=$(cat "${PORT_WINE_TMP_PATH}/tmp_yad_mh_fps_limit" | awk '{print $1}')

    MONITOR_HEIGHT="$(echo $PW_SCREEN_RESOLUTION | awk -F'x' '{print $2}')"
    MH_FONT_SIZE="font_size=$(( MONITOR_HEIGHT / 45 ))"


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

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

    if [[ "${FPS_LIMIT}" != "disabled" ]] ; then
        export GUI_MH_RESULT=${GUI_MH_RESULT},fps_limit=${FPS_LIMIT}
        export PW_MANGOHUD=1
    elif [[ "${FPS_LIMIT}" == "disabled" ]]
    then export GUI_MH_RESULT="$(echo ${GUI_MH_RESULT} | sed "s/,fps_limit=${FPS_LIMIT_VAR}//" | sed "s/,fps_limit=disabled//")"
    fi

    if [[ "${GUI_MH_RESULT}" =~ "fps_only" ]]; then
        export 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}"

    edit_db_from_gui MANGOHUD_CONFIG PW_MANGOHUD FPS_LIMIT
    export SKIP_CHECK_UPDATES=1
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}

portwine_create_shortcut () {
    pw_stop_progress_bar
    [[ ! -e ${portwine_exe} ]] && return 1
    pw_create_gui_png

    if [[ "$1" == "block_name" ]] ; then
        export name_desktop="${PORTPROTON_NAME}"
        OUTPUT=$("${pw_yad}" --title="$(eval_gettext "Choices")" --form \
        --window-icon "$PW_GUI_ICON_PATH/portproton.svg" \
        --image "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" \
        --field="$(eval_gettext "The shortcut will be created in the PortProton directory.")":LBL "" \
        --field="  ${name_desktop}":LBL "" \
        --field="  $(eval_gettext "Add shortcut to MENU -> GAMES")":CHK "TRUE" \
        --field="  $(eval_gettext "Add shortcut to Desktop")":CHK "TRUE" \
        --field="  $(eval_gettext "Add shortcut to STEAM library")":CHK "FALSE" \
        --button="$(eval_gettext "CREATE SHORTCUT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0)
        PW_YAD_OUT=$?
    else
        export name_desktop="${PORTPROTON_NAME}"
        OUTPUT=$("${pw_yad}" --title="$(eval_gettext "Choices")" --form \
        --window-icon "$PW_GUI_ICON_PATH/portproton.svg" \
        --image "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" \
        --field="$(eval_gettext "The shortcut will be created in the PortProton directory.")":LBL "" \
        --field="$(eval_gettext "Name")" "${name_desktop}" \
        --field="  $(eval_gettext "Add shortcut to MENU -> GAMES")":CHK "TRUE" \
        --field="  $(eval_gettext "Add shortcut to Desktop")":CHK "TRUE" \
        --field="  $(eval_gettext "Add shortcut to STEAM library")":CHK "FALSE" \
        --button="$(eval_gettext "CREATE SHORTCUT")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0)
        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 $2}')
        fi
        create_menu_desktop_shortcut=$(echo "$OUTPUT" | awk -F'|' '{print $3}')
        create_desktop_desktop_shortcut=$(echo "$OUTPUT" | awk -F'|' '{print $4}')
        create_menu_steam_shortcut=$(echo "$OUTPUT" | awk -F'|' '{print $5}')

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

        if [[ "${PORTPROTON_NAME}" != "${name_desktop}" ]]
        then mv -f "${PORT_WINE_PATH}/data/img/${PORTPROTON_NAME}.png" "${PORT_WINE_PATH}/data/img/${name_desktop}.png"
        fi

        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"
        } >> "${PORT_WINE_PATH}/${name_desktop}.desktop"
        chmod u+x "${PORT_WINE_PATH}/${name_desktop}.desktop"

        if [[ "${create_menu_desktop_shortcut}" == "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 [[ "${create_desktop_desktop_shortcut}" == "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 [[ "${create_menu_steam_shortcut}" == "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/"
                export SCPATH="${STUIDPATH}/config/$SCVDF"
                "${PORT_SCRIPTS_PATH}/add_in_steam.sh"
            done
            if [[ "${PW_SKIP_RESTART_STEAM}" != 1 ]] && pgrep -i steam &>/dev/null ; then
                if yad_question "$(eval_gettext "For adding shortcut to STEAM, needed restart.\n\nRestart STEAM now?")" ; then
                    pw_start_progress_bar_block "$(eval_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

        [[ "$1" != silent ]] && [[ "${PW_SKIP_RESTART_STEAM}" != 1 ]] && "${PORT_WINE_PATH}" 2>1 >/dev/null &
    fi
}

portwine_delete_shortcut () {
    rm -f "$(grep -il "${portwine_exe}" "${HOME}/.local/share/applications"/*.desktop)" &>/dev/null
    rm -f "$(grep -il "${portwine_exe}" "${PORT_WINE_PATH}"/*.desktop)" &>/dev/null
    # rm -f "$(grep -il "${portwine_exe}" "${STEAM_SCRIPTS}"/*.sh)" &>/dev/null
    if [[ -d "${HOME}/Desktop" ]] ; then
        rm -f "$(grep -il "${portwine_exe}" "${HOME}/Desktop"/*.desktop)" &>/dev/null
    elif [[ -d "${HOME}/Рабочий стол" ]] ; then
        rm -f "$(grep -il "${portwine_exe}" "${HOME}/Рабочий стол"/*.desktop)" &>/dev/null
    elif [[ $(xdg-user-dir DESKTOP) ]] ; then
        rm -f "$(grep -il "${portwine_exe}" "$(xdg-user-dir DESKTOP)"/*.desktop)" &>/dev/null
    fi
}

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

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 "$(eval_gettext "Starting prefix manager:") $PW_PREFIX_NAME"
    "${PORT_WINE_TMP_PATH}/winetricks" dlls list | awk -F'(' '{print $1}' 1> "${PORT_WINE_TMP_PATH}/dll_list"
    "${PORT_WINE_TMP_PATH}/winetricks" fonts list | awk -F'(' '{print $1}' 1> "${PORT_WINE_TMP_PATH}/fonts_list"
    "${PORT_WINE_TMP_PATH}/winetricks" settings list | awk -F'(' '{print $1}' 1> "${PORT_WINE_TMP_PATH}/settings_list"

    gui_prefix_manager () {
        unset SET_FROM_PFX_MANAGER_TMP SET_FROM_PFX_MANAGER
        old_IFS=$IFS
        IFS=$'\n'
        try_remove_file  "${PORT_WINE_TMP_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') ]] ; 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) }')" >> "${PORT_WINE_TMP_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) }')" >> "${PORT_WINE_TMP_PATH}/dll_list_tmp"
                fi
            fi
        done < "${PORT_WINE_TMP_PATH}/dll_list"
        try_remove_file  "${PORT_WINE_TMP_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) }')" >> "${PORT_WINE_TMP_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) }')" >> "${PORT_WINE_TMP_PATH}/fonts_list_tmp"
                fi
            fi
        done < "${PORT_WINE_TMP_PATH}/fonts_list"
        try_remove_file  "${PORT_WINE_TMP_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) }')" >> "${PORT_WINE_TMP_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) }')" >> "${PORT_WINE_TMP_PATH}/settings_list_tmp"
                fi
            fi
        done < "${PORT_WINE_TMP_PATH}/settings_list"
        pw_stop_progress_bar

        KEY_EDIT_MANAGER_GUI=$RANDOM
        "${pw_yad_v13_0}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=1 --list --checklist \
        --text="$(eval_gettext 'Select components to install in prefix:') <b>\"${PW_PREFIX_NAME}\"</b>, $(eval_gettext 'using wine:') <b>\"${PW_WINE_USE}\"</b>" \
        --column=set --column=dll --column=info < "${PORT_WINE_TMP_PATH}/dll_list_tmp" 1>> "${PORT_WINE_TMP_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad_v13_0}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=2 --list --checklist \
        --text="$(eval_gettext 'Select fonts to install in prefix:') <b>\"${PW_PREFIX_NAME}\"</b>, $(eval_gettext 'using wine:') <b>\"${PW_WINE_USE}\"</b>" \
        --column=set --column=dll --column=info < "${PORT_WINE_TMP_PATH}/fonts_list_tmp" 1>> "${PORT_WINE_TMP_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad_v13_0}" --plug=$KEY_EDIT_MANAGER_GUI --tabnum=3 --list --checklist \
        --text="$(eval_gettext 'Change config for prefix:') <b>\"${PW_PREFIX_NAME}\"</b>" \
        --column=set --column=dll --column=info < "${PORT_WINE_TMP_PATH}/settings_list_tmp" 1>> "${PORT_WINE_TMP_PATH}/to_winetricks" 2>/dev/null &

        "${pw_yad_v13_0}" --key=$KEY_EDIT_MANAGER_GUI --notebook \
        --width=900 --height=800 --expand \
        --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(eval_gettext 'PREFIX MANAGER')" \
        --button="$(eval_gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":1 \
        --button="$(eval_gettext "OK")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png"!"":0 \
        --tab-pos=bottom \
        --tab="$(eval_gettext 'DLLS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="$(eval_gettext 'FONTS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" \
        --tab="$(eval_gettext 'SETTINGS')"!"$PW_GUI_ICON_PATH/$TAB_SIZE.png"!"" 2>/dev/null
        YAD_STATUS="$?"
        if [[ "$YAD_STATUS" == "1" || "$YAD_STATUS" == "252" ]] ; then
            print_info "Nothing to do. Restarting PortProton..."
            stop_portwine &
            /usr/bin/env bash -c ${pw_full_command_line[*]} &
            exit 0
        fi
        try_remove_file  "${PORT_WINE_TMP_PATH}/dll_list_tmp"
        try_remove_file  "${PORT_WINE_TMP_PATH}/fonts_list_tmp"
        try_remove_file  "${PORT_WINE_TMP_PATH}/settings_list_tmp"

        for STPFXMNG in $(cat "${PORT_WINE_TMP_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=${old_IFS}
        try_remove_file  "${PORT_WINE_TMP_PATH}/to_winetricks"

        if [[ ! -z ${SET_FROM_PFX_MANAGER} ]] ; then
            pw_update_pfx_cover_gui "winetricks"
            echo "START WINETRICKS..." >> "${PORT_WINE_TMP_PATH}/update_pfx_log"
            echo "Try to install DLL in prefix: ${SET_FROM_PFX_MANAGER}" >> "${PORT_WINE_TMP_PATH}/update_pfx_log"
            print_info "Try to install DLL in prefix: ${SET_FROM_PFX_MANAGER}"
            ${pw_runtime} env PATH="${PATH}" LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" GST_PLUGIN_SYSTEM_PATH_1_0="" \
            "${PORT_WINE_TMP_PATH}/winetricks" -q -r -f ${SET_FROM_PFX_MANAGER} | tee -a "${PORT_WINE_TMP_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
	yad_info "$(eval_gettext "Attention working version of vulkan not detected!\nIt is recommended to run games in OpenGL (low performance possible)!")"
    fi
    echo "$(eval_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 "$(eval_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 [ "${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"
    if [[ "${PW_VULKAN_USE}" = "0" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DX9-11 to OPENGL" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "1" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DXVK v.${DXVK_STABLE_VER} and VKD3D-PROTON v.${VKD3D_STABLE_VER}" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "2" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DXVK v.${DXVK_GIT_VER} and VKD3D-PROTON v.${VKD3D_GIT_VER}" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "3" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - native DX9 on MESA drivers" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "4" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - OpenGL on MESA vulkan drivers" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "5" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DGVOODOO2 (DirectX 1-7, Glide 3dfx wrapper)" >> "${PORT_WINE_PATH}/PortProton.log"
    elif [[ "${PW_VULKAN_USE}" = "6" ]] ; then
        echo "PW_VULKAN_USE=${PW_VULKAN_USE} - DirectX to wined3d vulkan" >> "${PORT_WINE_PATH}/PortProton.log"
    else
        echo "PW_VULKAN_USE=${PW_VULKAN_USE}" >> "${PORT_WINE_PATH}/PortProton.log"
    fi
    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"
    echo "Operating system:" >> "${PORT_WINE_PATH}/PortProton.log"
    cat /etc/os-release | grep -oP 'PRETTY_NAME="\K[^"]+' >> "${PORT_WINE_PATH}/PortProton.log"
    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:$(cat /proc/cpuinfo | grep 'cpu cores' | sort -u | cut -d':' -f2)" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU logical cores: $(cat /proc/cpuinfo | grep 'processor' | wc -l)" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "CPU model name:$(cat /proc/cpuinfo | grep 'model name' | 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 "Graphic cards and drivers:" >> "${PORT_WINE_PATH}/PortProton.log"
    echo 'lspci -k | grep -EA3 VGA|3D|Display :' >> "${PORT_WINE_PATH}/PortProton.log"
    echo "$(lspci -k | grep -EA3 'VGA|3D|Display')" >> "${PORT_WINE_PATH}/PortProton.log"
    echo "----" >> "${PORT_WINE_PATH}/PortProton.log"
    [[ `command -v glxinfo` ]] && 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"
    "$PW_VULKANINFO_PORTABLE" 2>/dev/null | grep -E '^GPU|deviceName|driverName' >> "${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_v13_0 --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_v13_0}" --text-info --tail --button="STOP"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":0 --title="DEBUG" \
    --skip-taskbar --width=800 --height=400 --text "$(eval_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=$(cat "${PORT_WINE_PATH}/PortProton.log"  | awk '! a[$0]++')
    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_v13_0}" --file --directory --width=800 --height=500 --auto-close \
    --window-icon="$PW_GUI_ICON_PATH/portproton.svg" --title "$(eval_gettext "BACKUP PREFIX TO...")" \
    --button="$(eval_gettext "CANCEL")"!"$PW_GUI_ICON_PATH/$BUTTON_SIZE.png":1 \
    --button="$(eval_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="env PATH=\"${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 "$(eval_gettext "Backup has been successfully created for prefix:") $PW_PREFIX_NAME"
        if [[ ! -f "${PORT_WINE_TMP_PATH}/pfx_backup_info" ]] ; then
            yad_info "$(eval_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 "$(eval_gettext "An error occurred while backing up the prefix:") $PW_PREFIX_NAME"
    fi
    return 0
}

pw_edit_db () {
    if [[ "${XDG_SESSION_TYPE}" == "wayland" ]] ; then
        pw_gui_for_edit_db \
        PW_MANGOHUD PW_MANGOHUD_USER_CONF ENABLE_VKBASALT PW_VKBASALT_USER_CONF PW_NO_ESYNC PW_NO_FSYNC PW_USE_RAY_TRACING \
        PW_USE_NVAPI_AND_DLSS PW_USE_FAKE_DLSS PW_USE_FAKE_DLSS_3 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_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_USE_GAMESCOPE PW_DISABLE_COMPOSITING PW_USE_RUNTIME
    else
        pw_gui_for_edit_db \
        PW_MANGOHUD PW_MANGOHUD_USER_CONF ENABLE_VKBASALT PW_VKBASALT_USER_CONF PW_NO_ESYNC PW_NO_FSYNC PW_USE_RAY_TRACING \
        PW_USE_NVAPI_AND_DLSS PW_USE_FAKE_DLSS PW_USE_FAKE_DLSS_3 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_USE_GAMESCOPE PW_DISABLE_COMPOSITING PW_USE_RUNTIME
    fi
    # PW_FORCE_USE_VSYNC HEAP_DELAY_FREE
}

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_AUTOINSTAL=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 ENABLE_VKBASALT=0
    export PW_USE_D3D_EXTRAS=1
    . "${PORT_SCRIPTS_PATH}/pw_autoinstall/${PW_YAD_SET}"
}

gui_credits () {
    . "${PORT_SCRIPTS_PATH}/credits"
}
export -f gui_credits

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

run_desktop_b_click () {
    [[ ! -z "$1" ]] && echo "$1" > "${PORT_WINE_TMP_PATH}/tmp_yad_form"
    if [[ ! -z $(pidof -s yad) ]] || [[ ! -z $(pidof -s yad_v13_0) ]] ; 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 "$(eval_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 "$(eval_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" == "CDN" ]] ; then
        sed -i 's/="CDN"/="GITHUB"/g' "$USER_CONF"
        yad_info "$(eval_gettext "Mirror changed to:") GITHUB"
    else
        sed -i 's/="GITHUB"/="CDN"/g' "$USER_CONF"
        yad_info "$(eval_gettext "Mirror changed to:") CDN"
    fi
    print_info "Restarting PP for change mirror..."
    export SKIP_CHECK_UPDATES=1
    /usr/bin/env bash -c ${pw_full_command_line[*]} &
    exit 0
}
export -f change_mirror

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

gui_open_user_conf () {
    xdg-open "${PORT_WINE_PATH}/data/user.conf"
}
export -f gui_open_user_conf

gui_open_scripts_from_backup () {
    cd "${PORT_WINE_TMP_PATH}/scripts_backup/" || fatal
    PW_SCRIPT_FROM_BACKUP=$("${pw_yad_v13_0}" --file --width=650 --height=500 --auto-close \
    --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
