From ef62c23adcaf87544a44323227c9048b0f5fc536 Mon Sep 17 00:00:00 2001
From: Tio TROM <tio@trom.tf>
Date: Thu, 3 Aug 2023 18:50:55 +0200
Subject: [PATCH] continous checking

The previous script stopped working. Only at times it synced when you were to install a theme. The above code works all the time but uses a constant "checking" of the folders. What do you think about it? Courtesy of chatgpt hahaha
---
 bin/periodic/fix-flatpaks-theming | 41 ++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/bin/periodic/fix-flatpaks-theming b/bin/periodic/fix-flatpaks-theming
index 5c95b56..64a7fd9 100755
--- a/bin/periodic/fix-flatpaks-theming
+++ b/bin/periodic/fix-flatpaks-theming
@@ -1,14 +1,37 @@
 #!/bin/bash
+
+# Set the directories
 shared_themes_dir=/usr/share/themes/
-home_local_themes_dir="$HOME/.local/share/themes/"
-source_theme_dirs=("$shared_themes_dir" "$home_local_themes_dir")
-target_themes_dir="$HOME/.themes"
+local_themes_dir="$HOME/.themes/"
 
-mkdir -p "$target_themes_dir" "$home_local_themes_dir" ||
-	{ echo "failed to make directoris $target_themes_dir & $home_local_themes_dir"; exit 1; }
+# Function to synchronize themes from shared to local directory
+sync_themes() {
+    rsync -av --delete --progress "$shared_themes_dir" "$local_themes_dir"
+    echo "Directory $local_themes_dir is synchronized with $shared_themes_dir"
+}
 
-echo "Set watch for" "${source_theme_dirs[@]}" "directories ..."
-while inotifywait -qr -e 'modify,attrib,move,move_self,create,delete,delete_self,unmount' "${source_theme_dirs[@]}"; do
-	rsync -av --delete --progress "${source_theme_dirs[@]}" "$target_themes_dir"
-	echo "Directory $target_themes_dir is synchronized with" "${source_theme_dirs[@]}"
+# Initial synchronization
+sync_themes
+
+# Function to monitor shared themes directory
+monitor_shared_themes() {
+    inotifywait -m -r -e 'modify,attrib,move,move_self,create,delete,delete_self,unmount' "$shared_themes_dir" > /dev/null 2>&1 &
+}
+
+# Function to monitor local themes directory
+monitor_local_themes() {
+    inotifywait -m -r -e 'modify,attrib,move,move_self,create,delete,delete_self,unmount' "$local_themes_dir" > /dev/null 2>&1 &
+}
+
+# Start monitoring in the background
+monitor_shared_themes
+monitor_local_themes
+
+# Monitor changes and sync periodically
+while true; do
+    # Wait for a short period (e.g., 5 seconds) before syncing again
+    sleep 5
+
+    # Synchronize if there were changes in either directory
+    sync_themes
 done