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

CFLAGS="-mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti" 

P=sway
V=1.0
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 
# --I brute delete system files for elogind---reboot to clear up my fixs to test app

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 -Dtray=disabled --buildtype=plain -Dfish-completions=false -Dzsh-completions=false
meson configure # to show options
##############################  
# xwayland: true
# gdk-pixbuf: true
# systemd: false
# elogind: false
# tray: false
# man-pages: true
# WARNING: The sway binary must be setuid when compiled without (e)logind
# WARNING: You must do this manually post-install: chmod a+s /path/to/sway
################################################################
ninja 
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/

# I prefer lxterminal  and I will auto launch it on workspace 1, saves members one kb combo
CONF=$P/usr/local/etc/$P/config
sed 's|urxvt|lxterminal|' -i $CONF
echo '################
# xwayland disable
exec lxterminal
exec apps' >> $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

tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat > $P/usr/local/tce.installed/$P << 'EOF'
#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
RDIR=/run/user/1001
LOOP=/tmp/tcloop/sway/usr/local/etc/sway/config
HC=/home/$USER/.config/sway

# create home config for sway to find
if  [ ! -f $HC/config ] ; then
    if  [ ! -d $HC ] ; then
        mkdir -p $HC
    fi
   cp $LOOP $HC/
   chmod 644 $HC/config
   chown -R $USER:staff $HC
fi

# create run dir auto start of dbus via z command
[ -d $RDIR ] || ( mkdir -p $RDIR && chown $USER:staff $RDIR && chmod 700 $RDIR)
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# 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

# libva loads Xorg 3d but not the graphics kernel
echo 'wlroots.tcz
cairo.tcz
lxterminal.tcz
dbus.tcz
libva.tcz
graphics-KERNEL.tcz
json-c13.tcz  ' > $P.tcz.dep

echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          sway.tcz TESTING
Description:    i3 Tiling compatible compositor for Wayland
Version:        1.0
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           5.1M		
Extension_by:   aus9 at gmx dot com
Tags:           wayland sway
Comments:       You must NOT load sway on a libX or Xorg desktop as 
                your desktop will 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 compositor might suit members 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.

                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 run sway-dot
                to create a dot local bin file called z -which you can edit if needed.
                $ tce-load -i sway-dot  

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

                Running Sway
                ############
                config has list of current kb combos and other info
                You can change the main modifier key--Note the capitals where applicable
                logo key = Mod4  (our default and defined elsewhere as $mod)
                Alt key = Mod1
                Ctrl key = Control 
                Shift key = Shift 

                Create and move to new workspace (WS) by (eg) logo + 2 (or some other number)
                (The + sign means you hold down the logo key and press 2)

                Open a terminal by logo + Enter. One has been auto started on WS 1
                Repeating logo and enter will tile second terminal....and so on etc.
                Tiling occurs for all apps too if you duplicate the kb combos.

                Except when dmenu/bemenu (optional deps) are active, your active app
                has a bold title bar. Start dmenu by logo + d
               
                Quit active App (executable) by (config shows it as focused window)
                logo + shift + q

                Exitting sway----- to get back to X(org) DE/WM 
                #############
                logo + Shift + e 
                Use mouse to click correct button
                $ startx

                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)
                bemenu and j4-dmenu-desktop (can run with xwayland disabled)
                wl-clipboard (terminal clipboard manager)
                wf-recorder (screen recorder--no sound)
                
                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
                ########
                Tray icon support has been disabled this build. 
                Bottom of file has some non-standard configs below the ###s
                # xwayland disable  
                exec lxterminal
                exec apps

                ^^ remove the comment if you are confident using commands
                to maintain your system without FLTK GUIs
              
                You can 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 local terminal, another for web browser etc
                Remember your "move" kb combo actions the ACTIVE titlebar app

                Alternatively try at WS 1....use kb combo to -- create and move -- to workspace 2 
                = logo +2 ...use config "created" kb keybinding  to launch your fav app
                repeat to create and move to workspace 3...etc.etc
                IMHO my first way is faster. 

                I recommend you remove wbar and add sway then reboot to test sway.
                Otherwise you will need to kill wbar if you load a new package on sway.
                Remember sway will not auto launch over reboot, it needs to exit to prompt 
                and run z command. 
                  
                Compiled for 10.x 
Change-log:     2019/05/30 first version 
Current:        2019/05/30  ' > $P.tcz.info

echo 'Title:          sway-doc.tcz 
Description:    docs for Sway
Version:        1.0
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/sway
Copying-policy: Accompanied 
Size:           40K		
Extension_by:   aus9 at gmx dot com
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

                Compiled for 10.x 
Change-log:     2019/05/30 first version 
Current:        2019/05/30  ' > $P-doc.tcz.info

submitqc --libs
