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

# error: 'dynamic_cast' not permitted with '-fno-rtti'
export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe " 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe -fno-exceptions "

P=poppler
V=23.06.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
   
LIST="compiletc submitqc xz cmake perl5 lcms2-dev cairo-dev boost-dev glib2-dev"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# list may have more than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
rm -rf imlib2/loaders/*.la
rm -rf imlib2/filters/*.la
rm -rf gdk-pixbuf-2.0/2.10.0/loaders/*.la
cd /tmp
rm -rf /usr/lib/*.la

# poppler looks wrong place
ln -s /usr/local/include/nss /usr/include/nss

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://$P.freedesktop.org/$SRC.tar.xz" $USER
tar xvf $SRC*xz
cd $SRC
mkdir build && cd build 
U=/usr/local
# to get settings cmake -LAH ../

cmake -DCMAKE_INSTALL_PREFIX=$U -DCMAKE_INSTALL_LIBDIR=$U/lib -DCMAKE_BUILD_TYPE=Release \
-DBUILD_MANUAL_TESTS:BOOL=OFF -DBUILD_QT6_TESTS:BOOL=OFF \
-DENABLE_XPDF_HEADERS=ON -DENABLE_QT4=OFF -DENABLE_QT5=OFF -DENABLE_QT6:BOOL=OFF \
-DBUILD_QT6_TESTS:BOOL=OFF -DBUILD_GTK_TESTS:BOOL=OFF  \
-DENABLE_GOBJECT_INTROSPECTION:BOOL=OFF \
-DENABLE_LIBOPENJPEG=none -DCMAKE_VERBOSE_MAKEFILE=TRUE -DENABLE_UNSTABLE_API_ABI_HEADERS=ON ../
######################################################################################
# Building Poppler with support for:
# font configuration:   fontconfig
# use boost (Splash):   yes
# cairo output:         yes
# qt5 wrapper:          no
# qt6 wrapper:          no
# glib wrapper:         yes
# introspection:        no
# gtk-doc:              no
# cpp wrapper:          yes
# use libjpeg:          yes
# use libpng:           yes
# use libtiff:          yes
# use zlib compress:    yes
# use zlib uncompress:  no
# use nss3:             no
# use gpg:              no
# default signature backend:          None
# use curl:             no
# use libopenjpeg2:     no
# use lcms2:            yes
# use boost:            yes
# command line utils:   yes
# fuzz target:          no
# test data dir:        /tmp/poppler-23.06.0/../test
make -j5 # 6M20s
make -j5 install DESTDIR=/tmp/$P
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/

# main
#######
rm -rf $P/usr/local/share/man
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING
strip --strip-unneeded $P/usr/local/bin/*
strip --strip-unneeded $P/usr/local/lib/*

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

ls -hal

echo 'Title:          poppler-dev.tcz
Description:    poppler devs
Version:        23.06.0
Author:         Albert Astals Cid (forked from Derek Noonburg xpdf)
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           392K
Extension_by:   aus9
Tags:           pdf
Comments:       dev files

Change-log:     2020/09/25 v 20.09.0 on 12.x 
                2020/11/09 rebuilt for glib2 for sane support
Current:        2023/06/13 v 23.06.0 on 14.x ' > $P-dev.tcz.info

echo 'poppler.tcz 
cmake.tcz 
perl5.tcz 
lcms2-dev.tcz 
cairo-dev.tcz 
boost-dev.tcz
glib2-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          poppler.tcz
Description:    library for rendering PDF files
Version:        23.06.0
Author:         Albert Astals Cid (forked from Derek Noonburg xpdf)
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           1.6M
Extension_by:   aus9
Tags:           pdf
Comments:       library for rendering PDF files, and examining or
                modifying their structure.  
                poppler is a fork of xpdf-3.00
                
                pdfattach - add new PDF as an attachment to some PDF
                pdfdetach - lists or extracts embedded files 
                pdffonts - font analyzer
                pdfimages - image extractor
                pdfinfo - document information
                pdfseparate - page extraction tool
                pdftocairo - PDF to PNG/JPEG/PDF/PS/EPS/SVG converter using Cairo
                pdftohtml - PDF to HTML converter
                pdftoppm - PDF to PPM/PNG/JPEG image converter
                pdftops - PDF to PostScript converter
                pdftotext - text extraction
                pdfunite - document merging tool

Change-log:     2020/09/25 v 20.09.0 on 12.x 
                2020/11/09 rebuilt for glib2 for sane support
Current:        2023/06/13 v 23.06.0 on 14.x    ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # delete duplicates TCBs below
#[libpoppler.so.129] this TCE
#[libcairo.so.2] cairo
#[libfreetype.so.6]freetype fontconfig
#[liblcms2.so.2]liblcms2
readelf -d $P/usr/local/lib/* | grep 'NEEDED'
#[libgio-2.0.so.0]glib2
#[libgobject-2.0.so.0]"
#[libglib-2.0.so.0]"
#[libfontconfig.so.1]fontconfig
#[libjpeg.so.62]libjpeg-turbo libtiff liblcms2
#[libpng16.so.16]libpng -> fontconfig
#[libtiff.so.5]libtiff liblcms2

echo 'cairo.tcz
fontconfig.tcz
liblcms2.tcz
glib2.tcz
libjpeg-turbo.tcz     ' > $P.tcz.dep

submitqc --libs

rm -rf *.zsync



