def handle_cli_input
$APP_ROOT = File.dirname(options[:app_file]).gsub(/\/(lib|bin)\/?$/, '')
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.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
if @options[:extra_cli_options]
@options[:extra_cli_options].call(opts)
end
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
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