| 1 | #! /usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | // Duplicated, this script has no includes. |
|---|
| 5 | |
|---|
| 6 | // Asks the user a question. |
|---|
| 7 | // $default is the default answer |
|---|
| 8 | function read_line($question, $default = null) { |
|---|
| 9 | if ($default === null) { |
|---|
| 10 | echo "$question "; |
|---|
| 11 | } else { |
|---|
| 12 | echo "$question"." (default:$default) "; |
|---|
| 13 | } |
|---|
| 14 | $r = trim(fgets(STDIN)); |
|---|
| 15 | if ($r == "") { |
|---|
| 16 | $r = $default; |
|---|
| 17 | } |
|---|
| 18 | return $r; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | // Same as read_line, but returns true/false. |
|---|
| 22 | // default must be true/false, or null. |
|---|
| 23 | function read_bool($question, $default = null) { |
|---|
| 24 | while (true) { |
|---|
| 25 | if ($default === null) { |
|---|
| 26 | $answer = read_line($question); |
|---|
| 27 | } else if ($default) { |
|---|
| 28 | $answer = read_line($question, "yes"); |
|---|
| 29 | } else { |
|---|
| 30 | $answer = read_line($question, "no"); |
|---|
| 31 | } |
|---|
| 32 | if (preg_match("/^(true|y|yes|da)$/i", $answer)) { |
|---|
| 33 | return true; |
|---|
| 34 | } |
|---|
| 35 | if (preg_match("/^(false|n|no|nu)$/i", $answer)) { |
|---|
| 36 | return false; |
|---|
| 37 | } |
|---|
| 38 | echo "Answer with true/false/yes/no/etc.\n"; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // Add slashes to a string. |
|---|
| 43 | function slash_string($string, $start, $end) |
|---|
| 44 | { |
|---|
| 45 | // Strip start/end slash |
|---|
| 46 | if ($string[0] == '/') { |
|---|
| 47 | $string = substr($string, 1); |
|---|
| 48 | } |
|---|
| 49 | if ($string[strlen($string) - 1] == '/') { |
|---|
| 50 | $string = substr($string, 0, strlen($string) - 1); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | // Add start/end slash |
|---|
| 54 | if ($start) { |
|---|
| 55 | $string = '/'.$string; |
|---|
| 56 | } |
|---|
| 57 | if ($end) { |
|---|
| 58 | $string = $string.'/'; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // Tada. |
|---|
| 62 | return $string; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // Checks if the script is running as root. |
|---|
| 66 | function running_as_root() |
|---|
| 67 | { |
|---|
| 68 | return posix_getuid() == 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | // Replace config values in $srcfile and copy to $dstfile |
|---|
| 72 | function handle_config_file($vars, $srcfile, $dstfile) |
|---|
| 73 | { |
|---|
| 74 | $contents = file_get_contents($srcfile); |
|---|
| 75 | foreach ($vars as $k => $v) { |
|---|
| 76 | $contents = str_replace("--write-me-$k--", $v, $contents); |
|---|
| 77 | } |
|---|
| 78 | file_put_contents($dstfile, $contents); |
|---|
| 79 | |
|---|
| 80 | // FIXME perms when running as root |
|---|
| 81 | if (running_as_root()) { |
|---|
| 82 | chown($dstfile, getmyuid()); |
|---|
| 83 | chgrp($dstfile, getmygid()); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // Compiles longest common substrig written in c++ |
|---|
| 88 | function compile_lcs($IA_ROOT_DIR) |
|---|
| 89 | { |
|---|
| 90 | $command = "g++ " . $IA_ROOT_DIR . "common/lcs.cpp -O2 -static -o " . |
|---|
| 91 | $IA_ROOT_DIR . "common/lcs"; |
|---|
| 92 | |
|---|
| 93 | $last_line = system($command, $ret_val); |
|---|
| 94 | |
|---|
| 95 | if ($ret_val) { // check for errors |
|---|
| 96 | print("\nWARNING!!! There has been a problem when trying to compile a " . |
|---|
| 97 | "cpp source! Check your gcc/g++ installation then rerun " . |
|---|
| 98 | "the script.\nThis will not affect the setup except for " . |
|---|
| 99 | "comparing revisons, but you should take a look at this!\n\n"); |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return 1; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // --- |
|---|
| 107 | // Real code starts here |
|---|
| 108 | // --- |
|---|
| 109 | |
|---|
| 110 | print("\nHello, I am the infoarena setup script and I will try to guide ". |
|---|
| 111 | "you through setting up an infoarena developement working copy.\n"); |
|---|
| 112 | |
|---|
| 113 | if (running_as_root()) { |
|---|
| 114 | print("I see you are running as root, this is good.\n"); |
|---|
| 115 | } else { |
|---|
| 116 | print("You are not root. This is fine with me, but you'll probably ". |
|---|
| 117 | "have to do some thing by hand(as root)\n"); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | print("I'm going to ask you a couple of questions. Just press enter if you like ". |
|---|
| 121 | "the default value. I'm pretty good at guessing ;).\n\n"); |
|---|
| 122 | |
|---|
| 123 | // FIXME: Check dependencies? pecl nonsense is no longer mandatory. |
|---|
| 124 | // It should be done it setup.sh anyway, since we can't relie on php-cli. |
|---|
| 125 | |
|---|
| 126 | // Initialize config vars. |
|---|
| 127 | $config_vars = array(); |
|---|
| 128 | $config_vars['IA_ROOT_DIR'] = realpath(dirname($argv[0]) . '/../') . '/'; |
|---|
| 129 | $config_vars['IA_URL_HOST'] = 'http://localhost'; |
|---|
| 130 | $config_vars['IA_URL_PREFIX'] = '/infoarena2-dev/'; |
|---|
| 131 | $config_vars['IA_DB_HOST'] = 'localhost'; |
|---|
| 132 | $config_vars['IA_DB_USER'] = 'root'; |
|---|
| 133 | $config_vars['IA_DB_PASS'] = ''; |
|---|
| 134 | $config_vars['IA_DB_NAME'] = 'infoarena2'; |
|---|
| 135 | |
|---|
| 136 | // Ask user. |
|---|
| 137 | $config_vars['IA_ROOT_DIR'] = read_line("SVN checkout dir?", |
|---|
| 138 | $config_vars['IA_ROOT_DIR']); |
|---|
| 139 | $config_vars['IA_ROOT_DIR'] = slash_string( |
|---|
| 140 | realpath($config_vars['IA_ROOT_DIR']), true, true); |
|---|
| 141 | $config_vars['IA_AVATAR_FOLDER'] = |
|---|
| 142 | $config_vars['IA_ROOT_DIR'].'www/static/images/avatar/'; |
|---|
| 143 | $config_vars['IA_AVATAR_FOLDER'] = read_line("Avatar Folder?", |
|---|
| 144 | $config_vars['IA_AVATAR_FOLDER']); |
|---|
| 145 | $config_vars['IA_AVATAR_FOLDER'] = slash_string( |
|---|
| 146 | $config_vars['IA_AVATAR_FOLDER'], true, true); |
|---|
| 147 | $config_vars['IA_URL_HOST'] = read_line("Host part of url (with http)?", |
|---|
| 148 | $config_vars['IA_URL_HOST']); |
|---|
| 149 | $config_vars['IA_URL_HOST'] = slash_string( |
|---|
| 150 | $config_vars['IA_URL_HOST'], false, false); |
|---|
| 151 | $config_vars['IA_URL_PREFIX'] = read_line("Prefix part of url?", |
|---|
| 152 | $config_vars['IA_URL_PREFIX']); |
|---|
| 153 | $config_vars['IA_URL_PREFIX'] = slash_string( |
|---|
| 154 | $config_vars['IA_URL_PREFIX'], true, true); |
|---|
| 155 | |
|---|
| 156 | // Compile needed files |
|---|
| 157 | compile_lcs($config_vars['IA_ROOT_DIR']); |
|---|
| 158 | |
|---|
| 159 | // Database configuration here. |
|---|
| 160 | while (true) { |
|---|
| 161 | $config_vars['IA_DB_HOST'] = read_line("Database host?", |
|---|
| 162 | $config_vars['IA_DB_HOST']); |
|---|
| 163 | $config_vars['IA_DB_USER'] = read_line("Database connection username?", |
|---|
| 164 | $config_vars['IA_DB_USER']); |
|---|
| 165 | $config_vars['IA_DB_PASS'] = read_line("Database password?", |
|---|
| 166 | $config_vars['IA_DB_PASS']); |
|---|
| 167 | $config_vars['IA_DB_NAME'] = read_line("Database name?", |
|---|
| 168 | $config_vars['IA_DB_NAME']); |
|---|
| 169 | // FIXME: check database connection. |
|---|
| 170 | |
|---|
| 171 | $dblink = mysql_connect( |
|---|
| 172 | $config_vars['IA_DB_HOST'], |
|---|
| 173 | $config_vars['IA_DB_USER'], |
|---|
| 174 | $config_vars['IA_DB_PASS']); |
|---|
| 175 | |
|---|
| 176 | if (!$dblink) { |
|---|
| 177 | print("Can't connect to database, something must be wrong.\n"); |
|---|
| 178 | if (read_bool("Try again or ignore (CTRL-C to abort)?", true)) { |
|---|
| 179 | continue; |
|---|
| 180 | } else { |
|---|
| 181 | break; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if (!mysql_select_db($config_vars['IA_DB_NAME'], $dblink)) { |
|---|
| 186 | print("Can't select database.\n"); |
|---|
| 187 | if (read_bool("Should I try to create the database?", true)) { |
|---|
| 188 | if (!mysql_query("CREATE DATABASE {$config_vars['IA_DB_NAME']}")) { |
|---|
| 189 | die("Failed creating database, sorry."); |
|---|
| 190 | } |
|---|
| 191 | if (!mysql_select_db($config_vars['IA_DB_NAME'], $dblink)) { |
|---|
| 192 | die("Still can't select database.\n"); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | break; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | // Do the config monkey. |
|---|
| 200 | $ia_root = $config_vars['IA_ROOT_DIR']; |
|---|
| 201 | $ia_url = $config_vars['IA_URL_HOST'] . $config_vars['IA_URL_PREFIX']; |
|---|
| 202 | handle_config_file($config_vars, |
|---|
| 203 | $ia_root.'config.php.sample', $ia_root.'config.php'); |
|---|
| 204 | handle_config_file($config_vars, |
|---|
| 205 | $ia_root.'www/htaccess.sample', $ia_root.'www/.htaccess'); |
|---|
| 206 | handle_config_file($config_vars, |
|---|
| 207 | $ia_root.'eval/config.php.sample', $ia_root.'eval/config.php'); |
|---|
| 208 | handle_config_file($config_vars, |
|---|
| 209 | $ia_root.'apache.conf.sample', $ia_root.'apache.conf'); |
|---|
| 210 | |
|---|
| 211 | if ($dblink && read_bool("Should I try to import the sample database?", true)) { |
|---|
| 212 | $cmd = sprintf("mysql --user=%s --password=%s --host=%s %s < %s", |
|---|
| 213 | $config_vars['IA_DB_USER'], |
|---|
| 214 | $config_vars['IA_DB_PASS'], |
|---|
| 215 | $config_vars['IA_DB_HOST'], |
|---|
| 216 | $config_vars['IA_DB_NAME'], |
|---|
| 217 | $config_vars['IA_ROOT_DIR'] . "db.sql"); |
|---|
| 218 | print("Running $cmd\n"); |
|---|
| 219 | system($cmd); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | // Configure apache |
|---|
| 223 | if (!running_as_root()) { |
|---|
| 224 | print("I'd try to configure apache, but I'm not root.\n"); |
|---|
| 225 | } |
|---|
| 226 | if (running_as_root() && |
|---|
| 227 | read_bool("Should I try to configure apache for you?", true)) { |
|---|
| 228 | $sitename = slash_string($config_vars['IA_URL_PREFIX'], false, false); |
|---|
| 229 | $sitename = read_line("Site name?", $sitename); |
|---|
| 230 | system("chmod g+ws {$ia_root}attach"); |
|---|
| 231 | system("chmod g+ws {$ia_root}cache"); |
|---|
| 232 | system("chmod g+ws {$ia_root}www/static/images/tmp"); |
|---|
| 233 | system("chmod g+ws {$ia_root}www/static/images/latex"); |
|---|
| 234 | |
|---|
| 235 | $username = null; |
|---|
| 236 | if (preg_match('/\/home\/([^\/]*)/', IA_ROOT, $matches)) { |
|---|
| 237 | $username = $matches[1]; |
|---|
| 238 | } |
|---|
| 239 | // Debian/Ubuntu |
|---|
| 240 | if (is_dir('/etc/apache2/sites-available/') && |
|---|
| 241 | is_dir('/etc/apache2/sites-enabled/')) { |
|---|
| 242 | print("You seem to have a debian-ish apache2 setup.\n"); |
|---|
| 243 | system("rm -rf /etc/apache2/sites-available/$sitename"); |
|---|
| 244 | system("rm -rf /etc/apache2/sites-enabled/$sitename"); |
|---|
| 245 | system("ln -sf {$ia_root}apache.conf ". |
|---|
| 246 | "/etc/apache2/sites-available/$sitename"); |
|---|
| 247 | |
|---|
| 248 | // Enable site ad modules, then reload |
|---|
| 249 | system("a2enmod php5"); |
|---|
| 250 | system("a2enmod rewrite"); |
|---|
| 251 | system("a2ensite $sitename"); |
|---|
| 252 | system("/etc/init.d/apache2 reload"); |
|---|
| 253 | if ($username != null) { |
|---|
| 254 | system("adduser www-data {$username}"); |
|---|
| 255 | } |
|---|
| 256 | // Fedora/Redhat/others? |
|---|
| 257 | } else if (is_dir('/etc/httpd/conf.d/')) { |
|---|
| 258 | print('You seem to have a redhat-ish apache2 setup.\n'); |
|---|
| 259 | system("rm -rf /etc/httpd/infoarena2-dev"); |
|---|
| 260 | system("ln -sf {$ia_root}apache.conf /etc/httpd/conf.d/$sitename"); |
|---|
| 261 | system("service httpd restart"); |
|---|
| 262 | if ($username != null) { |
|---|
| 263 | system("usermod -G `id -g $username` -a apache"); |
|---|
| 264 | } |
|---|
| 265 | } else { |
|---|
| 266 | print("I can't figure out your system. I'm scared.\n"); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | // Running scripts to keep things simple for new developers |
|---|
| 271 | system("./{$ia_root}scripts/make-avatar-folder"); |
|---|
| 272 | // FIXME: configure forum |
|---|
| 273 | if (read_bool("Should I try to configure the forum (ugly db stuff)?", true)) { |
|---|
| 274 | system("{$ia_root}scripts/forum-fix"); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | print("FIXME: forum is not completely functional\n"); |
|---|
| 278 | print("FIXME: eval won't work, but it doesn't matter.\n"); |
|---|
| 279 | |
|---|
| 280 | ?> |
|---|