#!/bin/bash
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=sway
V=1.9
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="json-c13-dev compiletc submitqc wlroots-dev cmake meson libinput-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/swaywm/$P/archive/$V.zip " $USER
unzip $V*zip


# SKIP NEXT BIT
# elogind-dev was loaded by polkit-dev-- to stop sway seeing the big bits 
# brute delete system files for elogind
# may result in broken web browser until full reboot
#E=elogind
#L=lib$E
#cd /usr/local/lib
#rm -rf $E pkgconfig/$L.pc $L*
#rm -rf ../include/$E

cd /tmp/$SRC
meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dbuildtype=plain -Dxwayland=enabled \
-Dfish-completions=false -Dzsh-completions=false -Dtray=disabled -Dman-pages=disabled -Dwerror=false build

# xwayland        : YES
# gdk-pixbuf      : YES
# tray            : NO
# man-pages       : NO

# Subprojects
#  wlroots         : NO
# Neither a subproject directory nor a wlroots.wrap file was found.
# User defined options
# buildtype       : plain
# debug           : false
# prefix          : /usr/local
# strip           : true
# werror          : false
# fish-completions: false
# man-pages       : disabled
# tray            : disabled
# xwayland        : enabled
# zsh-completions : false
meson configure 

ninja -C build # 24 seconds
DESTDIR=/tmp/$P ninja -C build install
cd /tmp

# wallpapers
###########
mkdir -p $P-wallpapers/usr/local/share
mv $P/usr/local/share/backgrounds $P-wallpapers/usr/local/share

# main
######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE  $P/usr/local/share/doc/$P/

# config changes
##########################
CONF=$P/usr/local/etc/$P/config
sed '/### Output configuration/a\
# Please read info for sway-wallpaper TCE ' -i $CONF

sed 's|foot|lxterminal|' -i $CONF
sed 's|`man 5 sway-input`|https://man.archlinux.org/man/sway-input.5.en|' -i $CONF
# free up logo + letter and force exittc only exit option, force reload option too
sed 's|bindsym $mod+Shift+q kill|#bindsym $mod+Shift+q kill|' -i $CONF
sed 's|bindsym $mod+Shift+c reload|#bindsym $mod+Shift+c reload|' -i $CONF
sed 's|bindsym $mod+Shift+e|#bindsym $mod+Shift+e|' -i $CONF
sed 's|bindsym $mod+b|bindsym $mod+Alt+b|' -i $CONF
sed 's|bindsym $mod+v|bindsym $mod+Alt+v|' -i $CONF
sed 's|bindsym $mod+s|bindsym $mod+Alt+s|' -i $CONF
sed 's|bindsym $mod+w|bindsym $mod+Alt+w|' -i $CONF
sed 's|bindsym $mod+e|bindsym $mod+Alt+e|' -i $CONF
sed 's|bindsym $mod+f|bindsym $mod+Alt+f|' -i $CONF
sed 's|bindsym $mod+a|bindsym $mod+Alt+a|' -i $CONF
# can not free up mod+r as mod+right interfers --ditto left
# now add in my kb combos
echo '# TC additions
# $mod=Windows logo key...Enter=Return...WS=workspace
# My suggestion on edit of keys...do a search for +<letter> 

# fltk apps GUI
bindsym $mod+a exec apps
bindsym $mod+c exec cpanel
bindsym $mod+e exec exittc

# other GUIs, assumes TCE loaded if not a dep
# already set above logo+Enter executes lxterminal
bindsym $mod+f exec firefox
bindsym $mod+p exec pcmanfm
bindsym $mod+q kill   # kill or quit current active app, you stay on current WS
bindsym $mod+v exec vlc
bindsym $mod+z reload # test a changed config file within sway
bindsym $mod+Alt+l exec leafpad # remember left reserves l
bindsym $mod+Alt+q exec qmplay2 # remember q defined for kill below

# input type:keyboard xkb_capslock enabled
input type:keyboard xkb_numlock enabled

# add autostart apps here
exec leafpad ~/.config/sway/help.txt             ' >> $CONF


# TCZ them
#######
LIST="$P $P-wallpapers "
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:          sway-wallpapers.tcz 
Description:    Official wallpapers for sway
Version:        1.9
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: see sway TCE 
Size:           4.8M		
Extension_by:   aus9 @linuxquestions.org  
Tags:           wayland wallpaper
Comments:       Splits out wallpapers to make sway TCE smaller.
                This is TCE is a removeable dep once you edit your config as below
                --- changing the pathway and filenames of course

                Your current config =
                output * bg /usr/local/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
                Edit to something like
                output * bg /home/tc/2wallpaper/cat-lick.jpeg fill

                Save the config change and test it within sway by 
                logo + z
                If your wallpaper appears, delete this TCE from sway.tcz.dep

                You are free to have no wallpaper too by deleting this TCE and no config to 
                any image.

Change-log:     2024/04/22 v 1.9 on 15x split from main TCE
Current:        2024/04/22                                    ' > $P-wallpapers.tcz.info
# no dep as swaybg is a dep of sway

echo 'Title:          sway.tcz 
Description:    Tiling desktop for Wayland
Version:        1.9
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           332K		
Extension_by:   aus9 @linuxquestions.org  
Tags:           wayland sway
Comments:       UPGRADERS please read sway-wallpapers TCE and see     
                https://github.com/swaywm/sway/wiki
                                                
                Your active App is the last loaded app on your current workspace.
                OR its the one with the blue titlebar. 

                If you change ~/.config/sway/config, run from within sway 
                (Windows logo key) + z (see config ) --and check for  
                errors appearing at top of monitor.

                First use
                ##########
                Download this App and sway-dot, then load sway-dot
                $ tce-load -i sway-dot
                Exit to prompt and run 
                $  z  
                Later Uses, no need to load sway-dot 
                                             
                Exit Sway 
                #########
                press logo + e  -> will launch exittc

                Conflicting TCE=wbar, after testing, if you like sway,
                remove wbar as it interferes with sway please

                Optional dependencies 
                ##################
                - Changeable or removeable- leafpad.
                - Changeable - lxterminal but make sure you modify and test config please,
                  as you need a terminal set.
                - grim and slurp (take screenshots)
                - dmenu  (a launcher. Must have xwayland loaded and enabled)
                - wl-clipboard (terminal clipboard manager if not using a 
                              terminal with copy and paste support)
                - swaylock (lock screen read its info please)
                - swayidle ( with settings in config)
                - Removeable dep = sway-wallpapers but read its info on how to 
                   change to your own please
                - xwayland allows most apps to run on wayland.
                

                You can not use logo + l (or) r as these interfere with 
                shortcuts with logo + left (<- arrow) to shift focus to
                App left of current active app. Ditto right (-> arrow)
                Which is why leafpad can not use logo + l
                
                Known gremlins 
                ###############
                1) in sway for sudo su users,
                   command echo $PATH gives a reduced set of pathways 
                   to executables---so run
                   $ PATH=$PATH:/usr/local/bin:/usr/local/sbin

                2) Going from sway -> prompt -> startx
                   local user loses full pathway too! same fix as above
                   $ PATH=$PATH:/usr/local/bin:/usr/local/sbin
                   Note I do not use ondemand

                3) If you launch an app that would normally appear in wbar, and wbar is loaded
                   wbar interferes with sway -> $ pkill wbar before loading them
                  
Change-log:     2019/05/30 v 1.0 on 10x 
                2020/07/11 v 1.4 on 11x install script moved to sway-dot 
                2023/05/02 v 1.8.1 on 14x now depends on seatd TCE
Current:        2024/04/22 v 1.9 Split wallpapers to separate TCE, its a removable dep ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED' # ignore TCBs
#[libcairo.so.2]cairo -> pango
#[libjson-c.so.4]json-c13
#[libevdev.so.2]libevdev -> libinput
#[libinput.so.10] libinput                             
#[libudev.so.0] udev-lib -> libdrm        
#[libpango-1.0.so.0][libpangocairo-1.0.so.0] pango
#[libgobject-2.0.so.0][libglib-2.0.so.0] glib2 -> cairo -> pango      
#[libpcre2-8.so.0] pcre21048        
#[libpixman-1.so.0]pixman -> cairo -> pango    
#[libwayland-server.so.0]wayland 
#[libwlroots.so.12] wlroots             
#[libxkbcommon.so.0] libxkbcommon -> wlroots
#[libxcb.so.1]libxcb-icccm.so.4]libxcb   

# libva loads Xorg 3d but not the graphics kernel
# sway split out swaybg so add it here
echo 'pango.tcz
json-c13.tcz 
libinput.tcz
pcre21042.tcz
wayland.tcz
wlroots.tcz
libxcb.tcz
swaybg.tcz
graphics-KERNEL.tcz
leafpad.tcz
lxterminal.tcz 
seatd.tcz
dbus.tcz
shadow.tcz
sway-wallpapers.tcz          ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

