From eb76958daa30532cd4cd5daaf2ae95e099453eeb Mon Sep 17 00:00:00 2001
From: Ivan Malison <IvanMalison@gmail.com>
Date: Wed, 9 Oct 2024 11:29:28 -0600
Subject: [PATCH] Allow multiple backup files to be created by appending a
 counter to the backup file path

---
 modules/files/check-link-targets.sh | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/modules/files/check-link-targets.sh b/modules/files/check-link-targets.sh
index 215ff6c37..033d3b236 100644
--- a/modules/files/check-link-targets.sh
+++ b/modules/files/check-link-targets.sh
@@ -33,12 +33,16 @@ for sourcePath in "$@" ; do
     elif [[ ! -L "$targetPath" && -n "$HOME_MANAGER_BACKUP_EXT" ]] ; then
       # Next, try to move the file to a backup location if configured and possible
       backup="$targetPath.$HOME_MANAGER_BACKUP_EXT"
-      if [[ -e "$backup" ]]; then
-        errorEcho "Existing file '$backup' would be clobbered by backing up '$targetPath'"
-        collision=1
-      else
-        warnEcho "Existing file '$targetPath' is in the way of '$sourcePath', will be moved to '$backup'"
-      fi
+
+      # Check for backup existence and iterate until finding an available filename
+      counter=1
+      while [[ -e "$backup" ]]; do
+        backup="$targetPath.$HOME_MANAGER_BACKUP_EXT.$counter"
+        counter=$((counter + 1))
+      done
+
+      warnEcho "Existing file '$targetPath' is in the way of '$sourcePath', will be moved to '$backup'"
+      mv "$targetPath" "$backup"
     else
       # Fail if nothing else works
       errorEcho "Existing file '$targetPath' is in the way of '$sourcePath'"