diff --git a/setup.sh b/setup.sh index 1411f50..8426315 100644 --- a/setup.sh +++ b/setup.sh @@ -1,6 +1,22 @@ #!/usr/bin/env bash set -euo pipefail +# CLI: parse flags +PROMPT_DELETE=1 +while [[ $# -gt 0 ]]; do + case "$1" in + --no-prompt|-n) + PROMPT_DELETE=0 + shift + ;; + --prompt|-p) + PROMPT_DELETE=1 + shift + ;; + *) break + esac +done + echo ">>> Bootstrapping new Debian-like machine..." # 1. Update package list and install essentials @@ -23,10 +39,32 @@ dotfiles() { } echo ">>> Checking out dotfiles..." if ! dotfiles checkout; then - echo ">>> Conflicts detected! Backing up existing dotfiles..." - mkdir -p $HOME/.dotfiles-backup - dotfiles checkout 2>&1 | grep -E "\s+\." | awk '{print $1}' | \ - xargs -I{} mv {} $HOME/.dotfiles-backup/{} + echo ">>> Conflicts detected! Handling conflicting dotfiles..." + mkdir -p "$HOME/.dotfiles-backup" + + # Gather conflicts (paths with spaces supported) + conflicts=$(dotfiles checkout 2>&1 | grep -E "\s+\." | awk '{print $1}') + if [ -n "$conflicts" ]; then + if [ "$PROMPT_DELETE" = "0" ]; then + while IFS= read -r f; do + [ -z "$f" ] && continue + mv "$f" "$HOME/.dotfiles-backup/$f" + done <<< "$conflicts" + else + while IFS= read -r f; do + [ -z "$f" ] && continue + if [ -t 1 ]; then + read -r -p "Move '$f' to backup? (y/N): " resp + case "$resp" in + [Yy]* ) mv "$f" "$HOME/.dotfiles-backup/$f" ;; + * ) echo "Skipping '$f'." ;; + esac + else + echo "Non-interactive environment detected; skipping '$f'." + fi + done <<< "$conflicts" + fi + fi dotfiles checkout fi @@ -40,6 +78,4 @@ if [ ! -d ~/.tmux/plugins/tpm ]; then fi - -echo ">>> Done! Bash, tmux, vim, and dotfiles are ready." - +echo ">>> Done! Bash, tmux, vim, and dotfiles are ready."