# Deprecated for logstash-forwarder
#
file_inputs = node['rsyslog']['file_inputs']
pem_certificate = node['rsyslog']['pem_certificate']
pem_path = node['rsyslog']['pem_path']
pem_path_source = node['rsyslog']['pem_path_source']
pem_source_cookbook = node['rsyslog']['pem_source_cookbook']

directory '/etc/rsyslog.d' do
  action :create
end

file_inputs.each do |file_input_name, file_input|
  priority_i = file_input['priority'] ? file_input['priority'] : 99
  severity_str = file_input['severity'] ? file_input['severity'] : 'notice'
  facility_str = file_input['facility'] ? file_input['facility'] : 'local0'

  template "/etc/rsyslog.d/#{priority_i}-#{file_input_name}.conf" do
    source 'rsyslog/file-input.conf.erb'
    variables(
      file_name: file_input['file'],
      state_file: file_input_name,
      tag: file_input_name,
      priority: priority_i,
      severity: severity_str,
      facility: facility_str
    )
  end
end

# Installs /usr/lib64/rsyslog/lmnsd_gtls.so
# for tls tunnel encryption
package 'rsyslog-gnutls' do
  action :install
end

if pem_certificate && pem_path_source

  directory pem_path do
    action :create
    recursive true
  end

  remote_directory pem_path do
    source pem_path_source
    cookbook pem_source_cookbook
  end

  # as we are using tunnel encryption we'll need a cert
  cookbook_file "/etc/pki/tls/certs/#{node['rsyslog']['pem_certificate']}" do
    source "#{node['rsyslog']['pem_path_source']}/#{node['rsyslog']['pem_certificate']}"
    owner 'root'
    group 'root'
    mode 0644
  end

  # TODO: document what this is needed for
  node.override['rsyslog']['tls_ca_file'] = "#{pem_path}/#{pem_certificate}"
end

include_recipe 'rsyslog::server'
include_recipe 'rsyslog::client'

# Avoid 35-server-per-host.conf file to be configured
r = resources(template: "#{node['rsyslog']['config_prefix']}/rsyslog.d/35-server-per-host.conf")
r.action(:nothing)
