#!/usr/bin/env ruby
# frozen_string_literal: true

# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds

require 'logger'
require_relative '../lib/varuna'
require 'varuna/process_tuning'

# --version / --help short-circuit before composition / CPU pinning so
# `varuna-queued --version` works in environments where ilmarinen-mcp is
# not installed or pin_and_nice! cannot run.
if ARGV.include?('--version') || ARGV.include?('--help') || ARGV.include?('-h')
  Varuna::QueuedDaemon.run(ARGV)
  exit
end

Varuna::ProcessTuning.pin_and_nice!

composition = Varuna::QueueWatch::Composition.build_production(
  logger: Logger.new(
    $stdout,
    level: Logger::INFO,
    formatter: ->(sev, t, _p, m) { "[#{t.utc.iso8601}] #{sev} #{m}\n" }
  )
)
at_exit { composition.teardown.call }
Varuna::QueuedDaemon.run(ARGV, daemon: composition.daemon)
