name: Release builds

on:
  release:
    types: [ published ]

permissions:
  contents: write

jobs:
  # Set release to prerelease first
  prerelease:
    name: Set Prerelease
    runs-on: ubuntu-latest
    steps:
      - name: Prerelease
        uses: irongut/EditRelease@v1.2.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          id: ${{ github.event.release.id }}
          prerelease: true

  # Main release job for all platforms
  release:
    needs: prerelease
    strategy:
      matrix:
        build-type: [ 'standard', 'lite' ]
        target-platform: [ '', 'android', 'freebsd', 'linux_musl', 'linux_musl_arm' ]
    name: Release ${{ matrix.target-platform && format('{0} ', matrix.target-platform) || '' }}${{ matrix.build-type == 'lite' && 'Lite' || '' }}
    runs-on: ubuntu-latest
    steps:

      - name: Free Disk Space (Ubuntu)
        if: matrix.target-platform == ''
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'

      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install dependencies
        if: matrix.target-platform == ''
        run: |
          sudo snap install zig --classic --beta
          docker pull crazymax/xgo:latest
          go install github.com/crazy-max/xgo@latest
          sudo apt install upx

      - name: Build
        run: |
          bash build.sh release ${{ matrix.build-type == 'lite' && 'lite' || '' }} ${{ matrix.target-platform }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          FRONTEND_REPO: ${{ vars.FRONTEND_REPO }}

      - name: Upload assets
        uses: softprops/action-gh-release@v2
        with:
          files: build/compress/*
          prerelease: false

