#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

# checking if gcc supports -fno-rtti -fno-exceptions... no
export CFLAGS="-mtune=generic -Os -pipe "  
export CXXFLAGS="-mtune=generic -Os -pipe  "
ULI=/usr/local/include
export CPPFLAGS="-I$ULI/gtk-2.0 -I$ULI/gtk-2.0/gdk -I$ULI/gtk-2.0/gtk -I/usr/local/lib/gtk-2.0/include \
-I$ULI/glib-2.0 -I/usr/local/lib/glib-2.0/include -I$ULI/cairo -I$ULI/pango-1.0 -I$ULI/harfbuzz \
-I$ULI/gdk-pixbuf-2.0 -I$ULI/atk-1.0  -I$ULI/gimp-2.0 -I$ULI/gegl-0.4 -I$ULI/babl-0.1 "

P=gutenprint
PPD=$P-ppd
V=5.3.5-pre1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cups-dev gettext-dev gtk2-dev intltool libtool-dev libusb-dev "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done
# ls pcre -> no old pcre

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://sourceforge.net/projects/gimp-print/files/$P-5.3/5.3.5-pre1/$P-5.3.5-pre1.tar.xz " $USER
tar xvf $P*xz
cd $SRC
# based on --disable-static giving this warning.SNIP when building CUPS is very dangerous
./configure --prefix=/usr/local --with-cups=/usr/local --enable-simplified-cups-ppds \
--sysconfdir=/usr/local/etc --localstatedir=/var --with-doc
################################################### 
# 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 
# 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 \
                       --sysconfdir=/usr/local/etc --localstatedir=/var --with-doc

# Compiler:                           gcc
# Compiler Version:                   gcc version 13.2.0 (GCC) 
# Compiler options: -Disfinite=finite -mtune=generic -Os -pipe  -O3  -Wall -Wcast-align -Wstrict-prototypes -Wmissing-prototypes \
                    -Wmissing-declarations -Wnested-externs -Wwrite-strings -Werror-implicit-function-declaration -Winline \
                    -Wformat=2 -finline-limit=131072 -Wformat -Werror=format-security -D_POSIX_C_SOURCE=200809L -std=c99
# 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 6.6.8-tinycore64 #666 SMP Sat Dec 23 16:41:21 UTC 2023 x86_64 GNU/Linux

# gut> does not show that it really wants to create docs under u loc share gut. /doc  
# even after I did some manual edits for docdir and htmldir
make -j4 # 30 secs
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
##### NOTE la and a files
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/
rm -rf $P-dev/usr/local/lib/*.la 
chmod 644 $P-dev/usr/local/lib/*.a 

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
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
rm -rf $P/usr/local/share/$P/doc
cd $P-doc/usr/local/share/doc/$P
rm -rf AUTHORS COPYING ChangeLog NEWS README gutenprint-users-manual.odt  
mv reference-html HTML
mv FAQ.html HTML
cd /tmp

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $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
   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/cups/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:root $GP
chmod 755 $GP

# submit finds issue
chmod 755 $P/usr/local/lib/cups/backend/gutenprint53+usb

# TCZ them
#######
LIST="$P $P-dev $P-doc $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 'Title:          gutenprint-dev.tcz
Description:    dev files 
Version:        5.3.5-pre1
Author:         https://github.com/echiu64/gutenprint/blob/master/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           1.2M
Extension_by:   aus9 @linuxquestions.org
Tags:           cups printer ppd 
Comments:       Development files

Change-log:     2018/06/17 v 5.2.14 
                2019/01/04 v 5.3.1 add simplified PPD option on 10x
                2020/04/21 v 5.3.3 on 11x
                2023/03/28 v 5.3.4 on 14x 
Current:        2024/10/25 v 5.3.5-pre1 on 15x ' > $P-dev.tcz.info
echo 'gutenprint.tcz
cups-dev.tcz 
gettext-dev.tcz  
gtk2-dev.tcz
intltool.tcz  
libtool-dev.tcz  
libusb-dev.tcz    ' > $P-dev.tcz.dep

echo 'Title:          gutenprint-doc.tcz
Description:    help files 
Version:        5.3.5-pre1
Author:         https://github.com/echiu64/gutenprint/blob/master/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           1.1M
Extension_by:   aus9 @linuxquestions.org
Tags:           cups printer ppd 
Comments:       man pages and some docs
                manual is still for v 5.2 
                ditto enclosed PDF but there are html pages here

Change-log:     2024/10/25 v 5.3.5-pre1 on 15x
Current:        2024/10/25 ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:          gutenprint-locale.tcz
Description:    locale files 
Version:        5.3.5-pre1
Author:         https://github.com/echiu64/gutenprint/blob/master/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           3.7M
Extension_by:   aus9 @linuxquestions.org
Tags:           cups printer ppd 
Comments:       locales not tested by me

Change-log:     2018/06/17 v 5.2.14 
                2019/01/04 v 5.3.1 add simplified PPD option on 10x
                2020/04/21 v 5.3.3 on 11x
                2023/03/28 v 5.3.4 on 14x 
Current:        2024/10/25 v 5.3.5-pre1 on 15x    ' > $P-locale.tcz.info
echo 'gutenprint.tcz' > $P-locale.tcz.dep

echo 'Title:          gutenprint.tcz
Description:    printer drivers for most printers
Version:        5.3.5-pre1
Author:         https://github.com/echiu64/gutenprint/blob/master/AUTHORS
Original-site:  https://sourceforge.net/projects/gimp-print/
Copying-policy: GPL v2
Size:           5.9M
Extension_by:   aus9 @linuxquestions.org 
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 TCEs
                #############
                colord.tcz reduce log warnings or if printing colour images
                mupdf.tcz (Very tiny relative to total for evince)
                for print preview 
                1) choose print to file and choose pathway2/output.pdf
                2) $ mupdf /pathway2/output.pdf # output.pdf is default
                
                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 in the past on TX100, I no longer have it 
                $ 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 
                $ [ -f /var/run/cups/cupsd.pid ] || sudo /usr/local/etc/init.d/cups start
                $ 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. 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

                For persistence 
                ################
                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
                ###   ^ this dir contains all your printer PPDs
                
                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

                One issue to note is
                ####################
                If you have a USB-connected printer, you must have a printer connected 
                to each USB port that you plan to use and powered on when you restart CUPS.  
                If you do not do so, you will not be able to reinstall the printer. It is only 
                necessary to do this if you wish to update PPD files manually; if you use 
                cups-genppdupdate, you do not need to do this
                
Change-log:     2018/06/17 v 5.2.14 
                2019/01/04 v 5.3.1 add simplified PPD option on 10x
                2020/04/21 v 5.3.3 on 11x
                2023/03/28 v 5.3.4 on 14x 
Current:        2024/10/25 v 5.3.5-pre1 on 15x     ' > $P.tcz.info


readelf -d $P/usr/local/sbin/* | grep 'NEEDED' # ignore TCBs
[libcups.so.2] libcups but we need to start cups so -> cups
readelf -d $P/usr/local/bin/* | grep 'NEEDED' # show only extras
# [libgutenprint.so.8] this TCE...hmmm 14x was so.9?
# [libreadline.so.8] readline for epsons
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' 
#libgtk-x11-2.0.so.0]libgdk-x11-2.0.so.0] gtk2
#libpangocairo-1.0.so.0]libpangoft2-1.0.so.0]#libpango-1.0.so.0]pango -> gtk2
#libatk-1.0.so.0]at-spi2-core gtk2
#libcairo.so.2]cairo - pango - gtk2
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 gtk2
#libgio-2.0.so.0]libgobject-2.0.so.0]libglib-2.0.so.0]glib2 at-spi2-core gtk2
#libharfbuzz.so.0]harfbuzz freetype fontconfig cairo - pango - gtk2
#libfontconfig.so.1]fontconfig cairo - pango - gtk2
#libfreetype.so.6]freetype fontconfig cairo - pango - gtk2
readelf -d $P/usr/local/lib/cups/backend/* | grep 'NEEDED'
# [libusb-1.0.so.0] libusb
readelf -d $P/usr/local/lib/cups/driver/* | grep 'NEEDED' # no extras
readelf -d $P/usr/local/lib/cups/filter/* | grep 'NEEDED' # no extras

echo 'cups.tcz
readline.tcz
gtk2.tcz
libusb.tcz
ghostscript.tcz  ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync


