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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"

# json-c13 is not a building dep of wlroots but is for sway I load it first here
# to reduce sway system file fixes

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

# hwdata /usr/local/share/pkgconfig/hwdata.pc updated to /usr/local/lib/pkgconfig pathway

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://gitlab.freedesktop.org/$P/$P/-/archive/$V/$SRC.zip" $USER
unzip $SRC*zip
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
meson setup --prefix=/usr/local -Ddebug=false -Dexamples=false -Dlibliftoff=disabled -Drenderers=auto \
-Dstrip=true -Dsysconfdir=/usr/local/etc -Dwerror=false -Dxcb-errors=disabled -Dxwayland=enabled


meson configure > /tmp/configure
# drm-backend      : YES
# x11-backend       : YES
# libinput-backend : YES
# xwayland             : YES
# gles2-renderer   : YES
# vulkan-renderer  : YES
# gbm-allocator     : YES
# session                : YES
# color-management: YES
# xcb-errors               : NO
# egl                          : YES
# libliftoff                    : NO

#    User defined options
# debug       : false
# examples  : false
# libliftoff      : disabled
# 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

# main
######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P/

# 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-dev.tcz 
Description:    dev files
Version:        0.18.2
Author:         Drew DeVault
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           112K
Extension_by:   aus9 
Tags:           wayland sway labwc
Comments:       -

Change-log:     2019/05/20 v 0.5.0 on 10x 
                2020/06/26 v 0.10.1 on 11x 
                2023/04/19 v 0.16.2 on 14x 
                2024/01/04 v 0.17.1 on 15x (GNUser)
Current:        2025/03/30 v 0.18.2 on 16x (aus9)' > $P-dev.tcz.info

# json-c13 moved to bottom of dep as load order is bot to top

echo 'wlroots.tcz
glslang.tcz 
hwdata.tcz  
libdisplay-info-dev.tcz
libinput-dev.tcz  
libvulkan-dev.tcz
seatd-dev.tcz 
wayland-protocols-dev.tcz 
xwayland.tcz 
json-c13-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          wlroots.tcz 
Description:    Wayland compositor library
Version:        0.18.2
Author:         Drew DeVault
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           452K
Extension_by:   aus9 
Tags:           wayland sway labwc
Comments:       -

Change-log:     2019/05/20 v 0.5.0 on 10x 
                2020/06/26 v 0.10.1 on 11x 
                2023/04/19 v 0.16.2 on 14x 
                2024/01/04 v 0.17.1 on 15x (GNUser)
Current:        2025/03/30 v 0.18.2 on 16x, NOTE libwlroots.so is now dropped  (aus9)' > $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
#[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-res.so.0                                        libxcb  xwayland 

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

# submitqc does not understand dep boot order trick for json c13 load before json cs
# submitqc --libs --no-fix  
# rm -rf *.zsync

# submitqc is still changing dev info back to 15x....for reasons I know not...so not used now
