From 8df2bb6ff6d1346f2db03d31ba26e8e57401adba Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Wed, 4 Jan 2023 20:19:30 -0800 Subject: [PATCH] Remove invalid dotfile symlinks during setup --- setup.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/setup.sh b/setup.sh index 0f62ebf..b082bf2 100755 --- a/setup.sh +++ b/setup.sh @@ -28,6 +28,27 @@ while IFS= read -r -d $'\0'; do copy_files+=( "${REPLY//$SRC_DIR\//}" ) done < <(find "$SRC_DIR" -type f -print0) +# clear symlinks pointing to non-existent src files +symlink_check_dirs=() +while IFS= read -r -d $'\0'; do + symlink_check_dirs+=( "$(sed -r -e "s|^${SRC_DIR}/?||" <<< "$REPLY")" ) +done < <(find "$SRC_DIR" -type d -print0) +for dir in "${symlink_check_dirs[@]}"; do + while IFS= read -r -d $'\0'; do + # it is possible for `readlink` to fail if the link target does not + # exist (within multiple non-existent subdirectories?); such entries + # aren't valid to check anyway, skip them + link_target="$(readlink -f "$REPLY")" || continue + + # if a symlink is found which points to a SRC_DIR file but there's no + # file that it's pointing at, it's an old and outdated link; delete it + if [[ "$link_target" =~ ^"$SRC_DIR" && ! -f "$link_target" ]]; then + echo "Removing dead dotfiles symlink: '$REPLY'" + rm "$REPLY" + fi + done < <(find "$HOME/$dir" -maxdepth 1 -type l -print0) +done + if [ $remove -eq 1 ]; then for file in "${copy_files[@]}"; do if [ -L "$file" ]; then