parameters:
  ArtifactDirectory: not-specified
  OutputDirectory: not-specified
  RepositoryUrl: not-specified
  GroupID:
  ArtifactID:
  GPGExecutablePath: $(Pipeline.Workspace)/azure-sdk-build-tools/tools/gpg/bin/gpg.exe
  JavaRepoRoot: $(Pipeline.Workspace)/azure-sdk-for-java
  StageOnly: false
  ShouldPublish: true

steps:
  - ${{if eq(parameters.Target, 'Burner')}}:
    - task: PowerShell@2
      displayName: 'Publish to Burner Feed'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/Publish-MavenPackages.ps1
        arguments: >
          -ArtifactDirectory ${{ parameters.ArtifactDirectory }}
          -GroupIDFilter "${{ parameters.GroupID }}"
          -ArtifactIDFilter "${{ parameters.ArtifactID }}"
          -RepositoryUrl https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-$(Build.BuildID)/maven/v1
          -RepositoryUsername nobody
          -RepositoryPassword $(System.AccessToken)
          -GPGExecutablePath ${{ parameters.GPGExecutablePath }}
          -ShouldPublish:$${{parameters.ShouldPublish}}
          -InformationAction Continue

  - ${{if eq(parameters.Target, 'JavaDevFeed')}}:
    - task: PowerShell@2
      displayName: 'Publish to Java Dev Feed'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/Publish-MavenPackages.ps1
        arguments: >
          -ArtifactDirectory ${{ parameters.ArtifactDirectory }}
          -GroupIDFilter "${{ parameters.GroupID }}"
          -ArtifactIDFilter "${{ parameters.ArtifactID }}"
          -RepositoryUrl ${{ parameters.RepositoryUrl }}
          -RepositoryUsername nobody
          -RepositoryPassword $(System.AccessToken)
          -GPGExecutablePath ${{ parameters.GPGExecutablePath }}
          -ShouldPublish:$${{parameters.ShouldPublish}}
          -InformationAction Continue

  - ${{if eq(parameters.Target, 'AndroidDevFeed')}}:
    - task: PowerShell@2
      displayName: 'Publish to Andriod Public Dev Feed'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/Publish-MavenPackages.ps1
        arguments: >
          -ArtifactDirectory ${{ parameters.ArtifactDirectory }}
          -GroupIDFilter "${{ parameters.GroupID }}"
          -ArtifactIDFilter "${{ parameters.ArtifactID }}"
          -RepositoryUrl https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-android/maven/v1
          -RepositoryUsername nobody
          -RepositoryPassword $(System.AccessToken)
          -GPGExecutablePath ${{ parameters.GPGExecutablePath }}
          -ShouldPublish:$${{parameters.ShouldPublish}}
          -InformationAction Continue

  - ${{if eq(parameters.Target, 'MavenCentral')}}:
    - task: PowerShell@2
      displayName: 'Publish to Maven Central'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/Publish-MavenPackages.ps1
        arguments: >
          -ArtifactDirectory ${{ parameters.ArtifactDirectory }}
          -GroupIDFilter "${{ parameters.GroupID }}"
          -ArtifactIDFilter "${{ parameters.ArtifactID }}"
          -RepositoryUrl https://oss.sonatype.org/service/local/staging/deploy/maven2/
          -RepositoryUsername azuresdk
          -RepositoryPassword $(azuresdk-sonatype-password)
          -GPGExecutablePath ${{ parameters.GPGExecutablePath }}
          -StageOnly:$${{parameters.StageOnly}}
          -ShouldPublish:$${{parameters.ShouldPublish}}
          -InformationAction Continue

  - ${{if eq(parameters.Target, 'EsrpRelease')}}:
    - task: PowerShell@2
      displayName: 'Gpg sign and hash packages'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/SignAndHash-MavenPackages.ps1
        arguments: >
          -Path ${{ parameters.ArtifactDirectory }}
          -DestinationPath ${{ parameters.OutputDirectory }}
          -GroupIDFilter "${{ parameters.GroupID }}"
          -ArtifactIDFilter "${{ parameters.ArtifactID }}"
          -GPGExecutablePath ${{ parameters.GPGExecutablePath }}
          -InformationAction Continue
    - task: PowerShell@2
      displayName: 'Flatten output folder'
      inputs:
        pwsh: true
        workingDirectory: $(Agent.BuildDirectory)
        filePath: ${{ parameters.JavaRepoRoot }}/eng/scripts/Flatten-MavenPackageFolder.ps1
        arguments: >
          -Path ${{ parameters.OutputDirectory }}
          -InformationAction Continue
    - ${{if and(eq(parameters.ShouldPublish, 'true'), ne(parameters.StageOnly, 'true'))}}:
      - task: EsrpRelease@2
        displayName: 'Publish to ESRP'
        inputs:
          ConnectedServiceName: 'ESRP Release Service'
          Intent: 'PackageDistribution'
          ContentType: 'Maven'
          PackageLocation: ${{ parameters.OutputDirectory }}
          Owners: $(Build.RequestedForEmail)
          Approvers: 'wesh@microsoft.com'
          ServiceEndpointUrl: 'https://api.esrp.microsoft.com'
          MainPublisher: 'ESRPRELPACMANTEST'
          DomainTenantId: '72f988bf-86f1-41af-91ab-2d7cd011db47'
