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

P=grim
V=1.4.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson wayland-dev pixman-dev libjpeg-turbo-dev libpng-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://git.sr.ht/~emersion/$P/refs/download/v$V/$SRC.tar.gz   " $USER
tar xvf $SRC.*gz
cd $SRC

meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dman-pages=disabled build  
#    debug : false
#    prefix: /usr/local
#    strip : true
# man-pages: disabled
ninja -C build # seconds
cd build
DESTDIR=/tmp/$P ninja install
cd /tmp

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

# TCZ them
#######
LIST="$P "
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:          grim.tcz 
Description:    GRab IMages from sway 
Version:        1.4.0
Author:         emersion
Original-site:  https://sr.ht/~emersion/grim/
Copying-policy: Accompanied 
Size:           16K
Extension_by:   aus9 
Tags:           sway screen capture
Comments:       screenshot app limited to its current terminal and 
                can only be used on sway/wayland
                see slurp to capture outside current App

                Usage: grim -h
                default images go to $HOME/Pictures
                  
                online how to use
                https://git.sr.ht/~emersion/grim
                                               
Change-log:     2019/06/16 v 1.2.0 on 10x 
                2020/07/02 v 1.3.0 on 11x
Current:        2023/04/22 v 1.4.0 on 14x   ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'

# [libm.so.6] TCB
# [libpixman-1.so.0] pixman
# [libpng16.so.16] libpng
# [libwayland-client.so.0] wayland
# [libjpeg.so.62] libjpeg-turbo
# [libc.so.6]TCB

echo 'pixman.tcz
libpng.tcz
wayland.tcz
libjpeg-turbo.tcz ' > $P.tcz.dep


submitqc --libs
