#!/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"

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

# libinput >=1.14.0 have 1.19.3.... libseat >=0.2.0 have 0.7.0...pixman-1 have
# gles2 render dep egl and gbm ..glslang >=11.0.0 have 11.13.0...build seatd, hwdata 
# vulkan >=1.2.182 have 1.2.200
# but /render/vulkan/util.c:23:25: error: 'VK_PIPELINE_COMPILE_REQUIRED'
# so Juanito and graciously updated libvulkan

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

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
# 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
 
meson configure 
# drm-backend     : YES
# x11-backend     : YES
# libinput-backend: YES
# xwayland        : YES
# gles2-renderer  : YES
# vulkan-renderer : YES
# gbm-allocator   : YES
# xcb-errors      : NO
# egl             : YES
# User defined options
# buildtype       : plain
# debug           : false
# prefix          : /usr/local
# strip           : true
# sysconfdir      : /usr/local/etc
# werror          : false
# examples        : false
# renderers       : auto
# xcb-errors      : disabled
# xwayland        : enabled

ninja # 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 usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          wlroots-dev.tcz 
Description:    dev files
Version:        0.16.2
Author:         Drew DeVault
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           96K	
Extension_by:   aus9 
Tags:           wayland sway
Comments:       Development files

Change-log:     2019/05/20 Original 0.5.0 on 10x 
                2020/06/26 v 0.10.1 on 11x 
Current:        2023/04/19 v 0.16.2 on 14x ' > $P-dev.tcz.info

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

echo 'wlroots.tcz
libEGL-dev.tcz
libinput-dev.tcz  
libvulkan-dev.tcz
seatd-dev.tcz 
libdrm-dev.tcz 
xwayland.tcz 
hwdata-dev.tcz  
glslang.tcz 
json-c13-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          wlroots.tcz 
Description:    Wayland compositor library
Version:        0.16.2
Author:         Drew DeVault
Original-site:  https://gitlab.freedesktop.org/wlroots/wlroots
Copying-policy: Accompanied 
Size:           400K
Extension_by:   aus9 
Tags:           wayland sway
Comments:       Used to build/run sway
                Can be used to build other wayland
                desktops

Change-log:     2019/05/20 Original 0.5.0 on 10x 
                2020/06/26 v 0.10.1 on 11x 
Current:        2023/04/19 v 0.16.2 on 14x    ' > $P.tcz.info

readelf -d $P/usr/local/lib/lib$P.so | grep 'NEEDED'
#[libwayland-server.so.0]   wayland -> libEGL -> xwayland
#[libdrm.so.2]            libdrm
#[libgbm.so.1]  libEGL -> xwayland
#[libxkbcommon.so.0]   libxkbcommon
#[libudev.so.0] udev-lib libdrm
#[libpixman-1.so.0]pixman -> xwayland
#[libm.so.6]      TCB
#[libEGL.so.1]libEGL -> xwayland
#[libgbm.so.1]  "           "
#[libGLESv2.so.2]libGLESv2
#[libvulkan.so.1]libvulkan
#[libinput.so.10]libinput
#[libxcb.so.1]libxcb -> libxkbcommon
#[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]      "
#[libwayland-client.so.0]wayland -> xwayland
#[libseat.so.1]           seatd
#[libxcb-composite.so.0]libxcb -> libxkbcommon
#[libxcb-icccm.so.4]               "
#[libxcb-res.so.0]                  "
#[libc.so.6]            TCB

echo 'xwayland.tcz
libdrm.tcz
libxkbcommon.tcz
udev-lib.tcz
libGLESv2.tcz
libvulkan.tcz
libinput.tcz
seatd.tcz ' > $P.tcz.dep

submitqc --libs