variables:
  - group: 'jellyfin'

trigger:
  batch: true
  branches:
    include:
      - master
      - release*
  tags:
    include:
      - '*'

pr:
  branches:
    include:
      - '*'

jobs:
  - job: Test
    displayName: 'Test'

    pool:
      vmImage: 'ubuntu-latest'

    steps:
      - task: Gradle@2
        displayName: 'Run Tests'
        inputs:
          gradleWrapperFile: 'gradlew'
          tasks: 'test'
          publishJUnitResults: true
          testResultsFiles: '**/TEST-*.xml'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: 1.11
          sonarQubeRunAnalysis: false

  - job: Build
    displayName: 'Build'

    pool:
      vmImage: 'ubuntu-latest'

    steps:
      - script: 'echo "##vso[task.setvariable variable=JELLYFIN_VERSION]$(git describe --tags)"'
        condition: startsWith(variables['Build.SourceBranch'], 'refs/tags')
        displayName: 'Set Version Variable'

      - task: Gradle@2
        displayName: 'Build Debug'
        inputs:
          gradleWrapperFile: 'gradlew'
          tasks: 'assembleDebug'
          publishJUnitResults: false
          testResultsFiles: '**/TEST-*.xml'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: 1.11
          sonarQubeRunAnalysis: false

      - task: Gradle@2
        displayName: 'Build Release'
        inputs:
          gradleWrapperFile: 'gradlew'
          tasks: 'assembleRelease'
          publishJUnitResults: false
          testResultsFiles: '**/TEST-*.xml'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: 1.11
          sonarQubeRunAnalysis: false

      - task: CopyFiles@2
        displayName: 'Copy APKs'
        inputs:
          SourceFolder: 'app/build/outputs/apk/'
          Contents: '**/*.apk'
          TargetFolder: '$(Build.ArtifactStagingDirectory)'
          flattenFolders: true

      - task: PublishBuildArtifacts@1
        displayName: 'Publish APKs'
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)'
          ArtifactName: 'jellyfin-android'
          publishLocation: 'Container'

  - job: Publish
    displayName: 'Publish'

    dependsOn: Build
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags')

    pool:
      vmImage: 'ubuntu-latest'

    steps:
      - script: 'echo "##vso[task.setvariable variable=JELLYFIN_VERSION]$(git describe --tags)"'
        displayName: 'Set Version Variable'

      - task: DownloadPipelineArtifact@2
        displayName: 'Download APKs'
        inputs:
          source: 'current'
          artifact: 'jellyfin-android'
          path: '$(System.ArtifactsDirectory)'
          runVersion: 'latest'

      - task: DownloadSecureFile@1
        displayName: 'Download KeyStore'
        name: 'KeyStore'
        inputs:
          secureFile: 'keystore'

      - script: '$(find / -name apksigner -print -quit) sign --ks ${KEYSTORE_SECUREFILEPATH} --ks-pass pass:${PASSWORD} --out ${SYSTEM_ARTIFACTSDIRECTORY}/jellyfin-android-${JELLYFIN_VERSION}-libre-release.apk ${SYSTEM_ARTIFACTSDIRECTORY}/jellyfin-android-${JELLYFIN_VERSION}-libre-release-unsigned.apk'
        displayName: 'Sign Libre Release APK'
        env:
          PASSWORD: $(KeyStorePassword)

      - script: '$(find / -name apksigner -print -quit) sign --ks ${KEYSTORE_SECUREFILEPATH} --ks-pass pass:${PASSWORD} --out ${SYSTEM_ARTIFACTSDIRECTORY}/jellyfin-android-${JELLYFIN_VERSION}-proprietary-release.apk ${SYSTEM_ARTIFACTSDIRECTORY}/jellyfin-android-${JELLYFIN_VERSION}-proprietary-release-unsigned.apk'
        displayName: 'Sign Proprietary Release APK'
        env:
          PASSWORD: $(KeyStorePassword)

      - task: GithubRelease@0
        displayName: 'GitHub Upload'
        inputs:
          gitHubConnection: Jellyfin Release Download
          repositoryName: jellyfin/jellyfin-android
          assets: '$(System.ArtifactsDirectory)/*.apk'
          action: 'edit'
          assetUploadMode: 'replace'
          tag: '$(JELLYFIN_VERSION)'

      - task: CopyFilesOverSSH@0
        inputs:
          sshEndpoint: repository
          sourceFolder: '$(System.ArtifactsDirectory)'
          contents: '**'
          targetFolder: '/srv/repository/releases/client/android/versions/$(JELLYFIN_VERSION)'

      - task: SSH@0
        inputs:
          sshEndpoint: repository
          runOptions: 'inline'
          inline: 'cd /srv/repository/releases/client/android && rm -rf *.apk && ln -s versions/$(JELLYFIN_VERSION)/jellyfin-android-$(JELLYFIN_VERSION)-*.apk .'
