#!/usr/bin/env bash
# Author: Castro-Fidel (linux-gaming.ru)
# Development assistants: Cefeiko; Dezert1r; Taz_mania; Anton_Famillianov; gavr; RidBowt; chal55rus; UserDiscord
########################################################################
echo '
            █░░ █ █▄░█ █░█ ▀▄▀ ▄▄ █▀▀ ▄▀█ █▀▄▀█ █ █▄░█ █▀▀ ░ █▀█ █░█
            █▄▄ █ █░▀█ █▄█ █░█ ░░ █▄█ █▀█ █░▀░█ █ █░▀█ █▄█ ▄ █▀▄ █▄█

██████╗░░█████╗░██████╗░████████╗██████╗░██████╗░░█████╗░████████╗░█████╗░███╗░░██╗
██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗████╗░██║
██████╔╝██║░░██║██████╔╝░░░██║░░░██████╔╝██████╔╝██║░░██║░░░██║░░░██║░░██║██╔██╗██║
██╔═══╝░██║░░██║██╔══██╗░░░██║░░░██╔═══╝░██╔══██╗██║░░██║░░░██║░░░██║░░██║██║╚████║
██║░░░░░╚█████╔╝██║░░██║░░░██║░░░██║░░░░░██║░░██║╚█████╔╝░░░██║░░░╚█████╔╝██║░╚███║
╚═╝░░░░░░╚════╝░╚═╝░░╚═╝░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░░╚════╝░╚═╝░░╚══╝

'
if [[ $(id -u) = 0 ]] ; then
    echo "Do not run this script as root!"
    zenity --error --text "Do not run this script as root!"
    exit 1
fi
if [[ "${s_install}" != "1" && ! -x "`which "zenity" 2>/dev/null`" ]] ; then
    echo "You will need to install: zenity, and restart the port."
    xdg-open "https://linux-gaming.ru/portproton/" > /dev/null 2>&1 & exit 0
fi
. "$(dirname $(readlink -f "$0"))/functions_helper"

start_portwine () {
    export WINEPREFIX="${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"
    create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/"
    try_force_link_dir "${PORT_WINE_PATH}/data/prefixes" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/"
    export WINEARCH="win64"
    if [[ "${PW_VULKAN_USE}" == 3 ]] ; then 
        export PW_WINE_USE="${PW_PROTON_LG_VER}"
        echo "Force to use $PW_WINE_USE"
        if [ ! -d "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}" ] ; then
            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_tar_xz "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz" "${PORT_WINE_PATH}/data/dist/" ; then
                    try_remove_file "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz"
                    UNPACK_STATUS=0
                else
                    try_remove_file "${PORT_WINE_PATH}/data/tmp/${PW_WINE_USE}.tar.xz"
                    try_remove_dir "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}"
                fi
            fi
            [[ "${UNPACK_STATUS}" != 0 ]] && exit 1
        fi
        edit_db_from_gui PW_WINE_USE
    else
        pw_check_and_download_wine "${PW_WINE_USE}"
    fi
    pw_install_dll_in_prefix 
    init_wine_ver

    if [ ! -f "${HOME}/.Xresources" ]; then
        cp -f "${PORT_SCRIPTS_PATH}/.Xresources" "${HOME}"
        xrdb -merge "${HOME}/.Xresources"
    fi

    pw_init_runtime

    if [[ ! -z "${PW_LOG}" && "${PW_LOG}" != 0 ]] ; then
        export WINEDEBUG="fixme-all,err+loaddll,err+dll,err+file,err+reg"
        export DXVK_LOG_LEVEL="warn"
        export VKD3D_DEBUG="warn"
        export WINE_MONO_TRACE="E:System.NotImplementedException"
        export VK_LOADER_DEBUG="warn"
        export VKBASALT_LOG_LEVEL="warn"
    else
        export WINEDEBUG="-all"
        export DXVK_LOG_LEVEL="none"
        export VKD3D_SHADER_DEBUG="none"
        export VKD3D_DEBUG="none"
        export DXVK_LOG_PATH="none"
        export VKBASALT_LOG_LEVEL="none"
    fi

    #default wine-mono override for FNA games
    export WINE_MONO_OVERRIDES="Microsoft.Xna.Framework.*,Gac=n"

    if [[ ! -z "${PW_NO_ESYNC}" && "${PW_NO_ESYNC}" != 0 ]] ; then
        export WINEESYNC="0"
    else 
        export WINEESYNC="1"
    fi
    if [[ ! -z "${PW_NO_FSYNC}" && "${PW_NO_FSYNC}" != 0 ]] ; then
        export WINEFSYNC="0"
        export WINEFSYNC_FUTEX2="0"
    else
        export WINEFSYNC="1"
        export WINEFSYNC_FUTEX2="1"
        export WINEFSYNC_SPINCOUNT=100
    fi
    if [[ ! -z "${PW_FORCE_LARGE_ADDRESS_AWARE}" && "${PW_FORCE_LARGE_ADDRESS_AWARE}" != 0 ]]
    then export WINE_LARGE_ADDRESS_AWARE="1"
    else export WINE_LARGE_ADDRESS_AWARE="0"
    fi
    if [[ ! -z "${PW_OLD_GL_STRING}" && "${PW_OLD_GL_STRING}" != 0 ]] ; then
        export MESA_EXTENSION_MAX_YEAR="2003" #mesa override
        export __GL_ExtensionStringVersion="17700" #nvidia override
    fi
    if [[ ! -z "${PW_NO_WRITE_WATCH}" && "${PW_NO_WRITE_WATCH}" != 0 ]] ; then
        export WINE_DISABLE_WRITE_WATCH="1"
    fi
    if [[ ! -z "${PW_VULKAN_NO_ASYNC}" && "${PW_VULKAN_NO_ASYNC}" != 0 ]] ; then
        export RADV_DEBUG="llvm"
        var_vkd3d_config_update single_queue
    fi

    enabled_fake_nvidia_videocard () 
    {
        if [[ "${1}" == 1 ]] ; then
            sed -i /'dxgi.customDeviceId =/c dxgi.customDeviceId = 222F' "${DXVK_CONFIG_FILE}"
            sed -i /'dxgi.customVendorId =/c dxgi.customVendorId = 10de' "${DXVK_CONFIG_FILE}"
        elif [[ "${1}" == 0 ]] ; then
            sed -i /'dxgi.customDeviceId =/c # dxgi.customDeviceId = 222F' "${DXVK_CONFIG_FILE}"
            sed -i /'dxgi.customVendorId =/c # dxgi.customVendorId = 10de' "${DXVK_CONFIG_FILE}"
        fi
    }

    if [[ ! -z "${PW_USE_FAKE_DLSS}" && "${PW_USE_FAKE_DLSS}" == 1 && -d "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/" ]]
    then
        try_force_link_file "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/" 
        try_force_link_file "${PW_PLUGINS_PATH}/fake_dlss/${PW_FAKE_DLSS_VER}/nvngx.ini" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
        enabled_fake_nvidia_videocard 1
        export DXVK_NVAPIHACK=0
        export DXVK_ENABLE_NVAPI=1
        export WINE_HIDE_NVIDIA_GPU=0
    elif [[ ! -z "${PW_USE_NVAPI_AND_DLSS}" && "${PW_USE_NVAPI_AND_DLSS}" == 1 ]] && [[ -d "${WINEDIR}/lib/wine/nvapi/" ]]
    then
        try_force_link_file "${PW_PLUGINS_PATH}/nvapi/x32/nvapi.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/"
        try_force_link_file "${PW_PLUGINS_PATH}/nvapi/x64/nvapi64.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/" 
        try_remove_file "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/nvngx.ini"
        var_winedlloverride_update "nvapi,nvapi64,nvml=n;nvcuda=b"
        if [[ ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/nvngx.dll" || ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/_nvngx.dll" ]] ; then
            try_copy_file `find /usr/* -type f -name "nvngx.dll" 2>/dev/null | awk '{print $1}' | grep '/lib/'` "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
            try_copy_file `find /usr/* -type f -name "_nvngx.dll" 2>/dev/null | awk '{print $1}' | grep '/lib/'` "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"       
        fi
        echo "Try link nvml.dll.so to prefix"
        try_force_link_file "${PW_PLUGINS_PATH}"/nvml/lib/wine/nvml.dll.so "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/nvml.dll"
        try_force_link_file "${PW_PLUGINS_PATH}"/nvml/lib64/wine/nvml.dll.so "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/nvml.dll"
        export DXVK_NVAPIHACK=0
        export DXVK_ENABLE_NVAPI=1
        export WINE_HIDE_NVIDIA_GPU=0
        enabled_fake_nvidia_videocard 0
    else
        enabled_fake_nvidia_videocard 0
        export DXVK_NVAPIHACK=1
        export DXVK_ENABLE_NVAPI=0
        export WINE_HIDE_NVIDIA_GPU=1
    fi

    if [[ ! -z "${PW_PRIME_RENDER_OFFLOAD}" && "${PW_PRIME_RENDER_OFFLOAD}" != 0 ]] ; then
        export __NV_PRIME_RENDER_OFFLOAD=1
        check_nvidia_vk_icd_file
    fi
    if [[ ! -z "${PW_HEAP_DELAY_FREE}" && "${PW_HEAP_DELAY_FREE}" != 0 ]]
    then export WINE_HEAP_DELAY_FREE="1"
    else export WINE_HEAP_DELAY_FREE="0"
    fi
    if [[ ! -z "${PW_WINE_ALLOW_XIM}" && "${PW_WINE_ALLOW_XIM}" != 0 ]]
    then export WINE_ALLOW_XIM="1"
    else export WINE_ALLOW_XIM="0"
    fi
    if [[ ! -z "${PW_FIX_VIDEO_IN_GAME}" && "${PW_FIX_VIDEO_IN_GAME}" != 0 ]]
    then export WINE_DO_NOT_CREATE_DXGI_DEVICE_MANAGER="1"
    else export WINE_DO_NOT_CREATE_DXGI_DEVICE_MANAGER="0"
    fi
    
    [[ ! -z "${PW_MANGOHUD_USER_CONF}" && "${PW_MANGOHUD_USER_CONF}" != 0 ]] && unset MANGOHUD_CONFIG
    if [[ ! -z "${PW_USE_DXR11}" && "${PW_USE_DXR11}" != 0 ]] ; then 
        var_vkd3d_config_update dxr11
        var_radv_perftest_config_update rt
    elif [[ ! -z "${PW_USE_DXR10}" && "${PW_USE_DXR10}" != 0 ]] ; then 
        var_vkd3d_config_update dxr
        var_radv_perftest_config_update rt
    fi
    export optirun_on=""
    export check_optimus_manager=
    export PW_NVIDIA="$(lspci | grep NVIDIA)"
    if [ -x "`which optimus-manager 2>/dev/null`" ]; then
        export check_optimus_manager=`optimus-manager --status | grep Current | cut -f 2 -d':' | sed -e 's/^[[:space:]]*//'`
    fi
    if [[ ! -z "${PW_NVIDIA}" ]] && [[ "${check_optimus_manager}" != "intel" ]] && [[ "${DRI_PRIME}" != 1 ]] ; then
        if [ "${check_optimus_manager}" = "nvidia" ]; then
            check_nvidia_vk_icd_file
        elif [ -x "`which primusrun 2>/dev/null`" ]; then
            export optirun_on="primusrun"
            check_nvidia_vk_icd_file
        elif [ -x "`which optirun 2>/dev/null`" ]; then
            export optirun_on="optirun -b primus"
            check_nvidia_vk_icd_file
        elif [ -x "`which prime-run 2>/dev/null`" ]; then
            export optirun_on="prime-run"
            check_nvidia_vk_icd_file
        elif [[ -x "`which nvidia-settings 2>/dev/null`" ]] ; then 
            export __GLX_VENDOR_LIBRARY_NAME=nvidia
            check_nvidia_vk_icd_file
        fi
    else
        export DRI_PRIME=1
    fi
    case "$PW_FORCE_USE_VSYNC" in  # 0-FORCE_OFF, 1-FORCE_ON, 2-BY_DEFAULT
        "0")
        unset vblank_mode
        unset __GL_SYNC_TO_VBLANK ;;
        "1")
        export vblank_mode=1
        export __GL_SYNC_TO_VBLANK=1 ;;
    esac
    export int_xneur=0
    if [[ ! -z "`pgrep xneur`" ]]; then
        killall xneur
        export int_xneur=1
    fi
    if [[ ! -z "${PW_XKBD_RU_HACK}" && "${PW_XKBD_RU_HACK}" == 1 ]] ; then
        export PW_XKBD="$(setxkbmap -query | grep -w ru | awk '{print($2)}')"
        if [ ! -z ${PW_XKBD} ]; then
            setxkbmap us,ru
        fi
    fi
    if [[ -x "`which "gamemoderun" 2>/dev/null`" ]] && [[ "$PW_USE_GAMEMODE" = "1" ]] ; then
        export GAMEMODERUN=1
        PW_GAMEMODERUN_SLR="gamemoderun"
        systemctl enable --now --user gamemoded &>/dev/null
        echo "Gamemod will be launched."
    else
        export GAMEMODERUN=0
        echo "Gamemod is not installed or disabled in vars script or db file: PW_USE_GAMEMODE=$PW_USE_GAMEMODE"
    fi
    pw_other_fixes

    check_dirs_and_files_in_pfx () {
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c"
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices"
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/"
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
        [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/c:" ] && try_force_link_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/c:"
        [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/z:" ] && try_force_link_dir "/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/z:"
        create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"
        try_force_link_file "${WINEDIR}/share/fonts/"LiberationSans-Regular.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"arial.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"LiberationSans-Bold.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"arialbd.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"LiberationSerif-Regular.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"times.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"LiberationMono-Regular.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"cour.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"LiberationMono-Bold.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"courbd.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"msyh.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"msyh.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"malgun.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"malgun.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"micross.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"micross.ttf
        try_force_link_file "${WINEDIR}/share/fonts/"nirmala.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"nirmala.ttf

        try_force_link_file "${WINEDIR}/share/fonts/"simsun.ttc "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"simsun.ttc
        try_force_link_file "${WINEDIR}/share/fonts/"msgothic.ttc "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"msgothic.ttc
        
        try_force_link_file "${WINEDIR}/share/wine/fonts/"tahoma.ttf "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/Fonts/"tahoma.ttf
    }

    DOTPFX_URL="github.com/Castro-Fidel/PortWINE/releases/download/dotpfx${DOTPFX_VER}/dotpfx${DOTPFX_VER}.tar.xz"
    if [[ "${update_loc}" == "RUS" && "${PW_PREFIX_NAME}" == "DOTNET" && ! -f "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}"/.dotnet48 ]] ; then
        print_info "Download and install DOTNET..."
        if try_download "${DOTPFX_URL}" "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz" ; then
            unpack_tar_xz "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/"
            try_remove_file "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz"
        else
            try_remove_file "${PORT_WINE_PATH}/data/tmp/dotpfx.tar.xz"
            zenity_error_download && exit 1
        fi
    fi
    
    pw_update_pfx_cover_gui

    if [[ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}" || ! -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 [[ -f "${PW_WINELIB}/default_pfx.tar.xz" ]] ; then
            unpack_tar_xz "${PW_WINELIB}/default_pfx.tar.xz" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/" 2>/dev/null
        fi
        ${pw_runtime} env LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" "${WINELOADER}" wineboot -u &>>"${PORT_WINE_TMP_PATH}/update_pfx_log"
        wait_wineserver &&
        echo "${PW_WINE_USE}" > "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver"
    elif [[ -z "`cat "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver" | grep ${PW_WINE_USE}`" || ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices" ]] ; then 
        check_dirs_and_files_in_pfx
        ${pw_runtime} env LD_LIBRARY_PATH="${PW_LD_LIBRARY_PATH}" "${WINELOADER}" wineboot -i &>>"${PORT_WINE_TMP_PATH}/update_pfx_log"
        wait_wineserver &&
        echo "${PW_WINE_USE}" > "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/.wine_ver"
    fi
    [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/c:" ] && try_force_link_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/c:"
    [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/z:" ] && try_force_link_dir "/" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/dosdevices/z:"
    create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser"
    if [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/${USER}" ]; then
	    try_force_link_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/${USER}"
    fi
    create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/Public/Documents/Steam"
    create_new_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Documents/Steam"
    if [ ! -d "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/My Documents" ]
    then try_force_link_dir "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/Documents" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/users/steamuser/My Documents"
    fi
    #enabled BattleEye_Runtime and EasyAntiCheat_Runtime
    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"
    #try link d3d extras libs
    if  [[ -d "${PW_PLUGINS_PATH}/d3d_extras" ]]
    then PATH_TO_D3DEXTRAS="${PW_PLUGINS_PATH}/d3d_extras"
    else PATH_TO_D3DEXTRAS="${PW_WINELIB}/d3d_extras"
    fi
    for d3d_extras_from_plugins in 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
    do
        if [[ "${PW_USE_D3D_EXTRAS}" == 1 ]] ; then
            try_force_link_file "${PATH_TO_D3DEXTRAS}/x64/${d3d_extras_from_plugins}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
            try_force_link_file "${PATH_TO_D3DEXTRAS}/x32/${d3d_extras_from_plugins}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/"
            var_winedlloverride_update "${d3d_extras_from_plugins}=n,b"
        else
            if ! try_force_link_file "${WINEDIR}"/lib/wine/fakedlls/${d3d_extras_from_plugins}.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/"
            then try_force_link_file "${WINEDIR}"/lib/wine/i386-windows/${d3d_extras_from_plugins}.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/"
            fi
            if ! try_force_link_file "${WINEDIR}"/lib64/wine/fakedlls/${d3d_extras_from_plugins}.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
            then try_force_link_file "${WINEDIR}"/lib64/wine/x86_64-windows/${d3d_extras_from_plugins}.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/"
            fi
        fi
        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
    if [[ -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
    var_winedlloverride_update "steam_api,steam_api64,steamclient,steamclient64=n;dotnetfx35.exe,dotnetfx35setup.exe=b;lsteamclient,winemenubuilder.exe=d"

    # export TOTAL_VRAM=`"${PW_WINELIB}/runtime/files/bin/glxinfo" -B | grep Total | awk -F: '{print $2}' | awk '{print $1}'`
    # echo "TOTAL_VRAM=${TOTAL_VRAM}"

    echo "Try link wine DXGI by default..."
    if ! try_force_link_file "${WINEDIR}"/lib/wine/fakedlls/dxgi.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/dxgi.dll"
    then try_force_link_file "${WINEDIR}"/lib/wine/i386-windows/dxgi.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/dxgi.dll"
    fi
    if ! try_force_link_file "${WINEDIR}"/lib64/wine/fakedlls/dxgi.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/dxgi.dll"
    then try_force_link_file "${WINEDIR}"/lib64/wine/x86_64-windows/dxgi.dll "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/dxgi.dll"
    fi
    echo "Try link wine d3d filese by default..."
    for wine_build_dll in d3d12 d3d11 d3d10 d3d10core d3d10_1 d3d9 dxgi ; do
        if ! try_force_link_file "${WINEDIR}/lib/wine/${wine_build_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${wine_build_dll}.dll"
        then try_force_link_file "${WINEDIR}/lib/wine/i386-windows/${wine_build_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${wine_build_dll}.dll"
        fi
        if ! try_force_link_file "${WINEDIR}/lib64/wine/${wine_build_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${wine_build_dll}.dll"
        then try_force_link_file "${WINEDIR}/lib64/wine/x86_64-windows/${wine_build_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${wine_build_dll}.dll"
        fi
    done

    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
        echo "Use OpenGL"
        export GL_YIELD="NOTHING"
        export mesa_glthread=true
        var_winedlloverride_update "d3d11,d3d10,d3d10core,d3d10_1,d3d9,dxgi=b;d3d12="
    elif [[ "${PW_VULKAN_USE}" == "3" ]] ; then
        echo "Use GALLIUM-NINE (Native DX9 on MESA drivers)"
        export PW_GALLIUM_NINE_VER="0.8"
        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" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/d3d9.dll"
        try_force_link_file "${PW_GALLIUM_NINE_PATH}/lib64/d3d9-nine.dll.so" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/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
        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
            zenity_error "d3dadapter9.so.1.0.0 - ${loc_find_d3dadapter9_error}"
            stop_portwine
        fi
        export mesa_glthread=true
        export PW_DX12_DISABLE=1
        export ENABLE_VKBASALT=0
        export PW_MANGOHUD=0
        export WINE_FULLSCREEN_FSR=0
    else
        echo "Use VKD3D-PROTON and DXVK (DX 9-12 to vulkan)"
        if [[ "${PW_USE_WINE_DXGI}" == 1 ]] ; then 
            export CP_DXVK_FILES="d3d11 d3d10core d3d9 dxvk_config openvr_api_dxvk"  
        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" 
        fi

        if [[ "${PW_VULKAN_USE}" == "1" ]] ; then
            PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_STABLE_VER}"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_STABLE_VER}"
            echo "DXVK v.${DXVK_STABLE_VER} in use"
            echo "VKD3D-PROTON v.${VKD3D_STABLE_VER} in use"
        elif [[ "${PW_VULKAN_USE}" == "2" ]] ; then
            PATH_TO_DXVK_FILES="${PW_VULKAN_DIR}/dxvk-${DXVK_GIT_VER}"
            PATH_TO_VKD3D_FILES="${PW_VULKAN_DIR}/vkd3d-proton-${VKD3D_GIT_VER}"
            export ENABLE_VKBASALT=0 # need update vkbasalt for new Vulkan 1.3
            echo "DXVK v.${DXVK_GIT_VER} in use"
            echo "VKD3D-PROTON v.${VKD3D_GIT_VER} in use"
        fi

        echo "Try link native DXVK files..."
        for wine_dxvk_dll in ${CP_DXVK_FILES} ; do
            try_force_link_file "${PATH_TO_DXVK_FILES}/x32/${wine_dxvk_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${wine_dxvk_dll}.dll"
            try_force_link_file "${PATH_TO_DXVK_FILES}/x64/${wine_dxvk_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${wine_dxvk_dll}.dll"
            [[ "$?" == 0 ]] && var_winedlloverride_update "${wine_dxvk_dll}=n"
        done

        echo "Try link native VKD3D files..."
        for wine_vkd3d_dll in d3d12 libvkd3d-shader-1 # libvkd3d-proton-utils-3
        do
            try_force_link_file "${PATH_TO_VKD3D_FILES}/x86/${wine_vkd3d_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${wine_vkd3d_dll}.dll"
            try_force_link_file "${PATH_TO_VKD3D_FILES}/x64/${wine_vkd3d_dll}.dll" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${wine_vkd3d_dll}.dll"
        done
    fi
     
    if [[ "${PW_DX12_DISABLE}" == 1 ]]  
    then var_winedlloverride_update "d3d12="
    else var_winedlloverride_update "d3d12=n"
    fi

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

    try_remove_dir "${PORT_WINE_TMP_PATH}/mesa_shader_cache"
    try_remove_dir "${PORT_WINE_TMP_PATH}/vkd3d_cache"
    try_remove_dir "${PORT_WINE_TMP_PATH}/dxvk_cache"
    try_remove_dir "${PORT_WINE_TMP_PATH}/gl_shader_cache"
 
    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=0
        export VKD3D_SHADER_CACHE_PATH=0
        export RADV_DEBUG=nocache
    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

    for try_cp_dll_from_wine in "ntdll.dll" "vulkan-1.dll" ; do
        if ! try_copy_file "${WINEDIR}"/lib/wine/fakedlls/${try_cp_dll_from_wine} "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${try_cp_dll_from_wine}"
        then try_copy_file "${WINEDIR}"/lib/wine/i386-windows/${try_cp_dll_from_wine} "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/syswow64/${try_cp_dll_from_wine}"
        fi
        if ! try_copy_file "${WINEDIR}"/lib64/wine/fakedlls/${try_cp_dll_from_wine} "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${try_cp_dll_from_wine}"
        then try_copy_file "${WINEDIR}"/lib64/wine/x86_64-windows/${try_cp_dll_from_wine} "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c/windows/system32/${try_cp_dll_from_wine}"
        fi
    done

    #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}" == "vcrun2015" || "${need_install_dll_to_pfx}" == "vcrun2017" ]] ; then
                need_install_dll_to_pfx="vcrun2019"
                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"
            elif [ "${need_install_dll_to_pfx}" == "vcrun2019" ] ; then
                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"
            fi
            grep "${need_install_dll_to_pfx}" "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/winetricks.log"
            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
            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}" \
            "${PORT_WINE_TMP_PATH}/winetricks" -q -r -f ${PW_DLL_NEED_INSTALL} &>>"${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 `cat "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/system.reg" | grep "Windows $PW_WINDOWS_VER"` ]] ; 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}" \
        "${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
    sleep 1
    pw_stop_progress_bar &&
    pw_tray_icon &&
    if [[ "${PW_CHECK_AUTOINSTAL}" != "1" ]] ; then
        pw_start_progress_bar_cover "${PW_GUI_ICON_PATH}/covers/pw_loading_cover.gif"
    fi
    if [[ "${PW_PREFIX_NAME}" == "DOTNET" ]] && [[ "${PW_VULKAN_USE}" == "1" || "${PW_VULKAN_USE}" == "2" ]] ; then
        var_winedlloverride_update "libglesv2="
    fi
    add_in_start_portwine
}

pw_run () {
    unset GDK_BACKEND
    if [[ ! -z "$portwine_exe" ]] ; then
        cd "${PATH_TO_GAME}"
    else
        cd "${PORT_WINE_PATH}/data/prefixes/${PW_PREFIX_NAME}/drive_c"
    fi
    pw_mangohud_check
    pw_vkbasalt_check
    PW_LOG_TO_FILE="${PORT_WINE_PATH}/${portname}.log"
    print_var "WINEDIR" "WINEPREFIX" "WINEDLLOVERRIDES" "PATH_TO_GAME" "PW_WINE_USE" "PW_VULKAN_USE" "VKD3D_CONFIG" \
    "PW_LD_LIBRARY_PATH" "PATH" "WINEESYNC" "WINEFSYNC" "WINEFSYNC_FUTEX2" "WINEDLLPATH"
    if [[ "${PW_LOG}" == 1 ]] ; then
        echo "WINEDLLOVERRIDES=${WINEDLLOVERRIDES}" >> "${PW_LOG_TO_FILE}"
        echo "------------------------------------" >> "${PW_LOG_TO_FILE}"
        echo "Log WINE:" >> "${PW_LOG_TO_FILE}"
        ${optirun_on} ${PW_GAMEMODERUN_SLR} ${pw_runtime} \
        env ${PW_MANGOHUD_SLR} \
        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} \
        "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]} &>>"${PW_LOG_TO_FILE}"
    else
        if [[ "${PW_USE_TERMINAL}" == "1" ]] ; then
            export PW_ADD_TO_ARGS_IN_RUNTIME="--xterm"
            pw_init_runtime
        fi
        ${optirun_on} ${PW_GAMEMODERUN_SLR} ${pw_runtime} \
        env ${PW_MANGOHUD_SLR} \
        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}" \
        "${WINELOADER}" "$@" ${LAUNCH_PARAMETERS[@]}
    fi
}
export -f pw_run

###MAIN###
if [[ $(id -u) -eq 0 ]] ; then
    zenity_error "Do not run the script from the superuser!"
    exit 1
fi
[[ -z "${LANG}" ]] && export LANG=C

create_new_dir "${HOME}/.local/share/applications"
if [[ ! -z "${PW_SILENT_RESTART}" ]] && [[ "${PW_SILENT_RESTART}" = 1 ]] ; then
    export PW_GUI_DISABLED_CS=1
    unset PW_SILENT_RESTART
else
    unset PW_GUI_DISABLED_CS
fi
unset MANGOHUD MANGOHUD_DLSYM PW_NO_ESYNC PW_NO_FSYNC PW_VULKAN_USE WINEDLLOVERRIDES PW_NO_WRITE_WATCH PW_YAD_SET PW_ICON_FOR_YAD
unset PW_CHECK_AUTOINSTAL PW_VKBASALT_EFFECTS PW_VKBASALT_FFX_CAS PORTWINE_DB PORTWINE_DB_FILE PW_DISABLED_CREATE_DB
unset CHK_SYMLINK_FILE MESA_GL_VERSION_OVERRIDE PATH_TO_GAME PW_START_DEBUG PORTPROTON_NAME PORTWINE_CREATE_SHORTCUT_NAME
unset PW_PREFIX_NAME WINEPREFIX VULKAN_MOD PW_WINE_VER PW_ADD_TO_ARGS_IN_RUNTIME PW_GAMEMODERUN_SLR AMD_VULKAN_ICD

export portname=PortProton

cd "$(dirname "`readlink -f "$0"`")" && export PORT_SCRIPTS_PATH="$(pwd)"
cd "${PORT_SCRIPTS_PATH}/../../" && export PORT_WINE_PATH="$(pwd)"
export PORT_WINE_TMP_PATH="${PORT_WINE_PATH}/data/tmp"
rm -f "${PORT_WINE_TMP_PATH}"/*.tar.*

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

create_new_dir "${PORT_WINE_PATH}/data/prefixes"
create_new_dir "${PORT_WINE_PATH}/data/prefixes/DOTNET"
try_force_link_dir "${PORT_WINE_PATH}/data/prefixes" "${PORT_WINE_PATH}"

orig_IFS="$IFS"
IFS=$'\n'
for pfx_dir in $(ls -1 "${PORT_WINE_PATH}/data/prefixes/") ; do
    pfx_dir_new=`echo "${pfx_dir}" | awk '$1=$1' | sed -e s/[[:blank:]]/_/g`
    if [[ ! -d "${PORT_WINE_PATH}/data/prefixes/${pfx_dir_new^^}" ]] ; then
        mv -- "${PORT_WINE_PATH}/data/prefixes/$pfx_dir" "${PORT_WINE_PATH}/data/prefixes/${pfx_dir_new^^}"
    fi
done
IFS="$orig_IFS"

create_new_dir "${PORT_WINE_TMP_PATH}"/gecko
create_new_dir "${PORT_WINE_TMP_PATH}"/mono

export PW_VULKAN_DIR="${PORT_WINE_TMP_PATH}/VULKAN"
create_new_dir "${PW_VULKAN_DIR}"

cd "${PORT_SCRIPTS_PATH}"
. "${PORT_SCRIPTS_PATH}/var"

export PW_PLUGINS_PATH="${PORT_WINE_TMP_PATH}/plugins${PW_PLUGINS_VER}"  

. "${PORT_SCRIPTS_PATH}"/lang
. "${PORT_SCRIPTS_PATH}"/zen_yad_gui

export urlg="https://linux-gaming.ru/portproton/"
export PW_WINELIB="${PORT_WINE_TMP_PATH}/libs${PW_LIBS_VER}"
try_remove_dir "${PW_WINELIB}/var"
export PW_GUI_ICON_PATH="${PORT_WINE_PATH}/data/img/gui"
export install_ver=`cat "${PORT_WINE_TMP_PATH}/${portname}_ver" | head -n 1`
export WINETRICKS_DOWNLOADER="curl"
export USER_CONF="${PORT_WINE_PATH}/data/user.conf"
check_user_conf
# sed -i 's/ PP_/ PW_/g' "${USER_CONF}"
if [[ ! -d "${HOME}/PortWINE" ]] && [[ -d  "${HOME}/.local/share/PortWINE/PortProton/" ]] ; then
    ln -s "${HOME}/.local/share/PortWINE/" "${HOME}"
fi

if [[ -z "${INSTALLING_PORT}" ]] ; then
    . "${USER_CONF}"
    pw_port_update
    pw_download_libs
    pw_check_and_download_wine ${PW_PROTON_LG_VER}
    pw_check_and_download_plugins
    pw_init_db
    . "${PORT_SCRIPTS_PATH}"/lang
    pw_check_and_download_dxvk_and_vkd3d
    . "${USER_CONF}"
fi
