|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-License-Identifier: GPL-3.0-only |
| 3 | +# Copyright (c) 2026 Marcus Krueger |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# === IMPORT OPTIONS === |
| 7 | +DASH_VERSION="${VERSION:-latest}" |
| 8 | +PORT="${PORT:-7847}" |
| 9 | +AUTOSTART="${AUTOSTART:-true}" |
| 10 | +USERNAME="${USERNAME:-automatic}" |
| 11 | + |
| 12 | +# Skip installation if version is "none" |
| 13 | +if [ "${DASH_VERSION}" = "none" ]; then |
| 14 | + echo "[codeforge-dashboard] Skipping installation (version=none)" |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +echo "[codeforge-dashboard] Installing @coredirective/cf-dash..." |
| 19 | + |
| 20 | +# === FIND BUN === |
| 21 | +BUN="" |
| 22 | + |
| 23 | +for candidate in \ |
| 24 | + "$(command -v bun 2>/dev/null || true)" \ |
| 25 | + "/usr/local/bin/bun" \ |
| 26 | + "/root/.bun/bin/bun" \ |
| 27 | + "/home/vscode/.bun/bin/bun"; do |
| 28 | + if [ -n "$candidate" ] && [ -x "$candidate" ]; then |
| 29 | + BUN="$candidate" |
| 30 | + break |
| 31 | + fi |
| 32 | +done |
| 33 | + |
| 34 | +if [ -z "$BUN" ]; then |
| 35 | + echo "[codeforge-dashboard] ERROR: bun is not available" |
| 36 | + echo " Checked:" |
| 37 | + echo " - PATH" |
| 38 | + echo " - /usr/local/bin/bun" |
| 39 | + echo " - /root/.bun/bin/bun" |
| 40 | + echo " - /home/vscode/.bun/bin/bun" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +# Ensure downstream commands work |
| 45 | +export PATH="$(dirname "$BUN"):$PATH" |
| 46 | + |
| 47 | +echo "[codeforge-dashboard] Using bun at: $BUN" |
| 48 | + |
| 49 | +# === DETECT USER === |
| 50 | +if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then |
| 51 | + USERNAME="" |
| 52 | + for CURRENT_USER in vscode node codespace; do |
| 53 | + if id -u "${CURRENT_USER}" >/dev/null 2>&1; then |
| 54 | + USERNAME=${CURRENT_USER} |
| 55 | + break |
| 56 | + fi |
| 57 | + done |
| 58 | + [ -z "${USERNAME}" ] && USERNAME=root |
| 59 | +elif [ "${USERNAME}" = "none" ] || ! id -u "${USERNAME}" >/dev/null 2>&1; then |
| 60 | + USERNAME=root |
| 61 | +fi |
| 62 | + |
| 63 | +echo "[codeforge-dashboard] Installing for user: ${USERNAME}" |
| 64 | + |
| 65 | +# === INSTALL FROM NPM === |
| 66 | +# Install globally — this runs as root during Docker build |
| 67 | +if [ "${DASH_VERSION}" = "latest" ]; then |
| 68 | + "$BUN" install -g @coredirective/cf-dash |
| 69 | +else |
| 70 | + "$BUN" install -g "@coredirective/cf-dash@${DASH_VERSION}" |
| 71 | +fi |
| 72 | + |
| 73 | +# === SYMLINK TO SYSTEM PATH === |
| 74 | +# bun install -g puts binaries in the current user's global bin (e.g. /root/.bun/bin/). |
| 75 | +# During Docker build this is root's dir, but at runtime the user is vscode. |
| 76 | +# Create a wrapper at /usr/local/bin/ so the binary is on everyone's PATH. |
| 77 | +BUN_GLOBAL_BIN="$("$BUN" pm bin -g 2>/dev/null || echo "$HOME/.bun/bin")" |
| 78 | +INSTALLED_BIN="${BUN_GLOBAL_BIN}/codeforge-dashboard" |
| 79 | + |
| 80 | +if [ ! -x "$INSTALLED_BIN" ]; then |
| 81 | + echo "[codeforge-dashboard] WARNING: Expected binary not found at $INSTALLED_BIN" |
| 82 | + echo " Searching for it..." |
| 83 | + INSTALLED_BIN="$(find /root/.bun "$HOME/.bun" /usr/local -name codeforge-dashboard -type f 2>/dev/null | head -1 || true)" |
| 84 | + if [ -z "$INSTALLED_BIN" ]; then |
| 85 | + echo "[codeforge-dashboard] ERROR: Could not find installed codeforge-dashboard binary" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +fi |
| 89 | + |
| 90 | +echo "[codeforge-dashboard] Installed binary found at: $INSTALLED_BIN" |
| 91 | + |
| 92 | +# Create a system-wide wrapper that resolves the real binary at runtime |
| 93 | +cat > /usr/local/bin/codeforge-dashboard <<'WRAPPER' |
| 94 | +#!/bin/bash |
| 95 | +set -euo pipefail |
| 96 | +
|
| 97 | +BUN="${BUN:-$(command -v bun 2>/dev/null || echo "$HOME/.bun/bin/bun")}" |
| 98 | +
|
| 99 | +# Check runtime user's global bin first |
| 100 | +GLOBAL_BIN="$("$BUN" pm bin -g 2>/dev/null || echo "$HOME/.bun/bin")" |
| 101 | +if [ -x "$GLOBAL_BIN/codeforge-dashboard" ] && [ "$GLOBAL_BIN/codeforge-dashboard" != "$0" ]; then |
| 102 | + exec "$GLOBAL_BIN/codeforge-dashboard" "$@" |
| 103 | +fi |
| 104 | +
|
| 105 | +# Fallback: check root's global bin (installed during Docker build) |
| 106 | +for candidate in /root/.bun/bin/codeforge-dashboard /home/*/.bun/bin/codeforge-dashboard; do |
| 107 | + if [ -x "$candidate" ] && [ "$candidate" != "$0" ]; then |
| 108 | + exec "$candidate" "$@" |
| 109 | + fi |
| 110 | +done |
| 111 | +
|
| 112 | +echo "codeforge-dashboard: not found. Install with: bun install -g @coredirective/cf-dash" >&2 |
| 113 | +exit 1 |
| 114 | +WRAPPER |
| 115 | + |
| 116 | +chmod +x /usr/local/bin/codeforge-dashboard |
| 117 | + |
| 118 | +# === VERIFY === |
| 119 | +echo "[codeforge-dashboard] Verifying installation..." |
| 120 | +if "$INSTALLED_BIN" --version &>/dev/null; then |
| 121 | + echo "[codeforge-dashboard] ✓ codeforge-dashboard command is working" |
| 122 | +else |
| 123 | + echo "[codeforge-dashboard] WARNING: Could not verify (may need bun in PATH)" |
| 124 | +fi |
| 125 | + |
| 126 | +# === AUTOSTART HOOK === |
| 127 | +if [ "${AUTOSTART}" = "true" ]; then |
| 128 | + HOOK_DIR="/usr/local/devcontainer-poststart.d" |
| 129 | + mkdir -p "$HOOK_DIR" |
| 130 | + |
| 131 | + cat > "${HOOK_DIR}/50-codeforge-dashboard.sh" <<HOOK |
| 132 | +#!/bin/bash |
| 133 | +# Auto-start CodeForge Dashboard |
| 134 | +PORT="\${CODEFORGE_DASHBOARD_PORT:-${PORT}}" |
| 135 | +
|
| 136 | +# Check if already running on the target port |
| 137 | +if command -v lsof &>/dev/null && lsof -i ":\$PORT" &>/dev/null; then |
| 138 | + echo "Dashboard already running on port \$PORT" |
| 139 | + exit 0 |
| 140 | +fi |
| 141 | +
|
| 142 | +if ! command -v codeforge-dashboard &>/dev/null; then |
| 143 | + echo "codeforge-dashboard not found in PATH, skipping auto-start" |
| 144 | + exit 0 |
| 145 | +fi |
| 146 | +
|
| 147 | +nohup codeforge-dashboard --port "\$PORT" --host 0.0.0.0 > /tmp/codeforge-dashboard.log 2>&1 & |
| 148 | +echo \$! > /tmp/codeforge-dashboard.pid |
| 149 | +echo "Dashboard started on port \$PORT (PID: \$!)" |
| 150 | +HOOK |
| 151 | + |
| 152 | + chmod +x "${HOOK_DIR}/50-codeforge-dashboard.sh" |
| 153 | + echo "[codeforge-dashboard] Poststart hook installed (auto-launch on port ${PORT})" |
| 154 | +else |
| 155 | + echo "[codeforge-dashboard] Autostart disabled — run manually: codeforge-dashboard --port ${PORT}" |
| 156 | +fi |
| 157 | + |
| 158 | +# === SUMMARY === |
| 159 | +echo "" |
| 160 | +echo "[codeforge-dashboard] Installation complete" |
| 161 | +echo " Package: @coredirective/cf-dash@${DASH_VERSION}" |
| 162 | +echo " Command: codeforge-dashboard" |
| 163 | +echo " Port: ${PORT}" |
| 164 | +echo " Autostart: ${AUTOSTART}" |
0 commit comments