#
# Cookbook:: irrigate-fluentd
# Recipe:: windows_text_log_files
#
# Copyright: The Orchard
#

# Configure sources and filters
if node['fluentd']['application_logs'] && !node['fluentd']['application_logs'].empty?
  node['fluentd']['application_logs'].each do |_name, info|
    app_name = info['app_name']
    log_file = info['log_file']
    log_type = info['type']
    parse_format = info['format']
    template "c:/opt/td-agent/conf/conf.d/source-#{app_name}.conf" do
      source 'source_windows.conf.erb'
      variables ({
        parse_type: log_type,
        tag: app_name,
        format: parse_format,
        file: log_file,
      })
      notifies :restart, 'windows_service[fluentdwinsvc]', :delayed
    end
  end

  node['fluentd']['application_logs'].each do |name, info|
    app_name = info['app_name']
    log_type = info['log_type']
    filter = info['filter_type']
    template "c:/opt/td-agent/conf/conf.d/filter-#{app_name}.conf" do
      source 'filter_windows.conf.erb'
      variables ({
        name: app_name,
        type: name,
      })
      notifies :restart, 'windows_service[fluentdwinsvc]', :delayed
    end
  end
end

# Fluentd service
windows_service 'fluentdwinsvc' do
  supports start: true, restart: true, stop: true, configure_startup: true
  action :nothing
  startup_type :automatic
end
