#!/bin/bash
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS='-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72   '
export CXXFLAGS='-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72  '
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS
 
P=dmenu
V=5.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc wayland-dev libX11-dev libXft-dev libXinerama-dev "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

# list may be larger than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/liblto_plugin.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
rm -rf imlib2/loaders/*.la
rm -rf imlib2/filters/*.la
rm -rf gdk-pixbuf-2.0/2.10.0/loaders/*.la
rm -rf pulseaudio/libpulsedsp.la
rm -rf pulseaudio/libpulsecore-13.0.la
rm -rf pulseaudio/libpulsecommon-13.0.la
rm -rf libv4l/v4l2convert.la
rm -rf libv4l/v4l1compat.la
rm -rf libv4l/plugins/libv4l-mplane.la
rm -rf vala-0.46/libvalaccodegen.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://dl.suckless.org/tools/$SRC.tar.gz  " $USER
tar xvf $SRC*gz
cd $SRC

# fix pathways
sed 's|X11R6/include|local/include/X11|' -i config.mk
sed 's|X11R6/lib|local/lib|' -i config.mk
sed 's|include/freetype2|local/include/freetype2|' -i config.mk

make -j4 # seconds
make install DESTDIR=/tmp/$P
cd /tmp

# no doc
######
rm -rf $P/usr/local/share/man 

# main
######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE  $P/usr/local/share/doc/$P/

strip --strip-unneeded $P/usr/local/bin/$P
strip --strip-unneeded $P/usr/local/bin/stest

# 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:          dmenu.tcz 
Description:    Dynamic Menu Launcher for X or Sway
Version:        5.2
Author:         Hiltjo Posthuma
Original-site:  https://tools.suckless.org/dmenu/
Copying-policy: MIT/X Consortium
Size:           20K
Extension_by:   aus9 
Tags:           launcher menu
Comments:       A tool that can search executables on X (or Sway) 
                Then attempts to launch the highlighted entry           
                when you press the Enter key. Search is exact 
                spelling but not all letters needed

                How to run on X (tested on jwm)
                ###############
                $ dmenu_run 
                For a large font $ dmenu_run -fn sans-20
                Search bar will appear at top of your monitor
                
                Use arrow keys with numlock OFF to navigate to
                your target command or type more letters

                If you pressed enter, you are attempting to 
                launch the highlighted item and dmenu closes.
                Be aware not all commands are meant to be run 
                without parameters.

                If you change your mind press the Escape key. 
                While dmenu is active, you probably can not launch 
                keyboard shortcuts.

                If your search input is for a non-executable (eg) "lll" 
                you lose all hits and if you forget to Escape and press enter 
                you are launching a missing executable called "lll" and will get
                a not found error. 
                
                How to run on Sway
                ##################
                Press keys logo (and) d to start dmenu
                Follow above for the rest.
                
                Possible Glitches
                #################
                On flwm, dmenu may cover any open app that is
                at the top of your monitor. So to close an app,
                you may have to either launch an app or press escape,
                while dmenu active.
                You can safely ignore any msg about
                /home/tc/.cache/dmenu_run: No such file or directory
                ^ that file is created, its a list of executables
                                
Change-log:     2020/10/04 v 5.0 on 12x
Current:        2023/09/22 v 5.2 on 14x  " > $P.tcz.info


readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
# #[libX11.so.6]        libX11      
# #[libXinerama.so.1]   libXinerama
# #[libfontconfig.so.1] fontconfig  -> libXft
# #[libXft.so.2]         libXft

echo 'libX11.tcz      
libXinerama.tcz
libXft.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *zsync

