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

P=sway-dot
V=3
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 some config syntax to simplify the kb combos

# fltk GUI apps
 logo + a launches apps
 logo + c launches cpanel
 logo + e launches exittc

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

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

If you choose to exit sway to go back to libX/Xorg try (logo + e) -> choose prompt then $ startx
When in X........then run $ resetenv
This attempts to reset 2 environment values on X 

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 (prompt changes from $ to #)
PATH=$PATH:/usr/local/bin:/usr/local/sbin
EOF
chmod 644 $P/usr/local/share/$P/help.txt

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
chmod 755 $P/usr/local/share/sway-dot/resetenv


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

# wayland environments
GDK_BACKEND=wayland && export GDK_BACKEND
XDG_SESSION_TYPE=wayland && export XDG_SESSION_TYPE

# 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

# 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

# seatd stuff
if grep -q seat /etc/group; then
   sudo usermod -a -G seat tc
   else
       sudo groupadd seat
       sudo usermod -a -G seat tc
fi

sudo rm -rf /run/seatd.sock
seatd-launch dbus-run-session sway

EOF
chmod 755 $P/usr/local/share/sway-dot/z


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

# delete z help & resetenv -> start afresh
rm -rf $DOTLOCBIN/z $DOTLOCBIN/resetenv
rm -rf /home/$USER/.config/sway/help.txt

LOOP1=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/z
cp $LOOP1 $DOTLOCBIN/ 

LOOP2=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/resetenv
[ -f $DOTLOCBIN/resetenv ] || cp $LOOP2 $DOTLOCBIN 

# following works for all but help 
chmod 755 $DOTLOCBIN/*
chown $USER:staff $DOTLOCBIN/*

# install help.txt file
LOOP3=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/help.txt
DOTCONFSWAY=/home/$USER/.config/sway
[ -d $DOTCONFSWAY ] || mkdir -p $DOTCONFSWAY
cp $LOOP3 $DOTCONFSWAY
chmod 644 $DOTCONFSWAY/help.txt
chown $USER:staff $DOTCONFSWAY/help.txt
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775        $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'open source' > $P/usr/local/share/doc/$P/COPYING


# 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:        3
Author:         aus9 
Original-site:  Tinycore
Copying-policy: open source 
Size:           4.0K	
Extension_by:   aus9 @linuxquestions.org
Tags:           sway 
Comments:       This TCE is a DownloadOnly so not to be added to 
                your boot list file. It is run once by  
                $ tce-load -i sway-dot
                or load again if any of your changes causes a bork.
                Script destroys Z, help & resetenv 

                exe=z will load and launch sway (separate download)
                       
                exe=resetenv attempts to reset environment values, 
                     if you choose to go from sway to your X desktop
                     without a full reboot. Run it when you are in X
              
                sway should not be launched on X please
            
Change-log:     2019/05/17 v 1.0 on 10.x  
                2020/07/11 v 1.1 takes over all sway script functions
                2023/05/02 v 2 on 14x      
Current:        2024/04/18 v 3 on 15x ' > $P.tcz.info
submitqc --libs
rm -rf *.zsync
# no deps

