name: Flutter Schmutter

on:
  push:
    tags:
      - '*'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: checkout code
        uses: actions/checkout@v4
      
      - name: cache pub deps
        uses: actions/cache@v4
        with:
          path: ~/.pub-cache
          key: ${{ runner.os }}-pub-${{ hashFiles('**/pubspec.yaml') }}
          restore-keys: ${{ runner.os }}-pub-
      
      - name: set up jdk
        uses: actions/setup-java@v3
        with:
          java-version: '21'
          distribution: temurin
      
      - name: setup android sdk
        uses: android-actions/setup-android@v3
      
      - name: setup flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: 3.33.0-0.2.pre
          channel: master

      - name: install deps
        run: flutter pub get
        env:
          TAR_OPTIONS: --no-same-owner
      
      - name: build apk
        run: flutter build apk --release

      - name: release-build
        uses: akkuman/gitea-release-action@v1
        env:
          NODE_OPTIONS: '--experimental-fetch'
        with:
          files: |-
            build/app/outputs/flutter-apk/app-release.apk
          token: '${{secrets.RELEASE_TOKEN}}'

      - name: upload apk to f-droid server
        run: |
          BUILD_NUMBER=$(grep '^version:' pubspec.yaml | sed 's/.*+//')
          curl -X POST "https://flumm.io/pullfdroid.php" \
            -F "token=${{ secrets.PULLER_TOKEN }}" \
            -F "apk=@build/app/outputs/flutter-apk/app-release.apk" \
            -F "build=$BUILD_NUMBER"
