#
# Cookbook:: irrigate-python
# Recipe:: python38
#
# All rights reserved - Do Not Redistribute
#

if platform_family?('rhel')
  if node['platform_version'].start_with?('7')

    # The software collection needs to be installed first
    scl_packages = %w(
      centos-release-scl
    )

    scl_packages.each do |name|
      package name
    end

    packages = %w(
      rh-python38-python
      rh-python38-python-devel
      rh-python38-python-libs
      rh-python38-python-pip
      rh-python38-python-pyyaml
    )

    packages.each do |name|
      package name
    end

    python_packages = %w(
      awscli
    )

    python_packages.each do |name|
      execute 'install_package' do
        command ". /opt/rh/rh-python38/enable && pip3.8 install #{name}"
        not_if ". /opt/rh/rh-python38/enable && pip3.8 list | grep -i #{name}"
      end
    end

    # SCL packagees are sandboxed, so make sure they are available to all users.
    template '/etc/profile.d/python38.sh' do
      source 'python38.sh.erb'
      mode '0644'
      owner 'root'
      group 'root'
    end

  end

elsif platform_family?('amazon')
  if node['platform_version'].start_with?('2')

    execute 'install_package' do
      command 'amazon-linux-extras enable python3.8'
      not_if "amazon-linux-extras list | egrep -i 'python3.8.*enabled'"
    end

    # The software collection needs to be installed for python from amazon-linux-extras.
    python38_packages = %w(
      python38
      python38-devel
    )

    python38_packages.each do |name|
      package name
    end

  end

elsif platform_family?('debian')
  if node['platform_version'].start_with?('12')
    prerequisite_packages = %w(
      ca-certificates
      curl
      gpg
      locales-all
      wget
    )

    prerequisite_packages.each do |name|
      package name
    end

    apt_repository 'deadsnakes' do
      components ['main']
      deb_src true
      distribution 'jammy' # closest matching major ubuntu release
      key 'F23C5A6CF475977595C89F51BA6932366A755776'
      keyserver 'keyserver.ubuntu.com'
      trusted false
      uri 'https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu/'
    end

    packages = %w(
      python3.8
      python3.8-dev
      python3.8-venv
    )

    packages.each do |name|
      package name
    end

    # Install pip for Python 3.8
    execute 'install_pip3.8' do
      command 'curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3.8 get-pip.py && rm get-pip.py'
      not_if 'python3.8 -m pip --version'
    end

    # Install setuptools
    execute 'install_setuptools' do
      command 'python3.8 -m pip install --upgrade setuptools'
      not_if 'python3.8 -m pip show setuptools'
    end
  end
end
