on:
  push:
    # NOTE: Proposal to make this action available only for tags
    #tags:
    #- 'v*.*.*'

name: Flatpak
jobs:
  prepare-repo:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: cache of container
        id: cache-container
        uses: actions/cache@v3
        with:
          path: prepare-dist
          key: ${{ runner.os }}-container-${{ hashFiles('prepare-dist') }}
      - name: Run latest-tag
        id: latest-tag
        uses: oprypin/find-latest-tag@v1
        with:
          repository:
            luminous-devs/lume
            #FIXME: luminous-devs after merged fix, now it just won't find tags
            # repository: ${{ github.repository }}

      - name: Build container
        #        if: steps.cache-container.outputs.cache-hit != 'true'
        run: |
          docker buildx build -t flatpak-prepare-lume --build-arg=${{steps.latest-tag.outputs.tag}} --rm --output=. --target=final -f flatpak/Containerfile .
      - name: Copy flatpak files content
        run: |
          cp -r flatpak/*.xml flatpak/*.desktop flatpak/*.yml prepare-dist
      - uses: actions/upload-artifact@v4
        with:
          name: repo-dist
          path: prepare-dist

  flatpak:
    name: flatpak-bundle
    needs: prepare-repo
    runs-on: ubuntu-latest
    container:
      image: bilelmoussaoui/flatpak-github-actions:gnome-45
      options: --privileged
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: repo-dist
      - uses: actions/checkout@v4
        with:
          repository: flathub/shared-modules
          path: shared-modules
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: lume.flatpak
          manifest-path: nu.lume.Lume.yml
          restore-cache: false
           #          cache-key: flatpak-builder-${{ github.sha }}
      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          append_body: true
          files: lume.flatpak
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - uses: geekyeggo/delete-artifact@v4
        with:
          name: repo-dist
          token: ${{ secrets.GITHUB_TOKEN }}
