Index: trunk/locker/deploy/bin/rails
===================================================================
--- trunk/locker/deploy/bin/rails	(revision 2175)
+++ trunk/locker/deploy/bin/rails	(revision 2287)
@@ -28,6 +28,4 @@
         print "If you cannot determine the cause of the problem, please\n";
         print "feel free to contact sql\@mit.edu for assistance.\n";
-        open FAILED, ">.failed";
-        close FAILED;
         exit 1;
     }
@@ -43,5 +41,17 @@
 my $appdir = `basename $cwd`;
 chomp $appdir;
-my $appclass = ucfirst $appdir;
+
+open APPLICATION_RB, "config/application.rb";
+my $appclass;
+while(<APPLICATION_RB>) {
+    if (/module (\w+)\n/) {
+	$appclass = $1;
+	last;
+    }
+}
+close APPLICATION_RB;
+if (!$appclass) {
+    die "Couldn't find application class name - plase email scripts\@mit.edu with the names of your locker and the application you tried to create. Sorry!";
+}
 
 open PUBLIC_HTACCESS, ">public/.htaccess";
@@ -73,8 +83,8 @@
 #   RewriteBase /myrailsapp
 
+RewriteCond index.html -f
 RewriteRule ^\$ index.html [QSA]
-RewriteRule ^([^.]+)\$ \$1.html [QSA]
 RewriteCond %{REQUEST_FILENAME} !-f
-RewriteRule ^(.*)\$ dispatch.fcgi [QSA,L]
+RewriteRule ^(.*)\$ dispatch.fcgi/\$1 [QSA,L]
 
 # In case Rails experiences terminal errors
@@ -84,5 +94,4 @@
 #   ErrorDocument 500 /500.html
 
-RewriteBase /$addrend/public/
 EOF
 
@@ -91,5 +100,5 @@
 RewriteEngine On
 RewriteRule ^(.*)\$ public/\$1 [QSA,L]
-RewriteBase /$addrend/
+
 EOF
 
@@ -98,5 +107,4 @@
 unshift @railsenv, "# Uncomment below to put Rails into production mode";
 unshift @railsenv, "";
-unshift @railsenv, "ENV['RAILS_RELATIVE_URL_ROOT'] = \"/$addrend\"";
 untie @railsenv;
 
@@ -119,4 +127,24 @@
 untie @railswelcome;
 
+# set config.action_controller.asset_host for all environments,
+# so urls to static assets are generated correctly
+# regardless of how the app is accessed
+my $rails_assethost = "  config.action_controller.asset_host = \"//$USER.scripts.mit.edu/$appdir/public\"";
+my @environments = ('development', 'production', 'test');
+
+for my $environment (@environments) {
+    tie my @envfile, 'Tie::File', "config/environments/$environment.rb";
+    my $i = 0;
+    for (@envfile) {
+	if (/^end$/) {
+	    last;
+	}
+	++$i;
+    }
+    splice @envfile, $i, 1, ($rails_assethost, 'end');
+    untie @envfile;
+}
+
+
 tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi';
 for (@railsfcgi) {
@@ -133,5 +161,7 @@
 Thread.abort_on_exception = true
 
-class Rack::PathInfoRewriter
+# Strip public/dispatch.fcgi out of SCRIPT_NAME so Rails generates nicer URLs
+class ScriptNameRewriter
+  
   def initialize(app)
     \@app = app
@@ -139,8 +169,7 @@
 
   def call(env)
-    env["SCRIPT_NAME"] = ""
-    parts = env['REQUEST_URI'].split('?')
-    env['PATH_INFO'] = parts[0]
-    env['QUERY_STRING'] = parts[1].to_s
+    if env['SCRIPT_NAME'] =~ /dispatch\.fcgi/
+      env['SCRIPT_NAME'].gsub!(/public\\/dispatch\.fcgi/,'')
+    end
     \@app.call(env)
   end
@@ -152,5 +181,5 @@
 
   begin
-    Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(Rack::URLMap.new("/$appdir" => ${appclass}::Application))
+    Rack::Handler::FastCGI.run ScriptNameRewriter.new(Rack::URLMap.new("/" => ${appclass}::Application))
   rescue => e
    dispatch_logger.error(e)
@@ -165,5 +194,5 @@
    # List of specific files to watch for changes.
    Thread.current[:watched_files] = ['public/dispatch.fcgi',
-				     'public/.htaccess']
+                                     'public/.htaccess']
    # Sample filter: /(\.rb|\.erb)\$/.  Default filter: watch all files
    Thread.current[:watched_extensions] = //
@@ -194,5 +223,5 @@
        else
          return true if Thread.current[:watched_extensions] =~ absfile &&
-	   modified(absfile)
+           modified(absfile)
        end
      end
@@ -226,12 +255,18 @@
 chmod 0755,'public/dispatch.fcgi';
 
+# static-cat doesn't whitelist .txt files
+chmod 0777, 'public/robots.txt'; 
+
 # have to explicitly take a dependency on fcgi
+# ruby1.9 means we need to take a dependency on minitest 
+# for rails console to work
 open GEMFILE, ">>Gemfile";
 print GEMFILE "gem 'fcgi'\n";
+print GEMFILE "gem 'minitest'\n";
 close GEMFILE;
 
 print "Your application is located in:\n";
 print "  /mit/$USER/web_scripts/$addrend/\n";
-print "To run programs like rake or script/generate, run\n";
+print "To run programs like rake or rails generate, run\n";
 print "  'ssh -k $USER\@scripts' and cd to the above directory.\n\n";
 press_enter;
