#!/usr/bin/ruby.ruby3.4 

# To work without being installed as a gem:
libdir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
$:.unshift libdir unless $:.include? libdir

require 'nanite'
require 'nanite/admin'
require 'eventmachine'
require 'thin'
# IMPORTANT!
# You need raggi's patched async version of Thin at the moment to use
# the nanite-admin tool.
#
# raggi's Git repo contains a branch called 'async_for_rack' which contains the
# version of Thin you want to install.  raggi has apparently removed the 'master'
# branch of his Git repo so you may see a warning like that shown below.
#
# git clone git://github.com/raggi/thin.git thin-raggi-async
# ...
# warning: remote HEAD refers to nonexistent ref, unable to checkout.   <<==  IGNORE THIS
#
# cd thin-raggi-async/
# git checkout --track -b async_for_rack origin/async_for_rack
# warning: You appear to be on a branch yet to be born.                 <<==  IGNORE THIS
# warning: Forcing checkout of origin/async_for_rack.                   <<==  IGNORE THIS
# Branch async_for_rack set up to track remote branch refs/remotes/origin/async_for_rack.
# Switched to a new branch "async_for_rack"

# run : 'rake install' to build and install the Thin gem
# cd <NANITE>
# ./bin/nanite-admin

# When you need to update this Thin install you should be able to do a 'git pull' on the
# "async_for_rack" branch.

require File.dirname(__FILE__) + '/../lib/nanite'
require 'yaml'
require "optparse"

include Nanite::CommonConfig

options = {}

opts = OptionParser.new do |opts|
  opts.banner = "Usage: nanite-admin [-flags] [argument]"
  opts.define_head "Nanite Admin: a basic control interface for your nanite cluster."
  opts.separator '*'*80

  setup_mapper_options(opts, options)
  
  opts.on("--thin-debug", "Set the equivalent of the '--debug' flag on the Thin webserver.") do
    options[:thin_debug] = true
  end
end

opts.parse!

EM.run do
  Nanite.start_mapper(options)
  Nanite::Log.info "starting nanite-admin"
  Rack::Handler::Thin.run(Nanite::Admin.new(Nanite.mapper), :Port => 4000) do
    Thin::Logging.debug = options[:thin_debug]
  end
end