#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi
# reduced flags
export CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"

P=sane
V=git-20201109
SRC=backends
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools zsync wget net-snmp-dev cups-dev \
gettext-dev libjpeg-turbo-dev libtiff-dev curl-dev poppler09-dev \
libv4l2-dev libgphoto2-dev git automake bash autoconf-archive   "   
    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 5s
./configure --prefix=/usr/local --disable-nls --with-usb
##################################################################
# canon_pp backend requires libieee1284 library - disabling
# escl backend requires a newer JPEG library - disabling
# hpsj5s backend requires libieee1284 library - disabling
# mustek_pp backend requires libieee1284 or parport-directio libraries - disabling
# pint backend requires sys/scanio.h - disabling
# qcam backend requires (ioperm, inb and outb) or portaccess functions - disabling
# pnm backend not manually selected - disabling

# Build saned:   yes
# IPv6 support:  yes
# Avahi support: yes
# cURL support:  yes
# POPPLER_GLIB support:  yes
# SNMP support:  yes
# make looks wrong pathway /usr/lib/libssp.la
LOOP1=/tmp/tcloop/gcc_libs-dev/usr/local/lib/libssp.la
ln -s $LOOP1 /usr/lib/

make -j5 # takes about 4M 40s
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

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

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

# TCZ them
###########
LIST="$P $P-dev $P-doc lib$P"
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:          libsane.tcz  
Description:    libsane files
Version:        git-20201109
Author:         see doc/AUTHORS
Original-site:  http://sane-project.org/
Copying-policy: see doc 
Size:           36K		
Extension_by:   aus9
Tags:           scanner scan backend
Comments:       hplip can use just libsane, but also read sane TCE please
 
Change-log:     2020/11/19 original v git-20201109 on 12x split libsane
Current:        2020/11/19 ' > lib$P.tcz.info

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/13 original v git-20201109 on 12x 
Current:        2020/11/19 split libsane to separate TCE '> $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/13 original v git-20201109 on 12x 
Current:        2020/11/13  '> $P-doc.tcz.info

echo '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.5M
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. 
                Do not depend only on saned  own authentication.  Do not run saned
                as root if it is not necessary
                
                backends are: Some devices not supported see build script
                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 
                napscan 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 internet

                For GUI TCE to  crop/resize/conversion try mtpaint 

Change-log:     2020/11/13 original v git-20201109 on 12x 
Current:        2020/11/13 2020/11/19 split libsane to separate TCE ' > $P.tcz.info

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

echo 'libusb.tcz
libcups.tcz
libtiff.tcz ' > lib$P.tcz.dep

echo 'libsane.tcz ' > $P.tcz.dep
