#!/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 -fno-exceptions -fno-rtti"

P=swaylock
V=1.8.6
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake meson wayland-dev wayland-protocols-dev  \
libxkbcommon-dev cairo-dev gdk-pixbuf2-dev scdoc "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/swaywm/$P/releases/download/v$V/$SRC.tar.gz
tar xvf $SRC*gz
cd $SRC
mkdir build && cd build
meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dbuildtype=plain -Dman-pages=enabled \
 -Dfish-completions=false -Dpam=disabled -Dgdk-pixbuf=enabled -Dzsh-completions=false 

## ../meson.build:118: WARNING: The swaylock binary must be setuid when compiled without libpam
# Run-time dependency bash-completion found: NO

# buildtype       : plain
# debug           : false
# fish-completions: false
# gdk-pixbuf      : enabled
# man-pages       : enabled
# pam             : disabled
# prefix          : /usr/local
# strip           : true
# zsh-completions : false
meson configure
ninja  # 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

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

mkdir -p $P/usr/local/share/$P/files
mv $P/usr/local/bin/$P $P/usr/local/share/$P/files
rm -rf $P/usr/local/bin
 
# tce.install make suid obvious to user
###########
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
P=swaylock
FILE=/tmp/tcloop/$P/usr/local/share/$P/files/$P
# as we have disabled pam we need SUID on exe
cp $FILE /usr/local/bin/
chmod a+s /usr/local/bin/$P ' > $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 $P-doc"
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          swaylock-doc.tcz 
Description:    screen locker for wayland 
Version:        1.8.6
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/swaylock
Copying-policy: Accompanied 
Size:           4.0K		
Extension_by:   aus9 @linuxquestions.org
Tags:           wayland swaylock
Comments:       -

Change-log:     2024/11/23 v 1.8.0 on 15x 
Current:        2026/10/24 v 1.8.6 on 17x' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          swaylock.tcz 
Description:    screen locker for wayland 
Version:        1.8.6
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/swaylock
Copying-policy: Accompanied 
Size:           28K		
Extension_by:   aus9 @linuxquestions.org
Tags:           wayland swaylock
Comments:       Create a local login password (not root password). 
                You may like to persist the password over reboot? 
                add etc/passwd to your backup file
                --but swaylock works with current session password too 

                To LOCK
                $ swaylock -fl # (thats) hyphen foxtrot lima
                which daemonises the locker and locks screen.
                Swaylock prompts if you have caps lock on. It will not prompt 
                if you are using Shift key to input one or more letters in caps

                To UNLOCK
                <type passwd> You will see a circle appear and a change in indicator
                upon each press.......When you have typed full password, press Enter
                https://imgur.com/gkEmvF7
                green indicator shows correct input for each letter
                red indicator at least one error in input of password
                If you mistype ---you can try again

                TCE=swaylock-doc has loads of options if interested
                and bash-completion TCE might work (untested by me) 

Change-log:     2019/06/17 v 1.4 on 10x 
                2020/06/29 v 1.5 on 11x  
                2023/04/22 v 1.7.2 on 14x add install script for SUID
                2024/11/23 v 1.8.0 on 15x
Current:        2026/10/24 v 1.8.6 on 17x    ' > $P.tcz.info

readelf -d $P/usr/local/share/$P/files/$P | grep 'NEEDED' # exclude TCBs
# [libcairo.so.2]          cairo
# [libgdk_pixbuf-2.0.so.0] gdk-pixbuf2
# [libgobject-2.0.so.0]    glib2 cairo
# [libxkbcommon.so.0]     libxkbcommon
# [libwayland-client.so.0] wayland

echo 'cairo.tcz
gdk-pixbuf2.tcz
libxkbcommon.tcz
wayland.tcz       ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync
