#!/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=dmenu
V=4.8
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc libX11-dev libXft-dev libXinerama-dev freetype-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://dl.suckless.org/tools/dmenu-4.8.tar.gz " $USER
mkdir $P
tar xvf $SRC*gz
cd $SRC

# fix some pathways for TC
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 -j9
touch /tmp/mark
make install 
cd /tmp
find /usr/local -newer mark -not -type d > list 

# 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
# already stripped both  bin files
mv /usr/local/bin/stest $P/usr/local/bin

# locale may be set in dmenu_run so it make it writeable
mkdir -p $P/usr/local/share/$P
rm -rf  $P/usr/local/bin/dmenu_run 

tce.install
#############
mkdir -p $P/usr/local/tce.installed

cat >> $P/usr/local/tce.installed/$P <<'EOF2'
#!/bin/sh
cat >> /usr/local/bin/dmenu_run <<'EOF'
#!/bin/sh
# remove # from the LANG line and change to your locale
# use a UTF8 terminal too
#LANG="en_US.UTF-8" 
dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
EOF
chmod 755 /usr/local/bin/dmenu_run
EOF2
chown -R tc:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# doc
######
mkdir -p $P-doc/usr/local/share/man/man1
mv /usr/local/share/man/man1/$P* $P-doc/usr/local/share/man/man1/
mv /usr/local/share/man/man1/stest* $P-doc/usr/local/share/man/man1/

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

# 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

cat >> $P.tcz.info <<'EOF'
Title:          dmenu.tcz 
Description:    Dynamic Menu Launcher for X or Sway
Version:        4.8
Author:         Hiltjo Posthuma
Original-site:  https://tools.suckless.org/dmenu/
Copying-policy: MIT/X Consortium
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           sway launcher menu
Comments:       A tool that can search executables on X (or Sway) 
                Then attempt to launch the highlighted entry           
                when you press the Enter key. 
                Inputs search the user's $PATH
                It is not a fuzzy search tool but exact search.

                For this info file, dmenu (or dmenu active) means the visible menu 
                ----at top of monitor

                How to run on X (tested on openbox and fvwm)
                ###############
                $ dmenu_run 

                dmenu will appear at top of your monitor
                
                Input of "leaf" will return one highlighted
                result of "leafpad" for me. Pressing enter
                launches leafpad. Input of "lea" will give
                more hits including "clear" 

                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.
                BUT not all commands are meant to be run without
                parameters - options etc 

                If you pressed enter against a hit that 
                can only run in a terminal, it should appear in
                your active terminal.
                
                If you change your mind press Escape key 
                to exit dmenu. While dmenu is active, you may lose
                kb combos such as swapping workspaces.

                If your search input is (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. This is not a bug but operator error
                (Not that I have ever made this error joke)

                locale users
                ############
                /usr/local/bin/dmenu_run is a root writeable file
                Optional, edit to suit your locale 
                
                How to run on Sway
                ##################
                Press keys logo (and) d to start dmenu
                More info in Sway info or forum post
                Follow above for the rest.
                ######################################
                It does not seem Weston can use dmenu.

                man dmenu provides other ways of running eg
                dmenu_run -l 5

                Possible Glitches
                #################
                On fvwm, 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. 

                Compiled for 9x 
Change-log:     2018/07/26 first version 
Current:        2018/07/26 
EOF

echo 'Title:          dmenu-doc.tcz 
Description:    docs for dmenu
Version:        4.8
Author:         Hiltjo Posthuma
Original-site:  https://tools.suckless.org/dmenu/
Copying-policy: MIT/X Consortium
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           sway launcher menu
Comments:       2 man pages one for dmenu. 
                The other one I think is for 
                if-then tests called stest

                Compiled for 9x 
Change-log:     2018/07/26 first version 
Current:        2018/07/26 ' > $P-doc.tcz.info

submitqc --libs
