#!/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"
export LDFLAGS="-Wl,-O1" 

# 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.10.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson git cmake  \
libEGL-dev libinput-dev json-c13-dev "
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.10.1
# 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

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:           72K	
Extension_by:   aus9 
Tags:           wayland sway
Comments:       Development files

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

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:           260K
Extension_by:   aus9 
Tags:           wayland sway
Comments:       Used to build/run sway

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

# 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

submitqc --libs
