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