#!/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.0
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
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/swaywm/$P/releases/download/v$V/$SRC.tar.gz " $USER
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
# buildtype       : plain
# debug           : false
# fish-completions: false
# gdk-pixbuf      : enabled
# man-pages       : disabled
# 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
######
# pam disabled remove its file
rm -rf $P/usr/local/etc

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

# tce.install make suid obvious to user
###########
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
FILE=/usr/local/bin/swaylock
rm -rf $FILE
LOOP=/tmp/tcloop/swaylock$FILE
cp $LOOP /usr/local/bin/
chmod a+s $FILE
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 . -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.0
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:        2024/11/23 ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          swaylock.tcz 
Description:    screen locker for wayland 
Version:        1.8.0
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/swaylock
Copying-policy: Accompanied 
Size:           28K		
Extension_by:   aus9 @linuxquestions.org
Tags:           wayland swaylock
Comments:       Before using you will need to create a password for 
                local login. You may like to persist the password files 
                over reboot? etc/passwd and etc/shadow 

                To LOCK
                $ swaylock -fl # l for lima 1
                which daemonises the locker and locks screen
                And 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=sway has z file which uses shadow for password 
                management. Other Wayland desktops may not use shadow? 
                TCE=swaylock-doc has loads of options if interested            
                            
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
Current:        2024/11/23 v 1.8.0 on 15x    ' > $P.tcz.info

readelf -d $P/usr/local/bin/$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


# shadow is loaded by z executable to launch sway
echo 'cairo.tcz
gdk-pixbuf2.tcz
libxkbcommon.tcz
wayland.tcz       ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync
