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

# checking if gcc supports -fno-rtti -fno-exceptions... no
CFLAGS="-march=i486 -mtune=i686 -Os -pipe   " 
CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe "
echo $CFLAGS && echo $CXXFLAGS

P=libpipeline
V=1.5.7
SRC=$P-main
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget libtool-dev automake git coreutils"
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 \
https://gitlab.com/$P/$P/-/archive/main/$P-main.zip " $USER
# downloads as .zip
unzip $P*.zip
# unpacks as libpipeline-1.5.7-c9662e67d575299df2b87bc70f0e4865f04a8f01
cd $SRC*
# libtoolize && autoreconf --install----configure.ac:119: error: required file 'gl/lib/Makefile.in' not found
# ^ not libEGL or other such but gnulib
./bootstrap  # 20 seconds
./configure --prefix=/usr/local --enable-static=no
make -j4 # seconds
make install-strip DESTDIR=/tmp/$P
cd /tmp

# no doc
#####
rm -rf $P/usr/local/share/man 

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

# main
#####
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v3' > $P/usr/local/share/doc/$P/COPYING

# 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:          libpipeline-dev.tcz
Description:    dev files 
Version:        1.5.7
Author:         Colin Watson  
Original-site:  https://gitlab.com/libpipeline/libpipeline
Copying-policy: GPL v3
Size:           8K		
Extension_by:   aus9 
Tags:           pipeline 
Comments:       Development files

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

echo 'libpipeline.tcz
libtool-dev.tcz 
automake.tcz 
git.tcz
coreutils.tcz' > $P-dev.tcz.dep

echo 'Title:          libpipeline.tcz
Description:    a C library 
Version:        1.5.7
Author:         Colin Watson
Original-site:  https://gitlab.com/libpipeline/libpipeline
Copying-policy: GPL v3
Size:           36K		
Extension_by:   aus9 
Tags:           pipeline 
Comments:       library is used to safely construct pipelines 
                between subprocesses
                
Change-log:     2023/08/22 v 1.5.7 on 14.x
Current:        2023/08/22  '  > $P.tcz.info

readelf -d $P/usr/local/lib/$P.so | grep 'NEEDED' # TCB only