Repository navigation
Expand file tree
/
Copy pathsetup-and-run.sh
More file actions
339 lines (272 loc) · 8.91 KB
/
Copy pathsetup-and-run.sh
File metadata and controls
339 lines (272 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env bash
set -Eeuo pipefail
NO_LAUNCH=0
SKIP_SYSTEM=0
for arg in "$@"; do
case "$arg" in
--no-launch) NO_LAUNCH=1 ;;
--skip-system) SKIP_SYSTEM=1 ;;
-h|--help)
cat <<'EOF'
KNeuron Linux bootstrap
Usage:
./setup-and-run.sh
./setup-and-run.sh --no-launch
./setup-and-run.sh --skip-system
Options:
--no-launch Prepare everything but do not start KNeuron.
--skip-system Do not install OS-level packages; only verify/use what exists.
EOF
exit 0
;;
*)
echo "[ERROR] Unknown argument: $arg" >&2
exit 2
;;
esac
done
ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"
step() { printf '\n\033[1;36m==> %s\033[0m\n' "$1"; }
ok() { printf '\033[1;32m[OK]\033[0m %s\n' "$1"; }
warn() { printf '\033[1;33m[WARN]\033[0m %s\n' "$1"; }
die() { printf '\033[1;31m[ERROR]\033[0m %s\n' "$1" >&2; exit 1; }
command_exists() { command -v "$1" >/dev/null 2>&1; }
if [[ ! -f "$ROOT/package.json" ]]; then
die "package.json not found. Put setup-and-run.sh in the KNeuron repository root."
fi
if [[ "$(uname -s)" != "Linux" ]]; then
die "This script is for Linux."
fi
# ---------------------------------------------------------------------------
# Distribution / Tauri system dependencies
# ---------------------------------------------------------------------------
install_system_dependencies() {
if [[ "$SKIP_SYSTEM" -eq 1 ]]; then
warn "Skipping system package installation."
return
fi
if [[ -f /etc/os-release ]]; then
# shellcheck disable=SC1091
source /etc/os-release
else
die "Cannot detect Linux distribution (/etc/os-release is missing)."
fi
case "${ID:-}" in
ubuntu|debian|linuxmint|pop)
step "Installing Linux/Tauri/BCI system dependencies"
sudo apt-get update
sudo apt-get install -y \
build-essential \
curl \
wget \
file \
git \
pkg-config \
libssl-dev \
libwebkit2gtk-4.1-dev \
libxdo-dev \
libayatana-appindicator3-dev \
librsvg2-dev \
python3 \
python3-pip \
python3-venv \
bluez \
rfkill
;;
*)
die "Automatic system setup currently supports Ubuntu/Debian-family distributions. Detected: ${ID:-unknown}. Install Tauri v2 prerequisites manually, then rerun with --skip-system."
;;
esac
ok "Linux system dependencies installed"
}
# ---------------------------------------------------------------------------
# Node.js / npm
# ---------------------------------------------------------------------------
ensure_node() {
local need_node=1
if command_exists node && command_exists npm; then
local major
major="$(node -p 'process.versions.node.split(".")[0]')"
if [[ "$major" -ge 20 ]]; then
need_node=0
ok "Node $(node --version), npm $(npm --version)"
else
warn "Node $(node --version) is older than 20; installing current LTS via nvm."
fi
fi
if [[ "$need_node" -eq 1 ]]; then
if [[ "$SKIP_SYSTEM" -eq 1 ]]; then
die "Node.js >= 20 and npm are required."
fi
step "Installing Node.js LTS with nvm"
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
if [[ ! -s "$NVM_DIR/nvm.sh" ]]; then
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
fi
# shellcheck disable=SC1090
source "$NVM_DIR/nvm.sh"
nvm install --lts
nvm use --lts
command_exists node || die "Node installation failed."
command_exists npm || die "npm installation failed."
ok "Node $(node --version), npm $(npm --version)"
fi
}
# ---------------------------------------------------------------------------
# Rust / Cargo
# ---------------------------------------------------------------------------
ensure_rust() {
if command_exists cargo && command_exists rustc; then
ok "$(cargo --version)"
return
fi
if [[ "$SKIP_SYSTEM" -eq 1 ]]; then
die "Rust/cargo is required."
fi
step "Installing Rust stable toolchain"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
| sh -s -- -y --profile minimal --default-toolchain stable
# shellcheck disable=SC1090
source "$HOME/.cargo/env"
command_exists cargo || die "Rust installation failed."
ok "$(cargo --version)"
}
# ---------------------------------------------------------------------------
# Python / PyInstaller sidecars
# ---------------------------------------------------------------------------
ensure_python() {
command_exists python3 || die "python3 is required."
python3 - <<'PY'
import sys
if sys.version_info < (3, 10):
raise SystemExit(
f"Python >= 3.10 is required; found {sys.version.split()[0]}"
)
print(f"[OK] Python {sys.version.split()[0]}")
PY
}
build_sidecar() {
local dir="$1"
local name="$2"
local spec="$3"
local venv="$ROOT/$dir/.venv"
[[ -d "$ROOT/$dir" ]] || die "Missing directory: $dir"
[[ -f "$ROOT/$dir/$spec" ]] || die "Missing PyInstaller spec: $dir/$spec"
[[ -f "$ROOT/$dir/requirements.txt" ]] || die "Missing requirements.txt: $dir/requirements.txt"
step "Building $name"
if [[ ! -x "$venv/bin/python" ]]; then
python3 -m venv "$venv"
fi
"$venv/bin/python" -m pip install --upgrade pip setuptools wheel
"$venv/bin/python" -m pip install -r "$ROOT/$dir/requirements.txt"
"$venv/bin/python" -m pip install --upgrade pyinstaller
(
cd "$ROOT/$dir"
"$venv/bin/python" -m PyInstaller \
--clean \
--noconfirm \
"$spec"
)
local built=""
if [[ -f "$ROOT/$dir/dist/$name" ]]; then
built="$ROOT/$dir/dist/$name"
elif [[ -f "$ROOT/$dir/dist/$name/$name" ]]; then
built="$ROOT/$dir/dist/$name/$name"
else
built="$(
find "$ROOT/$dir/dist" -maxdepth 3 -type f -name "$name" -print -quit 2>/dev/null || true
)"
fi
[[ -n "$built" && -f "$built" ]] || die "PyInstaller completed, but $name was not found under $dir/dist."
mkdir -p "$ROOT/src-tauri/binaries"
local target
target="$(rustc -vV | awk '/^host:/ { print $2 }')"
[[ -n "$target" ]] || die "Could not determine Rust host target."
local destination="$ROOT/src-tauri/binaries/${name}-${target}"
cp -f "$built" "$destination"
chmod +x "$destination"
ok "$name -> src-tauri/binaries/$(basename "$destination")"
}
# ---------------------------------------------------------------------------
# Bluetooth / serial permissions
# ---------------------------------------------------------------------------
prepare_device_permissions() {
step "Checking Linux device permissions"
if command_exists systemctl; then
if systemctl list-unit-files bluetooth.service >/dev/null 2>&1; then
sudo systemctl enable --now bluetooth.service >/dev/null 2>&1 || true
fi
fi
if getent group dialout >/dev/null 2>&1; then
if id -nG "$USER" | tr ' ' '\n' | grep -qx dialout; then
ok "User is already in dialout group"
else
sudo usermod -aG dialout "$USER"
warn "Added $USER to dialout. Log out and back in before using serial/RFCOMM devices."
fi
fi
if command_exists rfkill; then
sudo rfkill unblock bluetooth || true
fi
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
printf '\n\033[1;36m============================================\033[0m\n'
printf '\033[1;36m KNeuron - Linux first-run bootstrap\033[0m\n'
printf '\033[1;36m============================================\033[0m\n'
install_system_dependencies
ensure_node
ensure_python
ensure_rust
prepare_device_permissions
step "Installing JavaScript dependencies"
if [[ -f "$ROOT/package-lock.json" ]]; then
npm ci
else
npm install
fi
ok "Node dependencies installed"
build_sidecar \
"brainaccess-sidecar" \
"brainaccess-bridge" \
"brainaccess-bridge.spec"
build_sidecar \
"ssvep-sidecar" \
"ssvep-classifier" \
"ssvep-classifier.spec"
build_sidecar \
"brainlink-sidecar" \
"brainlink-bridge" \
"brainlink-bridge.spec"
step "Verifying runtime assets"
[[ -d "$ROOT/public/modules/cortex" ]] \
&& ok "Cortex assets found" \
|| warn "public/modules/cortex is missing"
if [[ -d "$ROOT/public/neuorrun/Build" && -f "$ROOT/public/neuorrun/manifest.json" ]]; then
ok "Neuorrun WebGL runtime found"
else
warn "Neuorrun WebGL runtime is missing. Keep public/neuorrun/Build in the repository or build/copy Neuorrun separately."
fi
step "Verifying generated Linux sidecars"
HOST_TARGET="$(rustc -vV | awk '/^host:/ { print $2 }')"
for name in \
brainaccess-bridge \
ssvep-classifier \
brainlink-bridge
do
file="$ROOT/src-tauri/binaries/${name}-${HOST_TARGET}"
[[ -x "$file" ]] || die "Missing executable sidecar: $file"
size="$(du -h "$file" | awk '{print $1}')"
ok "$(basename "$file") ($size)"
done
printf '\n\033[1;32mKNeuron Linux setup completed successfully.\033[0m\n'
if [[ "$NO_LAUNCH" -eq 0 ]]; then
step "Starting KNeuron"
npm run tauri:dev
else
echo
echo "Launch later with:"
echo " npm run tauri:dev"
fi