name: Packaging
on:
  pull_request:
  push:
    tags:
      - '*'
    branches:
      - 'master'
      - 'ci-tests/**'  # Branch namespace can be used to test changes to test before going to master
jobs:
  aur:
    name: Arch Linux AUR Packaging
    runs-on: ubuntu-latest
    container:
      image: archlinux:base-devel
      options: --privileged
      volumes:
        - /sys/fs/cgroup:/sys/fs/cgroup

    steps:
      - name: Adding build user and configure system
        run: |
          sed -E -i "s|#? ?MAKEFLAGS=.*|MAKEFLAGS=-j$(nproc)|" /etc/makepkg.conf
          useradd builduser --create-home --password ""
          printf 'builduser ALL=(ALL) ALL\n' >> /etc/sudoers
          pacman -Syu --noconfirm --noprogressbar --needed git
          sudo -u builduser git config --global safe.directory '*'

      - name: Clone the openblack repo
        uses: actions/checkout@v4
        with:
          path: src

      - name: Clone the openblack AUR files
        run: |
          git clone https://aur.archlinux.org/openblack-git.git ${GITHUB_WORKSPACE}/openblack-git
          chown -R builduser ${GITHUB_WORKSPACE}/openblack-git

      - name: Install non-AUR dependencies
        shell: bash
        run: |
          source ${GITHUB_WORKSPACE}/openblack-git/PKGBUILD
          export makedepends=${makedepends[@]}
          export depends=${depends[@]}
          export makedepends=${makedepends/bgfx-cmake-git/}
          export makedepends=${makedepends/dr_libs-git/}
          export depends=${depends/bgfx/}
          export depends=${depends/entt/}
          export depends=${depends/imgui/}
          pacman -S --noconfirm --noprogressbar --needed mesa libx11 ${makedepends} ${depends}

      - name: Cache AUR Packages
        id: cache-packages
        uses: actions/cache@v4
        with:
          path: |
            entt/entt*.pkg.tar.*
            bgfx-cmake-git/bgfx-cmake-git*.pkg.tar.*
            imgui/imgui*.pkg.tar.*
            dr_libs-git/dr_libs-git*.pkg.tar.*
          key: ${{ runner.os }}-cmake

      - name: Build ${{ env.PKG }} from the AUR
        if: steps.cache-packages.outputs.cache-hit != 'true'
        run: |
          git clone https://aur.archlinux.org/${{ env.PKG }}.git ${GITHUB_WORKSPACE}/${{ env.PKG }}
          chown -R builduser ${GITHUB_WORKSPACE}/${{ env.PKG }}
          cd ${GITHUB_WORKSPACE}/${{ env.PKG }}
          sudo -u builduser makepkg
        env:
          PKG: entt

      - name: Build ${{ env.PKG }} from the AUR
        if: steps.cache-packages.outputs.cache-hit != 'true'
        run: |
          git clone https://aur.archlinux.org/${{ env.PKG }}.git ${GITHUB_WORKSPACE}/${{ env.PKG }}
          chown -R builduser ${GITHUB_WORKSPACE}/${{ env.PKG }}
          cd ${GITHUB_WORKSPACE}/${{ env.PKG }}
          sudo -u builduser makepkg
        env:
          PKG: bgfx-cmake-git

      - name: Build ${{ env.PKG }} from the AUR
        if: steps.cache-packages.outputs.cache-hit != 'true'
        run: |
          git clone https://aur.archlinux.org/${{ env.PKG }}.git ${GITHUB_WORKSPACE}/${{ env.PKG }}
          chown -R builduser ${GITHUB_WORKSPACE}/${{ env.PKG }}
          cd ${GITHUB_WORKSPACE}/${{ env.PKG }}
          sudo -u builduser makepkg
        env:
          PKG: imgui

      - name: Build ${{ env.PKG }} from the AUR
        if: steps.cache-packages.outputs.cache-hit != 'true'
        run: |
          git clone https://aur.archlinux.org/${{ env.PKG }}.git ${GITHUB_WORKSPACE}/${{ env.PKG }}
          chown -R builduser ${GITHUB_WORKSPACE}/${{ env.PKG }}
          cd ${GITHUB_WORKSPACE}/${{ env.PKG }}
          sudo -u builduser makepkg
        env:
          PKG: dr_libs-git

      - name: Install AUR dependencies
        run: pacman -U --noconfirm --noprogressbar ${GITHUB_WORKSPACE}/*/*.pkg.tar.*

      - name: Building and Packaging openblack from the AUR
        run: |
          cd ${GITHUB_WORKSPACE}/openblack-git
          sed "s,source=.*,source=('openblack::git+file://${GITHUB_WORKSPACE}/src')," -i PKGBUILD
          sudo -u builduser makepkg

      - run: echo "GITHUB_REF_NAME_DASHES=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV
        shell: bash

      - uses: actions/upload-artifact@v4
        with:
          name: openblack-arch-linux-pkg-${{ env.GITHUB_REF_NAME_DASHES }}
          path: openblack-git/openblack-git*.pkg.tar.*
          if-no-files-found: error

  flatpak:
    name: Package for flatpak
    runs-on: ubuntu-latest
    container:
      image: bilelmoussaoui/flatpak-github-actions:freedesktop-23.08
      options: --privileged
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Only build release
        run: |
          for file in vcpkg/triplets/*.cmake vcpkg/triplets/community/*.cmake; do
            echo "set(VCPKG_BUILD_TYPE release)" >> "$file"
          done
      - uses: flatpak/flatpak-github-actions/flatpak-builder@master
        with:
          bundle: openblack-${{github.sha}}.flatpak
          manifest-path: assets/resource/package/flatpak/org.openblack.openblack.yaml
          cache-key: flatpak-builder-${{ github.sha }}
          upload-artifact: false
      - run: echo "GITHUB_REF_NAME_DASHES=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV
        shell: bash
      - uses: actions/upload-artifact@v4
        with:
          name: openblack-${{env.GITHUB_REF_NAME_DASHES}}.flatpak
          path: openblack-${{github.sha}}.flatpak
          if-no-files-found: error

  appimage:
    name: Package for appimage
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Only build release
        run: |
          for file in vcpkg/triplets/*.cmake vcpkg/triplets/community/*.cmake; do
            echo "set(VCPKG_BUILD_TYPE release)" >> "$file"
          done
      - name: Install Linux dependencies
        run: |
          sudo apt update -y
          sudo apt install -y libgl1-mesa-dev libglu1-mesa-dev libltdl-dev libxext-dev
      - name: Export GitHub Actions cache environment variables
        uses: actions/github-script@v7
        with:
          script: |
            core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
            core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
      - name: Run script
        run: assets/resource/package/appimage/package-appimage.sh $GITHUB_REF_NAME
        env:
          VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
      - run: echo "GITHUB_REF_NAME_DASHES=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV
        shell: bash
      - uses: actions/upload-artifact@v4
        with:
          name: openblack-${{ env.GITHUB_REF_NAME_DASHES }}.AppImage
          path: openblack-*.AppImage
          if-no-files-found: error

  snapcraft:
    name: Package for snap
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Move snap config to project root
        run: cp -r assets/resource/package/snap .
      - uses: snapcore/action-build@v1
        id: snap
      - run: echo "GITHUB_REF_NAME_DASHES=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV
        shell: bash
      - uses: actions/upload-artifact@v4
        with:
          name: openblack-${{ env.GITHUB_REF_NAME_DASHES }}.snap
          path: ${{ steps.snap.outputs.snap }}
