# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT
#

parameters:
  - name: vcpkgToolSha
    displayName: 'Custom SHA of vcpkg-tool to use rather than bootstrap'
    type: string
    default: 'use default'
  - name: triplet
    type: string
  - name: jobName
    type: string
  - name: poolName
    type: string
  - name: dockerImage
    type: string
  - name: tripletPattern
    displayName: 'Enable the triplets which contain this substring'
    type: string
    default: ''

jobs:
- job: ${{ parameters.jobName }}
  condition: and(succeeded(), contains('^${{ parameters.triplet }}$', '${{ parameters.tripletPattern }}'))
  pool:
    name: ${{ parameters.poolName }}
  workspace:
    clean: resources
  timeoutInMinutes: 1440 # 1 day
  cancelTimeoutInMinutes: 1
  variables:
  - name: WORKING_ROOT
    value: /mnt/vcpkg-ci
  - name: VCPKG_DOWNLOADS
    value: /mnt/vcpkg-ci/downloads
  - group: vcpkg-asset-caching-credentials
  - name: X_VCPKG_ASSET_SOURCES
    value: "x-azurl,$(root-url-wus3),$(sas-wus3),readwrite"
  - group: vcpkg-binary-caching-credentials
  - name: X_VCPKG_BINARY_SOURCE_STUB
    value: "x-azblob,$(root-bin-url-wus3),$(sas-bin-wus3)"

  steps:
    # Note: /mnt is the Azure machines' temporary disk.
  - bash: |
      curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
      az login --identity
      az acr login --name AndContainerRegistry
    displayName: 'Set up managed identity'
  - bash: |
      sudo mkdir /home/agent -m=777
      sudo chown `id -u` /home/agent
      sudo mkdir ${{ variables.WORKING_ROOT }} -m=777
      sudo rm -rf ${{ variables.WORKING_ROOT }}/failure-logs
      sudo mkdir ${{ variables.WORKING_ROOT }}/failure-logs -m=777
      sudo mkdir ${{ variables.VCPKG_DOWNLOADS }} -m=777
      exit 0
    displayName: 'Create working directories'
  - bash: |
      docker run --rm \
      --mount type=bind,source=$(Build.Repository.LocalPath),target=/vcpkg \
      ${{ parameters.dockerImage }} \
      ./bootstrap-vcpkg.sh
    displayName: 'Bootstrap vcpkg'
    condition: eq('use default', '${{ parameters.vcpkgToolSha }}')
  - bash: |
      docker run --rm \
      --mount type=bind,source=$(Build.Repository.LocalPath),target=/vcpkg \
      ${{ parameters.dockerImage }} \
      ./scripts/azure-pipelines/bootstrap-from-source.sh ${{ parameters.vcpkgToolSha }}
    displayName: "Build vcpkg with CMake"
    condition: ne('use default', '${{ parameters.vcpkgToolSha }}')
  - bash: |
      CONTAINERS=$(docker ps -a -q)
      if [ -n "$CONTAINERS" ]; then
        docker rm -f $CONTAINERS
      fi
    displayName: 'Stop and remove all docker containers'
  - bash: |
      docker pull ${{ parameters.dockerImage }}
    displayName: 'Pull Docker Container'
  - task: Bash@3
    inputs:
      targetType: inline
      script: |
        USER=$(id --user)
        docker run --init -i \
        -a stderr \
        -a stdout \
        --user $USER \
        --mount type=bind,source=$(Build.Repository.LocalPath),target=/vcpkg \
        --mount type=bind,source=$(WORKING_ROOT)/failure-logs,target=/vcpkg/failure-logs \
        --mount type=bind,source=/mnt/vcpkg-ci,target=/mnt/vcpkg-ci \
        --env X_VCPKG_ASSET_SOURCES="x-azurl,$(root-url-wus3),$(sas-wus3),readwrite" \
        ${{ parameters.dockerImage }} \
        pwsh \
         -File /vcpkg/scripts/azure-pipelines/test-modified-ports.ps1 \
         -Triplet ${{ parameters.triplet }} \
         -BuildReason $(Build.Reason) \
         -BinarySourceStub "$(X_VCPKG_BINARY_SOURCE_STUB)" \
         -WorkingRoot ${{ variables.WORKING_ROOT }} \
         -IsLinuxHost
      failOnStderr: true
    displayName: '*** Test Modified Ports'
  - bash: |
      CONTAINERS=$(docker ps -a -q)
      if [ -n "$CONTAINERS" ]; then
        docker rm -f $CONTAINERS
      fi
    displayName: 'Stop and remove all docker containers'
    condition: always()
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: failure logs for ${{ parameters.triplet }}'
    inputs:
      PathtoPublish: '$(WORKING_ROOT)/failure-logs'
      ArtifactName: 'failure logs for ${{ parameters.triplet }}'
    condition: ne(variables['FAILURE_LOGS_EMPTY'], 'True')
  - bash: |
      python3 scripts/file_script.py /mnt/vcpkg-ci/installed/vcpkg/info/
    displayName: 'Build a file list for all packages'
    condition: always()
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: file lists for ${{ parameters.triplet }}'
    condition: always()
    inputs:
      PathtoPublish: scripts/list_files
      ArtifactName: 'file lists for ${{ parameters.triplet }}'
  - task: PublishTestResults@2
    displayName: 'Publish Test Results'
    condition: ne(variables['XML_RESULTS_FILE'], '')
    inputs:
      testRunTitle: ${{ parameters.triplet }}
      testResultsFormat: xUnit
      testResultsFiles: $(XML_RESULTS_FILE)
      platform: ${{ parameters.triplet }}
