module PhusionPassenger::Utils

def self.passenger_tmpdir(create = true)

directory if it doesn't exist.
temporary files. If +create+ is true, then this method creates the
Returns the directory in which to store Phusion Passenger-specific
def self.passenger_tmpdir(create = true)
	dir = @@passenger_tmpdir
	if dir.nil? || dir.empty?
		tmpdir = "/tmp"
		["PASSENGER_TEMP_DIR", "PASSENGER_TMPDIR"].each do |name|
			if ENV.has_key?(name) && !ENV[name].empty?
				tmpdir = ENV[name]
				break
			end
		end
		dir = "#{tmpdir}/passenger.1.0.#{Process.pid}"
		dir.gsub!(%r{//+}, '/')
		@@passenger_tmpdir = dir
	end
	if create && !File.exist?(dir)
		# This is a very minimal implementation of the subdirectory
		# creation logic in ServerInstanceDir.h. This implementation
		# is only meant to make the unit tests pass. For production
		# systems one should pre-create the temp directory with
		# ServerInstanceDir.h.
		system("mkdir", "-p", "-m", "u=rwxs,g=rwx,o=rwx", dir)
		system("mkdir", "-p", "-m", "u=rwxs,g=rwx,o=rwx", "#{dir}/generation-0")
		system("mkdir", "-p", "-m", "u=rwxs,g=rwx,o=rwx", "#{dir}/backends")
		system("mkdir", "-p", "-m", "u=rwxs,g=rwx,o=rwx", "#{dir}/spawn-server")
	end
	return dir
end