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