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

# error: ‘...’ handler must be the last handler for its try block [-fpermissive]
export CFLAGS="-mtune=generic -Os -pipe"  
export CXXFLAGS="-mtune=generic -Os -pipe -fpermissive "

P=sane
V=1.4.0
SRC=backends-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc automake autoconf-archive gettext-dev avahi-dev poppler23-dev libgphoto2-dev  "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
URL=https://gitlab.com/sane-project/backends/-/archive
/usr/local/bin/wget -nc --no-check-certificate \
$URL/$V/backends-$V.tar.bz2 
tar jxvf backends*
cd $SRC
sh autogen.sh
sh autogen.sh autoupdate

./configure --prefix=/usr/local  --localstatedir=/var --sbindir=/usr/local/bin \
--docdir="/usr/local/share/doc/$P" --sysconfdir=/usr/local/etc --without-systemd \
--disable-rpath --enable-pthread --with-avahi --with-pic  --with-usb

# -> Installation directories:
#Configuration: /usr/local/etc
#Libraries:     /usr/local/lib
#Binaries:      /usr/local/bin and /usr/local/bin
#Manpages:      /usr/local/share/man
#Documentation: /usr/local/share/doc/sane
#Lockfiles:     /var/lock
##-> Network parameters:
#Build saned:   yes
#IPv6 support:  no
#Avahi support: yes
#cURL support:  no
#POPPLER_GLIB support:  yes
#SNMP support:  no
#-> The following backends will be built:
# abaton agfafocus apple artec artec_eplus48u as6e avision bh canon canon630u canon_dr canon_lide70 
#cardscan coolscan coolscan2 coolscan3 dc25 dc210 dc240 dell1600n_net dmc epjitsu epson epson2 epsonds 
# escl fujitsu genesys gphoto2 gt68xx hp hp3500 hp3900 hp4200 hp5400 hp5590 hpljm1005 hs2p ibm kodak 
#kodakaio kvs1025 kvs20xx kvs40xx leo lexmark lexmark_x2600 ma1509 magicolor matsushita microtek microtek2 
# mustek mustek_usb mustek_usb2 nec net niash pie pieusb pixma plustek plustek_pp qcam ricoh ricoh2 rts8891
#  s9036 sceptre sharp sm3600 sm3840 snapscan sp15c st400 stv680 tamarack teco1 teco2 teco3 test u12 umax
# umax_pp umax1220u xerox_mfp p5 

find . -name Makefile -type f -exec sed -i 's/-g -O2//g' {} \;
make -j4 # 32seconds
make install-strip DESTDIR=/tmp/$P
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/
rm -rf $P/usr/local/lib/*.la 
rm -rf $P/usr/local/lib/$P/*.la

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# doc
######### LICENSE is info about why they chose GPL
mkdir -p $P-doc/usr/local/share/
mv $P/usr/local/share/doc $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share
cd $P-doc/usr/local/share/doc/$P
rm -rf README* ChangeLog* LICENSE PROJECTS backend-writing.txt NEWS
echo 'backends will be built:
abaton agfafocus apple artec artec_eplus48u as6e avision bh canon canon630u canon_dr canon_lide70 
cardscan coolscan coolscan2 coolscan3 dc25 dc210 dc240 dell1600n_net dmc epjitsu epson epson2 epsonds 
escl fujitsu genesys gphoto2 gt68xx hp hp3500 hp3900 hp4200 hp5400 hp5590 hpljm1005 hs2p ibm kodak 
kodakaio kvs1025 kvs20xx kvs40xx leo lexmark lexmark_x2600 ma1509 magicolor matsushita microtek microtek2 
mustek mustek_usb mustek_usb2 nec net niash pie pieusb pixma plustek plustek_pp qcam ricoh ricoh2 rts8891
s9036 sceptre sharp sm3600 sm3840 snapscan sp15c st400 stv680 tamarack teco1 teco2 teco3 test u12 umax
umax_pp umax1220u xerox_mfp p5  ' > list-of-compiled-scanners.txt
cd /tmp

# libsane
########
mkdir -p libsane/usr/local/
mv $P/usr/local/lib libsane/usr/local/

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v 2 ' > $P/usr/local/share/doc/$P/COPYING
chmod 755 $P/var/lock


# TCZ them
#######
LIST="$P $P-doc $P-locale $P-dev libsane"
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:          libsane.tcz 
Description:    lib files for sane
Version:        1.4.0
Author:         https://gitlab.com/sane-project/backends/-/blob/master/AUTHORS?ref_type=heads
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           4.6M
Extension_by:   aus9 
Tags:           scan scanner
Comments:       lib files 
               
Change-log:     2013/12/09 v 1.0.24 (Juanito)
                2025/02/23 v 1.3.1 on 16x (aus9)
Current:        2026/01/20 v 1.4.0 on 17x   ' > libsane.tcz.info

readelf -d libsane/usr/local/lib/*.so | grep 'NEEDED' 
# libusb-1.0.so.0 libusb  libgphoto2
# libxml2.so.2] libxml2 libgphoto2
readelf -d libsane/usr/local/lib/$P/*.so | grep 'NEEDED'
# libjpeg.so.62] libjpeg-turbo libpoppler23
# libavahi-common.so.3]#libavahi-client.so.3] libavahi
# libpng16.so.16] libpng libpoppler23
# libtiff.so.5 libtiff  libpoppler23
# libpoppler-glib.so.8]libpoppler23
# libgobject-2.0.so.0] [libglib-2.0.so.0] glib2 cairo libpoppler23
# libcairo.so.2] cairo libpoppler23
# libcurl.so.4 curl libpoppler23
# libgphoto2.so.6] [libgphoto2_port.so.12] libgphoto2
echo 'libgphoto2.tcz
libpoppler23.tcz
libavahi.tcz     ' > libsane.tcz.dep

# submit wants glib2-python.tcz libavahi.tcz libgphoto2.tcz  libpoppler07.tcz  libpoppler21.tcz libpoppler23.tcz pcre2.tcz
# then wants to reduce to libgphoto2.tcz libpoppler07.tcz glib2-python.tcz libpoppler23.tcz pcre2.tcz libpoppler21.tcz libavahi.tcz

echo 'Title:          sane-dev.tcz 
Description:    dev files
Version:        1.4.0
Author:         https://gitlab.com/sane-project/backends/-/blob/master/AUTHORS?ref_type=heads
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           12K
Extension_by:   aus9 
Tags:           scan scanner
Comments:       Development
               
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2026/01/20 v 1.4.0 on 17x     ' > $P-dev.tcz.info
echo 'sane.tcz
autoconf-archive.tcz
avahi-dev.tcz  
poppler23-dev.tcz
libgphoto2-dev.tcz' >   $P-dev.tcz.dep
 
echo 'Title:          sane-doc.tcz 
Description:    man pages and important info
Version:        1.4.0
Author:         https://gitlab.com/sane-project/backends/-/blob/master/AUTHORS?ref_type=heads
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           800K
Extension_by:   aus9 
Tags:           scan scanner
Comments:       Contains relevant info in html format
                and important text files such as warning about
                damage and security aspects of use if run as root

                list-of-compiled-scanners.txt might be useful?
               
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2026/01/20 v 1.4.0 on 17x      ' > $P-doc.tcz.info
echo 'man-db.tcz' >   $P-doc.tcz.dep

echo 'Title:          sane-locale.tcz 
Description:    language files
Version:        1.4.0
Author:         see sane-doc AUTHORS
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           932K
Extension_by:   aus9 
Tags:           scan scanner 
Comments:       not tested by me
               
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2026/01/20 v 1.4.0 on 17x      ' > $P-locale.tcz.info
echo 'sane.tcz ' > $P-locale.tcz.dep

echo 'Title:          sane.tcz 
Description:    Scanner Access Now Easy
Version:        1.3.1
Author:         https://gitlab.com/sane-project/backends/-/blob/master/AUTHORS?ref_type=heads
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           204K
Extension_by:   aus9 
Tags:           scan scanner 
Comments:       For scanners and some cameras
                Please read http://www.sane-project.org/sane-mfgs.html
                and heed any damage warnings. Then read the doc TCE
                and heed warnings about danger running as root.
                 
                TCE=simple-scan is the GUI frontend
                scanimage is the CLI frontend see $ man scanimage
                sane-find-scanner  -- useful if no scanner founds
                see /usr/local/etc/sane.d/brand.conf
                They are read only

                saned is the network daemon see $ man saned 
                & /usr/local/etc/sane.d/saned.conf (read only)

                example (I needed no config changes)
                $ scanimage -L
                device pixma:04A9180B_0581C8 is a CANON Canon PIXMA \
                MG3000 Series multi-function peripheral
                $ scanimage --format=jpeg > /tmp/1.jpeg
                $ scanimage --format=jpeg  --resolution 600  > /tmp/2.jpeg
                Note the higher the resolution to longer the delay before
                scan starts.
              
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2026/01/20 v 1.4.0 on 17x ' > $P.tcz.info     
readelf -d $P/usr/local/bin/* | grep 'NEEDED' # excluding libsane dependencies 
#libsane.so.1] libsane
#libusb-1.0.so.0 libsane
#libxml2.so.2 libsane
#libavahi-common.so.3]libavahi-client.so.3]libsane
#libpng16.so.16]libsane
#libjpeg.so.62]libsane
echo 'libsane.tcz' > $P.tcz.dep

# no submitqc checks
