--- railsnoautoreload/public/dispatch.fcgi	Wed Jan  6 02:22:22 2010
+++ railsautoreloaddeploy/public/dispatch.fcgi	Mon Jan 18 02:34:46 2010
@@ -21,4 +21,68 @@
 require File.dirname(__FILE__) + "/../config/environment"
 require 'fcgi_handler'
 
-RailsFCGIHandler.process!
+## Commented out by scripts.mit.edu autoinstaller
+## RailsFCGIHandler.process!
+
+## 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
