#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

export CFLAGS='-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp  '
export CXXFLAGS='-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp '
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS

P=libtiff
P1=tiff
V=4.5.1
SRC=$P1-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget libjpeg-turbo-dev liblzma-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 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 gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/liblto_plugin.la
cd /tmp
rm -rf /usr/lib/*.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://download.osgeo.org/$P/$SRC.tar.xz " $USER 
tar -xvf $SRC*xz 
cd $SRC
./configure --prefix=/usr/local --enable-static=no --disable-tests --disable-docs 


 
# Installation directory:             /usr/local
# Documentation directory:            ${prefix}/share/doc/tiff-4.5.1
# C compiler:           gcc -flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp   -Wall -W
# C++ compiler:       g++ -flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp 
# Build tools:                        yes
# Build tests:                        no
# Build contrib:                      yes
# Build docs:                         no
# Build deprecated features:          no
# Enable runtime linker paths:        no
# Enable linker symbol versioning:    yes
# Support Microsoft Document Imaging: yes
# Use Win32 IO:                       no
# Support for internal codecs:
# CCITT Group 3 & 4 algorithms:       yes
# Macintosh PackBits algorithm:       yes
# LZW algorithm:                      yes
# ThunderScan 4-bit RLE algorithm:    yes
# NeXT 2-bit RLE algorithm:           yes
# LogLuv high dynamic range encoding: yes
# Support for external codecs:
# ZLIB support:                       yes
# libdeflate support:                 no
# Pixar log-format algorithm:         yes
# JPEG support:                       no
# Old JPEG support:                   no
# JPEG 8/12 bit dual mode:            yes (libjpeg turbo >= 2.2 dual mode)
# ISO JBIG support:                   no
# LERC support:                       no
# LZMA2 support:                      yes
# ZSTD support:                       no
# WEBP support:                       no
# C++ support:                        yes
# OpenGL support:                     no

make -j4 # 45seconds
make install-strip DESTDIR=/tmp/$P # install-strip fails
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
rm -rf  $P/usr/local/lib/*.la 
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib

# bin
#####
mkdir -p $P-bin/usr/local
mv $P/usr/local/bin $P-bin/usr/local/

# main 
#####
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE.md  $P/usr/local/share/doc/$P/LICENSE

# TCZ them
#######
LIST="$P $P-dev $P-bin "
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:          libtiff-bin.tcz
Description:    bin files
Version:        4.5.1
Author:         Eric S. RaymondSam Leffler & Silicon Graphics, Inc.
Original-site:  https://download.osgeo.org/libtiff
Copying-policy: accompanied
Size:           200K
Extension_by:   aus9
Tags:           image tiff
Comments:       bin files
                
Change-log:     2016/05/14 v 4.0.6 on 8.x (bmarkus)
Current:        2023/07/27 v 4.5.1 on 14.x (aus9)  '  > $P-bin.tcz.info
readelf -d $P-bin/usr/local/bin/* | grep 'NEEDED' 
#[libtiff.so.6]libtiff
#[liblzma.so.5]liblzma dep of main
#[libjpeg.so.62]libjpeg-turbo dep of main
echo 'libtiff.tcz   ' >$P-bin.tcz.dep 
ldd $P-bin/usr/local/bin/* | # pcre/libffi6/libssl checks

echo 'Title:          libtiff-dev.tcz
Description:    dev files
Version:        4.5.1
Author:         Eric S. RaymondSam Leffler & Silicon Graphics, Inc.
Original-site:  https://download.osgeo.org/libtiff
Copying-policy: accompanied
Size:           36K
Extension_by:   aus9
Tags:           image tiff
Comments:       dev files
                
Change-log:     2016/05/14 v 4.0.6 on 8.x (bmarkus)
Current:        2023/07/27 v 4.5.1 on 14.x (aus9) '  > $P-dev.tcz.info
echo 'libtiff.tcz
liblzma-dev.tcz
libjpeg-turbo-dev.tcz' >$P-dev.tcz.dep

echo 'Title:          libtiff.tcz
Description:    Library for manipulating GIF files
Version:        4.5.1
Author:         Eric S. RaymondSam Leffler & Silicon Graphics, Inc.
Original-site:  https://download.osgeo.org/libtiff
Copying-policy: accompanied
Size:           200K
Extension_by:   aus9
Tags:           image tiff
Comments:       Libs only
                
Change-log:     2016/05/14 v 4.0.6 on 8.x (bmarkus)
Current:        2023/07/27 v 4.5.1 on 14.x (aus9) '  > $P.tcz.info

readelf -d $P/usr/local/lib/* | grep 'NEEDED' 
#[libjpeg.so.62]libjpeg-turbo
#[liblzma.so.5]liblzma
#[libtiff.so.6] this TCE
echo 'libjpeg-turbo.tcz
liblzma.tcz  ' >$P.tcz.dep

# no dep

