steps:

- task: CacheBeta@0
  displayName: HDF5 cache
  inputs:
    # The key should specify enough to avoid restoring an incompatible build.
    # Increment the first number to clear the cache if you change something
    # not captured by the key, e.g. dependencies.
    ${{ if eq(parameters.platform, 'windows') }}:
      key: 0 | HDF5 | "$(Agent.OS)" | "$(Agent.OSArchitecture)" | "$(HDF5_VERSION)" | "$(HDF5_VSVERSION)" | ci/get_hdf5_win.py
    ${{ if ne(parameters.platform, 'windows') }}:
      key: 0 | HDF5 | "$(Agent.OS)" | "$(Agent.OSArchitecture)" | "$(HDF5_VERSION)" | ci/get_hdf5_if_needed.sh
    path: $(HDF5_CACHE_DIR)
  condition: and(succeeded(), ne(variables['HDF5_VERSION'], ''))

- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.8'
    architecture: 'x64'
  displayName: 'Use Python 3.8'

- ${{ if eq(parameters.platform, 'windows') }}:
  - task: NuGetToolInstaller@0
    displayName: 'Use latest Nuget'

  - script: |
      nuget install zlib-msvc-x64 -ExcludeVersion -OutputDirectory "$(build.BinariesDirectory)"
      echo ##vso[task.prependpath]$(build.BinariesDirectory)\zlib-msvc-x64\build\native\bin_release
      echo ##vso[task.setvariable variable=CL]/I$(build.BinariesDirectory)\zlib-msvc-x64\build\native\include
      echo ##vso[task.setvariable variable=LINK]/LIBPATH:$(build.BinariesDirectory)\zlib-msvc-x64\build\native\lib_release
      echo ##vso[task.setvariable variable=ZLIB_ROOT]$(build.BinariesDirectory)\zlib-msvc-x64\build\native
    displayName: 'Install nuget dependencies'

- bash: |
    set -o errexit
    python -m pip install --upgrade pip
    pip install cibuildwheel==1.10.0
  displayName: 'Install cibuildwheel'

- script: env
  displayName: 'Print env'

# On Linux, the wheel will be
# built inside a Docker image which already contains HDF5.
- ${{ if eq(parameters.platform, 'windows') }}:
    - script: |
        python -m pip install requests
        python ci\\get_hdf5_win.py
      displayName: 'Ensure HDF5'
- ${{ if eq(parameters.platform, 'macos') }}:
    - script: |
        ci/get_hdf5_if_needed.sh
      displayName: 'Ensure HDF5'

- bash: |
    set -o errexit
    cibuildwheel --print-build-identifiers
    cibuildwheel --output-dir wheelhouse .
    python ci/bundle_hdf5_whl.py wheelhouse
  displayName: 'Build wheels'
- publish: $(System.DefaultWorkingDirectory)/wheelhouse
  artifact: ${{ parameters.platform }}_wheels
  displayName: 'Publish wheels'
