#!/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.7.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc meson wayland-dev wayland-protocols  \
libxkbcommon-dev cairo-dev gdk-pixbuf2-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/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=disabled \
 -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
# prefix          : /usr/local
# strip           : true
# fish-completions: false
# gdk-pixbuf      : enabled
# man-pages       : disabled
# pam             : disabled
# zsh-completions : false
meson configure
ninja  # seconds
DESTDIR=/tmp/$P ninja install
cd /tmp

# 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 "
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:          swaylock.tcz 
Description:    screen locker for wayland 
Version:        1.4
Author:         Drew DeVault
Original-site:  https://github.com/swaywm/swaylock
Copying-policy: Accompanied 
Size:           32K		
Extension_by:   aus9 
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 -f
                which daemonises the locker and locks screen

                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
                            
Change-log:     2019/06/17 v 1.4 on 10x 
                2020/06/29 v 1.5 on 11x  
Current:        2023/04/22 v 1.7.2 on 14x add install script for SUID    ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
# [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
# [libcrypt.so.1] TCB
# [libc.so.6]      "

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

submitqc --libs
