| 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 | exit 1 |
| 45 | fi |
| 46 | if grep Ubuntu /etc/issue >/dev/null 2>&1; then |
| 47 | echo "You appear to be running Ubuntu - please use .debs instead" |
| 48 | exit 1 |
| 49 | fi |
| 50 | echo "Linux detected" |
| 51 | os=Linux |
| 52 | user=jukebox |
| 53 | group=jukebox |
| 54 | ;; |
| 55 | * ) |
| 56 | echo |
| 57 | echo "WARNING: unknown operating system '$(uname -s)'" |
| 58 | echo "This script won't be able to do all setup on this platform" |
| 59 | os=unknown |
| 60 | user=daemon |
| 61 | group=daemon |
| 62 | ;; |
| 63 | esac |
| 64 | |
| 65 | echo |
| 66 | echo "This script will:" |
| 67 | echo " - overwrite any existing configuration" |
| 68 | case $os in |
| 69 | Mac ) |
| 70 | echo " - set the server up to be run at boot time" |
| 71 | echo " - start the server" |
| 72 | ;; |
| 73 | esac |
| 74 | echo |
| 75 | echo "If this is not what you want, press ^C." |
| 76 | echo ------------------------------------------------------------------------ |
| 77 | |
| 78 | while :; do |
| 79 | echo |
| 80 | echo "What directory or directories contain your music files:" |
| 81 | echo "(enter one or more directories separated by spaces)" |
| 82 | read -r roots |
| 83 | ok=true |
| 84 | for root in $roots; do |
| 85 | if [ ! -d $root ]; then |
| 86 | echo "'$root' does not exist" |
| 87 | ok=false |
| 88 | fi |
| 89 | done |
| 90 | if $ok; then |
| 91 | break |
| 92 | fi |
| 93 | done |
| 94 | |
| 95 | if [ -z "$encoding" ]; then |
| 96 | echo |
| 97 | echo "What filesystem encoding should I assume for track names?" |
| 98 | echo "(e.g. UTF-8, ISO-8859-1, ...)" |
| 99 | read -r encoding |
| 100 | fi |
| 101 | |
| 102 | while :; do |
| 103 | echo |
| 104 | echo "What TCP port should DisOrder listen on?" |
| 105 | echo "(enter 'none' for none)" |
| 106 | read -r port |
| 107 | case $port in |
| 108 | none ) |
| 109 | break |
| 110 | ;; |
| 111 | [^0-9] ) |
| 112 | echo "'$port' is not a valid port number" |
| 113 | continue |
| 114 | ;; |
| 115 | * ) |
| 116 | break |
| 117 | ;; |
| 118 | esac |
| 119 | done |
| 120 | |
| 121 | echo |
| 122 | echo "What host should DisOrder use as an SMTP server?" |
| 123 | read -r smtp_server |
| 124 | |
| 125 | echo |
| 126 | echo "What address should mail from DisOrder come from?" |
| 127 | read -r mail_sender |
| 128 | |
| 129 | echo |
| 130 | echo "Proposed DisOrder setup:" |
| 131 | echo " Music directory: $roots" |
| 132 | if [ $port = none ]; then |
| 133 | echo " Do not listen on a TCP port" |
| 134 | else |
| 135 | echo " TCP port to listen on: $port" |
| 136 | fi |
| 137 | echo " SMTP Server: $smtp_server" |
| 138 | echo " Sender address: $mail_sender" |
| 139 | |
| 140 | echo "Is this OK? (Enter 'y' or 'n')" |
| 141 | read -r ok |
| 142 | case $ok in |
| 143 | y ) |
| 144 | ;; |
| 145 | * ) |
| 146 | echo |
| 147 | echo "OK, didn't change anything." |
| 148 | exit 0 |
| 149 | ;; |
| 150 | esac |
| 151 | |
| 152 | mkdir -p pkgconfdir |
| 153 | |
| 154 | rm -f pkgconfdir/config.new |
| 155 | for root in $roots; do |
| 156 | echo "collection fs $encoding $root" >> pkgconfdir/config.new |
| 157 | done |
| 158 | for scratch in slap.ogg scratch.ogg; do |
| 159 | echo "scratch pkgdatadir/$scratch" >> pkgconfdir/config.new |
| 160 | done |
| 161 | echo "user $user" >> pkgconfdir/config.new |
| 162 | if [ $port != none ]; then |
| 163 | echo "listen 0.0.0.0 $port" >> pkgconfdir/config.new |
| 164 | fi |
| 165 | echo "smtp_server $smtp_server" >> pkgconfdir/config.new |
| 166 | echo "mail_sender $mail_sender" >> pkgconfdir/config.new |
| 167 | |
| 168 | echo |
| 169 | echo "Proposed pkgconfdir/config:" |
| 170 | sed < pkgconfdir/config.new 's/^/ /' |
| 171 | echo |
| 172 | echo "Is this OK? (Enter 'y' or 'n')" |
| 173 | read -r ok |
| 174 | case $ok in |
| 175 | y ) |
| 176 | ;; |
| 177 | * ) |
| 178 | echo |
| 179 | echo "OK, not installing it." |
| 180 | rm -f pkgconfdir/config.new |
| 181 | exit 0 |
| 182 | ;; |
| 183 | esac |
| 184 | echo |
| 185 | echo "Installing pkgconfdir/config" |
| 186 | mv pkgconfdir/config.new pkgconfdir/config |
| 187 | |
| 188 | if [ ! -f pkgconfdir/options.user ]; then |
| 189 | echo "Making sure pkgconfdir/options.user exists" |
| 190 | touch pkgconfdir/options.user |
| 191 | fi |
| 192 | |
| 193 | # pick ID1 ID2 ... IDn |
| 194 | # Echoes an ID matching none of ID1..IDn |
| 195 | pick() { |
| 196 | local n |
| 197 | n=250 # better not choose 0! |
| 198 | while :; do |
| 199 | ok=true |
| 200 | for k in "$@"; do |
| 201 | if [ $n = $k ]; then |
| 202 | ok=false |
| 203 | break |
| 204 | fi |
| 205 | done |
| 206 | if $ok; then |
| 207 | echo $n |
| 208 | return |
| 209 | fi |
| 210 | n=$((1+$n)) |
| 211 | done |
| 212 | } |
| 213 | |
| 214 | case $os in |
| 215 | Mac ) |
| 216 | # Apple don't seem to believe in creating a user as a discrete operation |
| 217 | if dscl / -read /Groups/$group >/dev/null 2>&1; then |
| 218 | echo "$group group already exists" |
| 219 | else |
| 220 | echo "Creating $group group" |
| 221 | gids=$(dscl / -list /Groups PrimaryGroupID|awk '{print $2}') |
| 222 | gid=$(pick $gids) |
| 223 | echo "(picked gid $gid)" |
| 224 | dscl / -create /Groups/$group |
| 225 | dscl / -create /Groups/$group PrimaryGroupID $gid |
| 226 | dscl / -create /Groups/$group Password \* |
| 227 | fi |
| 228 | if dscl / -read /Users/$user >/dev/null 2>&1; then |
| 229 | echo "$user user already exists" |
| 230 | else |
| 231 | echo "Creating $user user" |
| 232 | uids=$(dscl / -list /Users UniqueID|awk '{print $2}') |
| 233 | uid=$(pick $uids) |
| 234 | echo "(picked uid $uid)" |
| 235 | gid=$(dscl / -read /Groups/$group PrimaryGroupID | awk '{print $2}') |
| 236 | dscl / -create /Users/$user |
| 237 | dscl / -create /Users/$user UniqueID $uid |
| 238 | dscl / -create /Users/$user UserShell /usr/bin/false |
| 239 | dscl / -create /Users/$user RealName 'DisOrder server' |
| 240 | dscl / -create /Users/$user NFSHomeDirectory pkgstatedir |
| 241 | dscl / -create /Users/$user PrimaryGroupID $gid |
| 242 | dscl / -create /Users/$user Password \* |
| 243 | fi |
| 244 | ;; |
| 245 | FreeBSD ) |
| 246 | # FreeBSD has a simple well-documented interface |
| 247 | if pw groupshow $group >/dev/null 2>&1; then |
| 248 | echo "$group group already exists" |
| 249 | else |
| 250 | echo "Creating $group group" |
| 251 | pw groupadd $group |
| 252 | fi |
| 253 | if pw usershow $user >/dev/null 2>&1; then |
| 254 | echo "$user user already exists" |
| 255 | else |
| 256 | echo "Creating $user user" |
| 257 | pw useradd $user -w no -d pkgstatedir -g $group -c 'DisOrder user' |
| 258 | fi |
| 259 | ;; |
| 260 | Linux ) |
| 261 | if grep ^$group: /etc/group >/dev/null; then |
| 262 | echo "$group group already exists" |
| 263 | else |
| 264 | echo "Creating $group group" |
| 265 | groupadd $group |
| 266 | fi |
| 267 | if grep ^$user: /etc/passwd >/dev/null; then |
| 268 | echo "$user user already exists" |
| 269 | else |
| 270 | echo "Creating $user user" |
| 271 | useradd -d pkgstatedir -g $group $user -c 'DisOrder user' |
| 272 | fi |
| 273 | ;; |
| 274 | esac |
| 275 | |
| 276 | echo "Making sure that pkgstatedir exists" |
| 277 | mkdir -p pkgstatedir |
| 278 | chown $user:$group pkgstatedir |
| 279 | chmod 2755 pkgstatedir |
| 280 | |
| 281 | case $os in |
| 282 | Mac ) |
| 283 | echo "Installing the plist into /Library/LaunchDaemons" |
| 284 | cp examples/uk.org.greenend.rjk.disorder.plist /Library/LaunchDaemons/. |
| 285 | echo "Reloading launchd" |
| 286 | launchctl load /Library/LaunchDaemons |
| 287 | echo "Starting DisOrder server" |
| 288 | launchctl start uk.org.greenend.rjk.disorder |
| 289 | echo "Installing CGI" |
| 290 | install -m 555 server/disorder.cgi /Library/WebServer/CGI-Executables/disorder |
| 291 | echo "Setting up link to CGI's dependencies" |
| 292 | rm -f /Library/WebServer/Documents/disorder |
| 293 | ln -s pkgdatadir/static /Library/WebServer/Documents/disorder |
| 294 | echo |
| 295 | echo "You must sudo disorder setup-guest [--no-online-registration] next." |
| 296 | ;; |
| 297 | FreeBSD ) |
| 298 | echo "Installing startup script into /etc/rc.d" |
| 299 | install -m 555 examples/disorder.rc /etc/rc.d/disorder |
| 300 | echo "Starting DisOrder server" |
| 301 | /etc/rc.d/disorder start |
| 302 | echo "Identifying web server" |
| 303 | set /usr/local/www/* |
| 304 | case $# in |
| 305 | 0 ) |
| 306 | echo |
| 307 | echo "Could not find a web server" |
| 308 | exit 1 |
| 309 | ;; |
| 310 | 1 ) |
| 311 | ;; |
| 312 | * ) |
| 313 | echo |
| 314 | echo "Yikes! There seems to be more than one web server here." |
| 315 | echo "Guessing that you want $1." |
| 316 | echo |
| 317 | ;; |
| 318 | esac |
| 319 | web=$1 |
| 320 | echo "Found $web" |
| 321 | echo "Installing CGI" |
| 322 | install -m 555 server/disorder.cgi $web/cgi-bin/disorder |
| 323 | echo "Setting up link to CGI's dependencies" |
| 324 | rm -f $web/data/disorder |
| 325 | ln -s pkgdatadir/static $web/data/disorder |
| 326 | echo |
| 327 | echo "You must sudo disorder setup-guest [--no-online-registration] next." |
| 328 | ;; |
| 329 | * ) |
| 330 | echo "Sorry, I don't know how to install the server on this platform." |
| 331 | echo "You will have to do that by hand." |
| 332 | exit 1 |
| 333 | ;; |
| 334 | esac |