#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti "

P=seatd
V=0.8.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git meson cmake 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://git.sr.ht/~kennylevinsen/$P/archive/$V.tar.gz" $USER
tar xvf $V*gz
cd $SRC
mkdir build && cd build
meson setup --prefix=/usr/local -Dstrip=true -Ddebug=false -Dman-pages=enabled \
-Dexamples=disabled -Dlibseat-logind=disabled -Dserver=enabled -Dexamples=disabled  
# to show settings as I could have used
meson configure 
# seatd 0.8.0
# libseat-seatd  : YES
# libseat-builtin: NO
# libseat-systemd: NO
# libseat-elogind: NO
# server         : YES
# User defined options
# debug          : false
# prefix         : /usr/local
# strip          : true
# examples       : disabled
# libseat-logind : disabled
# man-pages      : enabled
# server         : enabled
###################
ninja # seconds
DESTDIR=/tmp/$P ninja install
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib

# 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/

# altho the website for sway claims SUID still available for sway
# not according to my console output..so I make seatd SUID
chmod a+s $P/usr/local/bin/*

#/tmp/seatd/usr/local/bin$ file *
seatd:        setuid, SNIP stripped
seatd-launch: setuid, SNIP stripped

# TCZ them
#######
LIST="$P $P-dev $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:          seatd-dev.tcz 
Description:    dev files
Version:        0.7.0
Author:         Kenny Levinsen
Original-site:  https://git.sr.ht/~kennylevinsen/seatd/refs
Copying-policy: Accompanied 
Size:           4.0K	
Extension_by:   aus9 @linuxquestions.org
Tags:           seat
Comments:       development files

Change-log:     2023/04/21 v 0.7.0 on 14x
Current:        2024/04/18 v 0.8.0 on 15x   ' > $P-dev.tcz.info
echo 'seatd.tcz
scdoc-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:          seatd-doc.tcz 
Description:    dev files
Version:        0.7.0
Author:         Kenny Levinsen
Original-site:  https://git.sr.ht/~kennylevinsen/seatd/refs
Copying-policy: Accompanied 
Size:           4.0K	
Extension_by:   aus9 @linuxquestions.org
Tags:           seat
Comments:       man pages

Change-log:     2023/04/21 v 0.7.0 on 14x
Current:        2024/04/18 v 0.8.0 on 15x   ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          seatd.tcz 
Description:    seat management daemon
Version:        0.8.0
Author:         Kenny Levinsen
Original-site:  https://git.sr.ht/~kennylevinsen/seatd/refs
Copying-policy: Accompanied 
Size:           36K
Extension_by:   aus9 @linuxquestions.org
Tags:           seat sway
Comments:       minimal seat management daemon, and a universal 
                seat management library

                Socket will be created at /run/seatd.sock
                seatd will normally destroy any exist socket file
                = /run/seatd.sock. If you see it can not change below
                commands to show " -n " before /run/seatd.sock
                
                Both seatd & seatd-launch are now SUID

                Examples
                seatd -g staff /run/seatd.sock & XDG_RUNTIME_DIR=/run/user/$(id -u) weston
                seatd -g staff /run/seatd.sock & XDG_RUNTIME_DIR=/run/user/$(id -u) labwc
               
Change-log:     2023/04/21 v 0.7.0 on 14x
Current:        2024/04/18 v 0.8.0 suid for both exes on 15x       ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # TCBS
readelf -d $P/usr/local/lib/libseat.so.1 | grep 'NEEDED' # same

# no main dep
submitqc --libs
rm -rf *.zsync

# pass1 submitqc changed my SUID ...redo and no submitqc second time

