#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi
# reduced flags due to make error 
export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe "

P=sane
V=git-20201109
SRC=backends
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools zsync wget net-snmp-dev avahi-dev \
gettext-dev libusb-dev libjpeg-turbo-dev tiff-dev curl-dev \
libv4l2-dev libgphoto2-dev git automake bash autoconf-archive \
poppler-dev  "   
    for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "git clone https://gitlab.com/sane-project/backends.git" $USER
mkdir $P
cd $SRC
sh autogen.sh
# takes about 2M
./configure --prefix=/usr/local --disable-nls --with-usb
##################################################################
# Build saned:   yes
# IPv6 support:  yes
# Avahi support: yes
# cURL support:  no
# POPPLER_GLIB support:  yes
# SNMP support:  yes

make -j5 # takes about 5M
make install-strip DESTDIR=/tmp/$P
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib/$P
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 
mv $P/usr/local/lib/$P/*a $P-dev/usr/local/lib/$P/
chmod 644 $P-dev/usr/local/lib/$P/*a

# doc
#####
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/doc $P-doc/usr/local/share
mv $P-doc/usr/local/share/doc/$P-backends $P-doc/usr/local/share/doc/$P
# there is a dir=sane-backends under this, left as is

# main
######
rm -rf $P/usr/local/share/man

# TCZ them
###########
LIST="$P $P-dev $P-doc "
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
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          sane-dev.tcz  
Description:    dev files
Version:        git-20201109
Author:         see doc/AUTHORS
Original-site:  http://sane-project.org/
Copying-policy: see doc TCE
Size:           32K		
Extension_by:   aus9
Tags:           scanner scan backend
Comments:       Dev files.
 
Change-log:     2020/11/10 original v git-20201109 on 12x 
Current:        2020/11/10  '> $P-dev.tcz.info

echo 'Title:          sane-doc.tcz  
Description:    licences and readmes
Version:        git-20201109
Author:         see doc/AUTHORS
Original-site:  http://sane-project.org/
Copying-policy: see doc 
Size:           1.1M		
Extension_by:   aus9
Tags:           scanner scan backend
Comments:       sane created before LGPL created.
                some of the (original) backend authors cannot 
                be contacted any longer, for various reasons, 
                not to mention each and every contributor who 
                sent in a patch. This effectively makes it 
                impossible for the SANE Project to relicense SANE

                Most can be GPL v2 
 
Change-log:     2020/11/10 original v git-20201109 on 12x 
Current:        2020/11/10  '> $P-doc.tcz.info

cat >> $P.tcz.info <<'EOF'
"Title:          sane.tcz  
Description:    Scanner Access Now Easy 
Version:        git-20201109
Author:         see doc/AUTHORS
Original-site:  http://sane-project.org/
Copying-policy: see doc TCE
Size:           4.7M		
Extension_by:   aus9
Tags:           scanner scan backend
Comments:       These are backends, plus a terminal command for scanning

                Avoiding damage on flatbed scanners
                Some flatbed scanners have no protection against 
                exceeding the physical scan area height. If scanner begins 
                making loud noises IMMEDIATELY turn off the scanner.

                Security problems with saned is not intended to be exposed 
                to the internet or other non-trusted networks.  Make sure 
                that access is limited by tcpwrappers and/or a firewall setup. 
                Don't depend only on saned's own authentication.  Don't run saned
                as root if it's not necessary
                
                backends are:
                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 ma1509 magicolor 
                matsushita microtek microtek2 mustek mustek_usb mustek_usb2 
                nec net niash pie pieusb pixma plustek plustek_pp ricoh ricoh2 
                rts8891 s9036 sceptre sharp sm3600 sm3840 snapscan sp15c st400 
                stv680 tamarack teco1 teco2 teco3 test u12 umax umax_pp umax1220u 
                v4l xerox_mfp p5 

                If not using hplip, 
                $ scanimage -o /tmp/scan.jpg
                Tested with usb canon, you may need avahi or net-snmp if you have non-usb?

                If error for above check
                $ scanimage -L # must show a hit SNIP means I truncated output 
                eg device `pixma:04A9180B_0581C8' is a CANON (SNIP)
                Then check configs under /usr/local/etc/sane.d/ 

Change-log:     2020/11/10 original v git-20201109 on 12x 
Current:        2020/11/10  
EOF 

echo 'sane.tcz
net-snmp-dev.tcz 
automake.tcz 
autoconf-archive.tcz
bash.tcz
git.tcz 
gettext-dev.tcz 
libusb-dev.tcz 
avahi-dev.tcz 
libjpeg-turbo-dev.tcz 
tiff-dev.tcz 
curl-dev.tcz
libv4l2-dev.tcz
libgphoto2-dev.tcz
poppler-dev.tcz     ' > $P-dev.tcz.dep

echo 'libusb.tcz
libavahi.tcz 
libtiff.tcz' > $P.tcz.dep

