#!/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 -std=gnu99"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=swayimg
V=4.6
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git meson cmake fontconfig-dev libjpeg-turbo-dev libwebp1-dev \
libxkbcommon-dev scdoc-dev wayland-protocols"    
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/artemsen/$P/archive/refs/tags/v$V.tar.gz \
https://raw.githubusercontent.com/artemsen/swayimg/refs/heads/master/extra/swayimgrc
tar xvf v*gz
cd $SRC
mkdir build && cd build
meson setup -Dbash=disabled -Ddesktop=false -Ddrm=disabled -Dgif=disabled -Djpeg=enabled -Dman=true  \
-Dpng=enabled -Dtiff=enabled -Dwayland=enabled -Dwebp=enabled -Dzsh=disabled 
# bash   : disabled
# desktop: false
# drm    : disabled
# gif    : disabled
# jpeg   : enabled
# man    : true
# png    : enabled
# tiff   : enabled
# wayland: enabled
# webp   : enabled
# zsh    : disabled
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
######
mkdir -p $P/usr/local/share/doc/$P $P/usr/local/share/$P/files
cp $SRC/LICENSE  $P/usr/local/share/doc/$P/
strip --strip-unneeded $P/usr/local/bin/$P

# create rc file for install script
chmod 644 swayimgrc
mv swayimgrc $P/usr/local/share/$P/files/

# tce.install
############# 
mkdir -p $P/usr/local/tce.installed 
echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
DIR=/home/$USER/.config/swayimg && SRC=/usr/local/share/swayimg/files/swayimgrc
[ -d $DIR ] || mkdir -p $DIR
if [ ! -f $DIR/swayimgrc ] ; then
   cp $SRC $DIR/
fi
chown -R $USER:staff $DIR # do the lot in one hit   ' > $P/usr/local/tce.installed/$P 
chown -R root:staff $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 . -name Makefile -type f -exec sed -i 's/-g -O2//g' {} \;
	cd /tmp
done

ls -hal

echo 'Title:          swayimg-doc.tcz 
Description:    man page for exe and the rc file
Version:        4.6
Author:         Artem Senichev 
Original-site:  https://github.com/artemsen/swayimg
Copying-policy: Accompanied 
Size:           12K		
Extension_by:   aus9 @linuxquestions.org
Tags:           wayland image viewer
Comments:       To view rc file run
                $ man swayimgrc.5

Change-log:     2025/12/04 v 4.6 on 16x
Current:        2025/12/04  ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          swayimg.tcz 
Description:    image viewer for wayland compositors
Version:        4.6
Author:         Artem Senichev 
Original-site:  https://github.com/artemsen/swayimg
Copying-policy: Accompanied 
Size:           80K		
Extension_by:   aus9 @linuxquestions.org
Tags:           wayland image viewer slideshow
Comments:       To reduce dependencies only supports
                jpg (jpeg) png tiff webp
                Some settings can be changed in 
                ~/.config/swayimg/swayimgrc
                Please read as lots of kb controls
                and the man pages have lots of info

                Main use - viewer or slideshow
                external image viewing is fragile YMMV

                swayimg -h offers full list
                cd <image-dir>
                swayimg -l # slideshow ~ 3 seconds (changeable)
                swayimg -g # gallery is slow to load on my AMD APU

example of gallery mode gets messy with more images
https://raw.githubusercontent.com/artemsen/swayimg/master/.github/gallery.png

wget --no-check-certificate <image-link> | swayimg
                  
Change-log:     2025/12/04 v 4.6 on 16x
Current:        2025/12/04  ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' 
#libwayland-client.so.0] wayland labwc woodland weston but submit will add it if I leave it out etc
#libxkbcommon.so.0] libxkbcommon
#libfontconfig.so.1] fontconfig
#libfreetype.so.6] freetype fontconfig
#libjpeg.so.62] libjpeg-turbo libtiff
#libpng16.so.16] libpng fontconfig
#libtiff.so.5] libtiff  
#libwebp.so.7] libwebpdemux.so.2] libwebp1


echo 'wayland.tcz
libxkbcommon.tcz
fontconfig.tcz
libtiff.tcz
libwebp1.tcz        ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

