#!/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.11
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc bash cmake meson wlroots-xwayland-dev json-c13-dev  "    
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/swaywm/$P/archive/refs/tags/$V.tar.gz
tar xvf $V*gz
cd $SRC
meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dfish-completions=false -Ddefault-wallpaper=false \
-Dzsh-completions=false -Dtray=disabled -Dman-pages=disabled -Dsd-bus-provider=libelogind -Dwerror=false build

# 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
# debug            : false
# default-wallpaper: false
# fish-completions : false
# man-pages        : disabled
# prefix           : /usr/local
# buildtype        : plain
# sd-bus-provider  : libelogind
# strip            : true
# tray             : disabled
# werror           : false
# zsh-completions  : false

cd build 
# meson configure > /tmp/configure 
ninja # 8 seconds
DESTDIR=/tmp/$P ninja install
cd /tmp

# 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 's|foot|lxterminal|' -i $CONF
sed 's|`man 5 sway-input`|https://man.archlinux.org/man/sway-input.5|' -i $CONF
# free up logo + letter and force exittc only exit option, force reload option too
sed '70,71d' -i $CONF  # delete bindsym $mod+Shift+q kill| 
sed '81,82d' -i $CONF  # delete bindsym $mod+Shift+c reload
sed '82,83d' -i $CONF # delete exit we need exittc
# free up some single letters for logo + letter
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
# sway now disable wallpapers so provide example 
sed 's|(more resolutions are available in /usr/local/share/backgrounds/sway/)||' -i $CONF 
sed 's|/usr/local/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png|/home/tc/wallpapers/166012.png|' -i $CONF 
sed 's|wmenu-run|wofi|' -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 if you edit keys...do a search for +<letter> 

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

# below assumes TCE are loaded 
# 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 l3afpad # 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 lxterminal            ' >> $CONF
# remove a sed induced file
rm -rf $P/usr/local/etc/$P/sed*

mkdir -p $P/usr/local/share/$P/files
echo '#!/bin/sh
tce-load -i sway  
seatd-launch dbus-run-session sway ' >  $P/usr/local/share/$P/files/sw
chmod 755 $P/usr/local/share/$P/files/sw

# tce.install
############# 
mkdir -p  $P/usr/local/tce.installed
echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
DIR1=/home/$USER/.local/bin && FILE1=$DIR1/sw && SRC1=/usr/local/share/sway/files/sw
[ -d $DIR1 ] || mkdir -p $DIR1 && chown $USER:staff $DIR1
[ -f $FILE1 ] || cp $SRC1 $DIR1/ && chmod 755 $FILE1 && chown $USER:staff $FILE1    

DIR2=/home/$USER/.config/sway && FILE2=$DIR2/config && SRC2=/usr/local/etc/sway/config
[ -d $DIR2 ] || mkdir -p $DIR2 && chown $USER:staff $DIR2
[ -f $FILE2 ] || cp $SRC2 $DIR2/ && chmod 644 $FILE2 && chown -R $USER:staff $FILE2   ' > $P/usr/local/tce.installed/$P 
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
#######
LIST="$P "
for Z in $LIST
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:          sway.tcz 
Description:    Tiling desktop for Wayland with full xwayland support
Version:        1.11
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           332K		
Extension_by:   aus9 
Tags:           wayland sway wlroots
Comments:       Supports workspaces. On first start you have workspace=WS1
                Create new one by KB shortcuts (logo + 2)
                If you do not open an app there and go to another WS, you lose WS2
                You can use mouse to click into each WS OR (logo + <number of WS>)
                Sway attempts to tile open apps on the same WS. If you move an app to a 
                fresh WS it automatically maximises! So if you had 2 open apps on WS1 and moved 
                one to WS2- by (logo + shift + 2) the remaining app on WS1 becomes maximised as 
                ONE app will always maximise if its the only app in that WS. You are free to use 
                WS1 and say WS6, no need to use WS1,2 etc

                After you load sway, exit to prompt and type sw and press enter to launch sway. 
                Please do not use ondemand with this TCE. 

                Your active App is the last loaded app on your current workspace OR its the one 
                with the blue titlebar. Sway does not have a context menu. Use wofi/dmenu, 
                KB shortcuts or terminal commands. No wallpaper has been set. Edit config to suit. 
                After you have made all your changes to ~/.config/sway/config, within sway use KB 
                shortcuts (logo + z) to check for errors. If there is at least 1 error -- a dialog 
                appears at top of monitor.
                
                Exit Sway
                #########
                press logo + e  -> will launch exittc

                Optional -changeable + removeable TCEs 
                #######################################
                NOTE TCEs below may need sway config edits. Read their relevant info files 
                wbar (libX) interferes with sway so remove it from boot list please
               
                --> Changeable - lxterminal but make sure you modify and test 
                     config please, as you need a terminal set.
                - slurp = screenshot tool 
                - dmenu = launcher recommended if you prefer larger fonts OR
                - wofi = wlroots launcher
                - wl-clipboard = terminal clipboard manager if not using a 
                                 terminal with copy and paste support
                - waybar = a bar OR 
                - sfwbar = a fancier bar 
                - swayimg = image viewer
                - swaylock = screenlocker 
               
Change-log:     2026/01/12 v 1.11 on 17x
Current:        2026/01/12                   '  > $P.tcz.info

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

# lxterminal is set it loads stuff as well  ...sway has bash completions
# sway UPSTREAM split out swaybg so add it here
echo 'bash.tcz
lxterminal.tcz
wlroots-xwayland.tcz
json-c13.tcz 
swaybg.tcz
graphics-KERNEL.tcz         ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

