control 'prod_sugarcrm' do
  attribute_file = '/tmp/kitchen/node_attributes.json'
  if file(attribute_file).exist?
    node = json(attribute_file).params

    case node['automatic']['platform_family']
    when 'rhel'
      if node['automatic']['platform_version'].to_s.start_with?('7')

        packages = %w(
          unzip
          zip
        )

        packages.each do |name|
          describe package name do
            it { should be_installed }
          end
        end

        describe service 'memcached' do
          it { should be_enabled }
          it { should be_running }
        end

        describe directory '/var/www/html' do
          its('owner') { should eq node['default']['php']['deploy_user'] }
          its('group') { should eq 'webdev' }
          its('mode') { should cmp '0775' }
        end

        sugarcrm_root_directory = '/var/www/html/sugarcrm'

        repo_url = 'git@github.com:/theorchard/sugarcrm.git'
        describe file("#{sugarcrm_root_directory}/.git/config") do
          it { should exist }
          its('owner') { should eq node['default']['php']['deploy_user'] }
          its('group') { should eq 'webdev' }
          its('content') { should match(/url = #{repo_url}/) }
        end

        describe directory sugarcrm_root_directory do
          its('mode') { should cmp '0775' }
        end

        describe file "#{sugarcrm_root_directory}/healthchk.html" do
          it { should exist }
          its('content') { should eq 'hello' }
          its('owner') { should eq node['default']['php']['deploy_user'] }
          its('group') { should eq 'webdev' }
          its('mode') { should cmp '0644' }
        end

        command = "cd #{sugarcrm_root_directory};"\
        '/opt/remi/php56/root/usr/bin/php -f cron.php >/dev/null 2>&1'
        describe crontab(node['default']['php']['deploy_user']) do
          its('commands') { should include command }
        end

        describe crontab(node['default']['php']['deploy_user']).commands(command) do
          its('hours') { should cmp '*' }
          its('minutes') { should cmp '*/10' }
        end

        apache_dir = node['default']['apache']['dir']
        conf_path = "#{apache_dir}/conf-available"
        # Check only if such file exists
        describe file "#{conf_path}/sugarcrm.conf" do
          it { should exist }
        end

        # Ensure the default vhost is disabled
        %w(default default.conf 000-default 000-default.conf).each do |site|
          describe file "#{apache_dir}/sites-enabled/#{site}" do
            it { should_not exist }
          end
        end

        # Vhost attributes are contained in roles
        node['normal']['webapps'].each do |vhost, vhost_info|
          describe file "#{apache_dir}/sites-available/#{vhost}.conf" do
            its('content') { should include "<VirtualHost *:#{vhost_info['port']}>" }
            its('content') { should include "ServerAdmin #{vhost_info['server_admin']}" }
            its('content') { should include "ServerName #{vhost_info['server_name']}" }
            its('content') { should include "DocumentRoot #{vhost_info['docroot']}" }
            # TODO: noto all params are checked, bette to use template rendering
          end
        end
      end
    else
      puts 'Unsupported platform'
    end

  else
    puts "File #{attribute_file} does not exist, skiping attribute-depending checks"
  end
end
