#!/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=wl-clipboard
V=2.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 'Title:          wl-clipboard-doc.tcz 
Description:    docs  
Version:        2.0.0
Author:         Sergey Bugaev
Original-site:  https://github.com/bugaevc
Copying-policy: GPL v3 
Size:           8.0K
Extension_by:   aus9 
Tags:           man 
Comments:       man pages and README 

Change-log:     2019/06/17 Original 1.0.0 on 10x  
Current:        2020/07/10 -> 2.0.0 on 11x
 ' > $P-doc.tcz.info

echo 'Title:          wl-clipboard.tcz 
Description:    command tool for WayLand copy or paste 
Version:        2.0.0
Author:         Sergey Bugaev
Original-site:  https://github.com/bugaevc
Copying-policy: GPL v3
Size:           32K	
Extension_by:   aus9 
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
                sakura which has a copy and paste function.

                basic commands either wl-copy or wl-paste.
                 
Change-log:     2019/06/17 Original 1.0.0 on 10x  
Current:        2020/07/10 -> 2.0.0 on 11x
  ' > $P.tcz.info

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

submitqc --libs
