#!/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.9.2
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  

# meson configure # to show settings as I could have used
# libseat-seatd  : YES
# libseat-builtin: NO
# libseat-systemd: NO
# libseat-elogind: NO
# server         : YES
# man-pages      : YES
# -----User defined options
# debug          : false
# examples       : disabled
# libseat-logind : disabled
# man-pages      : enabled
# prefix         : /usr/local
# server         : enabled
# strip          : true
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
######
FILES=/usr/local/share/seatd/files
mkdir -p $P/usr/local/share/doc/$P  $P$FILES
cp $SRC/LICENSE $P/usr/local/share/doc/$P/
mv $P/usr/local/bin/* $P$FILES
rm -rf $P/usr/local/bin

# tce.installed  script
#########################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# do not link to /tmp/tcloop for copy2fs members
SRC=/usr/local/share/seatd/files
FILE1=/usr/local/bin/seatd
FILE2=/usr/local/bin/seatd-launch
[ -f $FILE1 ] || sudo cp $SRC/seatd        /usr/local/bin
[ -f $FILE2 ] || sudo cp $SRC/seatd-launch /usr/local/bin
chmod a+s /usr/local/bin/seatd*
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod 775  -R $P/usr/local/tce.installed

# 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 . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          seatd-dev.tcz 
Description:    dev files
Version:        0.9.2
Author:         Kenny Levinsen
Original-site:  https://git.sr.ht/~kennylevinsen/seatd/refs
Copying-policy: Accompanied 
Size:           4.0K	
Extension_by:   aus9 
Tags:           seat sway weston labwc wayland woodland
Comments:       Development files

Change-log:     2023/04/21 v 0.7.0 on 14x
                2024/04/18 v 0.8.0 suid for both exes on 15x  
                2025/02/19 v 0.9.1 start with a fresh socket + suid by install script on 16x 
Current:        2026/01/27 v 0.9.2 modify install script on 16x    ' > $P-dev.tcz.info
echo 'seatd.tcz
scdoc-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:          seatd-doc.tcz 
Description:    dev files
Version:        0.9.2
Author:         Kenny Levinsen
Original-site:  https://git.sr.ht/~kennylevinsen/seatd/refs
Copying-policy: Accompanied 
Size:           4.0K	
Extension_by:   aus9 
Tags:           seat sway weston labwc wayland woodland
Comments:       man pages

Change-log:     2023/04/21 v 0.7.0 on 14x
                2024/04/18 v 0.8.0 suid for both exes on 15x  
                2025/02/19 v 0.9.1 start with a fresh socket + suid by install script on 16x 
Current:        2026/01/27 v 0.9.2 modify install script on 16x    ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

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

                If you see ../run/seatd.sock, refusing to start... run 
                $ sudo rm -rf /run/seatd.sock 
                mainly if you swap wayland desktops

                Assuming members need dbus for eg firefox
                No change to how members launch woodland or sway which is
                $ wood # (seatd-launch dbus-run-session woodland)
                $ sw #   (seatd-launch dbus-run-session sway) 
                $ seatd-launch dbus-run-session weston
                $ seatd-launch dbus-run-session labwc -s <terminal-name> 
               
Change-log:     2023/04/21 v 0.7.0 on 14x
                2024/04/18 v 0.8.0 suid for both exes on 15x  
                2025/02/19 v 0.9.1 start with a fresh socket + suid by install script on 16x 
Current:        2026/01/27 v 0.9.2 modify install script on 16x for copy2fs members ' > $P.tcz.info

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

submitqc --libs
rm -rf *.zsync


