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

P=grim
V=1.3.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson wayland-dev cairo-dev \
scdoc-dev libjpeg-turbo-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/emersion/$P/archive/v$V.zip   " $USER
unzip v$V*zip
cd $SRC
# meson defaults to usr/local
meson --strip build
ninja -C build
touch /tmp/mark
ninja -C build install
cd /tmp

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

# doc
#####
mkdir -p $P-doc/usr/local/share/man/man1
cp $SRC/build/*.1 $P-doc/usr/local/share/man/man1/
mkdir -p $P-doc/usr/local/share/doc/$P
cp $SRC/LICENSE  $P-doc/usr/local/share/doc/$P/

# TCZ them
#######
LIST="$P $P-doc "
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-doc.tcz 
Description:    docs for grim
Version:        1.3.0
Author:         emersion
Original-site:  https://github.com/emersion/grim
Copying-policy: Accompanied 
Size:           4.0K
Extension_by:   aus9 
Tags:           sway man
Comments:       docs  

Change-log:     2019/06/16 Original v 1.2.0 on 10x 
Current:        2020/07/02 -> v 1.3.0 on 11x
' > $P-doc.tcz.info

echo 'Title:          grim.tcz 
Description:    GRab IMages from sway 
Version:        1.3.0
Author:         emersion
Original-site:  https://github.com/emersion/grim
Copying-policy: Accompanied 
Size:           12K
Extension_by:   aus9 
Tags:           sway screen capture
Comments:       screenshot app limited to its current terminal and 
                can only be used on sway

                Usage: grim [options...] <output-file>
                  
                example without a mouse for current window    
                $ grim ~/Pictures/$(date +%Y-%m-%d-%H%M%S_grim.png) 
                # Use a timestamped filename under Pictures dir

                grim can not capture outside its active window (terminal) 
                to capture another window, see slurp please 
                                
Change-log:     2019/06/16 Original v 1.2.0 on 10x 
Current:        2020/07/02 -> v 1.3.0 on 11x 
' > $P.tcz.info

echo 'libjpeg-turbo.tcz
cairo.tcz' > $P.tcz.dep
echo 'man-db.tcz' > $P-doc.tcz.dep

submitqc --libs
