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

# build boost-1.83 first then this
# error: 'dynamic_cast' not permitted with '-fno-rtti'
export CFLAGS='-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp  '
export CXXFLAGS='-flto=auto -Os -pipe -fno-exceptions -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp '
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS

P=poppler23
V=23.08.0
SRC=poppler-$V
USER=`cat /etc/sysconfig/tcuser`
   
LIST="compiletc squashfs-tools wget cmake perl5 lcms2-dev cairo-dev boost-1.83-dev \
gobject-introspection-dev "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# list may be larger 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 gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/liblto_plugin.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
rm -rf pulseaudio/libpulsedsp.la
rm -rf pulseaudio/libpulsecore-13.0.la
rm -rf pulseaudio/libpulsecommon-13.0.la
rm -rf libv4l/v4l2convert.la
rm -rf libv4l/v4l1compat.la
rm -rf libv4l/plugins/libv4l-mplane.la
rm -rf vala-0.46/libvalaccodegen.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://$P.freedesktop.org/poppler-$V.tar.xz" $USER
tar xvf poppler*xz
cd $SRC
mkdir build && cd build 
U=/usr/local
# to get settings cmake -LAH ../
# nss not in repo dep so disable
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=ON \
-DENABLE_LIBOPENJPEG=none -DCMAKE_VERBOSE_MAKEFILE=TRUE -DWITH_GTK:BOOL=OFF \
-DENABLE_UNSTABLE_API_ABI_HEADERS=ON -DWITH_NSS3:BOOL=OFF \
-Dpoppler_IS_TOP_LEVEL:STATIC=OFF ../
######################################################################################
# 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:        yes
# gtk-doc:              no
# cpp wrapper:          yes
# use libjpeg:          yes
# use libpng:           yes
# use libtiff:          yes
# use zlib uncompress:  no
# use nss3:             no
# use gpg:              no
# default signature backend:          None
# use curl:             no
# use libopenjpeg2:     no
# use lcms2:            yes
# command line utils:   yes
# fuzz target:          no
# test data dir:        /tmp/poppler-23.08.0/../test
make -j4 # 8M50s
make -j4 install DESTDIR=/tmp/$P
cd /tmp

# bin
#####
mkdir -p $P-bin/usr/local
mv $P/usr/local/bin $P-bin/usr/local/
strip --strip-unneeded $P-bin/usr/local/bin/*

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

# gir
#####
mkdir -p $P-gir/usr/local/lib
mv $P/usr/local/lib/gir* $P-gir/usr/local/lib
mkdir -p $P-gir/usr/local/share
mv $P/usr/local/share/gir* $P-gir/usr/local/share

# lib
#######
mkdir -p lib$P/usr/local/share/doc/$P
echo 'GPL v2' > lib$P/usr/local/share/doc/$P/COPYING
mv $P/usr/local/lib lib$P/usr/local/
strip --strip-unneeded lib$P/usr/local/lib/*

# leaves man page in $P optional delete this dir
rm -rf $P

# TCZ them
#######  
LIST="$P-bin $P-dev $P-gir lib$P "
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:          libpoppler23.tcz
Description:    library for rendering PDF files
Version:        23.08.0
Author:         Albert Astals Cid 
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           1.1M
Extension_by:   aus9
Tags:           pdf
Comments:       pdf rendering library   
                poppler is a fork of xpdf-3.00
                likely to conflict with libpoppler
                
Change-log:     2023/08/18 v 23.08.0 on 14.x
Current:        2023/08/18    ' > lib$P.tcz.info
readelf -d lib$P/usr/local/lib/*.so | grep 'NEEDED' 
#[libpoppler.so.130]this TCE
#[libcairo.so.2]cairo
#[libfreetype.so.6]freetype fontconfig cairo
#[libgio-2.0.so.0][libgobject-2.0.so.0][libglib-2.0.so.0]glib2 cairo
#[libfontconfig.so.1]fontconfig cairo
#[libjpeg.so.62]libjpeg-turbo libtiff lcms2
#[liblcms2.so.2]lcm2
#[libpng16.so.16]libpng cario 
#[libtiff.so.6]libtiff lcms2
echo 'cairo.tcz
lcms2.tcz ' > lib$P.tcz.dep

echo 'Title:          poppler23-bin.tcz
Description:    bin files
Version:        23.08.0
Author:         Albert Astals Cid (forked from Derek Noonburg xpdf)
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           176K
Extension_by:   aus9
Tags:           pdf
Comments:       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:     2023/08/18 v 23.08.0 on 14.x
Current:        2023/08/18    ' > $P-bin.tcz.info
readelf -d $P-bin/usr/local/bin/* | grep 'NEEDED'
#[libpoppler.so.130] libpoppler23
#[libcairo.so.2]cairo "
#[libfreetype.so.6]   "
#[liblcms2.so.2]      "
echo 'libpoppler23.tcz' > $P-bin.tcz.dep

echo 'Title:          poppler23-dev.tcz
Description:    devs files
Version:        23.08.0
Author:         Albert Astals Cid 
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           392K
Extension_by:   aus9
Tags:           pdf
Comments:       dev files

Change-log:     2023/08/18 v 23.08.0 on 14.x
Current:        2023/08/18  ' > $P-dev.tcz.info

echo 'libpoppler23.tcz 
lcms2-dev.tcz 
cairo-dev.tcz 
boost-dev.tcz     ' > $P-dev.tcz.dep

echo 'Title:          poppler23-gir.tcz
Description:    gir files
Version:        23.08.0
Author:         Albert Astals Cid 
Original-site:  https://poppler.freedesktop.org/
Copying-policy: GPL v2
Size:           160K
Extension_by:   aus9
Tags:           pdf
Comments:       gir files

Change-log:     2023/08/18 v 23.08.0 on 14.x
Current:        2023/08/18  ' > $P-gir.tcz.info
echo 'libpoppler23.tcz ' > $P-gir.tcz.dep

# test pdftohtml sample.pdf > html page renders ok


