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

CFLAGS="-march=i486 -mtune=i686 -Os -pipe   " 
CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti  "
echo $CFLAGS && echo $CXXFLAGS

P=libpng
V=1.6.40
SRC=$P-$V
PATCH=libpng-1.6.39-apng.patch
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget "
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/i486-pc-linux-gnu/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/liblto_plugin.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://downloads.sourceforge.net/$P/$SRC.tar.xz \
https://downloads.sourceforge.net/sourceforge/$P-apng/$PATCH.gz "  $USER
tar xvf $SRC*xz
cd $SRC
gzip -cd ../$PATCH.gz | patch -p1
./configure --prefix=/usr/local --disable-static  --localstatedir=/var
make -j4 # 10 secs
make install-strip DESTDIR=/tmp/$P 
cd /tmp

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

# 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
rm -rf $P/usr/local/lib/*.la

# main
#######
rm -rf $P/usr/local/share/man
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE  $P/usr/local/share/doc/$P/	

# TCZ them
###########
LIST2="$P $P-dev $P-bin "
for Z in $LIST2
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:          libpng-bin.tcz
Description:    bin files
Version:        1.6.40
Author:         PNG TEAM        
Original-site:  https://sourceforge.net/projects/libpng/files/  
Copying-policy: accompanied
Size:           32K
Extension_by:   aus9
Tags:           graphics png
Comments:       executables 
                
Changelog:      2013/05/18 v 1.6.2 (juanito)
                2014/09/26 v 1.6.13
                2018/01/24 v 1.6.34
                2021/02/13 v 1.6.37
Current:        2023/08/17 v 1.6.40 on 14x (aus9)' > $P-bin.tcz.info

readelf -d $P-bin/usr/local/bin/* | grep 'NEEDED'
#[libpng16.so.16] libpng
echo 'libpng.tcz' > $P-bin.tcz.dep

echo 'Title:          libpng-dev.tcz
Description:    dev files
Version:        1.6.40
Author:         PNG TEAM        
Original-site:  https://sourceforge.net/projects/libpng/files/  
Copying-policy: accompanied
Size:           64K
Extension_by:   aus9
Tags:           graphics png
Comments:       dev files
                
Changelog:      2013/05/18 v 1.6.2 (juanito)
                2014/09/26 v 1.6.13
                2018/01/24 v 1.6.34
                2021/02/13 v 1.6.37
Current:        2023/08/17 v 1.6.40 on 14x (aus9)' > $P-dev.tcz.info
echo 'libpng.tcz' > $P-dev.tcz.dep

echo 'Title:          libpng.tcz
Description:    image fomat
Version:        1.6.40
Author:         PNG TEAM        
Original-site:  https://sourceforge.net/projects/libpng/files/  
Copying-policy: accompanied
Size:           140K
Extension_by:   aus9
Tags:           graphics png
Comments:       libraries used by other programs for reading and writing png files.
                Designed as a replacement for gif and, to a lesser extent, tiff.
                Includes patch to include animated png functionality in libpng.
                
Changelog:      2013/05/18 v 1.6.2 (juanito)
                2014/09/26 v 1.6.13
                2018/01/24 v 1.6.34
                2021/02/13 v 1.6.37
Current:        2023/08/17 v 1.6.40 on 14x (aus9)' > $P.tcz.info

readelf -d $P/usr/local/lib/libpng16.so.16.40.0 | grep 'NEEDED' # none

# no dep

# pngfix sample.png -> IDAT OK  maximum 15 15 1602 9264 sample.png





