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

P=sway-dot
USER=`cat /etc/sysconfig/tcuser`
LIST="submitqc "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
mkdir -p $P/usr/local/share/$P

# create help.txt
##################
cat >> $P/usr/local/share/$P/help.txt  <<'EOF'
Edit it for your own memory aids. Once you can remember them, you can comment out the action line in your config.

logo key means Windows key. In the config file ...+ means you hold down some named key AND another key(s)

On first boot you have one Work Space (WS) see the 1 top left hand corner.
Notice how leafpad, the last opened app has a blue title bar. Blue title bar indicates active app.
Any other App appearing on same WS will have black title bar(s)

logo + 2  will create, if it does not already exist, WS2 and move you there.
logo + Shift + <number> will move the active app from current WS to target WS 

If you do not open and leave anything open on WS2, and swap back to WS1 the 2 at top LH cnr will disappear.

Opening 2 or more Apps on same WS will tile them. See your config if you prefer stacking over tiling. 
In fact please read the config.

logo + d  will open dmenu (optional dep) and become the active app. Note any other apps open on same WS as dmenu become inactive. 
Plus, all keyboard combinations become inactive until dmenu is no longer active

You can use kb combinations to shift focus from app1 to app2....or click your mouse onto app2 etc
Below I have removed the syntax to simplify the kb combos

# fltk GUI apps
 logo (and) a launches apps
 logo (and) c launches cpanel
 logo (and) e launches exittc

# Some TCEs may not be loaded below
 logo (and) Enter launches lxterminal
 logo (and) f launches firefox
 logo (and) Alt (and) l launches leafpad 
 logo (and) p launches pcmanfm
 logo (and) Alt (and) q launches qmplay2 
 logo (and) v launches vlc

# and these important too
 logo (and) q kills or quits current active app, you stay on current WS
 logo (and) z test saved config file within sway
 logo (and) d launches dmenu

If you choose to exit sway to go back to libX/Xorg try
$ startx
After X/Xorg starts
$ resetenv
This attempts to reset 2 environment values on libX/xorg for  
GDK_BACKEND and XDG_SESSION_TYPE

However if "unexplained things" are playing up? Please do full reboot. 
One such gremlin is I tend to run sudo su but echo $PATH gives a reduced set.
For me, fix within sway is
$ sudo su
# PATH=$PATH:/usr/local/bin:/usr/local/sbin
EOF

create resetenv
################
cat >> $P/usr/local/share/$P/resetenv  <<'EOF'
#!/bin/sh
# wayland refuses (for me) to use one line like export VALUE=x
# reverse wayland values 
GDK_BACKEND=x11 && export GDK_BACKEND
XDG_SESSION_TYPE=x11 && export XDG_SESSION_TYPE
env | grep x11
EOF

# create z executable
######################
cat >> $P/usr/local/share/$P/z  <<'EOF'
#!/bin/sh
# some bits borrowed from weston
tce-load -i sway 

# start dbus and change some env values
sudo /usr/local/etc/init.d/dbus start

# wayland appears to need 2 lines to set env values
GDK_BACKEND=wayland      && export GDK_BACKEND
XDG_SESSION_TYPE=wayland && export XDG_SESSION_TYPE

# create run dir and export value
USER=`cat /etc/sysconfig/tcuser`
if [ ! -d /run/user/$(id -u "$USER") ]; then
   mkdir -p /run/user/$(id -u "$USER")
   chown "$USER":staff /run/user/$(id -u "$USER")
   chmod 700 /run/user/$(id -u "$USER")
fi
XDG_RUNTIME_DIR=/run/user/$(id -u) && export XDG_RUNTIME_DIR

# home config
LOOP=/tmp/tcloop/sway/usr/local/etc/sway/config
HC=/home/$USER/.config/sway
[ -d $HC ] || mkdir -p $HC
if [ ! -f $HC/config ] ; then
   cp $LOOP $HC/
   chmod 644 $HC/config
   chown -R $USER:staff $HC
fi

# seatd stuff
if grep -q seat /etc/group; then
   sudo usermod -a -G seat tc
   echo 'seatd group found and grouped'
   else
       sudo groupadd seat
       sudo usermod -a -G seat tc
       echo 'seatd group not found but grouped now'
fi

seatd-launch dbus-run-session sway         
EOF

############
tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`

# clear any existing z/help and create new ones
rm -rf /home/$USER/.local/bin/z
rm -rf /home/$USER/.config/sway/help.txt

# dot1 good for all but help
DOT1=/home/$USER/.local/bin

LOOP1=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/z
[ -d $DOT1 ]   || mkdir -p $DOT1
cp $LOOP1 $DOT1
chmod 755 $DOT1/z

# add new executable
LOOP2=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/resetenv
cp $LOOP2 $DOT1
chmod 755 $DOT1/resetenv

# following works for all but help and fixup root-paths
chown -R "$USER":staff $DOT1

# install help.txt file
LOOP4=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/help.txt
DOT2=/home/$USER/.config/sway
[ -d $DOT2 ] || mkdir -p $DOT2
cp $LOOP4 $DOT2
chmod 644 $DOT2/help.txt
chown -R "$USER":staff $DOT2                 
EOF
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 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-dot.tcz 
Description:    z launches sway and see below
Version:        2
Author:         aus9
Original-site:  Tinycore
Copying-policy: open source 
Size:           4.0K	
Extension_by:   aus9 
Tags:           sway
Comments:       /home/$USER/.local/bin/z will load and launch sway
                assumes you have downloaded/updated sway
                
                /home/$USER/.config/sway/help.txt will autostart with sway
               
                /home/$USER/.local/bin/resetenv attempts to reset environment 
                values, if you choose to go from sway back to your libX
                desktop without a full reboot. Best run once back in libX/Xorg

                This TCE is a DownloadOnly and not to be added to 
                your boot list file. It is run by ... $ tce-load -i sway-dot
                or load again if any of your changes causes a bork

                If you are not login-name = tc please edit z to replace 
                tc with your "login-name"
 
Change-log:     2019/05/17 v 1.0 on 10.x  
                2020/07/11 v 1.1 takes over all sway script functions
Current:        2023/05/02 v 2 on 14x      ' > $P.tcz.info

submitqc --libs
