---
app-id: dev.lizardbyte.sunshine
runtime: org.freedesktop.Platform
runtime-version: "22.08"
sdk: org.freedesktop.Sdk
sdk-extensions:
  - org.freedesktop.Sdk.Extension.node18
command: sunshine
separate-locales: false
finish-args:
  - --device=all  # access all devices
  - --env=PULSE_PROP_media.category=Manager  # allow sunshine to manage audio sinks
  - --filesystem=home  # need to save files in user's home directory
  - --share=ipc  # required for X11 shared memory extension
  - --share=network  # access network
  - --socket=pulseaudio  # play sounds using pulseaudio
  - --socket=wayland  # show windows using Wayland
  - --socket=x11  # show windows using X11
  - --system-talk-name=org.freedesktop.Avahi  # talk to avahi on the system bus
  - --talk-name=org.freedesktop.Flatpak  # talk to flatpak on the session bus

cleanup:
  - /include
  - /lib/cmake
  - /lib/pkgconfig
  - /lib/*.la
  - /lib/*.a
  - /share/man

modules:
  - name: boost
    disabled: false
    buildsystem: simple
    build-commands:
      - cd tools/build && bison -y -d -o src/engine/jamgram.cpp src/engine/jamgram.y
      - ./bootstrap.sh --prefix=$FLATPAK_DEST --with-libraries=locale,log,program_options,system,thread
      - ./b2 install variant=release link=shared runtime-link=shared cxxflags="$CXXFLAGS" linkflags="$LDFLAGS"
        -j $FLATPAK_BUILDER_N_JOBS
    sources:
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/b/boost1.74/boost1.74_1.74.0.orig.tar.xz
        sha256: 2467be4af625b5ae4b3c93fc7af196a09eba39c11a7338cd9e8b356fa44d2f45
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/b/boost1.74/boost1.74_1.74.0-17ubuntu1.debian.tar.xz
        sha256: 22e623d98c84eb3fec57e19ea371157a5bc8225ba4c5907f7e5155072317a31d
      - type: shell
        commands:
          - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done

  - name: avahi
    disabled: false
    cleanup:
      - /bin
      - /lib/avahi
      - /share
    config-opts:
      - --with-distro=none
      - --disable-gobject
      - --disable-introspection
      - --disable-qt3
      - --disable-qt4
      - --disable-qt5
      - --disable-gtk
      - --disable-core-docs
      - --disable-manpages
      - --disable-libdaemon
      - --disable-python
      - --disable-pygobject
      - --disable-mono
      - --disable-monodoc
      - --disable-autoipd
      - --disable-doxygen-doc
      - --disable-doxygen-dot
      - --disable-doxygen-xml
      - --disable-doxygen-html
      - --disable-manpages
      - --disable-xmltoman
      - --disable-libevent
    sources:
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/a/avahi/avahi_0.8.orig.tar.gz
        sha256: 060309d7a333d38d951bc27598c677af1796934dbd98e1024e7ad8de798fedda
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/a/avahi/avahi_0.8-6ubuntu1.debian.tar.xz
        sha256: ebf1dfe5e853b6bc6843e3bd784cb6af632041f305abd0e5415114f80c1dcea4
      - type: shell
        commands:
          - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done
          - autoreconf -ivf

  - name: libevdev
    disabled: false
    buildsystem: meson
    config-opts:
      - -Ddocumentation=disabled
      - -Dtests=disabled
    cleanup:
      - /bin
    sources:
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/libe/libevdev/libevdev_1.13.0+dfsg.orig.tar.xz
        sha256: a882e13ef1dd6bd227318080cabf60fe5af3c06471259d3acfc9dbfb202351a7
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/libe/libevdev/libevdev_1.13.0+dfsg-1.debian.tar.xz
        sha256: d33c56acbbfff2dc540e45c57a38d92210b5e7fd0947ac47fbe48183468aad74
      - type: shell
        commands:
          - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done

  - name: intel-mediasdk
    disabled: false
    buildsystem: cmake
    config-opts:
      - -DENABLE_OPENCL=ON
      - -DENABLE_X11_DRI3=ON
      - -DENABLE_WAYLAND=ON
      - -DENABLE_ITT=OFF
      - -DENABLE_TEXTLOG=OFF
      - -DENABLE_STAT=OFF
      - -DBUILD_ALL=OFF
      - -DBUILD_RUNTIME=ON
      - -DBUILD_SAMPLES=OFF
      - -DBUILD_TESTS=OFF
      - -DBUILD_TOOLS=OFF
      - -DUSE_SYSTEM_GTEST=OFF
      - -DMFX_ENABLE_KERNELS=ON
    only-arches:
      - x86_64
    sources:
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/universe/i/intel-mediasdk/intel-mediasdk_22.3.0.orig.tar.gz
        sha256: e1e74229f409e969b70c2b35b1955068de3d40db85ecc42bd6ff501468bc76d7
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/universe/i/intel-mediasdk/intel-mediasdk_22.3.0-1.debian.tar.xz
        sha256: 024d98d2f63443d2765a90cfe997d104e7b897694889f199ca8fb4d9ffdcf1dc
      - type: shell
        commands:
          - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done
    modules:
      - name: libdrm
        disabled: false
        buildsystem: meson
        sources:
          - type: archive
            url: http://archive.ubuntu.com/ubuntu/pool/main/libd/libdrm/libdrm_2.4.110.orig.tar.xz
            sha256: eecee4c4b47ed6d6ce1a9be3d6d92102548ea35e442282216d47d05293cf9737
          - type: archive
            url: http://archive.ubuntu.com/ubuntu/pool/main/libd/libdrm/libdrm_2.4.110-1ubuntu1.debian.tar.xz
            sha256: 464b9553861f39beddfaee6b8924734b02a0febfae3968e4ca1360f2972bba8b
          - type: shell
            commands:
              - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done

  - name: numactl
    buildsystem: autotools
    make-args:
      - install
    sources:
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/n/numactl/numactl_2.0.14.orig.tar.gz
        sha256: 1ee27abd07ff6ba140aaf9bc6379b37825e54496e01d6f7343330cf1a4487035
      - type: archive
        url: http://archive.ubuntu.com/ubuntu/pool/main/n/numactl/numactl_2.0.14-3ubuntu2.debian.tar.xz
        sha256: 49089e5be5367f6367f8b0389d1d523944432607783b53f0605705792e1015ee
      - type: shell
        commands:
          - for n in $(cat patches/series); do if [[ $n != "#"* ]]; then patch -Np1 -i "patches/$n" -d .; fi; done
    cleanup:
      - "/bin"

  # Caching is configured until here, not including CUDA
  - name: cuda
    disabled: false
    buildsystem: simple
    only-arches:
      - x86_64
      - aarch64
    cleanup:
      - "*"
    build-commands:
      - chmod u+x ./cuda.run
      - ./cuda.run --silent --toolkit --toolkitpath=$FLATPAK_DEST/cuda --no-opengl-libs --no-man-page --no-drm
        --tmpdir=$FLATPAK_BUILDER_BUILDDIR
      - rm -r $FLATPAK_DEST/cuda/nsight-systems-*
      - rm ./cuda.run
    sources:
      - type: file
        only-arches:
          - x86_64
        url: https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux.run
        sha256: 905e9b9516900839fb76064719db752439f38b8cb730b49335d8bd53ddfad392
        dest-filename: cuda.run
      - type: file
        only-arches:
          - aarch64
        url: https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux_sbsa.run  # yamllint disable-line rule:line-length
        sha256: cd13e9c65d4c8f895a968706f46064d536be09f9706bce081cc864b7e4fa4544
        dest-filename: cuda.run

  - name: sunshine
    disabled: false
    buildsystem: cmake
    no-make-install: false
    builddir: true
    build-options:
      append-path: /usr/lib/sdk/node18/bin
      build-args:
        - --share=network
      cxxflags: -I${C_INCLUDE_PATH}/libevdev-1.0
      env:
        npm_config_nodedir: /usr/lib/sdk/node18
        NPM_CONFIG_LOGLEVEL: info
    build-commands:
      # Install npm dependencies
      - cd ${FLATPAK_BUILDER_BUILDDIR} && npm install
    config-opts:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/app
      - -DCMAKE_CUDA_COMPILER=/app/cuda/bin/nvcc
      - -DSUNSHINE_ASSETS_DIR=share/sunshine
      - -DSUNSHINE_EXECUTABLE_PATH=/app/bin/sunshine
      - -DSUNSHINE_ENABLE_WAYLAND=ON
      - -DSUNSHINE_ENABLE_X11=ON
      - -DSUNSHINE_ENABLE_DRM=ON
      - -DSUNSHINE_ENABLE_CUDA=ON
      - -DSUNSHINE_CONFIGURE_FLATPAK=ON
    sources:
      - type: git
        url: "@GITHUB_CLONE_URL@"
        branch: "@GITHUB_BRANCH@"
        commit: "@GITHUB_COMMIT@"
    post-install:
      # use `sed` to update apps.json with prefixes required for flatpak
      # -r (regex)
      # -z (handle new lines) https://linuxhint.com/sed-replace-newline-with-space
      # `/gm` global and multiline
      - sed -r -z -i -e
        's/("((do)|(undo)|(cmd)|(detached))"\s*:\s*\[?\n*\s*")(.*")/\1flatpak-spawn --host \7/gm'
        /app/share/sunshine/apps.json
      - sed -i
        's%/app/bin/sunshine%flatpak run dev.lizardbyte.sunshine\nExecStop=flatpak kill dev.lizardbyte.sunshine%g'
        /app/share/sunshine/systemd/user/sunshine.service
      - install -D $FLATPAK_BUILDER_BUILDDIR/packaging/linux/flatpak/scripts/* /app/bin
