diff --git a/update-dots.sh b/update-dots.sh new file mode 100644 index 00000000..2b6395e0 --- /dev/null +++ b/update-dots.sh @@ -0,0 +1,86 @@ +#!/bin/bash +# This script updates the dotfiles by fetching the latest version from the Git repository and then replacing files +# that have not been modified by the user to preserve changes. The remaining files will be replaced with the new ones. + +cd "$(dirname "$0")" +export base="$(pwd)" + +function get_checksum() { + # Get the checksum of a specific file + md5sum "$1" | awk '{print $1}' +} + + +# Define the folders to update +folders=(".config" ".local") + +# Then check which files have been modified since the last update +modified_files=() + +# Find all files in the specified folders and their subfolders +while IFS= read -r -d '' file; do + # Calculate checksums + base_checksum=$(get_checksum "$base/$file") + home_checksum=$(get_checksum "$HOME/$file") + # Compare checksums and add to modified_files if necessary + if [[ $base_checksum != $home_checksum ]]; then + modified_files+=("$file") + fi +done < <(find "${folders[@]}" -type f -print0) + + +echo "Modified files: ${modified_files[@]}" + +# Output all modified files +if [[ ${#modified_files[@]} -gt 0 ]]; then + echo "The following files have been modified since the last update:" + for file in "${modified_files[@]}"; do + echo "$file" + done +else + echo "No files have been modified since the last update. All files will be replaced." +fi + +# Ask if the user wants to keep them +read -p "Do you want to keep these files untouched? [y/N] " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Keeping modified files." +else + echo "Replacing all files." + modified_files=() +fi + +# Then update the repository +git pull + +# Now only replace the files that are not modified by the user +for folder in "${folders[@]}"; do + # Find all files (including those in subdirectories) and copy them + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ -f "$file" ]]; then + if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" + fi + fi + done +done + +# Add the new files, because maybe the update added new files +for folder in "${folders[@]}"; do + # Find all files (including those in subdirectories) and copy them + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ ! -f "$HOME/$file" ]]; then + echo "Adding new file: $file" + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" + fi + done +done + +