name: "Copilot Setup Steps"

on:
  workflow_dispatch:
  push:
    paths:
      - .github/workflows/copilot-setup-steps.yml
  pull_request:
    paths:
      - .github/workflows/copilot-setup-steps.yml

jobs:
  copilot-setup-steps:
    runs-on: ubuntu-latest
    timeout-minutes: 15

    permissions:
      contents: read

    steps:
      - name: Checkout code including submodules
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Fetch full history and tags in submodules
        run: |
          git submodule foreach 'git fetch --unshallow || true'
          git submodule foreach 'git fetch --tags'

      - name: Setup Java 21
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Setup Go 1.24.1
        uses: actions/setup-go@v5
        with:
          go-version: '1.24.1'
          cache: true
          cache-dependency-path: syncthing/src/github.com/syncthing/syncthing/go.sum

      - name: Setup Android NDK
        env:
          COPILOT_BUILDTOOLS_DIR: '${{ runner.temp }}/copilot-build-tools'
        shell: bash
        run: |
          ANDROID_NDK_VERSION=r28
          ANDROID_NDK_ZIP="android-ndk-${ANDROID_NDK_VERSION}-linux.zip"
          ANDROID_NDK_URL="https://dl.google.com/android/repository/${ANDROID_NDK_ZIP}"
          mkdir -p "$COPILOT_BUILDTOOLS_DIR"
          wget -q "$ANDROID_NDK_URL" -O "$COPILOT_BUILDTOOLS_DIR/$ANDROID_NDK_ZIP"
          unzip -q "$COPILOT_BUILDTOOLS_DIR/$ANDROID_NDK_ZIP" -d "$COPILOT_BUILDTOOLS_DIR"
          echo "ANDROID_NDK_HOME=$COPILOT_BUILDTOOLS_DIR/android-ndk-${ANDROID_NDK_VERSION}" >> $GITHUB_ENV

      - name: Cache Gradle packages
        uses: actions/cache@v3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
