| 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 | # TODO we should use jukebox and create it if necessary; see below |
| 33 | user=daemon |
| 34 | group=daemon |
| 35 | ;; |
| 36 | FreeBSD ) |
| 37 | echo "FreeBSD detected" |
| 38 | os=FreeBSD |
| 39 | user=jukebox |
| 40 | group=jukebox |
| 41 | ;; |
| 42 | Linux ) |
| 43 | if grep Debian /etc/issue >/dev/null 2>&1; then |
| 44 | echo "You appear to be running Debian - please use .debs instead" |
| 45 | exit 1 |
| 46 | fi |
| 47 | if grep Ubuntu /etc/issue >/dev/null 2>&1; then |
| 48 | echo "You appear to be running Ubuntu - please use .debs instead" |
| 49 | exit 1 |
| 50 | fi |
| 51 | echo "Linux detected" |
| 52 | os=Linux |
| 53 | user=jukebox |
| 54 | group=jukebox |
| 55 | ;; |
| 56 | * ) |
| 57 | echo |
| 58 | echo "WARNING: unknown operating system '$(uname -s)'" |
| 59 | echo "This script won't be able to do all setup on this platform" |
| 60 | os=unknown |
| 61 | user=daemon |
| 62 | group=daemon |
| 63 | ;; |
| 64 | esac |
| 65 | |
| 66 | echo |
| 67 | echo "This script will:" |
| 68 | echo " - overwrite any existing configuration" |
| 69 | case $os in |
| 70 | Mac ) |
| 71 | echo " - set the server up to be run at boot time" |
| 72 | echo " - start the server" |
| 73 | ;; |
| 74 | esac |
| 75 | echo |
| 76 | echo "If this is not what you want, press ^C." |
| 77 | echo ------------------------------------------------------------------------ |
| 78 | |
| 79 | while :; do |
| 80 | echo |
| 81 | echo "What directory or directories contain your music files:" |
| 82 | echo "(enter one or more directories separated by spaces)" |
| 83 | read -r roots |
| 84 | ok=true |
| 85 | for root in $roots; do |
| 86 | if [ ! -d $root ]; then |
| 87 | echo "'$root' does not exist" |
| 88 | ok=false |
| 89 | fi |
| 90 | done |
| 91 | if $ok; then |
| 92 | break |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | if [ -z "$encoding" ]; then |
| 97 | echo |
| 98 | echo "What filesystem encoding should I assume for track names?" |
| 99 | echo "(e.g. UTF-8, ISO-8859-1, ...)" |
| 100 | read -r encoding |
| 101 | fi |
| 102 | |
| 103 | while :; do |
| 104 | echo |
| 105 | echo "What TCP port should DisOrder listen on?" |
| 106 | echo "(enter 'none' for none)" |
| 107 | read -r port |
| 108 | case $port in |
| 109 | none ) |
| 110 | break |
| 111 | ;; |
| 112 | [^0-9] ) |
| 113 | echo "'$port' is not a valid port number" |
| 114 | continue |
| 115 | ;; |
| 116 | * ) |
| 117 | break |
| 118 | ;; |
| 119 | esac |
| 120 | done |
| 121 | |
| 122 | echo |
| 123 | echo "What host should DisOrder use as an SMTP server?" |
| 124 | read -r smtp_server |
| 125 | |
| 126 | echo |
| 127 | echo "What address should mail from DisOrder come from?" |
| 128 | read -r mail_sender |
| 129 | |
| 130 | echo |
| 131 | echo "Proposed DisOrder setup:" |
| 132 | echo " Music directory: $roots" |
| 133 | if [ $port = none ]; then |
| 134 | echo " Do not listen on a TCP port" |
| 135 | else |
| 136 | echo " TCP port to listen on: $port" |
| 137 | fi |
| 138 | echo " SMTP Server: $smtp_server" |
| 139 | echo " Sender address: $mail_sender" |
| 140 | |
| 141 | echo "Is this OK? (Enter 'y' or 'n')" |
| 142 | read -r ok |
| 143 | case $ok in |
| 144 | y ) |
| 145 | ;; |
| 146 | * ) |
| 147 | echo |
| 148 | echo "OK, didn't change anything." |
| 149 | exit 0 |
| 150 | ;; |
| 151 | esac |
| 152 | |
| 153 | mkdir -p pkgconfdir |
| 154 | |
| 155 | rm -f pkgconfdir/config.new |
| 156 | for root in $roots; do |
| 157 | echo "collection fs $encoding $root" >> pkgconfdir/config.new |
| 158 | done |
| 159 | for scratch in slap.ogg scratch.ogg; do |
| 160 | echo "scratch pkgdatadir/$scratch" >> pkgconfdir/config.new |
| 161 | done |
| 162 | echo "user $user" >> pkgconfdir/config.new |
| 163 | if [ $port != none ]; then |
| 164 | echo "listen 0.0.0.0 $port" >> pkgconfdir/config.new |
| 165 | fi |
| 166 | echo "smtp_server $smtp_server" >> pkgconfdir/config.new |
| 167 | echo "mail_sender $mail_sender" >> pkgconfdir/config.new |
| 168 | |
| 169 | echo |
| 170 | echo "Proposed pkgconfdir/config:" |
| 171 | sed < pkgconfdir/config.new 's/^/ /' |
| 172 | echo |
| 173 | echo "Is this OK? (Enter 'y' or 'n')" |
| 174 | read -r ok |
| 175 | case $ok in |
| 176 | y ) |
| 177 | ;; |
| 178 | * ) |
| 179 | echo |
| 180 | echo "OK, not installing it." |
| 181 | rm -f pkgconfdir/config.new |
| 182 | exit 0 |
| 183 | ;; |
| 184 | esac |
| 185 | echo |
| 186 | echo "Installing pkgconfdir/config" |
| 187 | mv pkgconfdir/config.new pkgconfdir/config |
| 188 | |
| 189 | if [ ! -f pkgconfdir/options.user ]; then |
| 190 | echo "Making sure pkgconfdir/options.user exists" |
| 191 | touch pkgconfdir/options.user |
| 192 | fi |
| 193 | |
| 194 | case $os in |
| 195 | Mac ) |
| 196 | # TODO niutil? |
| 197 | ;; |
| 198 | FreeBSD ) |
| 199 | if pw groupshow $group >/dev/null 2>&1; then |
| 200 | echo "$group group already exists" |
| 201 | else |
| 202 | echo "Creating $group group" |
| 203 | pw groupadd $group |
| 204 | fi |
| 205 | if pw usershow $user >/dev/null 2>&1; then |
| 206 | echo "$user user already exists" |
| 207 | else |
| 208 | echo "Creating $user user" |
| 209 | pw useradd $user -w no -d pkgstatedir -g $group -c 'DisOrder user' |
| 210 | fi |
| 211 | ;; |
| 212 | Linux ) |
| 213 | if grep ^$group: /etc/group >/dev/null; then |
| 214 | echo "$group group already exists" |
| 215 | else |
| 216 | echo "Creating $group group" |
| 217 | groupadd $group |
| 218 | fi |
| 219 | if grep ^$user: /etc/passwd >/dev/null; then |
| 220 | echo "$user user already exists" |
| 221 | else |
| 222 | echo "Creating $user user" |
| 223 | useradd -d pkgstatedir -g $group $user -c 'DisOrder user' |
| 224 | fi |
| 225 | ;; |
| 226 | esac |
| 227 | |
| 228 | echo "Making sure that pkgstatedir exists" |
| 229 | mkdir -p pkgstatedir |
| 230 | chown $user:$group pkgstatedir |
| 231 | chmod 2755 pkgstatedir |
| 232 | |
| 233 | case $os in |
| 234 | Mac ) |
| 235 | echo "Installing the plist into /Library/LaunchDaemons" |
| 236 | cp server/uk.org.greenend.rjk.disorder.plist /Library/LaunchDaemons/. |
| 237 | echo "Reloading launchd" |
| 238 | launchctl load /Library/LaunchDaemons |
| 239 | echo "Starting DisOrder server" |
| 240 | launchctl start uk.org.greenend.rjk.disorder |
| 241 | echo "Installing CGI" |
| 242 | install -m 555 server/disorder.cgi /Library/WebServer/CGI-Executables/disorder |
| 243 | echo "Setting up link to CGI;'s dependencies" |
| 244 | rm -f /Library/WebServer/Documents/disorder |
| 245 | ln -s pkgdatadir/static /Library/WebServer/Documents/disorder |
| 246 | echo |
| 247 | echo "You must sudo disorder setup-guest [--no-online-registration] next." |
| 248 | ;; |
| 249 | * ) |
| 250 | echo "Sorry, I don't know how to install the server on this platform." |
| 251 | echo "You will have to do that by hand." |
| 252 | exit 1 |
| 253 | ;; |
| 254 | esac |