#!/bin/bash
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P1=wlroots
P=$P1-xwayland
V=0.19.2
SRC=$P1-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson cmake glslang hwdata libdisplay-info-dev \
libinput-dev libvulkan-dev seatd-dev xwayland  " 
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://gitlab.freedesktop.org/$P1/$P1/-/releases/$V/downloads/$P1-$V.tar.gz
tar xvf $P1*gz
cd $SRC
# fix header to allow ninja to complete compile
sed -i '5a#include <sys/sysmacros.h>' render/vulkan/vulkan.c

mkdir build && cd build
# used -Dauto_features=enabled initially
meson setup --prefix=/usr/local -Dbuildtype=plain -Ddebug=false -Dstrip=true \
-Dsysconfdir=/usr/local/etc -Dexamples=false -Dxcb-errors=disabled -Dxwayland=enabled \
-Drenderers=auto -Dwerror=false
  
# drm-backend     : YES
# x11-backend     : YES
# libinput-backend: YES
# xwayland        : YES
# gles2-renderer  : YES
# vulkan-renderer : YES
# gbm-allocator   : YES
#udmabuf-allocator: YES
# session         : YES
#color-management : YES
#xcb-errors       : NO
#egl              : YES
#libliftoff       : NO

#        User defined options
# buildtype       : plain
# debug           : false
# examples        : false
# prefix          : /usr/local
# renderers       : auto
# strip           : true
# sysconfdir      : /usr/local/etc
# werror          : false
# xcb-errors      : disabled
# xwayland        : enabled

ninja # 6 seconds
DESTDIR=/tmp/$P ninja install
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib
cp $P-dev/usr/local/lib/pkgconfig/wlroot-0.19.pc $P-dev/usr/local/lib/pkgconfig/wlroot.pc 

# main
######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P/
cd $P/usr/local/lib/
ln -s libwlroots-0.19.so libwlroots.so
cd /tmp

# TCZ them
#######
LIST="$P $P-dev "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          wlroots-xwayland-dev.tcz 
Description:    dev files
Version:        0.19.2
Author:         The wlroots contributors
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           124K	
Extension_by:   aus9 
Tags:           wayland sway wlroots
Comments:       May conflict with wlroots
                This TCE has full xwayland support  

Change-log:     2026/01/12 v 0.19.2 on 17x
Current:        2026/01/12  ' > $P-dev.tcz.info
echo 'wlroots-xwayland.tcz
glslang.tcz 
hwdata.tcz 
libdisplay-info-dev.tcz 
libinput-dev.tcz 
libvulkan-dev.tcz 
seatd-dev.tcz 
xwayland.tcz    ' > $P-dev.tcz.dep

echo 'Title:          wlroots-xwayland.tcz 
Description:    Wayland compositor lib as below
Version:        0.19.2
Author:         The wlroots contributors
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           496K
Extension_by:   aus9 
Tags:           wayland sway
Comments:       May conflict with wlroots
                This TCE has full xwayland support                

Change-log:     2026/01/12 v 0.19.2 on 17x
Current:        2026/01/12    ' > $P.tcz.info

readelf -d $P/usr/local/lib/*.so | grep 'NEEDED'
#[libwayland-server.so.0]libwayland-client.so.0]wayland xwayland
#[libdrm.so.2]libdrm xwayland
#[libxkbcommon.so.0]libxkbcommon
#[libpixman-1.so.0]pixman xwayland
#[libEGL.so.1]libEGL xwayland
#[libgbm.so.1]libEGL xwayland
#[libGLESv2.so.2]libGLESv2
#[libvulkan.so.1]libvulkan
#liblcms2.so.2 liblcms2
#[libudev.so.0] udev-lib xwayland
#[libseat.so.1] seatd
#libdisplay-info.so.1 libdisplay-info
#[libinput.so.10]libinput
#[libxcb.so.1][libxcb-dri3.so.0 libxcb-present.so.0]  +  
#[libxcb-render.so.0][libxcb-render-util.so.0][libxcb-shm.so.0] +        
#[libxcb-xfixes.so.0][libxcb-xinput.so.0]libxcb-composite.so.0  +
#[libxcb-icccm.so.4]libxcb-ewmh.so.2 libxcb-res.so.0      libxcb  xwayland

echo 'xwayland.tcz
libxkbcommon.tcz
libGLESv2.tcz
libvulkan.tcz
liblcms2.tcz
seatd.tcz 
libdisplay-info.tcz
libinput.tcz  ' > $P.tcz.dep

# no submit it uses ldd and wants too many deps
# sway builds and runs without borks
