#!/usr/bin/env ruby

$LOAD_PATH.unshift '/usr/share/foreman-proxy/lib'
$LOAD_PATH.unshift '/usr/share/foreman-proxy/modules'

require 'smart_proxy'
require 'smart_proxy_main'
require 'smart_proxy_omaha'
require 'smart_proxy_omaha/syncer'

module Proxy::LogBuffer
  class Decorator
    @@instance = Proxy::LogBuffer::Decorator.new(::Logger.new(STDOUT), 'STDOUT')
  end
end
include Proxy::Log

# Unload all other plugins
::Proxy::Plugins.instance.loaded.delete_if { |plugin| plugin[:name] != :omaha }

::Proxy::PluginInitializer.new(::Proxy::Plugins.instance).initialize_plugins

unless ::Proxy::Plugins.instance.plugin_enabled?(:omaha)
  logger.info "Omaha plugin not enabled. Exiting."
  exit
end

if !Proxy::SETTINGS.foreman_url
  logger.error "Foreman URL not configured"
  exit false
end

begin
  Proxy::Omaha::Syncer.new.run
rescue StandardError => e
  logger.error "#{e}"
  exit false
end
