From 4469e984c78f1c83aeeb84d97c44753166293c69 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 15 Feb 2024 09:34:27 +0100
Subject: [PATCH] RYUJINX: added a function to check for keys

---
 functions/functions.sh | 37 +++++++++++++++++++++++++++++++++++++
 retrodeck.sh           |  3 +++
 2 files changed, 40 insertions(+)

diff --git a/functions/functions.sh b/functions/functions.sh
index 98768855..5f66cbb5 100644
--- a/functions/functions.sh
+++ b/functions/functions.sh
@@ -559,6 +559,43 @@ easter_eggs() {
   cp -f "$new_splash_file" "$current_splash_file" # Deploy assigned splash screen
 }
 
+manage_ryujinx_keys() {
+  # This function checks if Switch keys are existing and symlinks them inside the Ryujinx system folder
+  # If the symlinks are broken it recreates them
+
+  echo "Checking Ryujinx Switch keys." #TODO logging
+  ryujinx_system="/var/config/Ryujinx/system"  # Set the path to the Ryujinx system folder
+  # Check if the keys folder exists
+  if [ -d "$bios_folder/switch/keys" ]; then
+      # Check if there are files in the keys folder
+      if [ -n "$(find "$bios_folder/switch/keys" -maxdepth 1 -type f)" ]; then
+          # Iterate over each file in the keys folder
+          for file in "$bios_folder/switch/keys"/*; do
+              filename=$(basename "$file")
+              symlink="$ryujinx_system/$filename"
+              
+              # Check if the symlink exists and is valid
+              if [ -L "$symlink" ] && [ "$(readlink -f "$symlink")" = "$file" ]; then
+                  echo "Found \"$symlink\" and it's a valid symlink." #TODO logging
+                  continue  # Skip if the symlink is already valid
+              fi
+              
+              # Remove broken symlink or non-symlink file
+              echo "Found \"$symlink\" but it's not a valid symlink. Repeiring it" #TODO logging
+              [ -e "$symlink" ] && rm "$symlink"
+
+              # Create symlink
+              ln -s "$file" "$symlink"
+              echo "Created symlink: \"$symlink\""
+          done
+      else
+          echo "No files found in $bios_folder/switch/keys. Continuing" #TODO logging
+      fi
+  else
+      echo "Directory $bios_folder/switch/keys does not exist. Maybe Ryujinx was never run. Continuing" #TODO logging
+  fi
+}
+
 quit_retrodeck() {
   pkill -f retrodeck
   pkill -f emulationstation
diff --git a/retrodeck.sh b/retrodeck.sh
index 97934a43..bc18205a 100644
--- a/retrodeck.sh
+++ b/retrodeck.sh
@@ -182,6 +182,9 @@ if [[ -f "$HOME/.steam/steam/controller_base/templates/RetroDECK_controller_conf
 fi
 # REMOVE BEFORE NEXT VERSION RELEASE
 
+# Linking switch keys for Ryujinx
+manage_ryujinx_keys
+
 # Normal Startup
 
 start_retrodeck