parameters:
- name: os
  type: string
  default: ubuntu2204

steps:
- task: Bash@3
  displayName: Download and install aqlprofile
  retryCountOnTaskFailure: 3
  inputs:
    targetType: inline
    workingDirectory: $(Agent.BuildDirectory)
    script: |
      set -e
      if [ "${{ parameters.os }}" = "ubuntu2204" ]; then
        packageName=$(curl -s https://repo.radeon.com/rocm/apt/$(REPO_RADEON_VERSION)/pool/main/h/hsa-amd-aqlprofile/ | grep -oP "href=\"\K[^\"]*$(lsb_release -rs)[^\"]*\.deb") && \
        wget -nv https://repo.radeon.com/rocm/apt/$(REPO_RADEON_VERSION)/pool/main/h/hsa-amd-aqlprofile/$packageName && \
        mkdir -p hsa-amd-aqlprofile && \
        dpkg-deb -R $packageName hsa-amd-aqlprofile
      elif [ "${{ parameters.os }}" = "almalinux8" ]; then
        sudo dnf -y install rpm-build cpio && \
        packageName=$(curl -s https://repo.radeon.com/rocm/rhel8/$(REPO_RADEON_VERSION)/main/ | grep -oP "hsa-amd-aqlprofile-[^\"]+\.rpm" | head -n1) && \
        wget -nv https://repo.radeon.com/rocm/rhel8/$(REPO_RADEON_VERSION)/main/$packageName && \
        mkdir -p hsa-amd-aqlprofile && \
        rpm2cpio $packageName | (cd hsa-amd-aqlprofile && cpio -idmv)
      else
        echo "Unsupported OS: ${{ parameters.os }}"
        exit 1
      fi && \
      mkdir -p $(Agent.BuildDirectory)/rocm && \
      cp -R hsa-amd-aqlprofile/opt/rocm-*/* $(Agent.BuildDirectory)/rocm && \
      rm -rf hsa-amd-aqlprofile $packageName
