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

parameters:
  - name: jobName
    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('^${{ replace(parameters.jobName, '_', '-') }}$', '${{ parameters.tripletPattern }}'))
  pool:
    name: 'PrAnd-1ES-Pool'
  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
  - name: ANDROID_NDK_HOME
    value: /android-ndk-r26d
  steps:
    # Note: /mnt is the Azure machines' temporary disk.
  - 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: |
      CONTAINERS=$(docker ps -a -q)
      if [ -n "$CONTAINERS" ]; then
        docker rm -f $CONTAINERS
      fi
    displayName: 'Stop and remove all docker containers'
  - task: AzureCLI@2
    displayName: '*** Test Modified Ports'
    inputs:
      azureSubscription: 'VcpkgPrFleet'
      scriptType: bash
      scriptLocation: 'inlineScript'  # Be very very careful that the exit code from the last pwsh is reported correctly
      inlineScript: |
        az acr login --name vcpkgandroidwus3
        docker pull ${{ parameters.dockerImage }}
        docker run --rm \
          --mount type=bind,source=$(Build.Repository.LocalPath),target=/vcpkg \
          ${{ parameters.dockerImage }} \
          ./bootstrap-vcpkg.sh
        end=`date -u -d "2 days" '+%Y-%m-%dT%H:%MZ'`
        assetSas=`az storage container generate-sas --name cache --account-name vcpkgassetcachewus3 --as-user --auth-mode login --https-only --permissions rcl --expiry $end -o tsv`
        binarySas=`az storage container generate-sas --name cache --account-name vcpkgbinarycachewus3 --as-user --auth-mode login --https-only --permissions rclw --expiry $end -o tsv`
        echo Minting SAS tokens valid through $end
        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,https://vcpkgassetcachewus3.blob.core.windows.net/cache,$assetSas,readwrite" \
        --env ANDROID_NDK_HOME="${{ variables.ANDROID_NDK_HOME }}" \
        ${{ parameters.dockerImage }} \
        pwsh \
         -File /vcpkg/scripts/azure-pipelines/test-modified-ports.ps1 \
         -Triplet ${{ replace(parameters.jobName, '_', '-') }} \
         -BuildReason $(Build.Reason) \
         -BinarySourceStub "x-azblob,https://vcpkgbinarycachewus3.blob.core.windows.net/cache,$binarySas" \
         -WorkingRoot ${{ variables.WORKING_ROOT }}
  - 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 ${{ replace(parameters.jobName, '_', '-') }}"
    inputs:
      PathtoPublish: '$(WORKING_ROOT)/failure-logs'
      ArtifactName: "failure logs for ${{ replace(parameters.jobName, '_', '-') }}"
    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 ${{ replace(parameters.jobName, '_', '-') }}"
    condition: always()
    inputs:
      PathtoPublish: scripts/list_files
      ArtifactName: "file lists for ${{ replace(parameters.jobName, '_', '-') }}"
  - task: PublishTestResults@2
    displayName: 'Publish Test Results'
    condition: ne(variables['XML_RESULTS_FILE'], '')
    inputs:
      testRunTitle: ${{ replace(parameters.jobName, '_', '-') }}
      testResultsFormat: xUnit
      testResultsFiles: $(XML_RESULTS_FILE)
      platform: ${{ replace(parameters.jobName, '_', '-') }}
