chiark / gitweb /
e89ddb4f78e641dbd4795c20822083e4f318d4ae
[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 while [ $# -gt 0 ]; do
25   opt="$1"
26   shift
27   case "$opt" in
28   -h | --help )
29     cat  <<EOF
30 Usage:
31   scripts/setup [OPTIONS]
32
33 Options:
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
41 EOF
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
73 done
74
75 echo
76 echo ------------------------------------------------------------------------
77 echo "DisOrder setup script"
78
79 case $(uname -s) in
80 Darwin )
81   echo "Mac OS X detected"
82   os=Mac
83   user=jukebox
84   group=jukebox
85   ;;
86 FreeBSD )
87   echo "FreeBSD detected"
88   os=FreeBSD
89   user=jukebox
90   group=jukebox
91   ;;
92 Linux )
93   if grep Debian /etc/issue >/dev/null 2>&1; then
94     echo "You appear to be running Debian - please use .debs instead"
95     echo
96   elif grep Ubuntu /etc/issue >/dev/null 2>&1; then
97     echo "You appear to be running Ubuntu - please use .debs instead"
98     echo
99   fi
100   echo "Linux detected"
101   os=Linux
102   user=jukebox
103   group=jukebox
104   ;;
105 * )
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"
109   os=unknown
110   user=daemon
111   group=daemon
112   ;;
113 esac
114
115 echo
116 echo "This script will:"
117 echo " - overwrite any existing configuration"
118 echo " - set the server up to be run at boot time"
119 echo " - start the server"
120 echo " - set up the web interface"
121 echo
122 echo "If this is not what you want, press ^C."
123 echo ------------------------------------------------------------------------
124
125 if [ -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
140     fi
141   done
142 fi
143
144 if [ -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
149 fi
150
151 if [ -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
170 fi
171
172 if [ -z "$smtp_server" ]; then
173   echo
174   echo "What host should DisOrder use as an SMTP server?"
175   read -r smtp_server
176 fi
177
178 if [ -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
192 fi
193
194 if [ -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
205 fi
206
207 echo
208 echo "Proposed DisOrder setup:"
209 echo " Music directory:       $roots"
210 if [ $port = none ]; then
211   echo " Do not listen on a TCP port"
212 else
213   echo " TCP port to listen on: $port"
214 fi
215 echo " SMTP Server:           $smtp_server"
216 echo " Sender address:        $mail_sender"
217 echo " Online registration:   $register"
218
219 echo "Is this OK?  (Enter 'y' or 'n')"
220 read -r ok
221 case $ok in
222 y )
223   ;;
224 * )
225   echo
226   echo "OK, didn't change anything."
227   exit 0
228   ;;
229 esac
230
231 mkdir -p pkgconfdir
232
233 rm -f pkgconfdir/config.new
234 for root in $roots; do
235   echo "collection fs $encoding $root" >> pkgconfdir/config.new
236 done
237 for scratch in slap.ogg scratch.ogg; do
238   echo "scratch pkgdatadir/$scratch" >> pkgconfdir/config.new
239 done
240 echo "user $user" >> pkgconfdir/config.new
241 if [ $port != none ]; then
242   echo "listen 0.0.0.0 $port" >> pkgconfdir/config.new
243 fi
244 echo "smtp_server $smtp_server" >> pkgconfdir/config.new
245 echo "mail_sender $mail_sender" >> pkgconfdir/config.new
246
247 echo
248 echo "Proposed pkgconfdir/config:"
249 sed < pkgconfdir/config.new 's/^/  /'
250 echo
251 echo "Is this OK?  (Enter 'y' or 'n')"
252 read -r ok
253 case $ok in
254 y )
255   ;;
256 * )
257   echo
258   echo "OK, not installing it."
259   rm -f pkgconfdir/config.new
260   exit 0
261   ;;
262 esac
263 echo
264 echo "Installing pkgconfdir/config"
265 mv pkgconfdir/config.new pkgconfdir/config
266
267 if [ ! -f pkgconfdir/options.user ]; then
268   echo "Making sure pkgconfdir/options.user exists"
269   touch pkgconfdir/options.user
270 fi
271
272 # pick ID1 ID2 ... IDn
273 # Echoes an ID matching none of ID1..IDn
274 pick() {
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
293 case $os in
294 Mac )
295   # Apple don't seem to believe in creating a user as a discrete operation
296   if dscl . -read /Groups/$group >/dev/null 2>&1; then
297     echo "$group group already exists"
298   else
299     echo "Creating $group group"
300     gids=$(dscl . -list /Groups PrimaryGroupID|awk '{print $2}')
301     gid=$(pick $gids)
302     echo "(picked gid $gid)"
303     dscl . -create /Groups/$group
304     dscl . -create /Groups/$group PrimaryGroupID $gid
305     dscl . -create /Groups/$group Password \*
306   fi
307   if dscl . -read /Users/$user >/dev/null 2>&1; then
308     echo "$user user already exists"
309   else
310     echo "Creating $user user"
311     uids=$(dscl . -list /Users UniqueID|awk '{print $2}')
312     uid=$(pick $uids)
313     echo "(picked uid $uid)"
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 \*
322   fi
323   ;;
324 FreeBSD )
325   # FreeBSD has a simple well-documented interface
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   ;;
339 Linux )
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   ;;
353 esac
354
355 echo "Making sure that pkgstatedir exists"
356 mkdir -p pkgstatedir
357 chown $user:$group pkgstatedir
358 chmod 2755 pkgstatedir
359
360 case $os in
361 Mac )
362   echo "Installing the plist into /Library/LaunchDaemons"
363   cp examples/uk.org.greenend.rjk.disorder.plist /Library/LaunchDaemons/.
364   echo "Reloading launchd"
365   launchctl load /Library/LaunchDaemons
366   echo "Starting DisOrder server"
367   launchctl start uk.org.greenend.rjk.disorder
368   CGIBIN=/Library/WebServer/CGI-Executables
369   DOCROOT=/Library/WebServer/Documents
370   sever_running=true
371   ;;
372 FreeBSD )
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"
396   CGIBIN=$web/cgi-bin
397   DOCROOT=$web/data
398   server_running=true
399   ;;
400 Linux )
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
442   ;;
443 * )
444   echo
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."
447   server_running=false
448   ;;
449 esac
450
451 echo
452 if [ -z "$DOCROOT" ]; then
453   echo "Cannot find your web server's document root"
454 else
455   echo "Setting up link to CGI's dependencies in $DOCROOT"
456   rm -f $DOCROOT/disorder
457   ln -s pkgdatadir/static $DOCROOT/disorder
458 fi
459
460 echo
461 if [ -z "$CGIBIN" ]; then
462   echo "Cannot find your web server's cgi-bin directory"
463 else
464   echo "Installing CGI in $CGIBIN"
465   install -m 555 server/disorder.cgi $CGIBIN/disorder
466 fi
467
468 if $server_running; then
469   first=true
470   sleep 5
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
478   if [ $register = y ]; then
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
485 fi
486
487 echo
488 echo Done