-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·698 lines (639 loc) · 32 KB
/
install.sh
File metadata and controls
executable file
·698 lines (639 loc) · 32 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/bin/bash
# ══════════════════════════════════════════════════════════════════════════
# Birdash — Complete installation script for Raspberry Pi 5
# https://github.com/ernens/birdash
#
# Usage:
# git clone https://github.com/ernens/birdash.git
# cd birdash
# chmod +x install.sh
# ./install.sh # interactive
# ./install.sh --yes # non-interactive (skip confirmation)
#
# Tested on: Raspberry Pi OS Lite 64-bit (Trixie/Bookworm)
# ══════════════════════════════════════════════════════════════════════════
set -e
ASSUME_YES=0
for arg in "$@"; do
case "$arg" in
-y|--yes|--non-interactive) ASSUME_YES=1 ;;
esac
done
# Auto-skip prompt when stdin isn't a TTY (e.g. piped from curl)
if [ ! -t 0 ]; then ASSUME_YES=1; fi
BIRDASH_USER=$(whoami)
BIRDASH_HOME=$(eval echo ~$BIRDASH_USER)
BIRDASH_DIR="$BIRDASH_HOME/birdash"
DB_DIR="$BIRDASH_HOME/birdash/data"
DB_PATH="$DB_DIR/birds.db"
SONGS_DIR="$BIRDASH_HOME/BirdSongs"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
step() { echo -e "\n${BLUE}[$1/$TOTAL_STEPS]${NC} $2"; }
ok() { echo -e " ${GREEN}✓${NC} $1"; }
warn() { echo -e " ${YELLOW}⚠${NC} $1"; }
fail() { echo -e " ${RED}✗${NC} $1"; exit 1; }
TOTAL_STEPS=12
echo ""
echo -e "${GREEN}══════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN} Birdash — Bird Detection Dashboard & Engine Installer${NC}"
echo -e "${GREEN}══════════════════════════════════════════════════════════${NC}"
echo ""
echo " User: $BIRDASH_USER"
echo " Home: $BIRDASH_HOME"
echo " Birdash: $BIRDASH_DIR"
echo " Platform: $(uname -m) $(cat /etc/os-release 2>/dev/null | grep PRETTY_NAME | cut -d= -f2 | tr -d '"')"
echo ""
if [ "$ASSUME_YES" = "1" ]; then
echo " (Non-interactive mode: proceeding automatically)"
else
read -p "Continue with installation? [Y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then exit 0; fi
fi
# ══════════════════════════════════════════════════════════════════════════
# Pre-flight: Passwordless sudo (required for in-app updates)
# ══════════════════════════════════════════════════════════════════════════
# The dashboard's in-app updater launches scripts without a TTY, so any
# sudo call that needs a password hangs forever. Without NOPASSWD configured
# now, future in-app updates will fail at the Caddy migrations and at the
# final `systemctl restart birdash`.
echo -e "\n${BLUE}[pre]${NC} Checking passwordless sudo for in-app updates..."
if sudo -n true 2>/dev/null; then
ok "Passwordless sudo already configured for $BIRDASH_USER"
else
echo ""
echo " In-app updates (from the dashboard UI) run without a terminal,"
echo " so sudo must work without a password prompt. Without this, the"
echo " next update will fail at the Caddy migrations and the service"
echo " restart."
echo ""
echo " This will write /etc/sudoers.d/010_pi-nopasswd:"
echo " $BIRDASH_USER ALL=(ALL) NOPASSWD: ALL"
echo ""
echo " Same as the Raspberry Pi Imager \"passwordless sudo\" option."
echo ""
if [ "$ASSUME_YES" = "1" ]; then
REPLY="y"
else
read -p " Configure passwordless sudo for $BIRDASH_USER? [Y/n] " -n 1 -r
echo
fi
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
echo "$BIRDASH_USER ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/010_pi-nopasswd > /dev/null
sudo chmod 0440 /etc/sudoers.d/010_pi-nopasswd
ok "Passwordless sudo configured (/etc/sudoers.d/010_pi-nopasswd)"
else
warn "Skipped — in-app updates from the UI will fail until you set this up"
warn "Manual setup later:"
warn " echo '$BIRDASH_USER ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/010_pi-nopasswd"
warn " sudo chmod 0440 /etc/sudoers.d/010_pi-nopasswd"
fi
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 1: System packages
# ══════════════════════════════════════════════════════════════════════════
step 1 "Installing system packages..."
PACKAGES="nodejs npm python3 python3-venv ffmpeg alsa-utils sqlite3 git nfs-common"
MISSING=""
for pkg in $PACKAGES; do
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
MISSING="$MISSING $pkg"
fi
done
if [ -n "$MISSING" ]; then
echo " Installing:$MISSING"
sudo apt update -qq
sudo apt install -y $MISSING
ok "System packages installed"
else
ok "All system packages already installed"
fi
# Caddy (from official repo if not installed)
if ! command -v caddy >/dev/null 2>&1; then
echo " Installing Caddy..."
sudo apt install -y caddy 2>/dev/null || {
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg 2>/dev/null
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null
sudo apt update -qq && sudo apt install -y caddy
}
ok "Caddy installed"
else
ok "Caddy already installed"
fi
# ttyd (web terminal)
if ! command -v ttyd >/dev/null 2>&1; then
echo " Installing ttyd..."
ARCH=$(uname -m)
[ "$ARCH" = "aarch64" ] && TTYD_ARCH="aarch64" || TTYD_ARCH="x86_64"
curl -sL "https://github.com/tsl0922/ttyd/releases/latest/download/ttyd.${TTYD_ARCH}" -o /tmp/ttyd
chmod +x /tmp/ttyd && sudo mv /tmp/ttyd /usr/local/bin/ttyd
ok "ttyd installed"
else
ok "ttyd already installed"
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 2: Node.js dependencies
# ══════════════════════════════════════════════════════════════════════════
step 2 "Installing Node.js dependencies..."
cd "$BIRDASH_DIR"
npm install --production --silent 2>/dev/null
ok "Node.js dependencies installed ($(node --version))"
# ══════════════════════════════════════════════════════════════════════════
# Step 3: Python virtual environment
# ══════════════════════════════════════════════════════════════════════════
step 3 "Setting up Python virtual environment..."
if [ ! -d "$BIRDASH_DIR/engine/venv" ]; then
python3 -m venv "$BIRDASH_DIR/engine/venv"
ok "Virtual environment created"
fi
"$BIRDASH_DIR/engine/venv/bin/pip" install --upgrade pip -q 2>/dev/null
"$BIRDASH_DIR/engine/venv/bin/pip" install -r "$BIRDASH_DIR/engine/requirements.txt" -q 2>/dev/null
# Ensure apprise is also available system-wide (reliable PATH discovery)
if ! command -v apprise &>/dev/null; then
sudo pip3 install apprise -q 2>/dev/null || true
fi
ok "Python dependencies installed ($(python3 --version))"
# ══════════════════════════════════════════════════════════════════════════
# Step 4: Create directory structure
# ══════════════════════════════════════════════════════════════════════════
step 4 "Creating directory structure..."
mkdir -p "$BIRDASH_DIR/engine/audio/incoming"
mkdir -p "$BIRDASH_DIR/engine/audio/processed"
mkdir -p "$BIRDASH_DIR/engine/models"
mkdir -p "$BIRDASH_DIR/photo-cache"
mkdir -p "$SONGS_DIR/Extracted/By_Date"
mkdir -p "$SONGS_DIR/StreamData"
mkdir -p "$DB_DIR"
mkdir -p "$BIRDASH_HOME/.ssh/sockets"
ok "Directories created"
# ══════════════════════════════════════════════════════════════════════════
# Step 5: Create/bootstrap databases
# ══════════════════════════════════════════════════════════════════════════
step 5 "Setting up databases..."
# Main detection database (birds.db)
if [ ! -f "$DB_PATH" ]; then
sqlite3 "$DB_PATH" <<'SQL'
CREATE TABLE IF NOT EXISTS detections (
Date DATE,
Time TIME,
Sci_Name VARCHAR(100) NOT NULL,
Com_Name VARCHAR(100) NOT NULL,
Confidence FLOAT,
Lat FLOAT,
Lon FLOAT,
Cutoff FLOAT,
Week INT,
Sens FLOAT,
Overlap FLOAT,
File_Name VARCHAR(100) NOT NULL,
Model VARCHAR(50)
);
CREATE INDEX IF NOT EXISTS idx_date_time ON detections(Date, Time DESC);
CREATE INDEX IF NOT EXISTS idx_com_name ON detections(Com_Name);
CREATE INDEX IF NOT EXISTS idx_sci_name ON detections(Sci_Name);
CREATE INDEX IF NOT EXISTS idx_date_sci ON detections(Date, Sci_Name);
CREATE INDEX IF NOT EXISTS idx_model ON detections(Model);
PRAGMA journal_mode=WAL;
SQL
ok "birds.db created at $DB_PATH"
else
ok "birds.db already exists ($(sqlite3 "$DB_PATH" 'SELECT COUNT(*) FROM detections') detections)"
fi
# Birdash validation database
if [ ! -f "$BIRDASH_DIR/birdash.db" ]; then
sqlite3 "$BIRDASH_DIR/birdash.db" <<'SQL'
CREATE TABLE IF NOT EXISTS validations (
date TEXT,
time TEXT,
sci_name TEXT,
status TEXT DEFAULT 'unreviewed',
notes TEXT DEFAULT '',
updated_at TEXT,
PRIMARY KEY(date, time, sci_name)
);
PRAGMA journal_mode=WAL;
SQL
ok "birdash.db created"
else
ok "birdash.db already exists"
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 6: Create birdnet.conf (detection settings shared with UI)
# ══════════════════════════════════════════════════════════════════════════
step 6 "Setting up configuration..."
sudo mkdir -p /etc/birdnet
# Select optimal Perch variant based on hardware
_PI_MODEL=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0' || echo "unknown")
if echo "$_PI_MODEL" | grep -q "Pi 5"; then
_PERCH_MODEL="perch_v2_original"
elif echo "$_PI_MODEL" | grep -qE "Pi 4|Pi 400"; then
_PERCH_MODEL="perch_v2_fp16"
else
_PERCH_MODEL="perch_v2_dynint8"
fi
echo " Optimal Perch model for $(echo $_PI_MODEL | grep -oP 'Pi \d+' || echo 'this hardware'): $_PERCH_MODEL"
# ── ZRAM: auto-tune compressed swap on low-RAM Pis ──────────────────────────
# Skips on hosts with ≥6 GB RAM (Pi 5 8GB, Pi 4 8GB) where modern RPi OS
# defaults are already adequate. On Pi 3 / Pi 4 ≤4GB, configures zram-size
# = 25-50% of RAM with zstd to absorb peaks (BirdNET + Perch + Node + Caddy
# concurrently can OOM-kill the engine without it).
if [ -x "$BIRDASH_DIR/scripts/configure_zram.sh" ]; then
echo " Auto-configuring zram (skips silently on ≥6 GB hosts)..."
bash "$BIRDASH_DIR/scripts/configure_zram.sh" 2>&1 | sed 's/^/ /' || warn "zram config failed (non-fatal, continuing)"
fi
# ── GeoIP: auto-detect location + language from public IP ───────────────────
# BirdNET filters species by geographic frequency (SF_THRESH), so shipping
# with LAT=0 LON=0 means ~0 detections until the user sets coordinates.
# ipapi.co is free (30k req/month, no key, HTTPS). Fails silently to 0,0.
_GEO_LAT="0.0"
_GEO_LON="0.0"
_GEO_LANG="en"
_GEO_CITY=""
_GEO_COUNTRY=""
_GEO_JSON=$(curl -s -m 5 https://ipapi.co/json 2>/dev/null || true)
if [ -n "$_GEO_JSON" ]; then
_GEO_PARSED=$(printf '%s' "$_GEO_JSON" | python3 -c '
import sys, json
try:
d = json.load(sys.stdin)
lat = d.get("latitude")
lon = d.get("longitude")
cc = (d.get("country_code") or "").upper()
city = d.get("city") or ""
country = d.get("country_name") or ""
if lat is None or lon is None:
sys.exit(1)
# Country → UI/database language
lang_map = {
"FR":"fr","BE":"fr","LU":"fr","MC":"fr",
"NL":"nl",
"DE":"de","AT":"de","CH":"de",
}
lang = lang_map.get(cc, "en")
print(f"{lat}|{lon}|{lang}|{city}|{country}")
except Exception:
sys.exit(1)
' 2>/dev/null || true)
if [ -n "$_GEO_PARSED" ]; then
_GEO_LAT=$(echo "$_GEO_PARSED" | cut -d'|' -f1)
_GEO_LON=$(echo "$_GEO_PARSED" | cut -d'|' -f2)
_GEO_LANG=$(echo "$_GEO_PARSED" | cut -d'|' -f3)
_GEO_CITY=$(echo "$_GEO_PARSED" | cut -d'|' -f4)
_GEO_COUNTRY=$(echo "$_GEO_PARSED" | cut -d'|' -f5)
ok "Location detected: ${_GEO_CITY:-?}, ${_GEO_COUNTRY:-?} (${_GEO_LAT}, ${_GEO_LON}) → lang=${_GEO_LANG}"
else
warn "GeoIP response could not be parsed — defaulting to 0,0 / en"
fi
else
warn "GeoIP lookup failed (offline?) — defaulting to 0,0 / en"
fi
if [ ! -f /etc/birdnet/birdnet.conf ]; then
sudo tee /etc/birdnet/birdnet.conf > /dev/null <<EOF
# Birdash detection configuration
# This file is read by both BirdEngine and Birdash dashboard
MODEL=$_PERCH_MODEL
SENSITIVITY=1.3
CONFIDENCE=0.7
OVERLAP=0.5
SF_THRESH=0.03
DATA_MODEL_VERSION=2
RECORDING_LENGTH=45
EXTRACTION_LENGTH=6
AUDIOFMT=mp3
DATABASE_LANG=$_GEO_LANG
LATITUDE=$_GEO_LAT
LONGITUDE=$_GEO_LON
RECS_DIR=$SONGS_DIR
PRIVACY_THRESHOLD=0
FULL_DISK=purge
PURGE_THRESHOLD=95
AUDIO_RETENTION_DAYS=90
# Dual-model (enabled by install.sh step 12 once BirdNET is downloaded)
DUAL_MODEL_ENABLED=0
SECONDARY_MODEL=
# Notifications (edit ntfy topic or leave empty)
NOTIFY_ENABLED=0
NOTIFY_RARE_SPECIES=1
NOTIFY_RARE_THRESHOLD=10
NOTIFY_FIRST_SEASON=1
NOTIFY_SEASON_DAYS=30
# Apprise (notification URLs, one per line)
APPRISE_NOTIFY_EACH_DETECTION=0
APPRISE_NOTIFY_NEW_SPECIES=0
APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY=0
APPRISE_WEEKLY_REPORT=0
EOF
ok "birdnet.conf created — edit /etc/birdnet/birdnet.conf with your coordinates"
else
ok "birdnet.conf already exists"
fi
# Engine config
if [ ! -f "$BIRDASH_DIR/engine/config.toml" ]; then
sed "s|BIRDASH_HOME|$BIRDASH_HOME|g" "$BIRDASH_DIR/engine/config.toml.example" > "$BIRDASH_DIR/engine/config.toml"
# Baseline: Perch-only (BirdNET will be promoted to primary in Step 11 if download succeeds)
sed -i "s|^model\s*=.*|model = \"$_PERCH_MODEL\"|" "$BIRDASH_DIR/engine/config.toml"
sed -i "s|^secondary_model\s*=.*|secondary_model = \"\"|" "$BIRDASH_DIR/engine/config.toml"
ok "engine/config.toml created — edit with your station location"
fi
# Dashboard local config
if [ ! -f "$BIRDASH_DIR/public/js/birdash-local.js" ]; then
cp "$BIRDASH_DIR/config/birdash-local.example.js" "$BIRDASH_DIR/public/js/birdash-local.js"
ok "birdash-local.js created — edit with your location"
fi
# ALSA config for shared mic access
# Auto-detect USB audio device and configure
USB_CARD=$(arecord -l 2>/dev/null | grep -oP 'card \K\d+(?=:.*USB)' | head -1)
if [ -n "$USB_CARD" ]; then
USB_NAME=$(arecord -l 2>/dev/null | grep "card ${USB_CARD}:" | sed 's/.*: \(.*\) \[.*/\1/')
ALSA_DEV="plughw:${USB_CARD},0"
echo " Detected USB audio: card $USB_CARD — $USB_NAME"
# Create audio_config.json from template if needed, then update with detected device
if [ ! -f "$BIRDASH_DIR/config/audio_config.json" ] && [ -f "$BIRDASH_DIR/config/audio_config.example.json" ]; then
cp "$BIRDASH_DIR/config/audio_config.example.json" "$BIRDASH_DIR/config/audio_config.json"
fi
if [ -f "$BIRDASH_DIR/config/audio_config.json" ]; then
python3 -c "
import json
with open('$BIRDASH_DIR/config/audio_config.json') as f: d=json.load(f)
d['device_id'] = '$ALSA_DEV'
d['device_name'] = '$USB_NAME'
with open('$BIRDASH_DIR/config/audio_config.json','w') as f: json.dump(d, f, indent=2)
" 2>/dev/null
fi
# Create .asoundrc with dsnoop (shared capture) + softvol boost
# dsnoop lets multiple processes read the same USB capture device at once,
# so the recording service and the audio-preview endpoint can coexist.
cat > "$BIRDASH_HOME/.asoundrc" <<ASOUND
# Auto-generated by Birdash for $USB_NAME
# dsnoop: allow multiple readers (recording service + dashboard preview)
pcm.dsnooper {
type dsnoop
ipc_key 2048
ipc_key_add_uid false
ipc_perm 0666
slave {
pcm "hw:${USB_CARD},0"
channels 1
rate 48000
format S16_LE
period_size 1024
buffer_size 8192
}
}
# Software gain boost (many USB mics have low sensitivity)
pcm.boosted {
type softvol
slave.pcm "dsnooper"
control {
name "Boost"
card ${USB_CARD}
}
min_dB -5.0
max_dB 30.0
}
pcm.birdash {
type plug
slave.pcm "boosted"
}
ASOUND
# Set hardware + software gain to max
amixer -c "$USB_CARD" set 'Mic Capture Volume' 100% 2>/dev/null || true
amixer -c "$USB_CARD" set 'Mic' 100% 2>/dev/null || true
# Update config to use boosted device
if [ -f "$BIRDASH_DIR/config/audio_config.json" ]; then
python3 -c "
import json
with open('$BIRDASH_DIR/config/audio_config.json') as f: d=json.load(f)
d['device_id'] = 'birdash'
with open('$BIRDASH_DIR/config/audio_config.json','w') as f: json.dump(d, f, indent=2)
" 2>/dev/null
fi
ok "Audio configured: $USB_NAME (card $USB_CARD, softvol boost enabled)"
else
# No USB device — still create config from template
if [ ! -f "$BIRDASH_DIR/config/audio_config.json" ] && [ -f "$BIRDASH_DIR/config/audio_config.example.json" ]; then
cp "$BIRDASH_DIR/config/audio_config.example.json" "$BIRDASH_DIR/config/audio_config.json"
fi
warn "No USB audio device detected — configure via Settings → Audio after plugging in a mic"
fi
# FUSE config for SSHFS
if ! grep -q "^user_allow_other" /etc/fuse.conf 2>/dev/null; then
sudo sed -i 's/^#user_allow_other/user_allow_other/' /etc/fuse.conf 2>/dev/null || true
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 7: Download ML models
# ══════════════════════════════════════════════════════════════════════════
step 7 "Downloading ML models..."
MODELS_DIR="$BIRDASH_DIR/engine/models"
HF_BASE="https://huggingface.co/ernensbjorn/perch-v2-int8-tflite/resolve/main"
# Helper: download model if missing or empty
download_model() {
local name="$1" url="$2" size_hint="$3"
local path="$MODELS_DIR/$name"
if [ -f "$path" ] && [ "$(stat -c%s "$path" 2>/dev/null || echo 0)" -gt 10000 ]; then
echo " ✓ $name already present"
return 0
fi
rm -f "$path" # remove empty placeholders
echo " Downloading $name ($size_hint)..."
wget -q --show-progress -O "$path" "$url" || { warn "Download failed: $name"; rm -f "$path"; return 1; }
if [ "$(stat -c%s "$path" 2>/dev/null || echo 0)" -lt 10000 ]; then
warn "$name download appears corrupt (too small), removing"
rm -f "$path"
return 1
fi
return 0
}
# Detect Pi model for optimal default
PI_MODEL=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0' || echo "unknown")
echo " Hardware: $PI_MODEL"
# Shared labels and indices (used by all Perch variants)
download_model "labels.txt" "$HF_BASE/labels.txt" "~300 KB"
download_model "bird_indices.json" "$HF_BASE/bird_indices.json" "~60 KB"
# Perch V2 INT8 (works on all Pi models)
download_model "perch_v2_dynint8.tflite" "$HF_BASE/perch_v2_dynint8.tflite" "~100 MB"
# FP16 and FP32 only on Pi 4/5 (too slow / too much RAM on Pi 3)
if echo "$PI_MODEL" | grep -qE "Pi 4|Pi 5|Pi 400"; then
download_model "perch_v2_fp16.tflite" "$HF_BASE/perch_v2_fp16.tflite" "~195 MB"
download_model "perch_v2_original.tflite" "$HF_BASE/perch_v2_original.tflite" "~390 MB"
ok "Perch V2 models downloaded (INT8 + FP16 + FP32)"
else
ok "Perch V2 INT8 downloaded (best for $(echo $PI_MODEL | grep -oP 'Pi \d+' || echo 'this hardware'))"
fi
# engine/models.py expects per-variant label/index filenames
# ({variant}_Labels.txt, Perch_v2_bird_indices.json). The HF release ships
# them under the shared names labels.txt and bird_indices.json — content
# is identical across Perch variants, so we just symlink each variant
# back to the shared file. Symlink not copy: zero extra disk, zero risk
# of stale duplicates after a re-download.
if [ -f "$MODELS_DIR/labels.txt" ]; then
for variant in perch_v2_dynint8 perch_v2_fp16 perch_v2_original; do
if [ -f "$MODELS_DIR/${variant}.tflite" ]; then
ln -sf labels.txt "$MODELS_DIR/${variant}_Labels.txt"
fi
done
fi
if [ -f "$MODELS_DIR/bird_indices.json" ] && [ ! -e "$MODELS_DIR/Perch_v2_bird_indices.json" ]; then
ln -sf bird_indices.json "$MODELS_DIR/Perch_v2_bird_indices.json"
fi
# BirdNET V2.4 + l18n translated labels are handled by step 11
# (download_birdnet.sh). Pre-step-11 warnings used to live here, but they
# fired even on installs where step 11 was about to install everything,
# which was misleading. Step 11 has its own clearer messaging.
# ══════════════════════════════════════════════════════════════════════════
# Step 8: Install systemd services
# ══════════════════════════════════════════════════════════════════════════
step 8 "Installing systemd services..."
for svc in config/birdash.service engine/birdengine.service engine/birdengine-recording.service engine/ttyd.service; do
svc_name=$(basename "$svc")
if [ -f "$BIRDASH_DIR/$svc" ]; then
sed -e "s|BIRDASH_USER|$BIRDASH_USER|g" -e "s|BIRDASH_HOME|$BIRDASH_HOME|g" \
"$BIRDASH_DIR/$svc" | sudo tee "/etc/systemd/system/$svc_name" > /dev/null
ok "$svc_name"
fi
done
sudo systemctl daemon-reload
# ══════════════════════════════════════════════════════════════════════════
# Step 9: Configure Caddy reverse proxy
# ══════════════════════════════════════════════════════════════════════════
step 9 "Configuring Caddy..."
if [ ! -f /etc/caddy/Caddyfile.bak ]; then
sudo cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.bak 2>/dev/null || true
fi
sudo tee /etc/caddy/Caddyfile > /dev/null <<EOF
:80 {
handle /birds/api/* {
uri strip_prefix /birds
reverse_proxy localhost:7474 {
flush_interval -1
transport http {
response_header_timeout 120s
}
}
}
handle /birds/terminal/* {
reverse_proxy localhost:7681
}
handle /birds/audio/* {
encode zstd gzip
uri strip_prefix /birds/audio
root * $SONGS_DIR/Extracted
file_server
}
@birds path /birds /birds/*
handle @birds {
encode zstd gzip
uri strip_prefix /birds
root * $BIRDASH_DIR/public
header Cache-Control "public, no-cache"
file_server
}
redir / /birds/ permanent
}
EOF
# Allow Caddy to read user files
chmod 711 "$BIRDASH_HOME"
# Apply the new Caddyfile if caddy is already running.
# (systemctl enable --now in step 12 would NOT re-read config on an
# already-active service.) We use `restart` not `reload` because Caddy
# 2.6.2's reload path panics on PKI updates (known upstream bug); restart
# is safe here since this is a non-interactive install and the brief
# outage is part of the install footprint.
if systemctl is-active caddy >/dev/null 2>&1; then
sudo systemctl restart caddy >/dev/null 2>&1 && ok "Caddy configured and restarted" || warn "Caddy restart failed"
else
ok "Caddy configured"
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 10: Set up cron jobs
# ══════════════════════════════════════════════════════════════════════════
step 10 "Setting up scheduled tasks..."
# Audio purge cron (daily at 3am)
if ! crontab -l 2>/dev/null | grep -q "purge_audio"; then
(crontab -l 2>/dev/null; echo "0 3 * * * $BIRDASH_DIR/engine/purge_audio.sh >> /tmp/purge_audio.log 2>&1") | crontab -
ok "Audio purge cron installed (daily 3am)"
else
ok "Audio purge cron already exists"
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 11: Download BirdNET V2.4 (optional — CC-BY-NC-SA 4.0 license)
# ══════════════════════════════════════════════════════════════════════════
step 11 "Downloading BirdNET V2.4..."
MODELS_DIR="$BIRDASH_DIR/engine/models"
if [ "${BIRDASH_SKIP_BIRDNET:-0}" = "1" ]; then
warn "BIRDASH_SKIP_BIRDNET=1 — skipping BirdNET download"
warn "You can install it later from the dashboard: Settings → Detection → Download BirdNET"
elif [ -f "$MODELS_DIR/BirdNET_GLOBAL_6K_V2.4_Model_FP32.tflite" ] \
&& [ -f "$MODELS_DIR/l18n/labels_en.json" ]; then
ok "BirdNET V2.4 already installed"
# Promote to dual-model mode automatically (primary = BirdNET, secondary = Perch)
sudo sed -i "s|^MODEL=.*|MODEL=BirdNET_GLOBAL_6K_V2.4_Model_FP32|" /etc/birdnet/birdnet.conf
sudo sed -i "s|^DUAL_MODEL_ENABLED=.*|DUAL_MODEL_ENABLED=1|" /etc/birdnet/birdnet.conf
sudo sed -i "s|^SECONDARY_MODEL=.*|SECONDARY_MODEL=$_PERCH_MODEL|" /etc/birdnet/birdnet.conf
ok "Dual-model enabled (BirdNET + $_PERCH_MODEL)"
else
echo ""
echo " BirdNET V2.4 is distributed under CC-BY-NC-SA 4.0 (non-commercial use)."
echo " See https://github.com/kahst/BirdNET-Analyzer for the full license."
echo " Set BIRDASH_SKIP_BIRDNET=1 before install to skip this download."
echo ""
if bash "$BIRDASH_DIR/engine/download_birdnet.sh" "$MODELS_DIR"; then
ok "BirdNET V2.4 downloaded"
# Enable dual-model (BirdNET primary + Perch secondary) for best accuracy
sudo sed -i "s|^MODEL=.*|MODEL=BirdNET_GLOBAL_6K_V2.4_Model_FP32|" /etc/birdnet/birdnet.conf
sudo sed -i "s|^DUAL_MODEL_ENABLED=.*|DUAL_MODEL_ENABLED=1|" /etc/birdnet/birdnet.conf
sudo sed -i "s|^SECONDARY_MODEL=.*|SECONDARY_MODEL=$_PERCH_MODEL|" /etc/birdnet/birdnet.conf
# Sync the engine config.toml as well
if [ -f "$BIRDASH_DIR/engine/config.toml" ]; then
sed -i "s|^model\s*=.*|model = \"BirdNET_GLOBAL_6K_V2.4_Model_FP32\"|" "$BIRDASH_DIR/engine/config.toml"
sed -i "s|^secondary_model\s*=.*|secondary_model = \"$_PERCH_MODEL\"|" "$BIRDASH_DIR/engine/config.toml"
fi
ok "Dual-model enabled (BirdNET + $_PERCH_MODEL)"
else
warn "BirdNET download failed — starting with Perch-only"
warn "Retry later from the dashboard: Settings → Detection → Download BirdNET"
fi
fi
# ══════════════════════════════════════════════════════════════════════════
# Step 12: Enable and start services
# ══════════════════════════════════════════════════════════════════════════
step 12 "Enabling and starting services..."
sudo systemctl daemon-reload
sudo systemctl enable --now caddy >/dev/null 2>&1 && ok "caddy enabled & started" || warn "caddy failed to start"
sudo systemctl enable --now birdash >/dev/null 2>&1 && ok "birdash enabled & started" || warn "birdash failed to start"
sudo systemctl enable --now birdengine >/dev/null 2>&1 && ok "birdengine enabled & started" || warn "birdengine failed to start"
sudo systemctl enable --now birdengine-recording >/dev/null 2>&1 && ok "birdengine-recording enabled & started" || warn "birdengine-recording failed to start"
sudo systemctl enable --now ttyd >/dev/null 2>&1 && ok "ttyd enabled & started" || warn "ttyd failed to start"
# ══════════════════════════════════════════════════════════════════════════
# Done!
# ══════════════════════════════════════════════════════════════════════════
echo ""
echo -e "${GREEN}══════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN} Installation complete!${NC}"
echo -e "${GREEN}══════════════════════════════════════════════════════════${NC}"
echo ""
echo -e " Dashboard:"
echo -e " ${GREEN}http://$(hostname).local/birds/${NC}"
echo -e " ${GREEN}http://$(hostname -I | awk '{print $1}')/birds/${NC}"
echo ""
echo -e " Next steps (from the dashboard → Settings):"
echo -e " • Station: GPS coordinates, language, timezone"
echo -e " • Detection: download BirdNET V2.4 (one-click)"
echo -e " • Audio: select USB device and verify levels"
echo ""
echo -e " Optional config files:"
echo -e " ${YELLOW}/etc/birdnet/birdnet.conf${NC} — BirdNET runtime settings"
echo -e " ${YELLOW}$BIRDASH_DIR/engine/config.toml${NC} — engine + BirdWeather + ntfy"
echo -e " ${YELLOW}$BIRDASH_DIR/public/js/birdash-local.js${NC} — eBird API key"
echo ""
TFLITE_COUNT=$(ls "$MODELS_DIR"/*.tflite 2>/dev/null | wc -l)
if [ "$TFLITE_COUNT" -eq 0 ]; then
echo -e " ${YELLOW}⚠ No TFLite models found in $MODELS_DIR/${NC}"
echo -e " ${YELLOW} Use the dashboard's Detection panel to download BirdNET V2.4${NC}"
echo ""
fi