Index: trunk/locker/deploy/bin/rails
===================================================================
--- trunk/locker/deploy/bin/rails	(revision 1422)
+++ trunk/locker/deploy/bin/rails	(revision 1426)
@@ -90,4 +90,7 @@
 
 tie my @railsenv, 'Tie::File', 'config/environment.rb';
+unshift @railsenv, "# ENV['RAILS_ENV'] ||= 'production'";
+unshift @railsenv, "# Uncomment below to put Rails into production mode";
+unshift @railsenv, "";
 unshift @railsenv, "ENV['RAILS_RELATIVE_URL_ROOT'] = \"/$addrend\"";
 untie @railsenv;
@@ -111,4 +114,76 @@
 untie @railswelcome;
 
+tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi';
+for (@railsfcgi) {
+    s/^[^#]*RailsFCGIHandler/## Commented out by scripts.mit.edu autoinstaller\n## RailsFCGIHandler/;
+}
+untie @railsfcgi;
+open RAILSFCGI, ">>public/dispatch.fcgi";
+print RAILSFCGI <<EOF;
+
+## Added by scripts.mit.edu autoinstaller to reload when app code changes
+Thread.abort_on_exception = true
+
+t1 = Thread.new do
+   RailsFCGIHandler.process!
+end
+
+t2 = Thread.new do
+   # List of directories to watch for changes before reload
+   Thread.current[:watched_dirs] = ['app', 'config', 'db', 'lib', 'public']
+   # Sample filter: /(\.rb|\.erb)\$/.  Default filter: watch all files
+   Thread.current[:watched_extensions] = //
+   # Iterations since last reload
+   Thread.current[:iterations] = 0
+
+   def modified(file)
+     begin
+       mtime = File.stat(file).mtime
+     rescue
+       false
+     else
+       if Thread.current[:iterations] == 0
+         Thread.current[:modifications][file] = mtime
+       end
+       Thread.current[:modifications][file] != mtime
+     end
+   end
+
+   # Don't symlink yourself into a loop.  Please.  Things will still work
+   # (Linux limits your symlink depth) but you will be sad
+   def modified_dir(dir)
+     Dir.new(dir).each do |file|
+       absfile = File.join(dir, file)
+       if FileTest.directory? absfile
+         next if file == '.' or file == '..'
+         return true if modified_dir(absfile)
+       else
+         return true if Thread.current[:watched_extensions] =~ absfile &&
+	   modified(absfile)
+       end
+     end
+     false
+   end
+
+   def reload
+     Thread.current[:modifications] = {}
+     Thread.current[:iterations] = 0
+     RailsFCGIHandler.reload!
+   end
+
+   Thread.current[:modifications] = {}
+   # Wait until the modify time changes, then reload.
+   while true
+     reload if Thread.current[:watched_dirs].inject(false) {|z, dir| z || modified_dir(File.join(File.dirname(__FILE__), '..', dir))}
+     Thread.current[:iterations] += 1
+     sleep 1
+   end
+end
+
+t1.join
+t2.join
+## End of scripts.mit.edu autoinstaller additions
+EOF
+
 print "Your application is located in:\n";
 print "  /mit/$USER/web_scripts/$addrend/\n";
