# File lib/picnic/cli.rb, line 55
    def handle_cli_input
#      if File.exists? options[:app_file]
        # try to use given app base path
        $APP_ROOT = File.dirname(options[:app_file]).gsub(/\/(lib|bin)\/?$/, '')
#      else
#        require 'rubygems'
#        
#        # fall back to using gem installation
#        matches = Gem::source_index.find_name(app)
#        raise LoadError, "#{app} gem doesn't appear to be installed!" if matches.empty?
#        
#        gem_spec = matches.last
#        $APP_ROOT = gem_spec.full_gem_path
#        
#        gem(app)
#      end
      
      unless File.file?(options[:app_file])
        raise ArgumentError, "options[:app_file] points to #{options[:app_file].inspect} but this does not appear to be a valid Camping application!}"
      end
      
      
      puts "Loading #{app.inspect} code from #{$APP_ROOT.inspect}..."
      
      $: <<  $APP_ROOT+"/lib"
      
      $PID_FILE     = @options[:pid_file]
      $CONFIG_FILE  = @options[:conf_file]
      $VERBOSE      = @options[:verbose]
      
      opts = OptionParser.new do |opts|
        #opts.banner = ""
        #opts.define_head ""
        #opts.separator ""
        opts.on("-d", "--daemonize", "Run daemonized in the background") do
          $DAEMONIZE = true
        end
        opts.on("-c", "--config FILE", "Use this config file (default is /etc/<app>/config.yml)") do |c| 
          puts "Using config file #{c.inspect}"
          $CONFIG_FILE = c
        end
        opts.on("-P", "--pid_file FILE", "Path to pid file (used only when running daemonized; default is /etc/<app>/<app>.pid)") do |p| 
          if $DAEMONIZE && !File.exists?(p)
            puts "Using pid file #{p.inspect}"
            $PID_FILE = p
          elsif File.exists?(p)
            puts "The pid file already exists.  Is #{app} running?\n" +
              "You will have to first manually remove the pid file at '#{p}' to start the server as a daemon."
            exit 1
          else
            puts "Not running as daemon.  Ignoring pid option"
          end
        end
        
        # optoinal block with additonal opts.on() calls specific to your application
        if @options[:extra_cli_options]
          @options[:extra_cli_options].call(opts)
        end   
      
        # No argument, shows at tail.  This will print an options summary.
        # Try it and see!
        opts.on_tail("-h", "--help", "Show this message") do
          puts opts
          exit
        end
        
        opts.on_tail("-v", "--version", "Show the application's version number") do
          require "#{$APP_ROOT}/lib/#{app}/version.rb"
          app_mod = Object.const_get(@options[:app_module])
          puts "#{app}-#{app_mod::VERSION::STRING}"
          exit
        end
      end
      
      begin
        opts.parse! ARGV
      rescue OptionParser::ParseError => ex
        STDERR.puts "!! #{ex.message}"
        puts "** use `#{File.basename($0)} --help` for more details..."
        exit 1
      end
      
      
      $CONF = Picnic::Conf.new
      $CONF.load_from_file(app, $APP_ROOT, $CONF_FILE)
      
      if $DAEMONIZE
        # TODO: use Process.daemon when RUBY_VERSION >= 1.9
        
        exit if fork
        Process.setsid
        exit if fork
        
        Dir.chdir $APP_ROOT
        File.umask 0000
        
        STDIN.reopen  $CONF.log[:file], "a"
        STDOUT.reopen $CONF.log[:file], "a"
        STDERR.reopen $CONF.log[:file], "a"
        
        File.open($PID_FILE, 'w'){ |f| f.write("#{Process.pid}") }
        at_exit { File.delete($PID_FILE) if File.exist?($PID_FILE) }
      end
      
      $CONF.database = :unused if $CONF.database.blank?
      
      server = Picnic::Server::Base.new($CONF, [options[:app_file]])
      server.start
    end