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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
export LDFLAGS="-Wl,-O1"

P=sway
V=1.4
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="wlroots-dev scdoc-dev"
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
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
mkdir build && cd build
meson --strip --prefix=/usr/local -Ddebug=false --buildtype=plain \
-Dfish-completions=false -Dzsh-completions=false -Dtray=disabled
##################################
# xwayland: true
# gdk-pixbuf: true
# systemd: false
# elogind: false
# tray: false # tray depends on systemd according to meson.build
# man-pages: true
###################
meson configure # SNIP show some outputs
###############
# debug: false
# strip: true
# fish completions: false
##########################
ninja  # takes seconds
DESTDIR=/tmp/$P ninja install
cd /tmp

# doc
#####
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/man $P-doc/usr/local/share/
mkdir -p $P-doc/usr/local/share/doc/$P
cp $SRC/LICENSE  $P-doc/usr/local/share/doc/$P/

# 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|alacritty|sakura|' -i $CONF
echo '# added by aus9
# xwayland disable
exec sakura
exec leafpad ~/.config/sway/help.txt' >> $CONF

# setuid is used to avoid elogind, as I could not get it 
# working correctly to enable tray support
chmod a+s $P/usr/local/bin/$P

# install script for sway now in sway-dot

# TCZ them
#######
LIST="$P $P-doc "
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-doc.tcz 
Description:    docs for Sway
Version:        1.4
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           52K		
Extension_by:   aus9 
Tags:           wayland man
Comments:       $ man sway (= $ man sway.1) for the compositor
                while $ man sway.5 is for the config file

                For non-US keyboard layouts $ man sway-input.5

                One man page refers to man xkeyboard-config
                TC does not appear to have instead try
                https://linux.die.net/man/7/xkeyboard-config

Change-log:     2019/05/30 Original 1.0 on 10x 
Current:        2020/07/11 -> 1.4 on 11x
' > $P-doc.tcz.info

echo 'Title:          sway.tcz 
Description:    Tiling desktop for Wayland
Version:        1.4
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           5.1M		
Extension_by:   aus9 
Tags:           wayland sway
Comments:       You must NOT load sway on a libX or Xorg desktop as 
                your desktop "should" crash...... Online wiki is here
                https://github.com/swaywm/sway/wiki
                Note the wiki also mentions polkit matters. I have US keyboard,  
                read the sway-doc for other inputs please.

                This wayland desktop might suit those who do not mind 
                reading the config and using keyboard combinations (kb combos) 
                to launch their fav apps. Kb combos tend to be faster than mouse, 
                but you may need a mouse to exit sway.

                If you change ~/.config/sway/config,  run from within sway
                $ sway -C (to check its correct)

                FIRST TIME USE
                ###############
                Download this App and sway-dot, then load sway-dot
                $ tce-load -i sway-dot
                Install script also adds memory.txt to config dir. 

                VIDEO DRIVERS
                #############
                Closed source drivers are not supported by sway. 
                 
                How to start Sway
                ###########
                Exit to console prompt and run
                $  z                 

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

                If you do not need a mouse, edit your home config line so it reads
                bindsym $mod+Shift+e exec 'swaymsg exit'

                Optional dependencies
                ##################
                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)
                leafpad can be removed from dep file if you prefer something else.
                Ditto sakura but make sure you modify and test config please.
          
                quick guide to change resolution and speed
                ######################################
                $ swaymsg -t get_outputs
                (look at output  ---mine defaults to highest  resolution @ 60Hz
                and modify the config file with my example showing)
                output HDMI-A-2 mode 1280x1024@75Hz
                (exit sway and re-start it then repeat get outputs command)

                Other info
                ########
                I recommend not removing # against xwayland line until you know
                what will be the consequences for any non-wayland supported app.
                xwayland allows most apps to run on wayland.
                
                How I use sway  YMMV
                Method A: You can "auto" launch more apps on WS 1 instead of creating 
                keybindings and then use move kb combos to move them to preferred 
                WS numbers eg I use a specific workspace for terminal, another 
                for web browser etc. 

                Method B: use kb combo to create and move focus to WS 2 
                = logo +2 ...use config "created" kb keybinding to launch a TCE
                then move to workspace 3...and launch your fav TCE
                IMHO Method A is faster. 

                I recommend you remove wbar, from boot list, if you like sway.
                  
Change-log:     2019/05/30 Original 1.0 on 10x 
Current:        2020/07/11 -> 1.4 on 11x install script moved to sway-dot
' > $P.tcz.info

# libva loads Xorg 3d but not the graphics kernel
echo 'wlroots.tcz
cairo.tcz
sakura.tcz
dbus.tcz
libva2.tcz
graphics-5.4.3-tinycore64.tcz
json-c13.tcz 
leafpad.tcz ' > $P.tcz.dep
echo 'man-db.tcz' > $P-doc.tcz.dep

submitqc --libs

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