# File lib/picnic/postambles.rb, line 151
    def mongrel
      require 'rubygems'
      require 'mongrel/camping'
      
      if $DAEMONIZE
        # check if log and pid are writable before daemonizing, otherwise we won't be able to notify
        # the user if we run into trouble later (since once daemonized, we can't write to stdout/stderr)
        check_pid_writable if $PID_FILE
        check_log_writable
      end
      
      self.create
      
      puts "\n** #{self} is starting. Look in #{Picnic::Conf.log[:file].inspect} for further notices."
      
      settings = {
        :host => Picnic::Conf.bind_address || "0.0.0.0", 
        :log_file => Picnic::Conf.log[:file], 
        :cwd => $APP_PATH
      }
      
      # need to close all IOs before daemonizing
      $LOG.close if $DAEMONIZE
      
      public_dirs = Picnic::Conf.public_dirs || Picnic::Conf.public_dir
      public_dirs = [public_dirs] unless 
        public_dirs.kind_of? Array || public_dirs.nil?
      
      begin
        app_mod = self
        mongrel = Mongrel::Configurator.new settings  do
          daemonize :log_file => Picnic::Conf.log[:file], :cwd => $APP_PATH if $DAEMONIZE
          app_mod.init_logger
          #app_mod.init_db_logger
          listener :port => Picnic::Conf.port do
            uri Picnic::Conf.uri_path, :handler => Mongrel::Camping::CampingHandler.new(app_mod)

            if public_dirs
              public_dirs.each do |d|
                dir = d[:dir]
                path = "#{Picnic::Conf.uri_path}/#{d[:path]}".gsub(/\/\/+/,'/')
                $LOG.debug("Mounting public directory #{dir.inspect} to path #{path.inspect}.")
                uri(path, :handler => Mongrel::DirHandler.new(dir))
              end
            end
            
            setup_signals
          end
        end
      rescue Errno::EADDRINUSE
        exit 1
      end
      
      mongrel.run
            
      
      if $DAEMONIZE && $PID_FILE
        write_pid_file
        unless File.exists? $PID_FILE
          $LOG.error "#{self} could not start because pid file #{$PID_FILE.inspect} could not be created."
          exit 1
        end
      end
      
      puts "\n** #{self} is running at http://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path} and logging to '#{Picnic::Conf.log[:file]}'"
      
      
      self.prestart if self.respond_to? :prestart
      mongrel.join

      clear_pid_file

      puts "\n** #{self} is stopped (#{Time.now})"
    end