#!/usr/bin/env bash
#Author: PortWINE
#EALauncher.exe
#EADesktop.exe
#Rating=5
#####################examples###########################
export PW_DLL_INSTALL="vcrun2019"               # Install DDL in port prefix (used winetricks) 
export PW_VULKAN_USE=1                       # dxvk, vkd3d or 0 for OpenGL
# export PW_USE_TERMINAL="1"
export PW_WINE_TKG_VER="7.20"
export PW_WINE_USE="WINE-${PW_WINE_TKG_VER}-STAGING-TKG-AMD64"
export PW_MUST_HAVE_DLL=""
export PW_PREFIX_NAME="EA_APP"
# export WINEDLLOVERRIDES="mscoree,mshtml="

check_download_wine_ver_for_ea () {
    if [ ! -d "${PORT_WINE_PATH}/data/dist/${PW_WINE_USE}" ] ; then
        if try_download "https://github.com/Kron4ek/Wine-Builds/releases/download/${PW_WINE_TKG_VER}/wine-${PW_WINE_TKG_VER}-staging-tkg-amd64.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
}
check_download_wine_ver_for_ea

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
