Files
dotfiles/uninstall.sh
T
2026-08-12 15:57:01 +00:00

42 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" 2>/dev/null && pwd -P || true)"
if [[ -n "$SCRIPT_DIR" && -e "$SCRIPT_DIR/.git" ]]; then
APP_PATH="${APP_PATH:-$SCRIPT_DIR}"
else
APP_PATH="${APP_PATH:-$HOME/.dotfiles}"
fi
readonly APP_PATH
readonly MANAGED_START='# >>> dotfiles >>>'
readonly MANAGED_END='# <<< dotfiles <<<'
remove_managed_link() {
local target=$1 expected=$2
if [[ -L "$target" && "$(readlink "$target")" == "$expected" ]]; then
rm -- "$target"
printf 'Removed %s\n' "$target"
fi
}
remove_managed_block() {
local file=$1 temp
[[ -f "$file" ]] || return
temp=$(mktemp)
awk -v start="$MANAGED_START" -v end="$MANAGED_END" '
$0 == start { managed = 1; next }
$0 == end { managed = 0; next }
!managed { print }
' "$file" > "$temp"
cat "$temp" > "$file"
rm -f "$temp"
}
remove_managed_link "$HOME/.vimrc" "$APP_PATH/.vimrc"
remove_managed_link "$HOME/.tmux.conf" "$APP_PATH/.tmux.conf"
remove_managed_block "$HOME/.bashrc"
printf '%s\n' 'Dotfile links and the managed Bash block were removed.'
printf '%s\n' 'The repository, Vim data/plugins, TPM, and backups were retained.'