name: App / Publish

on:
  push:
    tags:
      - v*

jobs:
  publish:
    name: Publish
    runs-on: ubuntu-24.04
    if: ${{ contains(github.repository_owner, 'jellyfin') }}
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Java
        uses: actions/setup-java@0f481fcb613427c0f801b606911222b5b6f3083a # v5.5.0
        with:
          distribution: temurin
          java-version-file: .tools-versions

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@3f131e8634966bd73d06cc69884922b02e6faf92 # v6.2.0

      - name: Set JELLYFIN_VERSION
        run: echo "JELLYFIN_VERSION=$(echo ${GITHUB_REF#refs/tags/v} | tr / -)" >> $GITHUB_ENV

      - name: Decode keystore
        run: echo "${{ secrets.KEYSTORE }}" | base64 --decode > keystore.jks

      - name: Assemble release files
        env:
          KEYSTORE_FILE: ${{ github.workspace }}/keystore.jks
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          SIGNING_KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          SIGNING_KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        run: ./gradlew assemble bundleRelease versionTxt

      - name: Prepare release archive
        run: |
          mkdir -p build/jellyfin-publish
          mv app/build/outputs/apk/libre/debug/jellyfin-android-*-libre-debug.apk build/jellyfin-publish/
          mv app/build/outputs/apk/libre/release/jellyfin-android-*-libre-release.apk build/jellyfin-publish/
          mv app/build/outputs/bundle/libreRelease/jellyfin-android-*-libre-release.aab build/jellyfin-publish/
          mv app/build/outputs/apk/proprietary/debug/jellyfin-android-*-proprietary-debug.apk build/jellyfin-publish/
          mv app/build/outputs/apk/proprietary/release/jellyfin-android-*-proprietary-release.apk build/jellyfin-publish/
          mv app/build/outputs/bundle/proprietaryRelease/jellyfin-android-*-proprietary-release.aab build/jellyfin-publish/ 
          mv app/build/version.txt build/jellyfin-publish/

      - name: Upload release archive to GitHub release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release upload "$GITHUB_REF_NAME" build/jellyfin-publish/* --clobber

      - name: Configure deployment
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.REPO_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa

      - name: Upload release archive to repo.jellyfin.org
        env:
          TARGET_PATH: /srv/incoming/android/v${{ env.JELLYFIN_VERSION }}/
          SOURCE_PATH: build/jellyfin-publish/
        run: rsync -vrptz -e "ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no" "$SOURCE_PATH" "${{ secrets.REPO_USER }}@${{ secrets.REPO_HOST }}:$TARGET_PATH"

      - name: Update repo.jellyfin.org symlinks
        env:
          REPO_HOST: ${{ secrets.REPO_HOST }}
          REPO_USER: ${{ secrets.REPO_USER }}
        run: |
          ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no "${REPO_USER}@${REPO_HOST}" "bash -s" <<EOF
            if [ -d "/srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}" ] && [ -n "${{ env.JELLYFIN_VERSION }}" ]; then
                sudo rm -r /srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}
            fi
            sudo mv /srv/incoming/android/v${{ env.JELLYFIN_VERSION }} /srv/repository/main/client/android/versions/v${{ env.JELLYFIN_VERSION }}
            cd /srv/repository/main/client/android;
            sudo rm -rf *.apk version.txt;
            sudo ln -s versions/v${{ env.JELLYFIN_VERSION }}/jellyfin-android-v${{ env.JELLYFIN_VERSION }}-*.apk .;
            sudo ln -s versions/v${{ env.JELLYFIN_VERSION }}/version.txt .;
          EOF
