name: Build mod

on:
  workflow_dispatch:
  #schedule:                 # раз в 36 часов
  #  - cron: "0 0 */3 * *"   # каждые 3 дня в 00:00
  #  - cron: "0 12 */3 * *"  # каждые 3 дня в 12:00

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Download APK
        run: |
          curl -L -o app.apk "https://mirror-dl.anixart-app.com/anixart-beta.apk"

      - name: Ensure aapt is installed
        run: |
          if ! command -v aapt &> /dev/null; then
            echo "aapt не найден, устанавливаем..."
            sudo apt-get update && sudo apt-get install -y --no-install-recommends android-sdk-build-tools
          fi

      - name: Export secrets
        env:
          KEYSTORE: ${{ secrets.KEYSTORE }}
          KEYSTORE_PASS: ${{ secrets.KEYSTORE_PASS }}
        run: |
          # Export so later steps can reference them
          echo "$KEYSTORE" | base64 -d > keystore.jks
          echo "$KEYSTORE_PASS" > keystore.pass

      - name: Prepare to build APK
        id: build
        run: |
          mkdir original
          mv app.apk original/
          python ./main.py init

      - name: Build APK
        id: build
        run: |
          python ./main.py build -f

      - name: Read title from report.log
        id: get_title
        run: |
          TITLE=$(head -n 1 modified/report.log)
          echo "title=${TITLE}" >> $GITHUB_OUTPUT

      - name: Setup go
        if: steps.build.outputs.BUILD_EXIT == '0'
        uses: actions/setup-go@v4
        with:
          go-version: '>=1.20'

      - name: Make release
        if: steps.build.outputs.BUILD_EXIT == '0'
        uses: https://gitea.com/actions/release-action@main
        with:
          title: ${{ steps.get_title.outputs.title }}
          body_path: modified/report.log
          draft: true
          api_key: '${{secrets.RELEASE_TOKEN}}'
          files: |-
            modified/**-mod.apk
            modified/report.log
