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

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

include Nanite::CommonConfig

options = {}

opts = OptionParser.new do |opts|
  opts.banner = "Usage: nanite-mapper [-flags] [argument]"
  opts.define_head "Nanite Mapper: clustered head unit for self assembling cluster of ruby processes."
  opts.separator '*'*80

  setup_mapper_options(opts, options)
end

opts.parse!

if ARGV[0] == 'stop' || ARGV[0] == 'status'
  mapper = Nanite::Mapper.new(options)
  pid_file = Nanite::PidFile.new(mapper.identity, mapper.options)
  unless pid = pid_file.read_pid
    puts "#{pid_file} not found"
    exit
  end
  if ARGV[0] == 'stop'
    puts "Stopping nanite mapper #{mapper.identity} (pid #{pid})"
    begin
      Process.kill('TERM', pid)
    rescue Errno::ESRCH
      puts "Process does not exist (pid #{pid})"
      exit
    end
    puts 'Done.'
  else
    if Process.getpgid(pid) != -1
      psdata = `ps up #{pid}`.split("\n").last.split
      memory = (psdata[5].to_i / 1024)
      puts "The mapper is alive, using #{memory}MB of memory"
    else
      puts "The mapper is not running but has a stale pid file at #{pid_file}"
    end
  end
  exit
end

EM.run do
  Nanite.start_mapper(options)
end
