parameters:
  name: 'Conda build job'
  vmImage: 'ubuntu-22.04'
  py_maj: '3'
  py_min: '9'
jobs:
- job: ${{ parameters.name }}
  timeoutInMinutes: 360
  
  pool:
    vmImage: ${{ parameters.vmImage }}
    
  steps: 
  # install conda on osx
  - ${{ if contains(parameters.vmImage, 'macOS') }}:
    - bash: |
        curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
        bash Miniconda3-latest-MacOSX-x86_64.sh -b -p $HOME/miniconda
        echo "##vso[task.prependpath]$HOME/miniconda/bin"
      displayName: 'Install Miniconda on macOS'
  #activate conda
  - ${{ if contains(parameters.vmImage, 'macOS') }}:
    - bash: |
        source $HOME/miniconda/bin/activate
        conda init bash
      displayName: 'Add conda to PATH'
  - ${{ if contains(parameters.vmImage, 'ubuntu') }}:
    - bash: echo "##vso[task.prependpath]$CONDA/bin"
      displayName: 'Add conda to PATH'
  - ${{ if contains(parameters.vmImage, 'win') }}:
    - powershell: Write-Host "##vso[task.prependpath]$env:CONDA\Scripts"
      displayName: 'Add conda to PATH'
  # Ubuntu install opengl items and remove swig packages that conflict with anaconda
  - ${{ if contains(parameters.vmImage, 'ubuntu') }}:
    - bash: |
        sudo apt-get update && \
        sudo apt-get -q -y install libglu1-mesa-dev libgl1-mesa-dev libxmu-dev libxi-dev && \
        sudo apt list --installed && \
        sudo apt remove swig swig4.0
      displayName: 'Install OpenGL headers'
  - bash: |
        conda config --set always_yes yes --set changeps1 no && \
        conda update -q conda && \
        conda info -a && \
        conda config --add channels https://conda.anaconda.org/conda-forge
    displayName: 'Conda config and info'
  - bash: conda create --yes --quiet --name build_env conda-build conda-verify libarchive python=${{ parameters.py_maj }}.${{ parameters.py_min }} anaconda-client
    displayName: 'Create Anaconda environment'
  - ${{ if eq(parameters.vmImage, 'windows-2022') }}:
    - script: |
        call "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat"
        echo "PATH=%PATH%"
        where cl.exe
        set CC=cl.exe
        set CXX=cl.exe
        call activate build_env
        conda-build --no-remove-work-dir --dirty ci/conda
      displayName: 'Set Windows environment and build'
      env:
        CXX: "cl.exe"
        CC: "cl.exe"
        PYTHONBUFFERED: 1
        PYTHON_VERSION: ${{ parameters.py_maj }}.${{ parameters.py_min }}
        PACKAGE_VERSION: $(Build.SourceBranchName)
        TOKEN: $(anaconda.TOKEN)
  - ${{ if not(contains(parameters.vmImage, 'win')) }}:
    - bash: |
        source activate build_env && \
        conda-build --no-remove-work-dir --dirty ci/conda
      displayName: 'Run conda build'
      failOnStderr: false
      env:
        PYTHONBUFFERED: 1
        PYTHON_VERSION: ${{ parameters.py_maj }}.${{ parameters.py_min }}
        PACKAGE_VERSION: $(Build.SourceBranchName)
        TOKEN: $(anaconda.TOKEN)
