#!/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=bemenu
V=0.1.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc doxygen cmake wayland-dev cairo-dev pango-dev libxkbcommon-dev "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

# fix
ln -s /usr/local/include/ncursesw/ncurses.h /usr/local/include/ncurses.h

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/Cloudef/$P/archive/$V.tar.gz " $USER
tar xvf $V*gz
cd $SRC
cmake  -DCMAKE_INSTALL_PREFIX=/usr/local CMAKE_BUILD_TYPE=MinSizeRel .

# make test --none found
make doxygen
make -j5
mkdir /tmp/$P
make install DESTDIR=/tmp/$P
cd /tmp

# no dev
#####
mkdir -p $P-dev/usr/local
mv $P/usr/local/include $P-dev/usr/local

# doc
#####
mkdir -p $P-doc/usr/local/share/doc
mv $SRC/doxygen/html $P-doc/usr/local/share/doc/bemenu
mv $SRC/LICENSE*  $P-doc/usr/local/share/doc/$P/
# ignore info files

# main 
######
strip --strip-unneeded $P/usr/local/bin/*
strip --strip-unneeded $P/usr/local/lib/$P/*
strip --strip-unneeded $P/usr/local/lib/*1.0

# TCZ them
#######
LIST="$P $P-doc $P-dev"
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 'bemenu.tcz
compiletc.tcz
submitqc.tcz 
doxygen.tcz 
cmake.tcz 
wayland-dev.tcz 
cairo-dev.tcz 
pango-dev.tcz 
libxkbcommon-dev.tcz' > $P-dev.tcz.dep

echo 'ncursesw.tcz
libxkbcommon.tcz
cairo.tcz ' > $P.tcz.dep

echo 'Title:          bemenu.tcz 
Description:    Dynamic menu library and client program  
Version:        0.1.0
Author:         Jari Vetoniemi
Original-site:  https://github.com/Cloudef/bemenu
Copying-policy: GPL v3 for client LGPL v3 for library and bindings
Size:           80K		
Extension_by:   aus9 at gmx dot com
Tags:           wayland bmenu
Comments:       Dynamic menu library and client program inspired by dmenu
                   Works on X or wayland

                   Please read j4-dmenu-desktop on how to use
                  
                  Compiled for 10.1
Change-log:     2019/06/22 first version 
Current:        2019/06/22  ' > $P.tcz.info

echo 'Title:          bemenu-doc.tcz 
Description:    docs for bemenu
Version:        0.1.0
Author:         Jari Vetoniemi
Original-site:  https://github.com/Cloudef/bemenu
Copying-policy: GPL v3 for client LGPL v3 for library and bindings
Size:           400K		
Extension_by:   aus9 at gmx dot com
Tags:           wayland man
Comments:       lots of docs

                 suggest you point your web browser to
                 /usr/local/share/doc/bemenu/index.html

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

echo 'Title:          bemenu-dev.tcz 
Description:    devs for bemenu
Version:        0.1.0
Author:         Jari Vetoniemi
Original-site:  https://github.com/Cloudef/bemenu
Copying-policy: GPL v3 for client LGPL v3 for library and bindings
Size:           8.0K		
Extension_by:   aus9 at gmx dot com
Tags:           wayland man
Comments:       header

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

submitqc --libs
