#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi
# removed -fno-exceptions -fno-rtti due to make error
# also getting -Wcast-function-type so try to fix below
export CFLAGS="-mtune=generic -Os -pipe -Wno-cast-function-type"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=notion
V=4.0.3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc  libX11-dev libXext-dev libSM-dev gettext-dev libXinerama-dev  \
libXrandr-dev  libXft-dev libICE-dev lua-5.4-dev lua-5.4 groff man-db   "
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/raboof/$P/archive/refs/tags/$V.tar.gz "  $USER
tar xvf $V*gz
cd $SRC

# delete exit from f12 menu as we need either exittc or exitcheck.sh
sed '377d' -i etc/cfg_bindings.lua
# I have left kludges alone to reduce lines of edits
# the various look*lua have non-universal font sizes...why?.....standardize them now
sed 's|12|14|' -i etc/look*.lua
sed 's|13|14|' -i etc/look*.lua
sed 's|10|14|' -i etc/look*.lua
# but leave look_newviolet_hidpi.lua at 20  untested
# enable statusbar in lieu of dock
sed 's|dock|statusbar|' -i etc/cfg_defaults.lua

make  -j4  # 15 secs
make install DESTDIR=/tmp/$P # no rule to strip
cd /tmp

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

# locale 
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# main
###### start with a clean doc dir
rm -rf $P/usr/local/share/doc/$P/*
echo 'LGPL v 2.1' > $P/usr/local/share/doc/$P/LICENSE
# notion combines welcome.txt and man page from non-std pathway
# /usr local/share/notion/.....and needs nroff (groff) to display this ugly combo doc

# tce.install
############# 
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
echo "notion" > /etc/sysconfig/desktop
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-doc $P-locale"
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          notion-doc.tcz
Description:    2 man pages
Version:        4.0.3
Author:         https://github.com/raboof/notion/blob/main/AUTHORS
Original-site:  https://notionwm.net/
Copying-policy: LGPL v 2.1
Size:           8.0K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm tiling
Comments:       Due to ugly rendering by inbuilt man page
                in main TCE, this has been made a dep of main TCE
                Once you no longer need to read this you can edit
                notion.tcz.dep to remove this doc TCE

Change-log:     2025/03/18 v 4.0.3 
Current:        2025/03/18  ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          notion-locale.tcz
Description:    language files
Version:        4.0.3
Author:         https://github.com/raboof/notion/blob/main/AUTHORS
Original-site:  https://notionwm.net/
Copying-policy: LGPL v 2.1
Size:           32K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm tiling
Comments:       not tested by me

Change-log:     2025/03/18 v 4.0.3 
Current:        2025/03/18  ' > $P-locale.tcz.info
echo 'notion.tcz' > $P-locale.tcz.dep

echo 'Title:          notion.tcz TESTING
Description:    Static tabbed tiling window manager
Version:        4.0.3
Author:         https://github.com/raboof/notion/blob/main/AUTHORS
Original-site:  https://notionwm.net/
Copying-policy: LGPL v 2.1
Size:           476K
Extension_by:   aus9 @ linuxquestions.org
Tags:           window manager wm tiling
Comments:       Do NOT use Apps "OnBoot" pulldown until you know how to use this WM.
 
                Supports autostarted software in ~/.X.d but all of them
                cover up the welcome page until you close them.
                
                Supports workspaces but no window decorations to use mouse kills
                Most GUIs allow Ctrl+q to quit eg l3afpad, pcmanfm but
                lxterminal uses  Ctrl+Shift+q. Otherwise use context menu to quit app.
                For a right hand mouse user - context menu is the right button 
               
                Static means you can manually resize frames.
                Keyboard shortcuts  https://notionwm.net/notionkeys.html
                Intro https://notionwm.net/tour.html
                Youtube https://youtu.be/4tkTc6U08DE?si=BCVPn44-23jBqreK
                Windows logo key=mod4=META  
                F2 will start lxterminal ditto logo+Enter
           
                 logo+x kills active frame, you start with 2 frames.
                Each frame can have multiple tabs. If you kill all frames
                accidently -> logo+F9 starts afresh.
                F11 may maximise current client (software) but only inside frame size
                If you plan to use firefox please kill one frame as F11 is also a firefox
                keybinding which interferes if you have more than one frame open

                logo+j --- starts inbuilt run command similar to flrun
                -----use TAB key to autocomplete when sufficient letters input
                Executing F2, logo+Enter or a run command creates a TAB in 
                current frame.
                Use mouse to focus frame or logo+TAB to cycle thru frames

                F12 main menu  = logo+m
                A combined welcome and man page is displayed on launch
                but it difficult to read instead run $ man notion 
    
               F12 and select looks to change theme. Bottom panel prompts to save 
               changes....input y to save. Check ~/.notion files 
               LUA users may want to copy files from /usr/local/etc/notion to ~/.notion
               If so also read $ man notionflux

               On my AMD APU I needed Xorg YMMV. I tested on a single monitor.
               Wayland not supported. ssh untested by me. Inbuilt man page displays 
               poorly with groff so TCE contains normal man pages so run
               $ man notion

               If you are in trouble try F3 input=exittc select shutdown or try
               logo+Enter  input=exittcheck.sh and press enter
               Experienced users can delete tce.installed/desktop and reload it 
               then startx

               Optional TCE  
               dmenu if you are unable to increase font size for inbuilt  launcher
               notion-doc is a removable as inbuilt man viewer uses groff and is "unreadable" IMHO            
               
Change-log:     2025/03/18 v 4.0.3 on 16x, inbuilt man page needs groff not man-db for welcome page
Current:        2025/03/18  ' > $P.tcz.info

# launches xterm so I made it lxterminal 
# notion welcome/man page needs groff

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # ignore TCBs
#libX11.so.6]libX11 libXext libXrandr lxterminal
#libXext.so.6]libXext libXrandr lxterminal
#liblua.so.5.4]lua-5.4-lib
#libreadline.so.8]readline lxterminal
readelf -d $P/usr/local/lib/$P/mod/*.so | grep 'NEEDED'
#[libXft.so.2]libXft lxterminal 
#libSM.so.6]libSM
#libICE.so.6]libICE
#[libXinerama.so.1]libXinerama lxterminal
#libXrandr.so.2]libXrandr lxterminal
readelf -d $P/usr/local/lib/$P/bin/* | grep 'NEEDED' # no extras
readelf -d $P/usr/local/share/notion/* | grep 'NEEDED' # no extras

echo 'lxterminal.tcz 
lua-5.4-lib.tcz
libSM.tcz
libICE.tcz 
groff.tcz 
notion-doc.tcz
' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

# autostart uses groff to display combined welcome man page
# but its ugly so keep man page and man-db too


