name: Build TV app

on:
  push:
  workflow_dispatch:

# No marketplace actions: this Gitea runner resolves bare action names
# against the local instance, and even with full URLs it cannot resolve
# annotated tags ("unsupported object type"). Everything below is plain
# shell, which also keeps the job readable.
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      ANDROID_SDK_ROOT: /opt/android-sdk
      CMDLINE_TOOLS: https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
    steps:
      - name: Checkout
        run: |
          git init -q .
          git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          git fetch -q --depth 1 origin "${GITHUB_SHA}"
          git checkout -q FETCH_HEAD

      - name: Install JDK
        run: |
          apt-get update -qq
          DEBIAN_FRONTEND=noninteractive apt-get install -y -qq openjdk-17-jdk-headless > /dev/null
          java -version

      - name: Install Android SDK
        run: |
          mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools"
          curl -sSL -o /tmp/tools.zip "$CMDLINE_TOOLS"
          unzip -q /tmp/tools.zip -d "$ANDROID_SDK_ROOT/cmdline-tools"
          mv "$ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools" "$ANDROID_SDK_ROOT/cmdline-tools/latest"
          # Accept licences by writing the hashes: piping "yes" into
          # sdkmanager dies of SIGPIPE (exit 141) under pipefail.
          mkdir -p "$ANDROID_SDK_ROOT/licenses"
          echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_SDK_ROOT/licenses/android-sdk-license"
          echo "84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_SDK_ROOT/licenses/android-sdk-preview-license"
          "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" \
            "platforms;android-35" "build-tools;35.0.0" "platform-tools" > /dev/null
          echo "sdk.dir=$ANDROID_SDK_ROOT" > local.properties

      - name: Unit tests
        run: ./gradlew testDebugUnitTest --no-daemon --stacktrace

      - name: Build debug APK
        run: ./gradlew assembleDebug --no-daemon --stacktrace

      - name: Summary
        if: always()
        run: |
          echo "APK:"; ls -la app/build/outputs/apk/debug/ 2>/dev/null || echo "  (kein Build)"
          echo "Tests:"; ls tests/runs/junit/ 2>/dev/null || echo "  (keine Reports)"
