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

P=sane
V=1.3.1
SRC=sane-backends-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc avahi-dev  poppler23-dev libgphoto2-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://gitlab.com/sane-project/backends/uploads/83bdbb6c9a115184c2d48f1fdc6847db/sane-backends-$V.tar.gz  " $USER
tar xvf sane*gz
cd $SRC
./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-libcurl --with-pic  --with-usb

# 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
#Lockfiles:            /var/lock
#-> Network parameters:
#Build saned:      yes
#IPv6 support:    yes
#Avahi support:  yes
#cURL support:  yes
#POPPLER_GLIB support:  yes
#SNMP support:  no
# 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 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 # 45seconds
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 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

# empty dirs found
rm -rf $P/usr/local/share/$P

# submitqc finds perms
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.3.1
Author:         see sane-doc AUTHORS
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           4.6M
Extension_by:   aus9 @linuxquestions.org
Tags:           scan scanner
Comments:       There is no libsane-dev after update
                replaced by sane-dev
               
Change-log:     2013/12/09 v 1.0.24 (Juanito)
Current:        2025/02/23   v 1.3.1 on 16x (aus9)   ' > 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
# libtiff.so.5 libtiff  libpoppler23
#libavahi-common.so.3]#libavahi-client.so.3] libavahi
#libpng16.so.16] libpng 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.3.1
Author:         see sane-doc AUTHORS
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           12K
Extension_by:   aus9 @linuxquestions.org
Tags:           scan scanner
Comments:       Development replaces libsane-dev
               
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2025/02/23      ' > $P-dev.tcz.info
echo 'sane.tcz
avahi-dev.tcz  
libpoppler23-dev.tcz
libgphoto2-dev.tcz' >   $P-dev.tcz.dep

echo 'Title:          sane-doc.tcz 
Description:    man pages and important info
Version:        1.3.1
Author:         see sane-doc AUTHORS
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           796K
Extension_by:   aus9 @linuxquestions.org
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:        2025/02/23      ' > $P-doc.tcz.info
echo 'man-db.tcz' >   $P-doc.tcz.dep

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

cat >> $P.tcz.info <<'EOF'
Title:          sane.tcz 
Description:    Scanner Access Now Easy
Version:        1.3.1
Author:         see sane-doc AUTHORS
Original-site:  https://gitlab.com/sane-project/backends
Copying-policy: GPL v 2
Size:           204K
Extension_by:   aus9 @linuxquestions.org
Tags:           scan scanner 
Comments:       For scanners and some cameras
                You must read http://www.sane-project.org/sane-mfgs.html
                and heed any damage warnings. Then read the doc TCE
                and heed warnings about danger of running as root.
                But I tested as local user.
               
                scanimage is the 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

                 sane-frontends eg xscanimage is 14 years old so not built
                             
Change-log:     2025/02/23 v 1.3.1 on 16x
Current:        2025/02/23      
EOF

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # excluding libsane dependencies 
#libsane.so.1] .......libsane
echo 'libsane.tcz' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

# submitqc gets confused with multiple libpoppler*...redo my dep
echo 'libgphoto2.tcz
libpoppler23.tcz
libavahi.tcz ' > libsane.tcz.dep

# changes order to sane-dev.....not a problem




