name: RMG

on: [push, pull_request]

jobs:
  build-linux:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        features: [ ON, OFF ]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: linux-features-${{ matrix.features }}
      - name: Install Packages
        run: |
          sudo apt-get -y update
          sudo apt-get -y upgrade
          sudo apt-get -y install cmake libhidapi-dev libsamplerate0-dev libspeex-dev libminizip-dev libsdl2-dev libsdl2-net-dev libfreetype6-dev \
            libgl1-mesa-dev libglu1-mesa-dev pkg-config zlib1g-dev binutils-dev libspeexdsp-dev qt6-base-dev libqt6svg6-dev libqt6websockets6-dev libvulkan-dev \
            build-essential nasm git zip appstream xvfb qt6ct mesa-vulkan-drivers
      - name: Prepare Environment
        run: |
          echo "GIT_REVISION=$(git describe --tags --always)" >> $GITHUB_ENV
      - name: Build RMG (AppImage)
        run: |
          export src_dir="$(pwd)"
          export build_dir="$(pwd)/Build/AppImage"
          export bin_dir="$(pwd)/Bin/AppImage"
    
          mkdir $build_dir Bin/ -p
          cmake -S "$src_dir" -B "$build_dir" -DCMAKE_BUILD_TYPE="Release" \
                -DDISCORD_RPC=${{ matrix.features }} \
                -DDRAG_DROP=${{ matrix.features }} \
                -DNETPLAY=${{ matrix.features }} \
                -DVRU=${{ matrix.features }} \
                -DUSE_ANGRYLION=${{ matrix.features }} \
                -DUPDATER=${{ matrix.features }} \
                -DAPPIMAGE_UPDATER=${{ matrix.features }} \
                -DPORTABLE_INSTALL=OFF \
                -DCMAKE_INSTALL_PREFIX="/usr" \
                -DCMAKE_INSTALL_LIBDIR="lib"  \
                -G "Unix Makefiles"

          cmake --build "$build_dir" --parallel "$(nproc)"
          cmake --install "$build_dir" --strip --prefix="$bin_dir/usr"
        shell: bash
      - name: Create AppImage
        if: ${{ matrix.features == 'ON' }}
        run: |
          ./Package/AppImage/Create.sh
        shell: bash
      - name: Upload RMG (AppImage)
        if: ${{ matrix.features == 'ON' }}
        uses: actions/upload-artifact@v4
        with:
          name: RMG-Portable-Linux64-${{ env.GIT_REVISION }}
          path: Bin/*.AppImage

  build-windows:
    runs-on: windows-2019
    strategy:
      matrix:
        features: [ ON, OFF ]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: windows-features-${{ matrix.features }}
      - uses: msys2/setup-msys2@v2
        with:
          path-type: inherit
          update: true
          msystem: ucrt64
          install: >-
            make
            mingw-w64-ucrt-x86_64-cmake
            mingw-w64-ucrt-x86_64-gcc
            mingw-w64-ucrt-x86_64-hidapi
            mingw-w64-ucrt-x86_64-freetype
            mingw-w64-ucrt-x86_64-libpng
            mingw-w64-ucrt-x86_64-SDL2
            mingw-w64-ucrt-x86_64-SDL2_net
            mingw-w64-ucrt-x86_64-qt6
            mingw-w64-ucrt-x86_64-hidapi
            mingw-w64-ucrt-x86_64-speexdsp
            mingw-w64-ucrt-x86_64-libsamplerate
            mingw-w64-ucrt-x86_64-nasm
            mingw-w64-ucrt-x86_64-minizip
            mingw-w64-ucrt-x86_64-vulkan-headers
            git
      - name: Prepare Environment
        run: |
          $env:revision = git describe --tags --always
          echo "GIT_REVISION=$env:revision" >> $env:GITHUB_ENV
        shell: pwsh
      - name: Build RMG (Portable)
        run: |
          export src_dir="$(pwd)"
          export build_dir="$(pwd)/Build/Release"

          cmake -S "$src_dir" -B "$build_dir" -DCMAKE_BUILD_TYPE="Release" \
                -DDISCORD_RPC=${{ matrix.features }} \
                -DDRAG_DROP=${{ matrix.features }} \
                -DNETPLAY=${{ matrix.features }} \
                -DVRU=${{ matrix.features }} \
                -DUSE_ANGRYLION=${{ matrix.features }} \
                -DUPDATER=${{ matrix.features }} \
                -DPORTABLE_INSTALL=ON \
                -G "MSYS Makefiles"

          cmake --build "$build_dir" --parallel "$(nproc)"
          cmake --install "$build_dir" --strip --prefix="$src_dir"
          cmake --build "$build_dir" --target=bundle_dependencies
        shell: msys2 {0}
      - name: Create Installer
        if: ${{ matrix.features == 'ON' }}
        run: .\Build\Release\Source\Installer\CreateInstaller.bat
      - name: Configure RMG (Portable)
        if: ${{ matrix.features == 'ON' }}
        run: touch Bin/Release/portable.txt
        shell: msys2 {0}
      - name: Upload RMG (Portable)
        if: ${{ matrix.features == 'ON' }}
        uses: actions/upload-artifact@v4
        with:
          name: RMG-Portable-Windows64-${{ env.GIT_REVISION }}
          path: Bin/Release/*
      - name: Upload RMG (Installer)
        if: ${{ matrix.features == 'ON' }}
        uses: actions/upload-artifact@v4
        with:
          name: RMG-Setup-Windows64-${{ env.GIT_REVISION }}
          path: Bin/*.exe
