#!/bin/sh set -e REPO_URL_SSH="git@github.com:h80r/dotfiles.git" DOTFILES_DIR="$HOME/Code/dotfiles" # ── Colors ──────────────────────────────────────────────────────────────────── RESET='\033[0m' BOLD='\033[1m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' RED='\033[0;31m' info() { printf "${BLUE} →${RESET} %s\n" "$1"; } success() { printf "${GREEN} ✓${RESET} %s\n" "$1"; } skip() { printf "${YELLOW} ⊘${RESET} %s (já instalado)\n" "$1"; } section() { printf "\n${BOLD}${BLUE}▶ %s${RESET}\n" "$1"; } error() { printf "${RED} ✗${RESET} %s\n" "$1" >&2; exit 1; } # ── Helpers ─────────────────────────────────────────────────────────────────── is_installed() { command -v "$1" > /dev/null 2>&1; } # ── Platform detection ──────────────────────────────────────────────────────── OS="$(uname -s)" case "$OS" in Darwin) PLATFORM="macos" ;; Linux) PLATFORM="linux" ;; *) error "Plataforma não suportada: $OS" ;; esac # ── Linux: atualizar lista de pacotes ───────────────────────────────────────── if [ "$PLATFORM" = "linux" ]; then info "atualizando lista de pacotes (apt)..." sudo apt-get update -qq fi # ── 0. Chave SSH ────────────────────────────────────────────────────────────── section "Chave SSH" if [ -f "$HOME/.ssh/id_ed25519" ]; then skip "chave SSH ~/.ssh/id_ed25519" else info "gerando chave SSH ed25519..." mkdir -p "$HOME/.ssh" chmod 700 "$HOME/.ssh" ssh-keygen -t ed25519 -C "h80rgbc@gmail.com" -f "$HOME/.ssh/id_ed25519" -N "" success "chave SSH gerada" info "iniciando ssh-agent..." eval "$(ssh-agent -s)" case "$PLATFORM" in macos) SSH_CONFIG="$HOME/.ssh/config" if [ ! -f "$SSH_CONFIG" ] || ! grep -q "Host github.com" "$SSH_CONFIG"; then info "configurando ~/.ssh/config para GitHub..." cat >> "$SSH_CONFIG" << 'EOF' Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519 Host github.com IgnoreUnknown UseKeychain EOF chmod 600 "$SSH_CONFIG" success "~/.ssh/config configurado" else skip "~/.ssh/config (bloco github.com já existe)" fi ssh-add --apple-use-keychain "$HOME/.ssh/id_ed25519" ;; linux) ssh-add "$HOME/.ssh/id_ed25519" ;; esac info "copiando chave pública para a área de transferência..." case "$PLATFORM" in macos) pbcopy < "$HOME/.ssh/id_ed25519.pub" success "chave pública copiada (pbcopy)" ;; linux) if ! is_installed xclip; then info "instalando xclip..." sudo apt-get install -y xclip success "xclip instalado" fi xclip -sel clip < "$HOME/.ssh/id_ed25519.pub" success "chave pública copiada (xclip)" ;; esac info "abrindo GitHub para adicionar a chave SSH..." case "$PLATFORM" in macos) open "https://github.com/settings/ssh/new" ;; linux) xdg-open "https://github.com/settings/ssh/new" 2>/dev/null || true ;; esac printf "\n ${BOLD}Pressione Enter quando terminar de adicionar a chave SSH no GitHub...${RESET}\n" read -r _ < /dev/tty info "testando conexão SSH com GitHub..." ssh_output="$(ssh -o StrictHostKeyChecking=accept-new -T git@github.com 2>&1 || true)" if printf '%s\n' "$ssh_output" | grep -q "successfully authenticated"; then success "conexão SSH com GitHub verificada" else error "falha na autenticação SSH com GitHub: $ssh_output" fi fi # ── 1. git ──────────────────────────────────────────────────────────────────── section "git" if is_installed git; then skip "git" else case "$PLATFORM" in macos) info "instalando Xcode Command Line Tools (inclui git)..." xcode-select --install 2>/dev/null || true until is_installed git; do info "aguardando instalação do git... (conclua o diálogo e pressione Enter)" read -r _ < /dev/tty done success "git instalado" ;; linux) info "instalando git..." sudo apt-get install -y git success "git instalado" ;; esac fi # ── 2. Homebrew ─────────────────────────────────────────────────────────────── section "Homebrew" if [ "$PLATFORM" = "linux" ]; then skip "Homebrew (Linux usa apt)" elif is_installed brew; then skip "brew" else info "instalando Homebrew..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" success "Homebrew instalado" fi # ── 3. Clone do repo de dotfiles ───────────────────────────────────────────── section "Dotfiles" if [ -d "$DOTFILES_DIR/.git" ]; then skip "repo já clonado em $DOTFILES_DIR" info "atualizando..." git -C "$DOTFILES_DIR" pull --ff-only else info "clonando $REPO_URL_SSH em $DOTFILES_DIR..." mkdir -p "$HOME/Code" git clone "$REPO_URL_SSH" "$DOTFILES_DIR" success "repo clonado" fi # ── Handoff ─────────────────────────────────────────────────────────────────── case "$PLATFORM" in macos) exec sh "$DOTFILES_DIR/scripts/mac-setup.sh" ;; linux) exec sh "$DOTFILES_DIR/scripts/linux-setup.sh" ;; esac