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

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe"
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe "

P=cups-filters
V=1.28.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync xz avahi-dev poppler-dev qpdf-dev dbus-dev dejavu-fonts-ttf cups-dev \
libpng-dev glib2-dev lcms2-dev freetype-dev fontconfig-dev "
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://www.openprinting.org/download/$P/$SRC.tar.xz " $USER

xz -d $SRC*xz && tar xvf $SRC.tar
cd $SRC

LIBQPDF_CFLAGS="-I/usr/local/include/qpdf" LIBQPDF_LIBS="-L/usr/local/lib -lqpdf -ljpeg" --prefix=/usr/local \
--disable-static --without-rcdir --with-pdftops=gs --with-mutool-path=gs --with-gs-path=/usr/local/bin/gs \
--with-pdftops-path=/usr/local/bin/gs --with-test-font-path=/usr/local/share/fonts/dejavu/DejaVuSans.ttf \
--with-cups-rundir=/var/run/cups



 --disable-nls 
 --disable-mutool --runstatedir=/var --enable-braille=no
####################################################
# CFLAGS: -march=armv8-a+crc -mtune=cortex-a72 -Os -pipe -Wall -std=gnu11 -D_GNU_SOURCE
# CXXFLAGS: -march=armv8-a+crc -mtune=cortex-a72 -Os -pipe  -Wall  -D_GNU_SOURCE
# LDFLAGS:                                   
# cups-config:     system
# font directory:  /usr/local/etc/fonts/conf.d
# foomatic:        yes
# init directory:  /etc/init.d....................................wrong see below
# cups dom socket: /usr/local/var/run/cups/cups.sock..............wrong see below
# poppler:         yes
# ghostscript:     yes
# gs-path:         /usr/local/bin/gs
# mutool:          no
# mutool-path:     
# ippfind-path:    system
# imagefilters:    yes
# jpeg:            yes
# pdftocairo-path: system
# pdftops:         hybrid
# pdftops-path:    system
# png:             yes
# php:             no
# php-config:      system
# shell:           /bin/bash
# test-font:       /usr/local/share/fonts/DejaVuSans.ttf
# tiff:            yes
# avahi:           yes
# dbus:            yes
# browsing:        dnssd cups
# werror:          no
# braille:	   no
# braille tables:  /usr/share/liblouis/tables............no such beast
# driverless:      yes
# apple-raster:    rastertopdf
# pclm:            yes
# local queue naming for remote CUPS queues: DNS-SD
# keep generated queues during shutdown:     no
# all ipp printer auto-setup: yes
# only driverless auto-setup: no
# only local auto-setup: no


sed 's|INITDDIR = /etc|INITDDIR = /usr/local/etc|' -i Makefile
sed 's|CUPS_DEFAULT_DOMAINSOCKET = /usr/local/var/run/cups/cups.sock|CUPS_DEFAULT_DOMAINSOCKET = /var/run/cups/cups.sock|' -i Makefile
make -j5 # about 1M 20s
mkdir /tmp/$P
make install-strip DESTDIR=/tmp/$P  # takes seconds
cd /tmp 

# 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 removed init.d
###### 
rm -rf $P/etc
rm -rf $P/usr/local/share/man 
rm -rf $P/usr/local/share/doc
rm -rf $P/usr/local/etc/init.d/cups-browsed

# main start afresh
######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/COPYING $P/usr/local/share/doc/$P

# TCZ them
#######
LIST="$P $P-dev"
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:          cups-filters-dev.tcz
Description:    cups-filters dev files
Version:        1.28.2
Author:         https://github.com/OpenPrinting/cups-filters/blob/master/AUTHORS
Original-site:  https://openprinting.github.io/projects/01-cups-filters/
Copying-policy: see COPYING
Size:           148K
Extension_by:   aus9
Tags:           printing 
Comments:       dev files

Change-log:     2020/09/26 Original 1.28.2
Current:        2020/09/26  ' > $P-dev.tcz.info

echo 'Title:          cups-filters.tcz
Description:    see below
Version:        1.28.2
Author:         https://github.com/OpenPrinting/cups-filters/blob/master/AUTHORS
Original-site:  https://openprinting.github.io/projects/01-cups-filters/
Copying-policy: see COPYING
Size:           800K
Extension_by:   aus9
Tags:           printing 
Comments:       This package contains backends, filters, and other software that
                is no longer maintained by Apple Inc. 
                
Change-log:     2020/09/26 Original 1.28.2
Current:        2020/09/26  ' > $P.tcz.info
 
echo 'cups-filters.tcz
xz.tcz 
avahi-dev.tcz 
poppler-dev.tcz 
qpdf-dev.tcz 
dbus-dev.tcz 
dejavu-fonts-ttf.tcz 
cups-dev.tcz 
libpng-dev.tcz 
glib2-dev.tcz 
lcms2-dev.tcz 
freetype-dev.tcz 
fontconfig-dev.tcz' > $P-dev.tcz.dep
echo 'poppler.tcz
qpdf.tcz
Xorg-fonts.tcz' > $P.tcz.dep

