#!/bin/sh
# 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"

P=seatd
V=0.7.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=disabled \
-Dexamples=disabled -Dlibseat-logind=disabled -Dserver=enabled -Dexamples=disabled  
# to show settings as I could have used
meson configure 
######################################## 
# 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      : disabled
# 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

# no doc  
####

# 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 consoloe output.....I prefer to do it by install script

# tce.install
###########
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# force SUID onto seatd-launch but not seatd
FILE=/usr/local/bin/seatd-launch
rm -rf $FILE
LOOP=/tmp/tcloop/seatd$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-dev "
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 
Tags:           seat
Comments:       Development files

Change-log:     2023/04/21 Original 0.7.0 on 14x
Current:        2023/04/21  
' > $P-dev.tcz.info

echo 'seatd.tcz
scdoc-dev.tcz ' > $P-dev.tcz.dep


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

                seatd-launch has SUID set and is used by 
                $HOME/.local/bin/z
                
Change-log:     2023/04/21 Original 0.7.0 on 14x
Current:        2023/04/21 
  ' > $P.tcz.info

readelf -d $P/usr/local/lib/libseat.so.1 | grep 'NEEDED'
#[libc.so.6] TCB....ditto usr/local/bin seatd

# no main dep

submitqc --libs