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

P1=simple-scan
P=$P1-gtk4
V=49.1
SRC=$P1-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake meson vala-dev libhandy1-dev libwebp1-dev itstool gtk4-dev libadwaita-dev imagemagick " 
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://download.gnome.org/sources/$P/49/$P1-$V.tar.xz
tar xvf $P*
cd $SRC
mkdir build && cd build
# used -Dauto_features=enabled initially
PREFIX=/usr/local
meson setup --prefix=$PREFIX --localstatedir=/var --buildtype=plain --default-library=shared --strip ../
# User defined options
# buildtype      : plain
# default_library: shared
# localstatedir  : /var
# prefix         : /usr/local
# strip          : true
ninja # 6 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/

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share
# move all help here then move english c back
mv $P/usr/local/share/help $P-locale/usr/local/share
mkdir -p $P/usr/local/share/help
mv $P-locale/usr/local/share/help/C $P/usr/local/share/help/
mv $P-locale/usr/local/share/help/en_GB $P/usr/local/share/help/

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v 3' > $P/usr/local/share/doc/$P/COPYING

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat << EOF > $P/usr/local/tce.installed/$P 
#!/bin/sh
glib-compile-schemas /usr/local/share/glib-2.0/schemas
gtk-update-icon-cache -q -f -t /usr/local/share/icons/hicolor
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# deskop stuff
##############
mkdir -p $P/usr/local/share/pixmaps
OLDPNG=$P/usr/local/share/icons/hicolor/scalable/apps/org.gnome.SimpleScan.svg
NEWPNG=$P/usr/local/share/pixmaps/$P.png
convert $OLDPNG -scale 48x48 $NEWPNG
DESK1=$P/usr/local/share/applications/org.gnome.SimpleScan.desktop
DESK=$P/usr/local/share/applications/$P.desktop
mv $DESK1 $DESK
# desktop says "# Translators: Do NOT translate or transliterate this text (this is an icon file name)!"
# but we are not translating we are simplifying
sed '164d' -i $DESK 
sed 's|org.gnome.SimpleScan|simple-scan-gtk4|' -i $DESK
sed 's|Scanning;|Scanning;Applications|' -i $DESK
echo 'X-FullPathIcon=/usr/local/share/pixmaps/simple-scan-gtk4.png' >> $DESK

# TCZ them
#######
LIST="$P $P-doc $P-locale "
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:          simple-scan-gtk4-doc.tcz 
Description:    man page in English only 
Version:        49.1
Author:         Robert Ancell & contributors
Original-site:  https://download.gnome.org/sources/simple-scan
Copying-policy: GPL v 3 
Size:           4.0K
Extension_by:   aus9 
Tags:           scanner scan gtk4 gnome
Comments:       -

Change-log:     2026/01/21 v 49.1 on 17x
Current:        2026/01/21    ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          simple-scan-gtk4-locale.tcz 
Description:    language files + non-EN help files
Version:        49.1
Author:         Robert Ancell & contributors
Original-site:  https://download.gnome.org/sources/simple-scan
Copying-policy: GPL v 3 
Size:           1.6M
Extension_by:   aus9 
Tags:           scanner scan gtk4 gnome
Comments:       This TCE has translations for command +
                for help. Main TCE is a dep so its safe
                to have only this TCE in your bootlist
                if you frequently scan

                For usb cable to PC run 
                $ simple-scan 
                For networked scanner read TCE=simple-scan please
                For further setup click various buttons. Your configs are saved
                ~/.config/simple-scan/state # delete to start afresh
                You may need to change paper size eg US Letter to A4 etc
                                
                If you see msg= Additional software required, you need
                to load sane which has more configs please. Next GUI says 
                searching for scanners and then you must see
                "Ready to Scan" With luck it will name your scanner
                When ready, click the scan button, saves to ~/Downloads

                If scanner not found, look at the output to 
                $ scanimage -L # DO NOT RUN AS ROOT please
                clicking F1 for help FAILS instead point your web browser to link
                /usr/local/share/help/<your country iso code>....they are xml text files

Change-log:     2026/01/21 v 49.1 on 17x
Current:        2026/01/21    ' > $P.tcz.info
echo 'simple-scan-gtk4.tcz ' > $P-locale.tcz.dep


echo 'Title:          simple-scan-gtk4.tcz 
Description:    gtk4 frontend to sane scanner 
Version:        49.1
Author:         Robert Ancell & contributors
Original-site:  https://download.gnome.org/sources/simple-scan
Copying-policy: GPL v 3 
Size:           312K
Extension_by:   aus9 
Tags:           scanner scan gtk4 gnome
Comments:       May conflict with simple-scan. Contains help files 
                for C lang and British English language. Non-EN help
                is in the locale TCE. 

                For usb cable to PC run 
                $ simple-scan 
                For networked scanner read TCE=simple-scan please
                For further setup click various buttons. Your configs are saved
                ~/.config/simple-scan/state # delete to start afresh
                You may need to change paper size eg US Letter to A4 etc
                                
                If you see msg= Additional software required, you need
                to load sane which has more configs please. Next GUI says 
                searching for scanners and then you must see
                "Ready to Scan" With luck it will name your scanner
                When ready, click the scan button, saves to ~/Downloads

                If scanner not found, look at the output to 
                $ scanimage -L # DO NOT RUN AS ROOT please
                clicking F1 for help FAILS instead point your web browser to link
                /usr/local/share/help/en_GB....they are xml text files

Change-log:     2026/01/21 v 49.1 on 17x
Current:        2026/01/21    ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P1 | grep 'NEEDED'
#libglib-2.0.so.0]libgio-2.0.so.0]libgobject-2.0.so.0]glib2 libadwaita
#libgtk-4.so.1]gtk4 libadwaita
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 libadwaita
#libcairo.so.2]cairo libadwaita
#libadwaita-1.so.0]libadwaita
#libgusb.so.2]libgusb libadwaita
#libsane.so.1]libsane libadwaita
#libcolord.so.2]colord libadwaita
#libwebp.so.7]#libwebpmux.so.3]libwebp1 libadwaita
# also needs svg -> libadwaita
# also needs gtk-update-icon-cache
echo 'libadwaita.tcz
gtk-update-icon-cache.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync




