-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·246 lines (197 loc) · 10 KB
/
install.sh
File metadata and controls
executable file
·246 lines (197 loc) · 10 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
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
log() { echo -e "${GREEN}[install]${NC} $1"; }
warn() { echo -e "${YELLOW}[warn]${NC} $1"; }
err() { echo -e "${RED}[error]${NC} $1" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || err "Must be run as root — log in as root first"
[ -n "${SUDO_USER:-}" ] && err "Must be run as root directly, not via sudo"
OWNTONE_BASE_URL="http://localhost:3689"
ENV_FILE="$SCRIPT_DIR/.env"
# ── --select-output mode ──────────────────────────────────────────────────────
# Shared: wait for OwnTone API, then interactively pick an output
owntone_select_output() {
log "Waiting for OwnTone API..."
for i in $(seq 1 30); do
if curl -sf "$OWNTONE_BASE_URL/api/config" >/dev/null 2>&1; then break; fi
[ "$i" -eq 30 ] && err "OwnTone API not responding"
sleep 1
done
# Retry to allow mDNS discovery time
local names=()
for i in $(seq 1 12); do
mapfile -t names < <(curl -s "$OWNTONE_BASE_URL/api/outputs" 2>/dev/null \
| jq -r '.outputs[].name' 2>/dev/null)
[ "${#names[@]}" -gt 0 ] && break
[ "$i" -eq 12 ] && err "No outputs found after 60s"
log "No outputs yet, retrying in 5s..."
sleep 5
done
echo ""
echo -e "${CYAN}Available outputs:${NC}"
for i in "${!names[@]}"; do echo " $((i+1))) ${names[$i]}"; done
echo ""
local sel
while true; do
read -rp "Select output [1-${#names[@]}]: " sel
if [[ "$sel" =~ ^[0-9]+$ ]] && [ "$sel" -ge 1 ] && [ "$sel" -le "${#names[@]}" ]; then
SELECTED_OUTPUT="${names[$((sel-1))]}"; break
fi
echo "Invalid selection, try again"
done
}
if [ "${1:-}" = "--select-output" ]; then
[ -f "$ENV_FILE" ] || err "No .env found at $ENV_FILE — run install first"
SELECTED_OUTPUT=""
owntone_select_output
if grep -q '^OUTPUT_NAME=' "$ENV_FILE"; then
sed -i "s|^OUTPUT_NAME=.*|OUTPUT_NAME=$SELECTED_OUTPUT|" "$ENV_FILE"
else
echo "OUTPUT_NAME=$SELECTED_OUTPUT" >> "$ENV_FILE"
fi
log "OUTPUT_NAME set to: $SELECTED_OUTPUT"
systemctl restart auto-stream 2>/dev/null && log "auto-stream restarted" || true
exit 0
fi
FIFO_DIR="/root/music/pipes"
FIFO_PATH="$FIFO_DIR/platenspeler.fifo"
OWNTONE_CONF="/etc/owntone.conf"
# ── OwnTone repo ──────────────────────────────────────────────────────────────
log "Adding OwnTone apt repository..."
wget -q -O - https://raw.githubusercontent.com/owntone/owntone-apt/refs/heads/master/repo/rpi/owntone.gpg \
| gpg --dearmor --output /usr/share/keyrings/owntone-archive-keyring.gpg
wget -q -O /etc/apt/sources.list.d/owntone.list \
https://raw.githubusercontent.com/owntone/owntone-apt/refs/heads/master/repo/rpi/owntone-trixie.list
# ── Packages ──────────────────────────────────────────────────────────────────
log "Installing packages..."
apt-get update -q
apt-get install -y git owntone alsa-utils sox bc curl jq
# ── ALSA ──────────────────────────────────────────────────────────────────────
log "Configuring ALSA..."
cp "$SCRIPT_DIR/asound.conf" /root/.asoundrc
# ── PGA Gain ──────────────────────────────────────────────────────────────────
log "Setting HiFiBerry ADC PGA gain..."
echo ""
echo -e "${CYAN}PGA Gain sets the ADC input level. 12dB (value 24) is recommended for record players.${NC}"
read -rp "PGA Gain value [default: 24 = 12dB]: " PGA_INPUT
PGA_GAIN="${PGA_INPUT:-24}"
if amixer -c 0 set 'PGA Gain Left' "$PGA_GAIN" >/dev/null 2>&1; then
amixer -c 0 set 'PGA Gain Right' "$PGA_GAIN" >/dev/null 2>&1
alsactl store 2>/dev/null || true
log "PGA Gain set to $PGA_GAIN and saved"
else
warn "Could not set PGA Gain — is the HiFiBerry ADC audio card configured?"
warn "You can set it manually later: amixer -c 0 set 'PGA Gain Left' 24"
fi
# ── FIFO pipes ────────────────────────────────────────────────────────────────
log "Creating FIFO pipes in $FIFO_DIR..."
mkdir -p "$FIFO_DIR"
[ ! -p "$FIFO_PATH" ] && mkfifo "$FIFO_PATH"
[ ! -p "${FIFO_PATH}.metadata" ] && mkfifo "${FIFO_PATH}.metadata"
if [ -f "$SCRIPT_DIR/platenspeler.png" ]; then
cp "$SCRIPT_DIR/platenspeler.png" "$FIFO_DIR/platenspeler.png"
fi
# ── OwnTone config ────────────────────────────────────────────────────────────
log "Configuring OwnTone music directory..."
if grep -q 'directories = {' "$OWNTONE_CONF"; then
sed -i 's|directories = {[^}]*}|directories = { "/root/music" }|' "$OWNTONE_CONF"
else
warn "Could not find 'directories' in $OWNTONE_CONF — add it manually:"
warn ' directories = { "/root/music" }'
fi
# ── systemd service ───────────────────────────────────────────────────────────
log "Installing auto-stream service..."
sed "s|ExecStart=.*|ExecStart=$SCRIPT_DIR/owntone-auto-stream.sh|" \
"$SCRIPT_DIR/auto-stream.service" \
> /etc/systemd/system/auto-stream.service
systemctl daemon-reload
systemctl enable auto-stream
# ── Start OwnTone ─────────────────────────────────────────────────────────────
log "Starting OwnTone..."
systemctl restart owntone
# ── Select output ────────────────────────────────────────────────────────────
SELECTED_OUTPUT=""
owntone_select_output
# ── Auto-detect OWNTONE_STREAM_URI ────────────────────────────────────────────
STREAM_URI=""
log "Waiting for OwnTone library scan..."
sleep 8
STREAM_URI=$(curl -s "$OWNTONE_BASE_URL/api/search?type=tracks&expression=path+starts+with+%27$(python3 -c "import urllib.parse; print(urllib.parse.quote('$FIFO_DIR'))" 2>/dev/null || echo "$FIFO_DIR")%27" 2>/dev/null \
| jq -r '.tracks.items[0].uri // empty' 2>/dev/null || true)
if [ -z "$STREAM_URI" ]; then
# Fallback: try listing all tracks and match by path
STREAM_URI=$(curl -s "$OWNTONE_BASE_URL/api/library/tracks" 2>/dev/null \
| jq -r --arg p "$FIFO_PATH" '.items[] | select(.path == $p) | .uri // empty' 2>/dev/null | head -n1 || true)
fi
if [ -n "$STREAM_URI" ]; then
log "Found stream URI: $STREAM_URI"
else
warn "Could not auto-detect stream URI. After OwnTone scans the library,"
warn "find it in the OwnTone UI and set OWNTONE_STREAM_URI in $ENV_FILE"
STREAM_URI="library:track:1"
fi
# ── Write .env ────────────────────────────────────────────────────────────────
if [ ! -f "$ENV_FILE" ]; then
log "Creating $ENV_FILE..."
cat > "$ENV_FILE" << EOF
FIFO_PATH=$FIFO_PATH
STREAM_DEV=stream
MONITOR_DEV=monitor
THRESHOLD=-50
CHECK_INTERVAL=5
SILENCE_TIMEOUT=300
OWNTONE_BASE_URL=$OWNTONE_BASE_URL
OUTPUT_NAME=${SELECTED_OUTPUT:-change_me}
OWNTONE_STREAM_URI=$STREAM_URI
# OWNTONE_VOLUME=50
EOF
else
# Update OUTPUT_NAME and OWNTONE_STREAM_URI in existing .env
if [ -n "$SELECTED_OUTPUT" ]; then
if grep -q '^OUTPUT_NAME=' "$ENV_FILE"; then
sed -i "s|^OUTPUT_NAME=.*|OUTPUT_NAME=$SELECTED_OUTPUT|" "$ENV_FILE"
else
echo "OUTPUT_NAME=$SELECTED_OUTPUT" >> "$ENV_FILE"
fi
fi
if [ -n "$STREAM_URI" ] && [ "$STREAM_URI" != "library:track:1" ]; then
if grep -q '^OWNTONE_STREAM_URI=' "$ENV_FILE"; then
sed -i "s|^OWNTONE_STREAM_URI=.*|OWNTONE_STREAM_URI=$STREAM_URI|" "$ENV_FILE"
else
echo "OWNTONE_STREAM_URI=$STREAM_URI" >> "$ENV_FILE"
fi
fi
fi
# ── Start auto-stream ─────────────────────────────────────────────────────────
ENV_READY=true
if grep -q 'change_me\|library:track:1' "$ENV_FILE" 2>/dev/null; then
ENV_READY=false
fi
if $ENV_READY; then
systemctl start auto-stream
STREAM_STATUS="$(systemctl is-active auto-stream)"
else
STREAM_STATUS="not started (review $ENV_FILE)"
fi
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
echo "══════════════════════════════════════════"
log "Install complete"
echo "══════════════════════════════════════════"
echo ""
echo " OwnTone UI: http://$(hostname).local:3689"
echo " OwnTone API: http://$(hostname).local:3689/api"
echo ""
echo " owntone: $(systemctl is-active owntone)"
echo " auto-stream: $STREAM_STATUS"
echo ""
if ! $ENV_READY; then
warn "Review $ENV_FILE — then start the service:"
echo " systemctl start auto-stream"
echo ""
fi