From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= <kapouer@melix.org>
Date: Sun, 10 Jan 2010 00:03:57 +0100
Subject: [PATCH] Allows environment variables to setup debian paths.

---
 app/models/attachment.rb          |    2 +-
 config/environment.rb             |   19 ++++++++++++++++++-
 config/initializers/10-patches.rb |    3 +++
 config/initializers/40-email.rb   |    2 +-
 public/dispatch.fcgi.example      |    7 +++++--
 5 files changed, 28 insertions(+), 5 deletions(-)

--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -43,7 +43,7 @@
                                                         "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"}
 
   cattr_accessor :storage_path
-  @@storage_path = "#{RAILS_ROOT}/files"
+  @@storage_path = ENV['RAILS_VAR'] ? File.join(ENV['RAILS_VAR'], 'files') : "#{RAILS_ROOT}/files"
   
   def validate
     if self.filesize > Setting.attachment_max_size.to_i.kilobytes
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -2,7 +2,15 @@
 
 # Uncomment below to force Rails into production mode when 
 # you don't control web/app server and can't set it the proper way
-# ENV['RAILS_ENV'] ||= 'production'
+ENV['RAILS_ENV'] ||= 'production'
+
+# for debian package : setup proper environment variables and paths
+# To run redmine as unprivileged user, see /usr/share/doc/redmine/README.Debian
+ENV['X_DEBIAN_SITEID'] ||= 'default'
+ENV['RAILS_ETC'] ||= "/etc/redmine/#{ENV['X_DEBIAN_SITEID']}"
+ENV['RAILS_LOG'] ||= "/var/log/redmine/#{ENV['X_DEBIAN_SITEID']}"
+ENV['RAILS_VAR'] ||= "/var/lib/redmine/#{ENV['X_DEBIAN_SITEID']}"
+ENV['RAILS_CACHE'] ||= "/var/cache/redmine/#{ENV['X_DEBIAN_SITEID']}"
 
 # Specifies gem version of Rails to use when vendor/rails is not present
 RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
@@ -30,9 +38,18 @@
   # (by default production uses :info, the others :debug)
   # config.log_level = :debug
 
+  # log path
+  config.log_path = File.join(ENV['RAILS_LOG'], "#{ENV['RAILS_ENV']}.log") unless !ENV['RAILS_LOG']
+
   # Enable page/fragment caching by setting a file-based store
   # (remember to create the caching directory and make it readable to the application)
   # config.action_controller.cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache"
+
+  # the file cache store
+  config.cache_store = :file_store, ENV['RAILS_CACHE']
+  
+  # Set Active Record's database.yml path
+  config.database_configuration_file = File.join(ENV['RAILS_ETC'], 'database.yml') unless !ENV['RAILS_ETC']
   
   # Activate observers that should always be running
   # config.active_record.observers = :cacher, :garbage_collector
--- a/config/initializers/10-patches.rb
+++ b/config/initializers/10-patches.rb
@@ -78,3 +78,6 @@
 end
 
 ActionMailer::Base.send :include, AsynchronousMailer
+
+# the session store
+ActionController::Base.session_options[:tmpdir] = File.join(ENV['RAILS_VAR'], 'sessions') unless !ENV['RAILS_VAR']
--- a/config/initializers/40-email.rb
+++ b/config/initializers/40-email.rb
@@ -1,7 +1,7 @@
 # Loads action_mailer settings from email.yml
 # and turns deliveries on if configuration file is found
 
-filename = File.join(File.dirname(__FILE__), '..', 'email.yml')
+filename = ENV['RAILS_ETC'] ? File.join(ENV['RAILS_ETC'], 'email.yml') : File.join(File.dirname(__FILE__), '..', 'email.yml')
 if File.file?(filename)
   mailconfig = YAML::load_file(filename)
 
--- a/public/dispatch.fcgi.example
+++ b/public/dispatch.fcgi.example
@@ -20,5 +20,8 @@
 #
 require File.dirname(__FILE__) + "/../config/environment"
 require 'fcgi_handler'
-
-RailsFCGIHandler.process!
+if ENV['RAILS_LOG']
+  RailsFCGIHandler.process! File.join(ENV['RAILS_LOG'], 'fastcgi.crash.log')
+else
+  RailsFCGIHandler.process!
+end
