chiark / gitweb /
803b0ae1c9fd4c28cef8d7911995ca16d3b67ebf
[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 case $os in
68 Mac )
69   echo " - set the server up to be run at boot time"
70   echo " - start the server"
71   ;;
72 esac
73 echo
74 echo "If this is not what you want, press ^C."
75 echo ------------------------------------------------------------------------
76
77 while :; 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
92 done
93
94 if [ -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
99 fi
100
101 while :; 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
118 done
119
120 echo
121 echo "What host should DisOrder use as an SMTP server?"
122 read -r smtp_server
123
124 while :; 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
136 done
137
138 echo
139 echo "Proposed DisOrder setup:"
140 echo " Music directory:       $roots"
141 if [ $port = none ]; then
142   echo " Do not listen on a TCP port"
143 else
144   echo " TCP port to listen on: $port"
145 fi
146 echo " SMTP Server:           $smtp_server"
147 echo " Sender address:        $mail_sender"
148
149 echo "Is this OK?  (Enter 'y' or 'n')"
150 read -r ok
151 case $ok in
152 y )
153   ;;
154 * )
155   echo
156   echo "OK, didn't change anything."
157   exit 0
158   ;;
159 esac
160
161 mkdir -p pkgconfdir
162
163 rm -f pkgconfdir/config.new
164 for root in $roots; do
165   echo "collection fs $encoding $root" >> pkgconfdir/config.new
166 done
167 for scratch in slap.ogg scratch.ogg; do
168   echo "scratch pkgdatadir/$scratch" >> pkgconfdir/config.new
169 done
170 echo "user $user" >> pkgconfdir/config.new
171 if [ $port != none ]; then
172   echo "listen 0.0.0.0 $port" >> pkgconfdir/config.new
173 fi
174 echo "smtp_server $smtp_server" >> pkgconfdir/config.new
175 echo "mail_sender $mail_sender" >> pkgconfdir/config.new
176
177 echo
178 echo "Proposed pkgconfdir/config:"
179 sed < pkgconfdir/config.new 's/^/  /'
180 echo
181 echo "Is this OK?  (Enter 'y' or 'n')"
182 read -r ok
183 case $ok in
184 y )
185   ;;
186 * )
187   echo
188   echo "OK, not installing it."
189   rm -f pkgconfdir/config.new
190   exit 0
191   ;;
192 esac
193 echo
194 echo "Installing pkgconfdir/config"
195 mv pkgconfdir/config.new pkgconfdir/config
196
197 if [ ! -f pkgconfdir/options.user ]; then
198   echo "Making sure pkgconfdir/options.user exists"
199   touch pkgconfdir/options.user
200 fi
201
202 # pick ID1 ID2 ... IDn
203 # Echoes an ID matching none of ID1..IDn
204 pick() {
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
223 case $os in
224 Mac )
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
253   ;;
254 FreeBSD )
255   # FreeBSD has a simple well-documented interface
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   ;;
269 Linux )
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   ;;
283 esac
284
285 echo "Making sure that pkgstatedir exists"
286 mkdir -p pkgstatedir
287 chown $user:$group pkgstatedir
288 chmod 2755 pkgstatedir
289
290 case $os in
291 Mac )
292   echo "Installing the plist into /Library/LaunchDaemons"
293   cp examples/uk.org.greenend.rjk.disorder.plist /Library/LaunchDaemons/.
294   echo "Reloading launchd"
295   launchctl load /Library/LaunchDaemons
296   echo "Starting DisOrder server"
297   launchctl start uk.org.greenend.rjk.disorder
298   CGIBIN=/Library/WebServer/CGI-Executables
299   DOCROOT=/Library/WebServer/Documents
300   sever_running=true
301   ;;
302 FreeBSD )
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"
326   CGIBIN=$web/cgi-bin
327   DOCROOT=$web/data
328   server_running=true
329   ;;
330 Linux )
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
372   ;;
373 * )
374   echo
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."
377   server_running=false
378   ;;
379 esac
380
381 echo
382 if [ -z "$DOCROOT" ]; then
383   echo "Cannot find your web server's document root"
384 else
385   echo "Setting up link to CGI's dependencies in $DOCROOT"
386   rm -f $DOCROOT/disorder
387   ln -s pkgdatadir/static $DOCROOT/disorder
388 fi
389
390 echo
391 if [ -z "$CGIBIN" ]; then
392   echo "Cannot find your web server's cgi-bin directory"
393 else
394   echo "Installing CGI in $CGIBIN"
395   install -m 555 server/disorder.cgi $CGIBIN/disorder
396 fi
397
398 if $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
417 fi
418
419 echo
420 echo Done