#!/usr/bin/env bash
#
# Helper script to install the non-Python Wayland dependencies on Ubuntu. This
# is required because most of the time the wlroots version we depend on is more
# recent than that which can be found in the Ubuntu package repository. This
# script is used for CI on GitHub and the ReadTheDocs environment.

set -e

# The versions we want
WAYLAND=1.22.0
WAYLAND_PROTOCOLS=1.32
WLROOTS=0.17.3
SEATD=0.6.4
LIBDRM=2.4.121
PIXMAN=0.42.0
XWAYLAND=22.1.9
HWDATA=0.364

# Packaged dependencies
sudo apt update
sudo apt-get install -y --no-install-recommends \
     libepoxy-dev \
     libegl1-mesa-dev \
     libgbm-dev \
     libgles2-mesa-dev \
     libinput-dev \
     libpciaccess-dev \
     libxcb-composite0-dev \
     libxcb-dri3-dev \
     libxcb-ewmh-dev \
     libxcb-icccm4-dev \
     libxcb-image0-dev \
     libxcb-present-dev \
     libxcb-render0-dev \
     libxcb-res0-dev \
     libxcb-xfixes0-dev \
     libxcb-xinput-dev \
     libxcb1-dev \
     libxfont-dev \
     libxkbcommon-dev \
     libxshmfence-dev \
     libtirpc-dev \
     xfonts-utils \
     xserver-xorg-dev \
     ninja-build \
     meson

# Build wayland
wget https://gitlab.freedesktop.org/wayland/wayland/-/releases/$WAYLAND/downloads/wayland-$WAYLAND.tar.xz
tar -xJf wayland-$WAYLAND.tar.xz
cd wayland-$WAYLAND
meson build -Ddocumentation=false --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build wayland-protocols
wget https://gitlab.freedesktop.org/wayland/wayland-protocols/-/releases/$WAYLAND_PROTOCOLS/downloads/wayland-protocols-$WAYLAND_PROTOCOLS.tar.xz
tar -xJf wayland-protocols-$WAYLAND_PROTOCOLS.tar.xz
cd wayland-protocols-$WAYLAND_PROTOCOLS
meson build -Dtests=false --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build libdrm
wget https://gitlab.freedesktop.org/mesa/drm/-/archive/libdrm-$LIBDRM/drm-libdrm-$LIBDRM.tar.gz
tar -xzf drm-libdrm-$LIBDRM.tar.gz
cd drm-libdrm-$LIBDRM
meson build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build seatd
wget https://github.com/kennylevinsen/seatd/archive/refs/tags/$SEATD.tar.gz
tar -xzf $SEATD.tar.gz
cd seatd-$SEATD
meson build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build pixman
wget https://gitlab.freedesktop.org/pixman/pixman/-/archive/pixman-$PIXMAN/pixman-pixman-$PIXMAN.tar.gz
tar -xzf pixman-pixman-$PIXMAN.tar.gz
cd pixman-pixman-$PIXMAN
meson build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build hwdata
wget https://github.com/vcrhonek/hwdata/archive/refs/tags/v$HWDATA.tar.gz
tar -xzf v$HWDATA.tar.gz
cd hwdata-$HWDATA
./configure --prefix=/usr --libdir=/lib --datadir=/usr/share
make
sudo make install
cd ../

# Build xwayland
wget https://gitlab.freedesktop.org/xorg/xserver/-/archive/xwayland-$XWAYLAND/xserver-xwayland-$XWAYLAND.tar.gz
tar -xzf xserver-xwayland-$XWAYLAND.tar.gz
cd xserver-xwayland-$XWAYLAND
meson build --prefix=/usr
ninja -C build
sudo ninja -C build install
cd ../

# Build wlroots
wget https://gitlab.freedesktop.org/wlroots/wlroots/-/archive/$WLROOTS/wlroots-$WLROOTS.tar.gz
tar -xzf wlroots-$WLROOTS.tar.gz
cd wlroots-$WLROOTS
meson build -Dexamples=false --prefix=/usr -Dxwayland=enabled
ninja -C build
sudo ninja -C build install
cd ../
