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

CFLAGS="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti" 

# 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.5.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="json-c13-dev compiletc submitqc meson  \
libEGL-dev libinput-dev  git cmake "
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://github.com/swaywm/$P/archive/$V.zip       " $USER
unzip $V*zip
cd $SRC
mkdir build && cd build
meson --strip --prefix=/usr/local -Ddebug=false --buildtype=plain -Dlogind=disabled 
meson configure 
# to show settings as I could have used 
########################################
# wlroots 0.5.0
# libcap: 1
# systemd: 0
# elogind: 0
# xwayland: 1
# x11_backend: 1
# xcb-icccm: 1
#xcb-errors: 0
###################
ninja 
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

# json-c13 moved to bottom of dep as load order is bot to top
echo 'wlroots.tcz
compiletc.tcz 
submitqc.tcz 
meson.tcz 
libEGL-dev.tcz
libinput-dev.tcz 
git.tcz 
cmake.tcz
json-c13-dev.tcz ' > $P-dev.tcz.dep

echo 'libinput.tcz
libcap.tcz
libxkbcommon.tcz
libGLESv2.tcz
libEGL.tcz
xwayland.tcz' > $P.tcz.dep

echo 'Title:          wlroots.tcz 
Description:    Wayland compositor library
Version:        0.5.0
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/wlroots
Copying-policy: Accompanied 
Size:           280K
Extension_by:   aus9 at gmx dot com
Tags:           wayland sway
Comments:       Used to build/run sway

                Compiled for 10.x 
Change-log:     2019/05/20 first version 
Current:        2019/05/20  ' > $P.tcz.info

echo 'Title:          wlroots-dev.tcz 
Description:    dev files
Version:        0.5.0
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/wlroots
Copying-policy: Accompanied 
Size:           68K	
Extension_by:   aus9 at gmx dot com
Tags:           wayland sway
Comments:       Development files

                Compiled for 10.x 
Change-log:     2019/05/20 first version 
Current:        2019/05/20  ' > $P-dev.tcz.info

submitqc --libs
