---
name: Build - Linux AppImage
permissions:
  contents: read

on:
  workflow_call:
    inputs:
      ci_version:
        required: true
        type: string

jobs:
  build:
    env:
      CI_VERSION: ${{ inputs.ci_version }}
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v5
        with:
          submodules: 'recursive'
          fetch-depth: 1

      - name: Setup environment
        run: |
          wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add -
          sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-1.4.313-jammy.list https://packages.lunarg.com/vulkan/1.4.313/lunarg-vulkan-1.4.313-jammy.list
          sudo apt update
          sudo apt install -y qt6-base-dev qt6-declarative-dev libqt6svg6-dev qml6-module-qtquick-controls qml6-module-qtquick-templates qml6-module-qtquick-layouts \
            qml6-module-qtqml-workerscript qml6-module-qtquick-window qml6-module-qtquick python3-pip nasm libgbm-dev libdrm-dev libfreetype-dev libasound2-dev \
            libdbus-1-dev libegl1-mesa-dev libgl1-mesa-dev libgles2-mesa-dev libglu1-mesa-dev libibus-1.0-dev libpulse-dev libudev-dev libx11-dev libxcursor-dev \
            libxext-dev libxi-dev libxinerama-dev libxkbcommon-dev libxrandr-dev libxss-dev libxt-dev libxv-dev libxxf86vm-dev libxcb-dri3-dev libx11-xcb-dev \
            libxfixes-dev libxtst-dev wayland-protocols libopus-dev libvdpau-dev libgl-dev libpipewire-0.3-dev liburing-dev vulkan-sdk cmake
          sudo pip3 install meson
          mkdir -p dep_root/{bin,include,lib}
          echo "DEP_ROOT=$PWD/dep_root" >> "${GITHUB_ENV}"
          echo "$PWD/dep_root/bin" >> "${GITHUB_PATH}"

      - name: Checkout SDL3
        uses: actions/checkout@v5
        with:
          repository: libsdl-org/SDL
          ref: release-3.4.4
          path: deps/SDL

      - name: Build SDL3
        working-directory: deps/SDL
        run: |
          cmake -DSDL_KMSDRM=OFF -DSDL_TEST_LIBRARY=OFF -DSDL_INSTALL_DOCS=OFF -S . -B build
          cmake --build build -j
          sudo cmake --install build

      - name: Checkout sdl2-compat
        uses: actions/checkout@v5
        with:
          repository: libsdl-org/sdl2-compat
          ref: release-2.32.66
          path: deps/sdl2-compat

      - name: Build sdl2-compat
        working-directory: deps/sdl2-compat
        run: |
          cmake -DSDL2COMPAT_TESTS=OFF -S . -B build
          cmake --build build -j
          sudo cmake --install build

      - name: Checkout SDL_ttf
        uses: actions/checkout@v5
        with:
          repository: libsdl-org/SDL_ttf
          ref: release-2.22.0
          path: deps/SDL_ttf
          submodules: 'recursive'

      - name: Build SDL_ttf
        working-directory: deps/SDL_ttf
        run: |
          ./autogen.sh
          ./configure
          make -j$(nproc)
          sudo make install

      - name: Checkout libva
        uses: actions/checkout@v5
        with:
          repository: intel/libva
          ref: 2.23.0
          path: deps/libva

      - name: Build libva
        working-directory: deps/libva
        run: |
          ./autogen.sh
          ./configure --enable-x11
          make -j$(nproc)
          sudo make install

      - name: Checkout libplacebo
        uses: actions/checkout@v5
        with:
          repository: haasn/libplacebo
          ref: v7.360.1
          path: deps/libplacebo
          submodules: 'recursive'

      - name: Build libplacebo
        working-directory: deps/libplacebo
        run: |
          meson setup build -Dvulkan=enabled -Dopengl=disabled -Ddemos=false
          ninja -C build
          sudo ninja install -C build

      - name: Build dav1d
        working-directory: deps
        env:
          DAV1D_VER: 1.5.3
        run: |
          git clone --branch $DAV1D_VER --depth 1 https://code.videolan.org/videolan/dav1d.git
          pushd dav1d
          meson setup build -Ddefault_library=static -Dbuildtype=release -Denable_tools=false -Denable_tests=false
          ninja -C build
          sudo ninja install -C build
          popd

      - name: Checkout FFmpeg
        uses: actions/checkout@v5
        with:
          repository: FFmpeg/FFmpeg
          ref: n8.1
          path: deps/FFmpeg

      - name: Build FFmpeg
        working-directory: deps/FFmpeg
        run: |
          ./configure --enable-pic --disable-static --enable-shared --disable-all --disable-autodetect --enable-avcodec --enable-avformat --enable-swscale \
            --enable-decoder=h264 --enable-decoder=hevc --enable-decoder=av1 \
            --enable-vaapi --enable-hwaccel=h264_vaapi --enable-hwaccel=hevc_vaapi --enable-hwaccel=av1_vaapi \
            --enable-vdpau --enable-hwaccel=h264_vdpau  --enable-hwaccel=hevc_vdpau --enable-hwaccel=av1_vdpau \
            --enable-libdrm --enable-vulkan --enable-hwaccel=h264_vulkan --enable-hwaccel=hevc_vulkan --enable-hwaccel=av1_vulkan \
            --enable-libdav1d --enable-decoder=libdav1d
          make -j$(nproc)
          sudo make install

      - name: Install linuxdeployqt
        working-directory: dep_root/bin
        run: |
          wget -O linuxdeployqt https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage
          chmod a+x linuxdeployqt

      - name: Build Binaries
        run: |
          sudo ldconfig
          scripts/build-appimage.sh

      - name: Upload Binaries
        uses: actions/upload-artifact@v5
        with:
          name: Moonlight-LinuxAppImage-${{ env.CI_VERSION }}
          path: build/installer-release/Moonlight-${{ env.CI_VERSION }}-x86_64.AppImage
          compression-level: 0
          if-no-files-found: error
