steps:
- pwsh: |
    Get-ChildItem ENV:
  displayName: Capture environment

- template: release-SetReleaseTagAndContainerName.yml

- pwsh: |
    $name = "{0}_{1:x}" -f '$(releaseTag)', (Get-Date).Ticks
    Write-Host $name
    Write-Host "##vso[build.updatebuildnumber]$name"
  displayName: Set Release Name

- pwsh: |
    $azcopy = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"

    & $azcopy /Source:https://$(StorageAccount).blob.core.windows.net/$(AzureVersion) /Dest:$(System.ArtifactsDirectory) /S /SourceKey:$(StorageAccountKey)
  displayName: Download Azure Artifacts

- pwsh: |
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse | Select-Object -ExpandProperty Name
  displayName: Capture Artifact Listing

- pwsh: |
    $message = @()
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse -filter *.rpm | ForEach-Object {
        if($_.Name -notmatch 'powershell\-(preview-|lts-)?\d\.\d\.\d(_[a-z]*\.\d+)?-1.rh.x86_64\.rpm')
        {
            $messageInstance = "$($_.Name) is not a valid package name"
            $message += $messageInstance
            Write-Warning $messageInstance
        }
    }
    if($message.count -gt 0){throw ($message | out-string)}
  displayName: Validate RPM package names

- pwsh: |
    $message = @()
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse -filter *.tar.gz | ForEach-Object {
        if($_.Name -notmatch 'powershell-(lts-)?\d\.\d\.\d\-([a-z]*.\d+\-)?(linux|osx|linux-alpine)+\-(x64\-fxdependent|x64|arm32|arm64)\.(tar\.gz)')
        {
            $messageInstance = "$($_.Name) is not a valid package name"
            $message += $messageInstance
            Write-Warning $messageInstance
        }
    }
    if($message.count -gt 0){throw ($message | out-string)}
  displayName: Validate Tar.Gz Package Names

- pwsh: |
    $message = @()
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse -filter *.pkg | ForEach-Object {
        if($_.Name -notmatch 'powershell-(lts-)?\d\.\d\.\d\-([a-z]*.\d+\-)?osx(\.10\.12)?\-(x64|arm64)\.pkg')
        {
            $messageInstance = "$($_.Name) is not a valid package name"
            $message += $messageInstance
            Write-Warning $messageInstance
        }
    }
    if($message.count -gt 0){throw ($message | out-string)}
  displayName: Validate PKG Package Names

- pwsh: |
    $message = @()
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse -include *.zip, *.msi | ForEach-Object {
        if($_.Name -notmatch 'PowerShell-\d\.\d\.\d\-([a-z]*.\d+\-)?win\-(fxdependent|x64|arm32|arm64|x86|fxdependentWinDesktop)\.(msi|zip){1}')
        {
            $messageInstance = "$($_.Name) is not a valid package name"
            $message += $messageInstance
            Write-Warning $messageInstance
        }
    }

    if($message.count -gt 0){throw ($message | out-string)}
  displayName: Validate Zip and MSI Package Names

- pwsh: |
    $message = @()
    Get-ChildItem $(System.ArtifactsDirectory)\* -recurse -filter *.deb | ForEach-Object {
        if($_.Name -notmatch 'powershell(-preview|-lts)?_\d\.\d\.\d([\-~][a-z]*.\d+)?-\d\.deb_amd64\.deb')
        {
            $messageInstance = "$($_.Name) is not a valid package name"
            $message += $messageInstance
            Write-Warning $messageInstance
        }
    }
    if($message.count -gt 0){throw ($message | out-string)}
  displayName: Validate Deb Package Names
