#!/usr/bin/env bash # cgit installer set -euo pipefail CGIT_SERVER="https://git.sourcemod.lol" CGIT_URL="${CGIT_SERVER}/api.php?a=client" if [ -t 1 ]; then R='\033[0;31m' G='\033[0;32m' Y='\033[1;33m' B='\033[0;34m' BOLD='\033[1m' NC='\033[0m' else R='' G='' Y='' B='' BOLD='' NC='' fi ok() { printf "${G}✓${NC} %s\n" "$*"; } info() { printf "${B}→${NC} %s\n" "$*"; } warn() { printf "${Y}!${NC} %s\n" "$*"; } die() { printf "${R}✗ %s${NC}\n" "$*" >&2; exit 1; } # ── Detect platform ─────────────────────────────────────────────────────────── PLATFORM="linux" if [[ "${MSYSTEM:-}" == MINGW* || "${MSYSTEM:-}" == MSYS* || \ "${OSTYPE:-}" == "msys" || "${OSTYPE:-}" == "cygwin" ]]; then PLATFORM="gitbash" elif [[ "${OSTYPE:-}" == "darwin"* ]]; then PLATFORM="mac" elif grep -qi microsoft /proc/version 2>/dev/null; then PLATFORM="wsl" fi declare -A PLATFORM_NAMES=(["gitbash"]="Windows (Git Bash)" ["wsl"]="Windows (WSL)" ["mac"]="macOS" ["linux"]="Linux") printf "\n${BOLD}cgit installer${NC} · %s\n\n" "${PLATFORM_NAMES[$PLATFORM]}" # ── Requirements ────────────────────────────────────────────────────────────── info "Checking requirements..." [[ "${BASH_VERSION%%.*}" -ge 4 ]] || { [[ "$PLATFORM" == "mac" ]] && die "bash 4+ required. Run: brew install bash" [[ "$PLATFORM" == "gitbash" ]] && die "bash 4+ required. Update Git for Windows: https://git-scm.com" die "bash 4+ required (you have $BASH_VERSION)" } command -v python3 &>/dev/null || die "python3 not found → https://python.org/downloads" command -v curl &>/dev/null || die "curl not found" ok "Requirements satisfied" # ── Download ────────────────────────────────────────────────────────────────── info "Downloading cgit..." TMP=$(mktemp) trap 'rm -f "$TMP"' EXIT curl -fsSL "$CGIT_URL" -o "$TMP" || die "Download failed — is $CGIT_SERVER reachable?" head -1 "$TMP" | grep -q "env bash\|bin/bash\|bin/sh" || die "Downloaded file doesn't look right" chmod +x "$TMP" ok "Downloaded ($(wc -c < "$TMP" | tr -d ' ') bytes)" # ── Install ─────────────────────────────────────────────────────────────────── info "Installing..." INSTALL_DIR="" try_install() { local dir="$1" if [[ -d "$dir" && -w "$dir" ]]; then cp "$TMP" "$dir/cgit" INSTALL_DIR="$dir" return 0 fi return 1 } case "$PLATFORM" in gitbash) # Git Bash automatically adds ~/bin to PATH when the directory exists. # This is the safest and cleanest location on Windows — no PATH editing needed. mkdir -p "$HOME/bin" cp "$TMP" "$HOME/bin/cgit" INSTALL_DIR="$HOME/bin" ;; mac|linux|wsl) try_install /usr/local/bin \ || { command -v sudo &>/dev/null && sudo cp "$TMP" /usr/local/bin/cgit && INSTALL_DIR=/usr/local/bin; } \ || try_install /usr/bin \ || { mkdir -p "$HOME/.local/bin" && try_install "$HOME/.local/bin"; } \ || die "No writable install location found" ;; esac INSTALL_PATH="$INSTALL_DIR/cgit" ok "Installed to $INSTALL_PATH" # ── PATH ───────────────────────────────────────────────────────────────────── in_path() { [[ ":${PATH}:" == *":${1}:"* ]]; } if in_path "$INSTALL_DIR"; then ok "$INSTALL_DIR is already on PATH" else # Pick config file case "$PLATFORM" in gitbash) # ~/bin is added by Git Bash's /etc/profile automatically — no config edit needed. # Just need a new terminal. But we add it anyway in case this Git Bash version skips it. RC="$HOME/.bash_profile" ;; mac) RC="${HOME}/$( [[ "$SHELL" == */zsh ]] && echo .zshrc || echo .bash_profile )" ;; wsl|linux) RC="$HOME/.bashrc" [[ -f "$HOME/.bash_profile" && ! -f "$HOME/.bashrc" ]] && RC="$HOME/.bash_profile" ;; esac # Write to config if not already there if ! grep -qF "$INSTALL_DIR" "$RC" 2>/dev/null; then touch "$RC" printf '\n# cgit\nexport PATH="%s:$PATH"\n' "$INSTALL_DIR" >> "$RC" ok "Added $INSTALL_DIR to PATH in $RC" fi # Make it available right now in this process too export PATH="$INSTALL_DIR:$PATH" fi # ── Verify ──────────────────────────────────────────────────────────────────── # Use 'bash path/to/cgit' instead of running directly — avoids shebang issues on Windows if bash "$INSTALL_PATH" help &>/dev/null; then ok "cgit works" else warn "Installed but verification failed — check that python3 is available" fi # ── Done ────────────────────────────────────────────────────────────────────── printf "\n${G}${BOLD}Done!${NC}\n\n" if [[ "$PLATFORM" == "gitbash" ]]; then printf " ${Y}Open a new Git Bash window${NC} — cgit will be on your PATH.\n" printf " (Git Bash adds ~/bin to PATH automatically on startup)\n\n" elif [[ "$PLATFORM" == "wsl" || "$PLATFORM" == "linux" ]]; then in_path "$INSTALL_DIR" || printf " Run: ${BOLD}source %s${NC}\n\n" "$RC" elif [[ "$PLATFORM" == "mac" ]]; then in_path "$INSTALL_DIR" || printf " Run: ${BOLD}source %s${NC}\n\n" "$RC" fi printf " cgit clone %s/REPONAME\n" "$CGIT_SERVER" printf " cgit help\n\n"