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

export CFLAGS='-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72   '
export CXXFLAGS='-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72  '
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS

P=glslang
V=13.0.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget cmake python3.11 "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
rm -rf imlib2/loaders/*.la
rm -rf imlib2/filters/*.la
rm -rf gdk-pixbuf-2.0/2.10.0/loaders/*.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/liblto_plugin.la
rm -rf pulseaudio/libpulsedsp.la
rm -rf pulseaudio/libpulsecore-13.0.la
rm -rf pulseaudio/libpulsecommon-13.0.la
rm -rf libv4l/v4l2convert.la
rm -rf libv4l/v4l1compat.la
rm -rf libv4l/plugins/libv4l-mplane.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/KhronosGroup/$P/archive/refs/tags/$V.zip "  $USER
unzip $V.zip
cd $SRC
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local  -DCMAKE_INSTALL_LIBDIR=/usr/local/lib -DBUILD_TESTING:BOOL=OFF -DENABLE_CTEST:BOOL=OFF \
-DENABLE_HLSL:BOOL=OFF -DENABLE_OPT:BOOL=OFF -DCMAKE_SKIP_RPATH=TRUE -DBUILD_EXTERNAL=OFF -DBUILD_SHARED_LIBS:BOOL=ON ../
cd ..
make -j4 # 4M50s ? OR seconds not sure why?
make install DESTDIR=/tmp/$P # no rule to strip
cd /tmp

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

strip -s $P/usr/local/bin/$P
strip -s $P/usr/local/bin/spirv-remap

strip --strip-unneeded $P/usr/local/lib/*.so.$V

# TCZ them
###########
LIST2="$P  "
for Z in $LIST2
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:          glslang.tcz
Description:    OpenGL & OpenGLES shader front end & validator
Version:        13.0.0
Author:         Michael D Adams
Original-site:  https://github.com/KhronosGroup/glslang
Copying-policy: accompanied
Size:           2.3M
Extension_by:   aus9
Tags:           opengl gles graphics
Comments:       Make dep of wlroots
 
Current:        2023/09/22 v 13.0.0 on 14.x
Current:        2023/09/22                  ' > $P.tcz.info

readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # TCBs
readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
# [libglslang.so.13] [libSPIRV.so.13] this TCE
readelf -d $P/usr/local/bin/spirv-remap| grep 'NEEDED' # extra is
# [libSPVRemapper.so.13] this TCE

# no dep set 