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