Commit | Line | Data |
---|---|---|
91370169 RK |
1 | #! /bin/bash |
2 | # | |
3 | # This file is part of DisOrder | |
4 | # Copyright (C) 2008 Richard Kettlewell | |
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
8 | # the Free Software Foundation; either version 2 of the License, or | |
9 | # (at your option) any later version. | |
10 | # | |
11 | # This program is distributed in the hope that it will be useful, but | |
12 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | # General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 | # USA | |
20 | # | |
21 | ||
22 | set -e | |
23 | ||
3b8ce6f6 RK |
24 | while [ $# -gt 0 ]; do |
25 | opt="$1" | |
26 | shift | |
27 | case "$opt" in | |
28 | -h | --help ) | |
29 | cat <<EOF | |
a64fa2a9 | 30 | |
3b8ce6f6 RK |
31 | Usage: |
32 | scripts/setup [OPTIONS] | |
33 | ||
34 | Options: | |
35 | --root ROOT Add a root (can be used multiple times) | |
36 | --encoding ENCODING Filename encoding | |
37 | --port PORT TCP port to listen on or 'none' | |
38 | --smtp-server HOSTNAME SMTP server | |
39 | --email ADDRESS Origin email address | |
40 | --register y|n Enable/disable online registration | |
a64fa2a9 RK |
41 | --play local|network Choose local or network play |
42 | --mcast ADDRESS PORT Set multicast address and port for --play network | |
3b8ce6f6 | 43 | -h, --help Display this message |
a64fa2a9 RK |
44 | |
45 | Sets up a basic DisOrder installation. You must have run 'make install' | |
46 | first. Use scripts/teardown to stop the server and deconfigure. | |
47 | ||
3b8ce6f6 RK |
48 | EOF |
49 | exit 0 | |
50 | ;; | |
a64fa2a9 RK |
51 | --version | -V ) |
52 | echo "DisOrder scripts/setup _version_" | |
53 | exit 0 | |
54 | ;; | |
3b8ce6f6 RK |
55 | --root ) |
56 | roots="$roots $1" | |
57 | shift | |
58 | ;; | |
59 | --encoding ) | |
60 | encoding="$1" | |
61 | shift | |
62 | ;; | |
63 | --port ) | |
64 | port="$1" | |
65 | shift | |
66 | ;; | |
67 | --smtp-server ) | |
68 | smtp_server="$1" | |
69 | shift | |
70 | ;; | |
71 | --email ) | |
72 | mail_sender="$1" | |
73 | shift | |
74 | ;; | |
75 | --register ) | |
76 | register="$1" | |
77 | shift | |
78 | ;; | |
a64fa2a9 RK |
79 | --play ) |
80 | play="$1" | |
81 | shift | |
82 | ;; | |
83 | --mcast ) | |
84 | play=network | |
85 | mcast_address="$1" | |
86 | shift | |
87 | mcast_port="$1" | |
88 | shift | |
89 | ;; | |
3b8ce6f6 RK |
90 | * ) |
91 | echo >&2 "ERROR: unknown option '$opt'" | |
92 | exit 1 | |
93 | ;; | |
94 | esac | |
95 | done | |
96 | ||
91370169 RK |
97 | echo |
98 | echo ------------------------------------------------------------------------ | |
99 | echo "DisOrder setup script" | |
100 | ||
101 | case $(uname -s) in | |
102 | Darwin ) | |
dda76819 RK |
103 | echo "Mac OS X detected" |
104 | os=Mac | |
d255d211 RK |
105 | user=jukebox |
106 | group=jukebox | |
91370169 | 107 | ;; |
dda76819 RK |
108 | FreeBSD ) |
109 | echo "FreeBSD detected" | |
110 | os=FreeBSD | |
111 | user=jukebox | |
112 | group=jukebox | |
113 | ;; | |
114 | Linux ) | |
115 | if grep Debian /etc/issue >/dev/null 2>&1; then | |
116 | echo "You appear to be running Debian - please use .debs instead" | |
ce195bb5 RK |
117 | echo |
118 | elif grep Ubuntu /etc/issue >/dev/null 2>&1; then | |
dda76819 | 119 | echo "You appear to be running Ubuntu - please use .debs instead" |
ce195bb5 | 120 | echo |
dda76819 RK |
121 | fi |
122 | echo "Linux detected" | |
123 | os=Linux | |
124 | user=jukebox | |
125 | group=jukebox | |
126 | ;; | |
91370169 | 127 | * ) |
dda76819 RK |
128 | echo |
129 | echo "WARNING: unknown operating system '$(uname -s)'" | |
130 | echo "This script won't be able to do all setup on this platform" | |
91370169 RK |
131 | os=unknown |
132 | user=daemon | |
133 | group=daemon | |
134 | ;; | |
135 | esac | |
136 | ||
137 | echo | |
138 | echo "This script will:" | |
139 | echo " - overwrite any existing configuration" | |
1a8b03f3 RK |
140 | echo " - set the server up to be run at boot time" |
141 | echo " - start the server" | |
142 | echo " - set up the web interface" | |
91370169 RK |
143 | echo |
144 | echo "If this is not what you want, press ^C." | |
145 | echo ------------------------------------------------------------------------ | |
146 | ||
3b8ce6f6 RK |
147 | if [ -z "$roots" ]; then |
148 | while :; do | |
149 | echo | |
150 | echo "What directory or directories contain your music files:" | |
151 | echo "(enter one or more directories separated by spaces)" | |
152 | read -r roots | |
153 | ok=true | |
154 | for root in $roots; do | |
155 | if [ ! -d $root ]; then | |
156 | echo "'$root' does not exist" | |
157 | ok=false | |
158 | fi | |
159 | done | |
160 | if $ok; then | |
161 | break | |
91370169 RK |
162 | fi |
163 | done | |
3b8ce6f6 | 164 | fi |
91370169 RK |
165 | |
166 | if [ -z "$encoding" ]; then | |
167 | echo | |
168 | echo "What filesystem encoding should I assume for track names?" | |
169 | echo "(e.g. UTF-8, ISO-8859-1, ...)" | |
170 | read -r encoding | |
171 | fi | |
172 | ||
3b8ce6f6 RK |
173 | if [ -z "$port" ]; then |
174 | while :; do | |
175 | echo | |
176 | echo "What TCP port should DisOrder listen on?" | |
177 | echo "(enter 'none' for none)" | |
178 | read -r port | |
179 | case $port in | |
180 | none ) | |
181 | break | |
182 | ;; | |
183 | [^0-9] ) | |
184 | echo "'$port' is not a valid port number" | |
185 | continue | |
186 | ;; | |
187 | * ) | |
188 | break | |
189 | ;; | |
190 | esac | |
191 | done | |
192 | fi | |
193 | ||
a64fa2a9 RK |
194 | if [ -z "$play" ]; then |
195 | while :; do | |
196 | echo | |
197 | echo "How do you want to play sound? Enter 'local' to use a local sound" | |
198 | echo "device or 'network' to multicast sound across your network." | |
199 | read -r play | |
200 | case $play in | |
201 | 'local' | network ) | |
202 | break | |
203 | ;; | |
204 | * ) | |
205 | echo "Enter 'local' or 'network'" | |
206 | continue | |
207 | ;; | |
208 | esac | |
209 | done | |
210 | fi | |
211 | ||
212 | if [ "x$play" = xnetwork ]; then | |
213 | if [ -z "$mcast_address" ]; then | |
214 | echo | |
215 | echo "Enter destination address for network transmission" | |
216 | echo "(e.g. a multicast address)" | |
217 | read -r mcast_address | |
218 | fi | |
219 | if [ -z "$mcast_port" ]; then | |
220 | while :; do | |
221 | echo | |
222 | echo "Enter destination port for network transmission" | |
223 | read -r mcast_port | |
224 | case $mcast_port in | |
225 | none ) | |
226 | break | |
227 | ;; | |
228 | [^0-9] ) | |
229 | echo "'$mcast_port' is not a valid port number" | |
230 | continue | |
231 | ;; | |
232 | * ) | |
233 | break | |
234 | ;; | |
235 | esac | |
236 | done | |
237 | fi | |
238 | fi | |
239 | ||
3b8ce6f6 RK |
240 | if [ -z "$mail_sender" ]; then |
241 | while :; do | |
242 | echo | |
243 | echo "What address should mail from DisOrder come from?" | |
244 | read -r mail_sender | |
245 | case "$mail_sender" in | |
246 | *@* ) | |
247 | break | |
248 | ;; | |
249 | * ) | |
250 | echo "Email address must contain an '@' sign" | |
251 | ;; | |
252 | esac | |
253 | done | |
254 | fi | |
ce195bb5 | 255 | |
3b8ce6f6 RK |
256 | if [ -z "$register" ]; then |
257 | while :; do | |
258 | echo | |
259 | echo "Do you want to enable online registration? (Enter 'y' or 'n')" | |
260 | read -r register | |
6f52f7c5 | 261 | case $register in |
3b8ce6f6 RK |
262 | y | n ) |
263 | break | |
264 | ;; | |
265 | esac | |
266 | done | |
267 | fi | |
91370169 RK |
268 | |
269 | echo | |
270 | echo "Proposed DisOrder setup:" | |
271 | echo " Music directory: $roots" | |
272 | if [ $port = none ]; then | |
273 | echo " Do not listen on a TCP port" | |
274 | else | |
275 | echo " TCP port to listen on: $port" | |
276 | fi | |
6f52f7c5 RK |
277 | if [ ! -z "$smtp_server" ]; then |
278 | echo " SMTP Server: $smtp_server" | |
279 | fi | |
91370169 | 280 | echo " Sender address: $mail_sender" |
3b8ce6f6 | 281 | echo " Online registration: $register" |
a64fa2a9 RK |
282 | if [ $play = network ]; then |
283 | echo " Send sound to: $mcast_address port $mcast_port" | |
284 | fi | |
91370169 RK |
285 | |
286 | echo "Is this OK? (Enter 'y' or 'n')" | |
287 | read -r ok | |
288 | case $ok in | |
289 | y ) | |
290 | ;; | |
291 | * ) | |
292 | echo | |
293 | echo "OK, didn't change anything." | |
294 | exit 0 | |
295 | ;; | |
296 | esac | |
297 | ||
298 | mkdir -p pkgconfdir | |
299 | ||
300 | rm -f pkgconfdir/config.new | |
301 | for root in $roots; do | |
302 | echo "collection fs $encoding $root" >> pkgconfdir/config.new | |
303 | done | |
304 | for scratch in slap.ogg scratch.ogg; do | |
305 | echo "scratch pkgdatadir/$scratch" >> pkgconfdir/config.new | |
306 | done | |
307 | echo "user $user" >> pkgconfdir/config.new | |
308 | if [ $port != none ]; then | |
309 | echo "listen 0.0.0.0 $port" >> pkgconfdir/config.new | |
a64fa2a9 RK |
310 | fi |
311 | if [ $play = network ]; then | |
312 | echo "broadcast $mcast_address $mcast_port" >> pkgconfdir/config.new | |
91370169 | 313 | fi |
6f52f7c5 RK |
314 | if [ ! -z "$smtp_server" ]; then |
315 | echo "smtp_server $smtp_server" >> pkgconfdir/config.new | |
316 | fi | |
919c5c40 | 317 | echo "mail_sender $mail_sender" >> pkgconfdir/config.new |
91370169 RK |
318 | |
319 | echo | |
320 | echo "Proposed pkgconfdir/config:" | |
321 | sed < pkgconfdir/config.new 's/^/ /' | |
322 | echo | |
323 | echo "Is this OK? (Enter 'y' or 'n')" | |
324 | read -r ok | |
325 | case $ok in | |
326 | y ) | |
327 | ;; | |
328 | * ) | |
329 | echo | |
330 | echo "OK, not installing it." | |
331 | rm -f pkgconfdir/config.new | |
332 | exit 0 | |
333 | ;; | |
334 | esac | |
335 | echo | |
336 | echo "Installing pkgconfdir/config" | |
337 | mv pkgconfdir/config.new pkgconfdir/config | |
338 | ||
919c5c40 RK |
339 | if [ ! -f pkgconfdir/options.user ]; then |
340 | echo "Making sure pkgconfdir/options.user exists" | |
341 | touch pkgconfdir/options.user | |
342 | fi | |
343 | ||
d255d211 RK |
344 | # pick ID1 ID2 ... IDn |
345 | # Echoes an ID matching none of ID1..IDn | |
346 | pick() { | |
347 | local n | |
348 | n=250 # better not choose 0! | |
349 | while :; do | |
350 | ok=true | |
351 | for k in "$@"; do | |
352 | if [ $n = $k ]; then | |
353 | ok=false | |
354 | break | |
355 | fi | |
356 | done | |
357 | if $ok; then | |
358 | echo $n | |
359 | return | |
360 | fi | |
361 | n=$((1+$n)) | |
362 | done | |
363 | } | |
364 | ||
dda76819 RK |
365 | case $os in |
366 | Mac ) | |
d255d211 | 367 | # Apple don't seem to believe in creating a user as a discrete operation |
d7b0c55c | 368 | if dscl . -read /Groups/$group >/dev/null 2>&1; then |
d255d211 RK |
369 | echo "$group group already exists" |
370 | else | |
371 | echo "Creating $group group" | |
d7b0c55c | 372 | gids=$(dscl . -list /Groups PrimaryGroupID|awk '{print $2}') |
d255d211 RK |
373 | gid=$(pick $gids) |
374 | echo "(picked gid $gid)" | |
d7b0c55c RK |
375 | dscl . -create /Groups/$group |
376 | dscl . -create /Groups/$group PrimaryGroupID $gid | |
377 | dscl . -create /Groups/$group Password \* | |
d255d211 | 378 | fi |
d7b0c55c | 379 | if dscl . -read /Users/$user >/dev/null 2>&1; then |
d255d211 RK |
380 | echo "$user user already exists" |
381 | else | |
382 | echo "Creating $user user" | |
d7b0c55c | 383 | uids=$(dscl . -list /Users UniqueID|awk '{print $2}') |
d255d211 RK |
384 | uid=$(pick $uids) |
385 | echo "(picked uid $uid)" | |
d7b0c55c RK |
386 | gid=$(dscl . -read /Groups/$group PrimaryGroupID | awk '{print $2}') |
387 | dscl . -create /Users/$user | |
388 | dscl . -create /Users/$user UniqueID $uid | |
389 | dscl . -create /Users/$user UserShell /usr/bin/false | |
390 | dscl . -create /Users/$user RealName 'DisOrder server' | |
391 | dscl . -create /Users/$user NFSHomeDirectory pkgstatedir | |
392 | dscl . -create /Users/$user PrimaryGroupID $gid | |
393 | dscl . -create /Users/$user Password \* | |
d255d211 | 394 | fi |
dda76819 RK |
395 | ;; |
396 | FreeBSD ) | |
d255d211 | 397 | # FreeBSD has a simple well-documented interface |
dda76819 RK |
398 | if pw groupshow $group >/dev/null 2>&1; then |
399 | echo "$group group already exists" | |
400 | else | |
401 | echo "Creating $group group" | |
402 | pw groupadd $group | |
403 | fi | |
404 | if pw usershow $user >/dev/null 2>&1; then | |
405 | echo "$user user already exists" | |
406 | else | |
407 | echo "Creating $user user" | |
408 | pw useradd $user -w no -d pkgstatedir -g $group -c 'DisOrder user' | |
409 | fi | |
410 | ;; | |
411 | Linux ) | |
412 | if grep ^$group: /etc/group >/dev/null; then | |
413 | echo "$group group already exists" | |
414 | else | |
415 | echo "Creating $group group" | |
416 | groupadd $group | |
417 | fi | |
418 | if grep ^$user: /etc/passwd >/dev/null; then | |
419 | echo "$user user already exists" | |
420 | else | |
421 | echo "Creating $user user" | |
422 | useradd -d pkgstatedir -g $group $user -c 'DisOrder user' | |
423 | fi | |
424 | ;; | |
425 | esac | |
426 | ||
91370169 RK |
427 | echo "Making sure that pkgstatedir exists" |
428 | mkdir -p pkgstatedir | |
429 | chown $user:$group pkgstatedir | |
430 | chmod 2755 pkgstatedir | |
431 | ||
432 | case $os in | |
dda76819 RK |
433 | Mac ) |
434 | echo "Installing the plist into /Library/LaunchDaemons" | |
244348f8 | 435 | cp examples/uk.org.greenend.rjk.disorder.plist /Library/LaunchDaemons/. |
91370169 RK |
436 | echo "Reloading launchd" |
437 | launchctl load /Library/LaunchDaemons | |
438 | echo "Starting DisOrder server" | |
439 | launchctl start uk.org.greenend.rjk.disorder | |
64ac73bb | 440 | server_running=true |
91370169 | 441 | ;; |
06638b8d RK |
442 | FreeBSD ) |
443 | echo "Installing startup script into /etc/rc.d" | |
444 | install -m 555 examples/disorder.rc /etc/rc.d/disorder | |
445 | echo "Starting DisOrder server" | |
446 | /etc/rc.d/disorder start | |
ce195bb5 RK |
447 | server_running=true |
448 | ;; | |
449 | Linux ) | |
450 | echo "Looking for init scripts directory" | |
451 | for d in /etc/rc.d /etc; do | |
452 | if [ -d $d/init.d ]; then | |
453 | RC_D=$d | |
454 | break | |
455 | fi | |
456 | done | |
457 | if [ -z "$RC_D" ]; then | |
458 | echo "Cannot find your init scripts directory" | |
459 | else | |
460 | echo "Installing init script into $RC_D/init.d" | |
461 | install -m 755 examples/disorder.init $RC_D/init.d/disorder | |
462 | echo "Linking init script into $RC_D/rc*.d" | |
463 | for n in 2 3 4 5; do | |
464 | echo " $RC_D/rc$n.d/S99disorder -> $RC_D/init.d/disorder" | |
465 | rm -f $RC_D/rc$n.d/S99disorder | |
466 | ln -s $RC_D/init.d/disorder $RC_D/rc$n.d/S99disorder | |
467 | done | |
468 | for n in 0 1 6; do | |
469 | echo " $RC_D/rc$n.d/K01disorder -> $RC_D/init.d/disorder" | |
470 | rm -f $RC_D/rc$n.d/K01disorder | |
471 | ln -s $RC_D/init.d/disorder $RC_D/rc$n.d/K01disorder | |
472 | done | |
473 | echo "Starting DisOrder server" | |
474 | $RC_D/init.d/disorder start | |
475 | fi | |
ce195bb5 | 476 | server_running=true |
06638b8d | 477 | ;; |
91370169 | 478 | * ) |
ce195bb5 | 479 | echo |
91370169 RK |
480 | echo "Sorry, I don't know how to install the server on this platform." |
481 | echo "You will have to do that by hand." | |
ce195bb5 | 482 | server_running=false |
91370169 RK |
483 | ;; |
484 | esac | |
ce195bb5 | 485 | |
ce195bb5 | 486 | if $server_running; then |
1a8b03f3 | 487 | first=true |
4f70fa29 | 488 | sleep 5 |
1a8b03f3 RK |
489 | while ! disorder version >/dev/null 2>&1; do |
490 | if $first; then | |
491 | echo "Waiting for server startup to complete..." | |
492 | first=false | |
493 | fi | |
494 | sleep 1 | |
495 | done | |
3b8ce6f6 | 496 | if [ $register = y ]; then |
ce195bb5 RK |
497 | echo "Creating guest user with 'register' right" |
498 | disorder setup-guest | |
499 | else | |
500 | echo "Creating guest user without 'register' right" | |
501 | disorder setup-guest --no-online-registration | |
502 | fi | |
503 | fi | |
504 | ||
505 | echo | |
506 | echo Done |