#!/usr/bin/env bash set -euo pipefail RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' info() { echo -e "${GREEN}[+]${NC} $*"; } warn() { echo -e "${YELLOW}[!]${NC} $*"; } die() { echo -e "${RED}[✗] FATAL:${NC} $*" >&2; exit 1; } install_service() { local uv_bin uv_bin=$(command -v uv) local api_port api_port=$(grep -E '^API_PORT=' .env 2>/dev/null | cut -d '=' -f2) api_port=${api_port:-3671} mkdir -p ~/.config/systemd/user cat > ~/.config/systemd/user/theplug.service </dev/null || true fi # ── 3. spin up dependencies ─────────────────────────────────────────────────── info "Starting Redis and MongoDB..." docker compose up -d redis mongodb || die "docker compose failed — are Redis/MongoDB defined in compose.yml?" # ── 4. ensure uv is available ───────────────────────────────────────────────── if ! command -v uv &>/dev/null; then warn "uv not found — installing..." curl -LsSf https://astral.sh/uv/install.sh | sh || die "uv installation failed" export PATH="$HOME/.local/bin:$PATH" command -v uv &>/dev/null || die "uv installed but still not in PATH — open a new shell and retry" info "uv installed: $(uv --version)" fi # ── 5. venv + deps ──────────────────────────────────────────────────────────── info "Pinning Python 3.14..." uv python pin 3.14 info "Creating venv..." uv venv info "Syncing dependencies..." uv sync # ── 6. install service + start ──────────────────────────────────────────────── install_service systemctl --user start theplug echo -e "" info "Automatically generated Secrets:" info " MONGO_PASS: $MONGO_PASS" info " JWT_SECRET: $JWT_SECRET" info " AUTH_PASS: $AUTH_PASS - questa è importante" warn "Save them — these won't be showed again (non è vero basta che leggi $PLUG_DIR/.env)" echo -e "" info "Installed thePlug in $PLUG_DIR" warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs" echo -e ""