#!/usr/bin/env bash # Первичная настройка нового Linux-хоста: SSH, Gitea keys, clone Rules, init-cursor в shell profile. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONFIG_DIR="$ROOT/local" RULES_DEST="${RULES_DEST:-$HOME/CursorRules}" CLONE_URL="${CLONE_URL:-ssh://git@git.kalinamall.lan:2222/PapaTramp/Rules.git}" SKIP_CLONE="${SKIP_CLONE:-0}" SKIP_PROFILE="${SKIP_PROFILE:-0}" WHAT_IF="${WHAT_IF:-0}" log_step() { printf '[*] %s\n' "$1"; } log_ok() { printf '[+] %s\n' "$1"; } log_warn() { printf '[!] %s\n' "$1"; } read_kv_file() { local file="$1" local line key value while IFS= read -r line || [[ -n "$line" ]]; do [[ "$line" =~ ^[[:space:]]*# ]] && continue [[ "$line" != *=* ]] && continue key="${line%%=*}" value="${line#*=}" key="${key%"${key##*[![:space:]]}"}" value="${value#"${value%%[![:space:]]*}"}" printf '%s=%s\n' "$key" "$value" done < "$file" } log_step 'Bootstrap: new machine setup' user_cfg="$CONFIG_DIR/git-user.conf" if [[ -f "$user_cfg" ]]; then while IFS= read -r kv; do key="${kv%%=*}" value="${kv#*=}" case "$key" in UserName) git config --global user.name "$value"; log_ok "git user.name = $value" ;; UserEmail) git config --global user.email "$value"; log_ok "git user.email = $value" ;; esac done < <(read_kv_file "$user_cfg") fi export CONFIG_DIR WHAT_IF bash "$ROOT/scripts/setup-git-ssh.sh" bash "$ROOT/scripts/register-gitea-ssh-keys.sh" if [[ "$WHAT_IF" != '1' ]]; then log_step 'Testing git@git.kalinamall.lan' ssh -T -o BatchMode=yes -o ConnectTimeout=10 git@git.kalinamall.lan || true fi if [[ "$SKIP_CLONE" != '1' ]]; then if [[ "$WHAT_IF" == '1' ]]; then log_step "WhatIf: clone $CLONE_URL -> $RULES_DEST" elif [[ -d "$RULES_DEST/.git" ]]; then log_step "Updating $RULES_DEST" git -C "$RULES_DEST" pull --ff-only log_ok 'CursorRules updated' else [[ -e "$RULES_DEST" ]] && { echo "Path exists and is not a git repo: $RULES_DEST" >&2; exit 1; } log_step "Cloning $CLONE_URL" git clone "$CLONE_URL" "$RULES_DEST" log_ok "Cloned to $RULES_DEST" fi fi if [[ "$SKIP_PROFILE" != '1' ]]; then profile_hook="source \"$RULES_DEST/init-cursor.sh\"" for profile in "$HOME/.bashrc" "$HOME/.zshrc"; do [[ -f "$profile" ]] || continue if ! grep -Fq "$profile_hook" "$profile" 2>/dev/null; then if [[ "$WHAT_IF" == '1' ]]; then log_step "WhatIf: add to $profile" else printf '\n%s\n' "$profile_hook" >> "$profile" log_ok "Profile updated: $profile" fi else log_ok "init-cursor already in $profile" fi done fi printf '\n' log_ok 'Bootstrap complete. Open a new shell, then in a project: init-cursor'