name: Release

on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build-flatpak:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install flatpak and flatpak-builder
        run: |
          sudo apt-get update -q
          sudo apt-get install -y flatpak flatpak-builder

      - name: Add Flathub remote
        run: |
          flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

      - name: Install runtimes
        run: |
          flatpak install -y --user \
            org.freedesktop.Platform//24.08 \
            org.freedesktop.Sdk//24.08 \
            org.freedesktop.Sdk.Extension.rust-stable//24.08

      - name: Generate cargo-sources.json
        run: |
          pip3 install tomlkit aiohttp
          git clone --depth=1 https://github.com/flatpak/flatpak-builder-tools.git /tmp/flatpak-builder-tools
          python3 /tmp/flatpak-builder-tools/cargo/flatpak-cargo-generator.py \
            Cargo.lock -o flatpak/cargo-sources.json

      - name: Build Flatpak
        run: |
          flatpak-builder --force-clean --user --repo=repo build-dir \
            flatpak/io.github.HakanSeven12.H7CAD.ci.yml

      - name: Create bundle
        run: |
          flatpak build-bundle repo H7CAD.flatpak io.github.HakanSeven12.H7CAD

      - name: Upload bundle to release
        if: github.event_name == 'release'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.ref_name }} H7CAD.flatpak --clobber --repo ${{ github.repository }}

  build-windows:
    runs-on: windows-latest
    permissions:
      contents: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Build
        run: cargo build --release

      - name: Upload exe to release
        if: github.event_name == 'release'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.ref_name }} target/release/H7CAD.exe --clobber --repo ${{ github.repository }}
