logs = []
logs << '/var/log/tomcat-alfresco/alfresco.log.json' if node['alfresco']['components'].include?('repo')
logs << '/var/log/tomcat-share/share.log.json' if node['alfresco']['components'].include?('share')
logs << '/var/log/tomcat-solr/solr.log.json' if node['alfresco']['components'].include?('solr')

log_group_name = node['hosts']['hostname'].split('.').first # should be the unique hostname

logs.each do |log_file|
  node.default['cwlogs']['logfiles'][log_file] = {
    datetime_format: '%Y-%m-%dT%H:%M:%S.%fZ',
    file: log_file,
    buffer_duration: 5000,
    log_stream_name: "#{node['aws']['instance_id']}#{log_file}",
    initial_position: 'start_of_file',
    log_group_name: log_group_name
  }
end

if node['alfresco']['components'].include?('nginx')
  node.default['cwlogs']['logfiles']['/var/log/nginx/host.access.log'] = {
    datetime_format: '%d/%b/%Y:%H:%M:%S %z',
    file: '/var/log/nginx/host.access.log',
    buffer_duration: 5000,
    log_stream_name: "#{node['aws']['instance_id']}/var/log/nginx/host.access.log",
    initial_position: 'start_of_file',
    log_group_name: log_group_name
  }
end

other_logs = [
  '/var/log/secure',
  '/var/log/messages',
  '/var/log/cloud-init.log',
  '/var/log/audit/audit.log',
  '/var/log/cron'
]

other_logs.each do |logfile|
  node.default['cwlogs']['logfiles'][logfile] = {
    datetime_format: '%b %d %H:%M:%S',
    file: logfile,
    buffer_duration: 5000,
    log_stream_name: "#{node['aws']['instance_id']}#{logfile}",
    initial_position: 'start_of_file',
    log_group_name: log_group_name
  }
end

service 'awslogs' do
  action :restart
end
