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

# due to meson recreate missing la file
########################################
cat >> /usr/local/lib/libatk-1.0.la  <<'EOF'
# libatk-1.0.la - a libtool library file
# Generated by libtool (GNU libtool) 2.4.6 Debian-2.4.6-0.1
#
# Please DO NOT delete this file!
# It is necessary for linking the library.

# The name that we can dlopen(3).
dlname='libatk-1.0.so.0'

# Names of this library.
library_names='libatk-1.0.so.0.22810.1 libatk-1.0.so.0 libatk-1.0.so'

# The name of the static archive.
old_library=''

# Linker flags that cannot go in dependency_libs.
inherited_linker_flags=''

# Libraries that this one depends upon.
dependency_libs=' -L/usr/local/lib /usr/local/lib/libgobject-2.0.la /usr/local/lib/libffi.la /usr/local/lib/libglib-2.0.la /usr/local/lib/libpcre.la -lpthread'

# Names of additional weak libraries provided by this library
weak_library_names=''

# Version information for libatk-1.0.
current=22810
age=22810
revision=1

# Is this an already installed library?
installed=yes

# Should we warn about portability when linking against -modules?
shouldnotlink=no

# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''

# Directory that this library needs to be installed in:
libdir='/usr/local/lib'
EOF

CFLAGS="-mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe \
-fno-exceptions -fno-rtti" 

P=gutenprint
PPD=$P-ppd
V=5.3.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cups2-dev gettext ijs-dev libusb-dev xz"
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://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
####################################################
# -------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 (/usr/local/lib/gutenprint)#
#    Executable directory: /usr/local/bin (/usr/local/bin)
#    XML data directory:   /usr/local/share/gutenprint/5.3/xml
#    Module directory:      /usr/local/lib/gutenprint/5.3/modules (/usr/local/lib/gutenprint/5.3/modules)
#    Install sample images:  yes
#  ---------General configuration:
#    Compiler:                                   gcc
#    Compiler Version:                       gcc version 7.2.0 (GCC) 
#    Compiler options: -Disfinite=finite  -O3  -Wmissing-prototypes -Wmissing-declarations\
## -Wnested-externs -Wwrite-strings -Werror-implicit-function-declaration -Winline -Wformat=2 -finline-limit=131072
#    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 4.14.10-tinycore64 #2018 SMP Mon Jan 1 16:07:42 UTC 2018 x86_64 GNU/Linux
#### make takes about 30 seconds using -j9 while make check took years so...
make -j9
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 

# doc
###### samples to remain in main this build
# make-install-output installed a different pathway to doc
mkdir -p $P-doc/usr/local/share/doc
mv $P/usr/local/share/man $P-doc/usr/local/share
mv $P/usr/local/share/$P/doc $P-doc/usr/local/share/doc/$P

#######
# 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

# to resolve previous Error from http://localhost:631/admin/log/error_log
# = Unable to open listen socket for address [v1.::1]:631 (etc)
CONF=/usr/local/etc/cups/cupsd.conf
LOOP=/tmp/tcloop/cups2/usr/local/etc/cups/cupsd.conf.default
rm -rf $CONF
cp $LOOP $CONF
sed 's|localhost:631|127.0.0.1:631|' -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-doc $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
done

ls -hal

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

echo 'cups2.tcz
libusb.tcz
ijs.tcz
ghostscript.tcz 
gnutls35.tcz' > $P.tcz.dep
echo 'gutenprint.tcz' > $P-locale.tcz.dep
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          gutenprint.tcz
Description:    printer drivers for most printers
Version:        5.3.1
Author:         see -doc/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           4.9M
Extension_by:   aus9 at gmx dot com
Tags:           cups printer ppd 
Comments:       To know if gutenprint is suitable, the list of printers can be
                searched here
                http://gimp-print.sourceforge.net/p_Supported_Printers.php

               UPGRADERS---please delete all persistent files except your root pw
               ########  ---reboot and then start again
                
                Optional dependency load colord.tcz to reduce
                warnings in error log. Printing still works without it.

                Wireless (WL) Printing
                #############
                For my Canon MG 3060 I found I did not need avahi loaded YMMV. 
                You may need avahi loaded and its daemon started but note
                that on first use, you must run the generate-ppd command
                before starting avahi.
                             
                WL ink levels can be checked by using your web browser to 
                go to your static ip for your wl printer eg 192.168.1.7
                -- once the printer is set up

                Epson ink levels, once printer is set up
                ############    tested on usb cable model TX100 
                $ tce-load -wi readline
                # escputil -i --raw-device /dev/usb/lp0

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

                Preview
                ########
                Needs evince.tcz to preview. Tested on leafpad.

                How to use  after reading above
                ########### 
                $ tce-load -i gutenprint 
                
                First time use or if you have not saved the files
                ##############
                The generate-ppd.sh  starts cups but will not load avahi
                $ sudo generate-ppd.sh
                
                Next, /usr/local/etc/cups is owned by root so set up a root password 
                $ sudo passwd

                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 some point in that process
                either at the beginning or after selecting your model, a popup will 
                ask for user, input root and your new password please.

                Next (recommended) pulldown administration and make your printer
                the server default. Then test printer. 
                $ tce-load -i avahi (and start it if wl printer not detected)

                For persistence 
                ################
                (1) Add these lines to /opt/.filetool.lst 
                etc/shadow
                usr/local/etc/cups/cupsd.conf
                usr/local/etc/cups/printers.conf
                usr/local/etc/cups/ppd/Canon_MG3000_series.ppd (change to your PPD)
                
                (2) You can choose to add gutenprint.tcz to your 
                boot.lst depending on how frequent you think you might do printing.

                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.2
                before rebooting and loosing 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

                Optional dep for usb trouble-shooting=usb-utils for command lsusb
 

                Compiled for 9x 
Change-log:     2018/06/17 first version 
Current:        2019/01/04  v 5.2.14 -> 5.3.1 add simplified PPD option  ' > $P.tcz.info

echo 'Title:          gutenprint-dev.tcz
Description:    dev files 
Version:        5.3.1
Author:         see -doc/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:	           880K
Extension_by:   aus9 at gmx dot com
Tags:           cups printer ppd 
Comments:       Development files

                Compiled for 9x 
Change-log:     2018/06/17 first version 
Current:        2019/01/04  v 5.2.14 -> 5.3.1 add simplified PPD option  ' > $P-dev.tcz.info

echo 'Title:          gutenprint-locale.tcz
Description:    locale files 
Version:        5.3.1
Author:         see -doc/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:	           3.3M
Extension_by:   aus9 at gmx dot com
Tags:           cups printer ppd 
Comments:       locales not tested by me

                Compiled for 9x 
Change-log:     2018/06/17 first version 
Current:        2019/01/04  v 5.2.14 -> 5.3.1 add simplified PPD option  ' > $P-locale.tcz.info

echo 'Title:          gutenprint-doc.tcz
Description:    man pages and docs 
Version:        5.3.1
Author:         see -doc/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:         	  2.0M
Extension_by:   aus9 at gmx dot com
Tags:           cups printer ppd 
Comments:       Note sub-directory to doc dir

                --reference-html...navigate your web browser to 
                   book1.html if you are interested in the 
                   "Developer Guide"

                Compiled for 9x 
Change-log:     2018/06/17 first version 
Current:        2019/01/04  v 5.2.14 -> 5.3.1 add simplified PPD option  ' > $P-doc.tcz.info

submitqc --libs
