parameters:
  langauge: ''
  repo: $(Horton.Repo)
  commit: $(Horton.Commit)
  forced_image: $(Horton.ForcedImage)
  job_tag: 'linux'
  variant: ''
  repo_address: $(IOTHUB-E2E-REPO-ADDRESS)
  repo_user: $(IOTHUB-E2E-REPO-USER)
  repo_password: $(IOTHUB-E2E-REPO-PASSWORD)

steps:
- template: steps-ensure-e2e-fx-repo.yaml

- powershell: |
    function IsEmpty($s) { ( ($s -eq $null) -or ( $s -eq "" ) -or ( $s.StartsWith("$") ) ) } 

    function GetArchitecture() {
      $plat = (uname -m) | Out-String     
      if ($plat.StartsWith("armv7")) {
        "arm64"
      } else {
        "x64"
      }
    }

    $BuildImage="yes"
    if ( -not (IsEmpty($env:ForcedImage)) ) {
      $BuildImage="no"
    }


    $IsPr = "no"
    $LanguageToPass=$env:Language
    $RepoToPass=$env:Repo
    $CommitToPass = $env:Commit
    $VariantToPass = $env:Variant

    if (IsEmpty($CommitToPass)) {
        $CommitToPass="master"
    }

    if (IsEmpty($VariantToPass)) {
        $VariantToPass=""
    }

    if (IsEmpty($LanguageToPass)) {
        if ($VariantToPass -like "node16") {
            $LanguageToPass="node"
        } elseif ($VariantToPass -like "node18") {
            $LanguageToPass="node"
        } elseif ($VariantToPass -like "py37") {
            $LanguageToPass="pythonv2"
        } elseif ($VariantToPass -like "py38") {
            $LanguageToPass="pythonv2"
        } elseif ($VariantToPass -like "py39") {
            $LanguageToPass="pythonv2"
        } elseif ($VariantToPass -like "py310") {
            $LanguageToPass="pythonv2"
        }
    }

    if (IsEmpty($RepoToPass)) {
        if ($LanguageToPass -like 'c') {
            $RepoToPass="Azure/azure-iot-sdk-c"
        } elseif ($LanguageToPass -like 'node') {
            $RepoToPass="Azure/azure-iot-sdk-node"
        } elseif ($LanguageToPass -like 'csharp') {
            $RepoToPass="Azure/azure-iot-sdk-csharp"
        } elseif ($LanguageToPass -like 'java') {
            $RepoToPass="Azure/azure-iot-sdk-java"
        } elseif ($LanguageToPass -like 'pythonv2') {
            $RepoToPass="Azure/azure-iot-sdk-python"
        }
    }

    if ($RepoToPass -like 'https://github.com/*') {
      $RepoToPass=$RepoToPass.Substring('https://github.com/'.length)
    }

    if ("$env:SYSTEM_PULLREQUEST_TARGETBRANCH" -ne "") {
      $CommitToPass = $env:SYSTEM_PULLREQUEST_TARGETBRANCH
      $IsPr = "yes"
    }

    if ( $CommitToPass -like "master" ) {
      if ($LanguageToPass -like 'pythonv2') {
        $CommitToPass="v2"
      } else {
        $CommitToPass="main"
      }
    } 

    $Architecture=GetArchitecture

    Write-Host "##vso[task.setvariable variable=architecture]${Architecture}"
    Write-Host "##vso[task.setvariable variable=buildImage]${BuildImage}"
    Write-Host "##vso[task.setvariable variable=IsPr]${IsPr}"
    Write-Host "##vso[task.setvariable variable=RepoToPass]${RepoToPass}"
    Write-Host "##vso[task.setvariable variable=CommitToPass]${CommitToPass}"
    Write-Host "##vso[task.setvariable variable=LanguageToPass]${LanguageToPass}"
    Write-Host "##vso[task.setvariable variable=VariantToPass]${VariantToPass}"

    Write-Host "Architecture=${Architecture}"
    Write-Host "BuildImage=${BuildImage}"
    Write-Host "IsPr=${IsPr}"
    Write-Host "RepoToPass=${RepoToPass}"
    Write-Host "CommitToPass=${CommitToPass}"
    Write-Host "LanguageToPass=${LanguageToPass}"
    Write-Host "VariantToPass=${VariantToPass}"

  displayName: Custom task execution logic
  env:
    Commit: ${{ parameters.commit }}
    ForcedImage: ${{ parameters.forced_image }}
    Repo: ${{ parameters.repo }}
    Variant: ${{ parameters.variant }}
    Language: ${{ parameters.language }}
  ignoreLASTEXITCODE: false
  errorActionPreference: Stop
  failOnStderr: true

- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
    architecture: $(architecture)

- bash: source ${HORTON_FRAMEWORKROOT}/bin/activate_horton && pip list
  displayName: 'Create Horton environment'
  condition: and(succeeded(), eq(variables['buildImage'],'yes'))

- bash: |
    cd ${BUILD_SOURCESDIRECTORY} &&
    echo "saving patch from ${CommitToPass} to HEAD" &&
    git diff -p origin/${CommitToPass} --binary > ${AGENT_BUILDDIRECTORY}/patchfile &&
    mkdir -p $(Build.ArtifactStagingDirectory)/${{ parameters.job_tag }} &&
    cp ${AGENT_BUILDDIRECTORY}/patchfile $(Build.ArtifactStagingDirectory)/${{ parameters.job_tag }} &&
    mv ${AGENT_BUILDDIRECTORY}/patchfile ${HORTON_FRAMEWORKROOT}/docker_images/$(LanguageToPass)
  displayName: "create patch for pull request changes"
  condition: and(
      succeeded(), 
      eq(variables['buildImage'],'yes'),
      not(eq(variables['Horton.FrameworkRoot'], variables['Build.SourcesDirectory'])),
      eq(variables['IsPr'],'yes')
      )
  env:
    CommitToPass: $(CommitToPass)

- task: PublishBuildArtifacts@1
  displayName: "publish patch"
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)/'
    ArtifactName: 'patch'
  condition: and(
      succeeded(), 
      eq(variables['buildImage'],'yes'),
      not(eq(variables['Horton.FrameworkRoot'], variables['Build.SourcesDirectory'])),
      eq(variables['IsPr'],'yes')
      )

- bash: |
    docker login -u ${{ parameters.repo_user }} -p ${{ parameters.repo_password }} ${{ parameters.repo_address }} 
  displayName: 'docker login'
  condition: and(succeeded(), eq(variables['buildImage'],'yes'))

- bash: |
    ${HORTON_FRAMEWORKROOT}/bin/horton build --language $(LanguageToPass) --repo $(RepoToPass) --commit $(CommitToPass) --variant $(VariantToPass)
  displayName: "build docker image ${{ parameters.language }} ${{ parameters.variant }}"
  env: 
    IOTHUB_E2E_REPO_ADDRESS: $(IOTHUB-E2E-REPO-ADDRESS)
    IOTHUB_E2E_REPO_USER: $(IOTHUB-E2E-REPO-USER)
    IOTHUB_E2E_REPO_PASSWORD: $(IOTHUB-E2E-REPO-PASSWORD)
  condition: and(succeeded(), eq(variables['buildImage'],'yes'))

