install.sh

#!/usr/bin/env bash
# Idempotent installer for my stow-managed dotfiles.
# See mercemay.top/src/dotfiles/ for the long version.
set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$here"

packages=(zsh tmux git nvim)

require() {
	command -v "$1" >/dev/null 2>&1 || {
		echo "missing: $1" >&2
		exit 1
	}
}

require stow
require git

ensure_parent_dirs() {
	# stow will create missing parent dirs, but does *not* create
	# $HOME/.config, and gets confused if the target subdir doesn't exist.
	mkdir -p "$HOME/.config"
	mkdir -p "$HOME/.cache/zsh"
}

ensure_parent_dirs

for pkg in "${packages[@]}"; do
	[[ -d "$pkg" ]] || { echo "skip $pkg (no such dir)"; continue; }

	# Dry run first: stow -n prints conflicts without touching anything.
	if ! stow -n -v -R -t "$HOME" "$pkg" >/dev/null 2>&1; then
		echo "conflicts detected for $pkg:"
		stow -n -v -R -t "$HOME" "$pkg" 2>&1 | grep -E '^(\* |LINK|UNLINK)' || true
		read -r -p "overwrite existing files in \$HOME? [y/N] " ans
		case "$ans" in
			[yY]*) ;;
			*) echo "skipping $pkg"; continue ;;
		esac
		stow --adopt -v -R -t "$HOME" "$pkg"
		echo "adopted existing files; review with git diff"
	else
		stow -v -R -t "$HOME" "$pkg"
	fi
done

echo "done. open a new shell to pick up new config."