#!/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
#################################################################
echo 'This file should remain under ~/.config/sway as help.txt
edit it for your own memory aids
Once you can remember them, you can # out the action line in your config

X means libX or Xorg desktops

$ exittc # allows you to shutdown or reboot without going thru X (non-sway)

logo key means Windows key
+ means you hold down some named key AND press one or more other keys
                
On first boot you have one Work Space (WS) see the 1 top left hand corner?
To create and move to new WS number 2, press 
logo + 2 

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

To open a terminal ...press 
logo + Enter

Repeating action in same WS will tile the terminals

Except when dmenu (optional dep) is active, 
your active app has a bold title bar. 
Start dmenu by press 
logo + d 

Quit active App, press  
logo + shift + q

To exit sway to console prompt, press 
logo + Shift + e
startx # will start your last loaded X desktop
' > $P/usr/local/share/$P/help.txt
########################################################################3
# create z template
##############
echo '#!/bin/sh
# some bits borrowed from weston 
tce-load -i sway
sudo /usr/local/etc/init.d/dbus start
GDK_BACKEND=wayland
XDG_SESSION_TYPE=wayland
export $GDK_BACKEND
export $XDG_SESSION_TYPE
read USER < /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

# create home config for sway to find
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

XDG_RUNTIME_DIR=/run/user/$(id -u) dbus-run-session sway
' > $P/usr/local/share/$P/z

tce.install
#############
mkdir -p $P/usr/local/tce.installed

echo '#!/bin/sh
read USER < /etc/sysconfig/tcuser

# create dot local bin file
LOOP1=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/z
DOT1=/home/$USER/.local/bin
[ -d $DOT1 ] || mkdir -p $DOT1
[ -x $DOT1/z ] || cp $LOOP1 $DOT1
chmod 755 $DOT1/z
chown -R "$USER":staff $DOT1 

# create help.txt file
LOOP2=/tmp/tcloop/sway-dot/usr/local/share/sway-dot/help.txt
DOT2=/home/$USER/.config/sway
[ -d $DOT2 ] || mkdir -p $DOT2
[ -f $DOT2/help.txt ] || cp $LOOP2 $DOT2
chmod 644 $DOT2/help.txt
chown -R "$USER":staff $DOT2
' > $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 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:    Mainly local bin to start sway
Version:        1.1
Author:         aus9
Original-site:  Tinycore
Copying-policy: open source 
Size:           4.0K	
Extension_by:   aus9 
Tags:           sway
Comments:       startx is used to start a X WM while sway runs 
                on Wayland so to allow people to try sway I 
                have elected to give them a dot local bin file 
                ..z..to use at console prompt to launch sway

                See sway TCE info on how to exit sway and then 
                you can run startx to start your preferred DE/WM,
                if you do not like sway.

                That means this app is a DownloadOnly and not to be 
                added to your boot list file. It is run once by 
                $ tce-load -i sway-dot

                I have attempted to put a help.txt file under
                ~/.config/sway which will autostart with sway 

                sway-dot can be loaded without sway loaded.
                
Change-log:     2019/05/17 Original v 1.0 on 10.x  
Current:        2020/07/11 -> 1.1 takes over all sway script functions
' > $P.tcz.info

submitqc --libs
