#!/bin/sh set -e DOTFILES_REPO="https://github.com/Jamminroot/.dotfiles.git" DOTFILES_DIR="$HOME/.dotfiles" NVIM_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/nvim" # --- Helpers --- info() { printf '\033[1;34m[dotf]\033[0m %s\n' "$*"; } ok() { printf '\033[1;32m[dotf]\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m[dotf]\033[0m %s\n' "$*"; } fail() { printf '\033[1;31m[dotf]\033[0m %s\n' "$*"; } has() { command -v "$1" >/dev/null 2>&1; } try_sudo() { if [ "$(id -u)" -eq 0 ]; then "$@" elif has sudo && sudo -n true 2>/dev/null; then sudo "$@" elif has sudo; then sudo "$@" else return 1 fi } # --- Install neovim --- install_nvim() { if has nvim; then ok "neovim already installed: $(nvim --version | head -1)" return 0 fi info "Installing neovim..." # Try package manager with sudo first if has apt-get; then try_sudo apt-get update -qq && try_sudo apt-get install -y -qq neovim && return 0 elif has dnf; then try_sudo dnf install -y -q neovim && return 0 elif has pacman; then try_sudo pacman -S --noconfirm --needed neovim && return 0 elif has zypper; then try_sudo zypper install -y neovim && return 0 elif has apk; then try_sudo apk add --no-cache neovim && return 0 elif has brew; then brew install neovim && return 0 elif has nix-env; then nix-env -iA nixpkgs.neovim && return 0 fi # Fallback: prebuilt binary to ~/.local (no root needed) info "Package manager install failed or unavailable, trying prebuilt binary..." ARCH=$(uname -m) OS=$(uname -s) if [ "$OS" = "Darwin" ]; then URL="https://github.com/neovim/neovim/releases/latest/download/nvim-macos-${ARCH}.tar.gz" elif [ "$OS" = "Linux" ]; then case "$ARCH" in x86_64) URL="https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.tar.gz" ;; aarch64) URL="https://github.com/neovim/neovim/releases/latest/download/nvim-linux-aarch64.tar.gz" ;; *) fail "Unsupported architecture: $ARCH"; return 1 ;; esac else fail "Unsupported OS: $OS" return 1 fi TMPDIR=$(mktemp -d) if has curl; then curl -fsSL "$URL" -o "$TMPDIR/nvim.tar.gz" elif has wget; then wget -qO "$TMPDIR/nvim.tar.gz" "$URL" else fail "Neither curl nor wget found" return 1 fi mkdir -p "$HOME/.local" tar xzf "$TMPDIR/nvim.tar.gz" -C "$HOME/.local" --strip-components=1 rm -rf "$TMPDIR" # Ensure ~/.local/bin is in PATH case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) export PATH="$HOME/.local/bin:$PATH" for rc in "$HOME/.bashrc" "$HOME/.zshrc"; do if [ -f "$rc" ]; then grep -q '.local/bin' "$rc" 2>/dev/null || echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$rc" fi done ;; esac if has nvim; then ok "neovim installed: $(nvim --version | head -1)" else fail "Could not install neovim" return 1 fi } # --- Dotfiles --- sync_dotfiles() { if [ -d "$DOTFILES_DIR/.git" ]; then info "Dotfiles found, pulling latest..." git -C "$DOTFILES_DIR" pull --ff-only || git -C "$DOTFILES_DIR" pull --rebase ok "Dotfiles updated" else info "Cloning dotfiles..." if [ -e "$DOTFILES_DIR" ]; then BACKUP="$DOTFILES_DIR.backup.$(date +%Y%m%d%H%M%S)" warn "Moving existing $DOTFILES_DIR to $BACKUP" mv "$DOTFILES_DIR" "$BACKUP" fi git clone "$DOTFILES_REPO" "$DOTFILES_DIR" ok "Dotfiles cloned" fi } # --- Symlink --- link_config() { if [ -L "$NVIM_CONFIG" ]; then rm "$NVIM_CONFIG" elif [ -e "$NVIM_CONFIG" ]; then BACKUP="$NVIM_CONFIG.backup.$(date +%Y%m%d%H%M%S)" warn "Backing up existing nvim config to $BACKUP" mv "$NVIM_CONFIG" "$BACKUP" fi mkdir -p "$(dirname "$NVIM_CONFIG")" ln -sf "$DOTFILES_DIR/nvim" "$NVIM_CONFIG" ok "Linked $DOTFILES_DIR/nvim -> $NVIM_CONFIG" } # --- Plugins --- sync_plugins() { if has nvim; then info "Syncing plugins..." nvim --headless "+Lazy! sync" +qa 2>/dev/null || true ok "Plugins synced" fi } # --- Main --- main() { echo "" info "dotf - dotfiles bootstrap" echo "" if ! has git; then fail "git is required but not found" exit 1 fi install_nvim sync_dotfiles link_config sync_plugins echo "" ok "All done! Run 'nvim' to get started." echo "" } main