-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·193 lines (164 loc) · 7.01 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·193 lines (164 loc) · 7.01 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
#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# Sonos Controller — Installer
# Installs dependencies, builds the app, and sets up launchd daemons so the
# controller and the Sonos API run automatically at boot (even when locked).
# ─────────────────────────────────────────────────────────────────────────────
set -euo pipefail
# ── Colours ───────────────────────────────────────────────────────────────────
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; BOLD='\033[1m'; NC='\033[0m'
ok() { echo -e "${GREEN}✓${NC} $*"; }
warn() { echo -e "${YELLOW}!${NC} $*"; }
die() { echo -e "${RED}✗ $*${NC}"; exit 1; }
step() { echo -e "\n${BOLD}▸ $*${NC}"; }
echo ""
echo -e "${BOLD} Sonos Controller — Installer${NC}"
echo " ────────────────────────────────"
echo ""
# ── Resolve install directory (the folder this script lives in) ───────────────
INSTALL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ok "Install directory: $INSTALL_DIR"
# ── Check prerequisites ───────────────────────────────────────────────────────
step "Checking prerequisites"
if ! command -v brew &>/dev/null; then
warn "Homebrew not found. Installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
ok "Homebrew"
if ! command -v node &>/dev/null; then
warn "Node.js not found. Installing via Homebrew..."
brew install node
fi
NODE_MAJOR=$(node -e "console.log(parseInt(process.version.slice(1)))")
if [ "$NODE_MAJOR" -lt 18 ]; then
die "Node.js v18 or later is required (found v$(node -v)). Run: brew upgrade node"
fi
ok "Node.js $(node -v)"
if ! command -v git &>/dev/null; then
warn "git not found. Installing via Homebrew..."
brew install git
fi
ok "git"
NODE_PATH="$(which node)"
# ── Install dependencies & build ─────────────────────────────────────────────
step "Installing dependencies and building the app"
cd "$INSTALL_DIR"
npm install
npm run build
ok "Build complete — output in dist/"
SONOS_API_DIR="$INSTALL_DIR/node_modules/node-sonos-http-api"
ok "node-sonos-http-api ready at node_modules/node-sonos-http-api"
# ── Gather config from user ───────────────────────────────────────────────────
step "Configuring your Sonos connection"
echo ""
echo " Your Sonos room name is shown in the Sonos app — tap the speaker name to find it."
echo " It is case-sensitive (e.g. \"Living Room\" not \"living room\")."
echo ""
read -rp " Room name: " ROOM_NAME
while [ -z "$ROOM_NAME" ]; do
warn "Room name cannot be empty."
read -rp " Room name: " ROOM_NAME
done
read -rp " Sonos API port [5005]: " API_PORT
API_PORT="${API_PORT:-5005}"
read -rp " Controller port [3000]: " CTRL_PORT
CTRL_PORT="${CTRL_PORT:-3000}"
# Write initial config to sonos-data.json
cat > "$INSTALL_DIR/sonos-data.json" <<JSON
{
"sonos-config": {
"host": "localhost",
"port": "$API_PORT",
"room": "$ROOM_NAME"
}
}
JSON
ok "Config saved to sonos-data.json"
# ── Create launchd plists ─────────────────────────────────────────────────────
step "Setting up background services (requires sudo)"
PLIST_API="/Library/LaunchDaemons/com.sonos.api.plist"
PLIST_CTRL="/Library/LaunchDaemons/com.sonos.controller.plist"
sudo tee "$PLIST_API" > /dev/null <<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sonos.api</string>
<key>ProgramArguments</key>
<array>
<string>$NODE_PATH</string>
<string>$SONOS_API_DIR/server.js</string>
</array>
<key>WorkingDirectory</key>
<string>$SONOS_API_DIR</string>
<key>EnvironmentVariables</key>
<dict>
<key>PORT</key>
<string>$API_PORT</string>
</dict>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/sonos-api.log</string>
<key>StandardErrorPath</key>
<string>/tmp/sonos-api-error.log</string>
</dict>
</plist>
XML
sudo tee "$PLIST_CTRL" > /dev/null <<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sonos.controller</string>
<key>ProgramArguments</key>
<array>
<string>$NODE_PATH</string>
<string>$INSTALL_DIR/server.js</string>
</array>
<key>WorkingDirectory</key>
<string>$INSTALL_DIR</string>
<key>EnvironmentVariables</key>
<dict>
<key>PORT</key>
<string>$CTRL_PORT</string>
</dict>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/sonos-controller.log</string>
<key>StandardErrorPath</key>
<string>/tmp/sonos-controller-error.log</string>
</dict>
</plist>
XML
ok "Launchd plists written"
# ── Load the daemons ──────────────────────────────────────────────────────────
step "Starting services"
# Unload first in case of a reinstall
sudo launchctl unload "$PLIST_API" 2>/dev/null || true
sudo launchctl unload "$PLIST_CTRL" 2>/dev/null || true
sudo launchctl load "$PLIST_API"
sudo launchctl load "$PLIST_CTRL"
ok "Services started"
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}${BOLD} All done!${NC}"
echo ""
echo -e " Sonos API: ${BOLD}http://localhost:${API_PORT}${NC}"
echo -e " Controller: ${BOLD}http://localhost:${CTRL_PORT}${NC}"
echo ""
echo " Both services start automatically at boot, even when the Mac is locked."
echo ""
echo " Useful commands:"
echo " View controller logs: tail -f /tmp/sonos-controller.log"
echo " View API logs: tail -f /tmp/sonos-api.log"
echo " Restart controller: sudo launchctl kickstart -k system/com.sonos.controller"
echo " Restart API: sudo launchctl kickstart -k system/com.sonos.api"
echo ""