# Prerequisites
# 1. C:\LocalPackages\sa3client.1.4.1.nupkg exists on CI-SRV01
#    (download from https://www.nuget.org/api/v2/package/sa3client/1.4.1 on a machine with internet,
#     then copy it to the server)
# 2. Package_C1P053XXXXXX.data is uploaded to Pipelines > Library > Secure files
#    and the pipeline is authorized to use it
# 3. C1_SERIAL_KEY and C1_LICENSE_PASSWORD are defined as secret variables
#    (pipeline variables or a variable group)

trigger:
- main

pool:
  name: Default   # self-hosted pool that contains AG01 and AG02

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
# Gets the activation package file from the list of secure files in the library
- task: DownloadSecureFile@1
  name: c1ActivationPackage
  displayName: 'Download ComponentOne activation package'
  inputs:
    secureFile: 'Package_C1P053XXXXXX.data'   # use your exact file name

# Step 1: install sa3client from the local folder (no internet needed)
- task: PowerShell@2
  displayName: 'Install sa3client (offline)'
  inputs:
    targetType: 'inline'
    errorActionPreference: 'stop'
    failOnStderr: false
    script: |
      $ErrorActionPreference = 'Stop'

      if (-not (Test-Path "C:\LocalPackages\sa3client.1.4.1.nupkg")) {
        throw "sa3client.1.4.1.nupkg was not found in C:\LocalPackages on this agent"
      }

      $toolsDir = Join-Path $env:USERPROFILE ".dotnet\tools"

      if (-not (Test-Path (Join-Path $toolsDir "sa3client.exe"))) {
        Write-Host "Installing sa3client..."
        dotnet tool install -g sa3client --add-source "C:\LocalPackages" --ignore-failed-sources
        if ($LASTEXITCODE -ne 0) { throw "sa3client install failed with exit code $LASTEXITCODE" }
      }

# Step 2: activate the pipeline key with the package file, then verify
- task: PowerShell@2
  displayName: 'Activate ComponentOne pipeline license'
  inputs:
    targetType: 'inline'
    errorActionPreference: 'stop'
    failOnStderr: false
    script: |
      $ErrorActionPreference = 'Stop'

      $pkgDir = Join-Path $env:AGENT_TEMPDIRECTORY "C1LocalActivationPackage"
      New-Item -ItemType Directory -Force -Path $pkgDir | Out-Null
      Copy-Item "$(c1ActivationPackage.secureFilePath)" $pkgDir -Force

      if ([string]::IsNullOrWhiteSpace($env:C1_SERIAL_KEY)) { throw "C1_SERIAL_KEY is empty or not set" }
      if ([string]::IsNullOrWhiteSpace($env:C1_LICENSE_PASSWORD)) { throw "C1_LICENSE_PASSWORD is empty or not set" }

      sa3client local-activate "$env:C1_SERIAL_KEY" "$env:C1_LICENSE_PASSWORD" --path "$pkgDir"
      if ($LASTEXITCODE -ne 0) { throw "sa3client activation failed with exit code $LASTEXITCODE" }

      sa3client list --all
  env:
    C1_SERIAL_KEY: $(C1_SERIAL_KEY)
    C1_LICENSE_PASSWORD: $(C1_LICENSE_PASSWORD)

# Build steps (activation must come before these)
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
