#
# Cookbook Name:: cdh
# Recipe:: yum_repo
#
# Copyright 2013, whitestar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

def get_repo_urls(cdh_version, node_platform, node_platform_version, kernel_machine)
  platform = nil
  case node_platform
    when 'centos', 'rhel'
      platform = 'redhat'
  end
  platform_major_version = node_platform_version.split('.')[0]
  
  platform_ctx = nil
  normalized_kernel_machine = nil
  case cdh_version.split(/[.u]/)[0]
    when '4'
      normalized_kernel_machine = (platform_major_version == '5') \
        ? 'x86_64' \
        : kernel_machine
      platform_ctx = "#{platform}/#{platform_major_version}/#{normalized_kernel_machine}"
    when '3'
      platform_ctx = (platform_major_version == '5') \
        ? "#{platform}" \
        : "#{platform}/#{platform_major_version}/x86_64"
  end

  cdh_repo_pkg = nil
  cdh_repo_ctx = nil
  cdh_repo_key_ctx = nil
  case cdh_version
    when '4.2.0'  # 
      cdh_repo_pkg = "cloudera-cdh"
      cdh_repo_ctx = "cdh4/one-click-install/#{platform_ctx}/#{cdh_repo_pkg}-4-0.#{normalized_kernel_machine}.rpm"
      cdh_repo_key_ctx = "cdh4/#{platform}/#{platform_major_version}/x86_64/cdh/RPM-GPG-KEY-cloudera"
    when '3u6', '3u5'  # End of Maintenance (EOM): June 20th, 2013.
      cdh_repo_pkg = 'cdh3-repository'
      cdh_repo_ctx = "#{platform_ctx}/cdh/#{cdh_repo_pkg}-1.0-1.noarch.rpm"
      cdh_repo_key_ctx = "#{platform_ctx}/cdh/RPM-GPG-KEY-cloudera"
    else
      Chef::Application.fatal!("Non supported version: #{cdh_version}")
  end

  download_site_url = 'http://archive.cloudera.com'

  return [
    cdh_repo_pkg,
    "#{download_site_url}/#{cdh_repo_ctx}",
    "#{download_site_url}/#{cdh_repo_key_ctx}"
  ]
end


if node[:platform_family] == 'rhel' then
  cdh_version = node['cdh']['version']
  cdh_repo_pkg, cdh_repo_url, cdh_repo_key_url \
    = get_repo_urls(cdh_version, node[:platform], node[:platform_version], node[:kernel][:machine])

  file_cache_path = Chef::Config[:file_cache_path]
  downloaded_repo_pkg = "#{file_cache_path}/#{File::basename(cdh_repo_url)}"

  remote_file downloaded_repo_pkg do
    source cdh_repo_url
    not_if "rpm -qi #{cdh_repo_pkg}"
    action :create_if_missing
  end

  #cdh_repo_add_command \
  #  = "sudo yum --nogpgcheck localinstall #{downloaded_repo_pkg}"
  package cdh_repo_pkg do
    source downloaded_repo_pkg
    options '--nogpgcheck'
    not_if "rpm -qi #{cdh_repo_pkg}"
    action :install
  end

  #cdh_repo_key_add_command \
  #  = "sudo rpm --import #{cdh_repo_key_url}"
  yum_key 'RPM-GPG-KEY-cloudera' do
    url cdh_repo_key_url
    action :add
  end
end
