#
# Cookbook:: irrigate-wowza
# Recipe:: default
#
# Copyright:: (C) 2022 The Orchard
#
# All rights reserved - Do Not Redistribute
#

if node['wowza']['aws']['credentials_data_bag_name'] && !node['wowza']['aws']['credentials_data_bag_name'].empty?
  wowza_credentials                                 = data_bag_item('secrets', node['wowza']['aws']['credentials_data_bag_name'])
  node.default['wowza']['aws']['silentInstallKey']  = wowza_credentials['silentInstallKey'].chomp
  node.default['wowza']['aws']['LicenseKey']        = wowza_credentials['LicenseKey'].chomp
  node.default['wowza']['aws']['access_key_id']     = wowza_credentials['access_key_id'].chomp
  node.default['wowza']['aws']['secret_access_key'] = wowza_credentials['secret_access_key'].chomp
  node.default['wowza']['aws']['webusername']       = wowza_credentials['webusername'].chomp
  node.default['wowza']['aws']['webpassword']       = wowza_credentials['webpassword'].chomp
  node.default['wowza']['aws']['sharedsecret']      = wowza_credentials['sharedsecret'].chomp
end

options_file = '/usr/local/wse4.opt'
# Create option file for the installer
template options_file do
  source 'wse4.opt.erb'
  owner 'root'
  group 'root'
  mode  '0755'
end

# Download Wowza Package
remote_file node['wowza']['download_dir'] do
  source node['wowza']['source']
  owner 'root'
  group 'root'
  mode  '0755'
end

# Run Wowza installer
execute 'run_installer' do
  command "#{node['wowza']['download_dir']} --optionfile #{options_file} && touch /var/cache/.wowza_installed"
  creates '/var/cache/.wowza_installed'
  cwd '/usr/local'
end

# Array of the application.xml that require to be deployed
applications = node['wowza']['applications']

applications.each do |application, application_details|
  directory "/usr/local/WowzaStreamingEngine/applications/#{application}" do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
    action :create
  end

  directory "/usr/local/WowzaStreamingEngine/conf/#{application}" do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
    action :create
  end

  cupertino_encryption_base_url = application_details['cupertinoEncryptionBaseURL'] || node['wowza']['aws']['cupertinoEncryptionBaseURL']

  template "/usr/local/WowzaStreamingEngine/conf/#{application}/Application.xml" do
    source 'Application.xml.erb'
    owner 'root'
    group 'root'
    mode '0644'
    variables(
      name: application,
      allow_origin: application_details['allow_origin'],
      cupertino_encryption_base_url: cupertino_encryption_base_url
    )
    notifies :restart, 'service[WowzaStreamingEngine]', :delayed
  end
end

# Create MediaCache.xml for vods3
template '/usr/local/WowzaStreamingEngine/conf/MediaCache.xml' do
  source 'MediaCache.xml.erb'
  owner 'root'
  group 'root'
  mode  '0644'
  notifies :restart, 'service[WowzaStreamingEngine]', :delayed
end

# Create Tune.xml
template '/usr/local/WowzaStreamingEngine/conf/Tune.xml' do
  source 'Tune.xml.erb'
  owner 'root'
  group 'root'
  mode  '0644'
  notifies :restart, 'service[WowzaStreamingEngine]', :delayed
end

# Download HLS encryption jar file from S3
s3_file '/usr/local/WowzaStreamingEngine/lib/HLSEncryption.jar' do
  remote_path node['wowza']['aws']['s3_remote_path']
  bucket node['wowza']['aws']['s3_bucket']
  aws_access_key_id wowza_credentials['access_key_id']
  aws_secret_access_key wowza_credentials['secret_access_key']
  owner 'root'
  group 'root'
  mode  '0644'
  action :create
end

# Restart WowzaStreamingEngine service on port 1935
service 'WowzaStreamingEngine' do
  action [:enable, :restart]
end

# Restart WowzaStreamingEngineManager service on port 8088
service 'WowzaStreamingEngineManager' do
  action [:enable, :restart]
end
