#!/bin/bash
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

# inject please the following:
# libatk-1.0.la  libgobject-2.0.la libgobject-2.0.la libEGL.la
# libgbm.la libGLESv2.la libdrm.la libglapi.la libgdk_pixbuf-2.0.la
# libgio-2.0.la libgmodule-2.0.l 

export CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv6zk \
-mtune=arm1176jzf-s -mfpu=vfp"

P=gutenprint
PPD=$P-ppd
V=5.3.3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync cups-dev gettext ijs-dev libusb-dev xz "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

# gimp2-dev removed as plugin crashed in attempting to print in gimp via "print with gutenprint"
# --with-gimp2-as-gutenprint removed from config line

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://sourceforge.net/projects/gimp-print/files/$P-5.3/$V/$SRC.tar.xz  " $USER

xz -d $SRC*xz && tar xvf $SRC.tar
cd $SRC
# based on --disable-static giving this warning...
# WARNING: Use of --disable-static-genppd or --disable-static
# when building CUPS is very dangerous
./configure --prefix=/usr/local --with-cups=/usr/local --enable-simplified-cups-ppds=only 
####################################################
# -------Features:
# Build CUPS: yes, installing in /usr/local
# Build CUPS 1.2 enhancements:yes
# Build CUPS PPD files: .......................................no
# Generate PS level 3 CUPS PPD files: yes
# Build genppd statically:            yes
# Build CUPS dyesub USB backend:      yes
# Build EPSON inkjet utility:         yes
# Build enhanced Print plugin for GIMP:....................... no
# Build test programs:                yes
# Build testpattern generator:        yes
# -------- Installation summary:
#    Installation prefix:/usr/local
#    Exec prefix:        /usr/local (${prefix})
#    Data directory:     /usr/local/share/gutenprint
#    Library directory:  /usr/local/lib/gutenprint 
#    Executable directory:/usr/local/bin 
#    XML data directory:  /usr/local/share/gutenprint/5.3/xml
#    Module directory:    /usr/local/lib/gutenprint/5.3/modules 
#    Install sample images: yes
#   General configuration:
#   Configure arguments:--prefix=/usr/local --with-cups=/usr/local --enable-simplified-cups-ppds=only      
#    Compiler:         gcc
#    Compiler Version: gcc version 10.1.0 (GCC)
#    Compiler options:-Disfinite=finite -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp \
##-O3 -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp -Wmissing-prototypes -Wmissing-declarations \
##-Wnested-externs -Wwrite-strings -Werror-implicit-function-declaration -Winline -Wformat=2 \
##-finline-limit=131072 -Wformat -Werror=format-security -Wmissing-prototypes 
#    Build static libraries: yes
#    Build shared libraries: yes
#    Maintainer mode:        no
#    Use i18n:               yes
#    Generate profiling information: no
#    Generate debugging symbols: no
#    Use modules:                static
#    Use readline libraries:     yes
# uname -a output:Linux box 5.4.51-piCore-v7l+ #1 SMP Sun Jul 19 11:25:01 UTC 2020 armv7l GNU/Linux
make -j5 # RPi4 2m20s
mkdir /tmp/$P
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

# 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/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 

# no doc
###### samples to remain in main this build
rm -rf $P/usr/local/share/man 
rm -rf $P/usr/local/share/$P/doc 
rm -rf $P-doc/usr/local/share/doc/gutenprint/reference-html

#######
# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'see doc tcz for COPYING' > $P/usr/local/share/doc/$P/COPYING
# needed for PPD generation command
mkdir -p $P/usr/local/share/cups/model/gutenprint/5.3
GP=$P/usr/local/bin/generate-ppd.sh
###################################
cat >> $GP <<'EOF'
#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

CONF=/usr/local/etc/cups/cupsd.conf
LOOP=/tmp/tcloop/cups/usr/local/etc/cups/cupsd.conf.default
rm -rf $CONF
cp $LOOP $CONF
sed 's|Port 631|127.0.0.1:631|' -i $CONF
sed 's|DefaultEncryption Never||'  -i $CONF

# generate the ppds and start cups
echo 'generation of ppds takes about one minute
uncompressed PPDs can be viewed here
/usr/local/share/cups/model/gutenprint/5.3/    '
/usr/local/sbin/cups-genppd.5.3 -Z
/usr/local/etc/init.d/cups restart
EOF
####################
chown root:staff $GP
chmod 755 $GP

# TCZ them
#######
LIST="$P $P-dev $P-locale "
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:          gutenprint-dev.tcz
Description:    dev files
Version:        5.3.3
Author:         https://sourceforge.net/p/gimp-print/source/ci/master/tree/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:	        752K
Extension_by:   aus9
Tags:           cups printer ppd
Comments:       Development files

Change-log:     2020/09/08 Original 5.3.3 12x
Current:        2020/09/08  ' > $P-dev.tcz.info

echo 'Title:          gutenprint-locale.tcz
Description:    locale files
Version:        5.3.3
Author:         https://sourceforge.net/p/gimp-print/source/ci/master/tree/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:	        3.3M
Extension_by:   aus9
Tags:           cups printer ppd
Comments:       locales not tested by me

Change-log:     2020/09/08 Original 5.3.3 12x
Current:        2020/09/08  ' > $P-locale.tcz.info

echo 'Title:          gutenprint.tcz
Description:    printer drivers for most printers
Version:        5.3.3
Author:         https://sourceforge.net/p/gimp-print/source/ci/master/tree/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           4.5M
Extension_by:   aus9
Tags:           cups printer ppd
Comments:       Not for scanning. To know if gutenprint is suitable see
                http://gimp-print.sourceforge.net/p_Supported_Printers.php

                Optional dependencies Not all may be in repo
                ##################
                colord.tcz reduce log warnings or if printing colour images
                evince.tcz for print preview
                readline.tcz for Epson ink levels
                librest.tcz for reduced errors for gimp2

                gimp2-- I recommend you use print preview to resize images please.

                Wireless (WL) Printing
                #############
                I recommend you set up your router to reserve an IP for your WL printer first.
                Be aware that some printers advertised as wireless printers may not have
                full hardware.     WL ink levels can be checked by using your web browser
                eg url= 192.168.1.7  -- once the printer IP is set.

                Epson ink levels, once printer is set up
                ############    tested on TX100
                $ tce-load -wi readline
                $ escputil --raw-device=/dev/usb/lp0 --ink-level
                 ^ if that fails....repeat...if fails try as root.

                Extra printer calibration
                ###################
                $ sudo cups-calibrate
                (and enter only numbers as directed after viewing the test page)

                How to use  after reading above
                ###########
                $ tce-load -i gutenprint
                $ sudo /usr/local/etc/init.d/cups restart
                $ sudo passwd # to set up a root passwd, if not already set
                ---as /usr/local/etc/cups is root owned
                Next, open your web browser to  http://localhost:631/admin

                Do NOT click on "Find New Printer", instead click on Add Printer
                Then make your selection. Remember to click the "Add Printer" button
                to then get options to change paper size etc. At the end of that process
                a popup (ensure you allow popups in browser settings) will
                ask for user, input root and your new password please. I did not need
                locale set.

                After setup (recommended) pulldown administration and make your printer
                the server default.

                During first setup, config for sharing will fail. YMMV. We fix after
                defaults setup -> go back to http://localhost:631/admin
                Tick the box for sharing and any other changes you wish
                -> cups server will restart. Then click on printers button
                click on the printer you just set up
                status (ideally should show) Idle, Accepting Jobs, Shared, Server Default

                Then test printer.

                If printer not found for WL printers run
                $ tce-load -i avahi
                For my Canon and my Epson I did not need to run next command
                Only run it if you can not find your printer PPD or maybe if you are runnng avahi.
                $ sudo generate-ppd.sh

                You may need to edit the config file if no printing occurs after printer set.
                Then try root powers change localhost:631 so it reads
                "127.0.0.1:631" without the quotes. YMMV.
                If you run the gen command, this edit is attempted by my script.

                For persistence
                ################
                Add these lines to /opt/.filetool.lst
                usr/local/etc/cups/cupsd.conf
                usr/local/etc/cups/printers.conf
                usr/local/etc/cups/ppd

                (2) You can choose to add gutenprint.tcz to your
                boot.lst depending on how frequent you think you might do printing.
                You can also command to start cups by
                $ sudo echo '/usr/local/etc/init.d/cups start' >>  /opt/bootlocal.sh

                On reboot
                ##########
                Assuming you have done the persistence suggestions, load gutenprint 
                and then start cups with
                $ sudo /usr/local/etc/init.d/cups start
                and if you are using a WL printer and need avahi
                $ sudo /usr/local/etc/init.d/avahi start

                #######################
                NO MATCHING MODEL FOUND
                ######################
                If you are not sure which PPD is best for you, uncompressed PPDs are
                created at /usr/local/share/cups/model/gutenprint/5.3
                after running $ sudo generate-ppd.sh

                Before rebooting and losing all of them except your persistent ones,
                you may like to copy some into your home dir. CUPS allows you to try
                a PPD from a non-system pathway.....but for gutenprint it needs to have
                been designed for that version of gutenprint. It may take a few tries
                where you can not find your exact model listed. Good Luck!
                eg you have make Z model 142 so try model 140 then 100 etc
                cups also supplies generic models but these tend to be a bit limited
                Use the forum if you have any issues please.

Change-log:     2020/09/08 Original 5.3.3 12x
Current:        2020/09/08   ' > $P.tcz.info

echo 'gutenprint.tcz
cups-dev.tcz 
gettext.tcz 
ijs-dev.tcz 
libusb-dev.tcz 
xz.tcz ' > $P-dev.tcz.dep

echo 'cups.tcz
libusb.tcz
ijs.tcz
ghostscript.tcz
gnutls.tcz' > $P.tcz.dep
echo 'gutenprint.tcz' > $P-locale.tcz.dep
