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

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

P=wl-clipboard
V=1.0.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson wayland-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/bugaevc/$P/archive/v$V.zip " $USER
unzip v$V*zip
cd $SRC
meson build
cd build 
ninja
touch /tmp/mark
ninja install
cd /tmp

# doc
#####
mkdir -p $P-doc/usr/local/share/man/man1
mv /usr/local/share/man/man1/wl* $P-doc/usr/local/share/man/man1
mkdir -p $P-doc/usr/local/share/doc/$P
cp $SRC/README.md $P-doc/usr/local/share/doc/$P/

# main
######
mkdir -p $P/usr/local/bin
mv /usr/local/bin/wl* $P/usr/local/bin
strip --strip-unneeded $P/usr/local/bin/*

# 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 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          wl-clipboard.tcz 
Description:    command tool for WayLand copy or paste 
Version:        1.0.0
Author:         Sergey Bugaev
Original-site:  https://github.com/bugaevc
Copying-policy: GPL v3
Size:           20K	
Extension_by:   aus9 at gmx dot com
Tags:           man-db man 
Comments:       Wayland copy and paste command line tool
                see doc README.txt on how to use please
                or man pages. Not necessary if you continue to use
                lxterminal.

                basic commands either wl-copy or wl-paste.
 
                Compiled for 10.1 
Change-log:     2019/06/17 first version  
Current:        2019/06/17  ' > $P.tcz.info

echo 'Title:          wl-clipboard-doc.tcz 
Description:    docs  
Version:        1.0.0
Author:         Sergey Bugaev
Original-site:  https://github.com/bugaevc
Copying-policy: GPL v3 
Size:           8.0K
Extension_by:   aus9 at gmx dot com
Tags:           man-db man 
Comments:       man pages and README 

                Compiled for 10.1
Change-log:     2019/06/17 first version  
Current:        2019/06/17  ' > $P-doc.tcz.info

submitqc --libs
