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

# I can not find how to create locales some README<lang> are visible but no POs

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"

P=sway
V=1.8.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc wlroots-dev pcre2-dev cmake meson "    
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

# 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 include/$E
rm -rf pkgconfig/$L.pc
rm -rf $E
rm -rf $L*
cd /tmp/$SRC


# -Dauto_features=enabled
mkdir build && cd build
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

##################################
# 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
# fish-completions: false
# man-pages       : disabled
# tray            : disabled
# xwayland        : enabled
# zsh-completions : false
meson configure 
###############
ninja  # takes seconds
DESTDIR=/tmp/$P ninja install
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/

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.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
# now add in my kb combos
echo '# added by aus9
# $mod=windows logo.... Enter=Return # line 79 defines normal (=left mouse button) 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, may need TCE loaded as optional dep
# already set above logo+Enter executes lxterminal
bindsym $mod+f exec firefox
bindsym $mod+Alt+l exec leafpad # remember left reserves l
bindsym $mod+p exec pcmanfm
bindsym $mod+Alt+q exec qmplay2 # remember q defined for kill below
bindsym $mod+v exec vlc

# and these important too
bindsym $mod+q kill   # kill or quit current active app, you stay on current WS
bindsym $mod+z reload # test a changed config file within sway
# already set above ..logo+d executes dmenu

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

# add autostart apps here
exec leafpad ~/.config/sway/help.txt             ' >> $CONF
############################################
# sway 1.4 had SUID set, from now that is not an option
# load script for sway now in sway-dot

# 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:          sway.tcz 
Description:    Tiling desktop for Wayland
Version:        1.8.1
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           5.2M		
Extension_by:   aus9 
Tags:           wayland sway
Comments:       Online wikis 
                https://github.com/swaywm/sway/wiki
                https://wiki.archlinux.org/title/Sway
                Your config is $HOME/.config/sway/config
                
                This TCE may suit members who do not mind reading the config and 
                using keyboard shortcuts/combinations to launch their fav apps. 
                You can still use a mouse to shift focus, or click into an already
                opened workspace or for web browsing.

                Know your active App is the last loaded app on your current workspace.
                OR its the one with the blue titlebar, while inactive Apps are black titlebar.

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

                FIRST TIME USE
                ###############
                Download this App and sway-dot, then load sway-dot
                $ tce-load -i sway-dot
                If you make changes to z and z crashes, delete it
                and reload sway-dot. 

                VIDEO DRIVERS
                #############
                Closed source drivers are not supported by sway. 
                 
                How to start Sway
                ###########
                Exit to prompt and run as non-root user please
                $  z                 

                Running Sway
                ############
                Read the auto started help.txt file in sway.

                Exit Sway for shutdown/reboot/exit to prompt 
                #########
                press logo + e

                Optional dependencies
                ##################
                leafpad can be removed from dep file if you prefer something else.
                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 info please)
 
                ########
                xwayland allows most apps to run on wayland.
                I recommend you remove wbar, from boot list, if you like sway.

                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
                
                caplock is set to off---numlock to on...see config if interested

                2 gremlins 
                ##########
                1) in sway for sudo su users,
                 command echo $PATH gives a reduced set of pathways to executables
                 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
                  
Change-log:     2019/05/30 Original 1.0 on 10x 
                2020/07/11 -> 1.4 on 11x install script moved to sway-dot 
Current:        2023/05/02 -> 1.8.1 on 14x now depends on seatd TCE      ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
#[libcairo.so.2]  cairo   pango
#[libjson-c.so.4]    json-c13
#[libevdev.so.2]libevdev -> libinput wlroots
#[libinput.so.10]                      "          
#[libudev.so.0] udev-lib libdrm        "
#[libm.so.6]       TCB
#[libpango-1.0.so.0]   pango
#[libgobject-2.0.so.0]glib2 pango      
#[libglib-2.0.so.0]           "
#[libpcre.so.1]   pcre        "
#[libGLESv2.so.2]libGLESv2    "
#[libpixman-1.so.0]pixman     "
#[libwayland-server.so.0]wayland wlroots
#[libwlroots.so.10]               "
#[libxkbcommon.so.0] libxkbcommon "
#[libxcb-icccm.so.4]     libxcb   "
#[libxcb.so.1]                    "
#[libpangocairo-1.0.so.0] pango
#[libc.so.6]   TCB

# libva loads Xorg 3d but not the graphics kernel
# z needs shadow to create seatd group
# sway split it swaybg so add it here
echo 'lxterminal.tcz
leafpad.tcz
shadow.tcz
dbus.tcz
pango.tcz
wlroots.tcz
libva2.tcz
graphics-6.1.2-tinycore64.tcz
swaybg.tcz
json-c13.tcz             ' > $P.tcz.dep

submitqc --libs

# Repo Maintainer or builder
################## Full reboot before you can do any more work please
# due to me deleting elogind stuff