From 2f9f705dd48bcdae8d45c580d820b475a894680e Mon Sep 17 00:00:00 2001
From: Htylol <dixperm909@yandex.ru>
Date: Sat, 28 Sep 2024 05:15:26 +0500
Subject: [PATCH] Added background_pid

---
 data_from_portwine/scripts/functions_helper | 37 ++++++++++++++++-----
 data_from_portwine/scripts/start.sh         |  4 +--
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/data_from_portwine/scripts/functions_helper b/data_from_portwine/scripts/functions_helper
index 2213d1d5..4cf43ce8 100755
--- a/data_from_portwine/scripts/functions_helper
+++ b/data_from_portwine/scripts/functions_helper
@@ -675,6 +675,31 @@ check_flatpak () {
 }
 export -f check_flatpak
 
+background_pid () {
+    local arg1=$1 # --start или --end
+    local arg2=$2 # Название команды
+    local arg3=$3 # Номер процесса (1,2,3..)
+    get_bg_pid () {
+        eval "echo \${$1}"
+    }
+
+    if [[ "$arg1" == --start ]] ; then
+        eval "$arg2 &"
+        PID=$!
+        export bg_pid"${arg3}"="$PID"
+    elif [[ "$arg1" == --end ]] ; then
+        PID=$(get_bg_pid bg_pid"${arg3}")
+        [[ $PID == "" ]] && return 1
+        while true ; do
+            if [[ ! $(jobs -p) =~ $PID ]] ; then
+                return 0
+            fi
+            sleep 0.005
+        done
+    fi
+}
+export -f background_pid
+
 unpack () {
     case $1 in
          *.tar.xz) local command="tar -Jxhf";;
@@ -1952,11 +1977,7 @@ pw_skip_update () {
 
 pw_skip_update_new () {
     if [[ "${SKIP_CHECK_UPDATES_NEW}" != "1" ]] ; then
-        while true ; do
-            if [[ ! $(jobs -p) =~ $PID_SKIP_UPDATE ]] ; then
-                break
-            fi
-        done
+        background_pid --end "pw_skip_update" "1"
 
         if [[ -f "${PW_TMPFS_PATH}/gamescope.tmp" ]] ; then
             export GAMESCOPE_INSTALLED="1"
@@ -5563,9 +5584,9 @@ portwine_start_debug () {
     kill_portwine
     export PW_LOG=1
     if [[ -z "$VULKAN_DRIVER_NAME" ]] || [[ "$VULKAN_DRIVER_NAME" == "llvmpipe" ]] ; then
-    pw_notify_send -i warning \
-    "${translations[Attention working version of vulkan not detected!]}" \
-    "${translations[It is recommended to run games in OpenGL (low performance possible)!]}"
+        pw_notify_send -i warning \
+        "${translations[Attention working version of vulkan not detected!]}" \
+        "${translations[It is recommended to run games in OpenGL (low performance possible)!]}"
     fi
     echo "${translations[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 "${translations[To diagnose the problem, copy ALL of the log to discord server: https://discord.gg/FTaheP99wE]}" >> "${PORT_WINE_PATH}/PortProton.log"
diff --git a/data_from_portwine/scripts/start.sh b/data_from_portwine/scripts/start.sh
index 1cebd139..ed8dc518 100755
--- a/data_from_portwine/scripts/start.sh
+++ b/data_from_portwine/scripts/start.sh
@@ -267,9 +267,7 @@ if [[ "${SKIP_CHECK_UPDATES}" != 1 ]] ; then
     PW_FILESYSTEM=$(stat -f -c %T "${PORT_WINE_PATH}")
     export PW_FILESYSTEM
 
-    pw_skip_update &
-    PID_SKIP_UPDATE=$(jobs -p)
-    export PID_SKIP_UPDATE="${PID_SKIP_UPDATE//*[[:space:]]/}"
+    background_pid --start "pw_skip_update" "1"
 fi
 
 # create lock file