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

# cheats off LFS and Paul_123 
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=boost
P1=$P-1.83
V=1.83.0
SRC=boost_1_83_0
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 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
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
rm -rf libharfbuzz.la
rm -rf libharfbuzz-subset.la
rm -rf libharfbuzz-gobject.la
rm -rf libgamin-1.la
rm -rf libfam.la
rm -rf libgraphite2.la
rm -rf libfreetype.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://boostorg.jfrog.io/artifactory/main/release/1.83.0/source/boost_1_83_0.tar.bz2 " $USER
tar jxvf $P*bz2 # 2M
cd $SRC
sed -i '/#include.*phoenix.*tuple.hpp.*/d'  boost/phoenix/stl.hpp
./bootstrap.sh --prefix=/usr/local --with-python=python3     # 1M45
./b2 stage -j4 --prefix=/usr/local link=shared variant=release threading=multi \
runtime-link=shared # 21M
touch /tmp/mark
./b2 install --prefix=/usr/local link=shared variant=release threading=multi \
runtime-link=shared # 3M
find /usr/local/ -newer /tmp/mark -not -type d > /tmp/full.list
cd /tmp 

# dev
#####
mkdir -p $P1-dev/usr/local/include
mv /usr/local/include/$P $P1-dev/usr/local/include/
mkdir -p $P1-dev/usr/local/lib/
mv /usr/local/lib/libboost_test_exec_monitor.a $P1-dev/usr/local/lib/
mv /usr/local/lib/libboost_exception.a $P1-dev/usr/local/lib/
mv /usr/local/lib/cmake $P1-dev/usr/local/lib/

# main
######
mkdir -p $P1/usr/local/share/doc/$P1
cp $SRC/LICENSE_1_0.txt $P1/usr/local/share/doc/$P1/LICENCE
mkdir -p $P1/usr/local/lib
mv /usr/local/lib/lib$P* $P1/usr/local/lib

# TCZ them
#######
LIST="$P1 $P1-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:          boost-1.83-dev.tcz
Description:    dev files
Version:        1.83.0
Author:         Boost Team
Original-site:  https://www.boost.org
Copying-policy: accompanied
Size:           31.4M
Extension_by:   aus9
Tags:           C++
Comments:       dev files

Change-log:     2023/08/18 v 1.83.0 14.x
Current:        2023/08/18    ' > $P1-dev.tcz.info
echo 'boost-1.83.tcz' > $P1-dev.tcz.dep

echo 'Title:          boost-1.83.tcz
Description:    C++ source libraries
Version:        1.83.0
Author:         Boost Team
Original-site:  https://www.boost.org
Copying-policy: accompanied
Size:           4.3M
Extension_by:   aus9
Tags:           C++ 
Comments:       Boost provides a set of free peer-reviewed 
                portable C++ source libraries.
                Likely to conflict with boot TCE if loaded

Change-log:     2023/08/18 v 1.83.0 14.x
Current:        2023/08/18  ' > $P1.tcz.info
 
readelf -d $P1/usr/local/lib/*.so | grep 'NEEDED'# delete TCBs duplicates
#[libboost_context.so.1.83.0]this TCE
#[libboost_filesystem.so.1.83.0]"
#[libboost_atomic.so.1.83.0]"
#[libboost_regex.so.1.83.0]"
#[libboost_container.so.1.83.0]"
#[libboost_thread.so.1.83.0]"
#[libboost_chrono.so.1.83.0]"
#[libboost_log.so.1.83.0]"
#[libboost_serialization.so.1.83.0]"

# no main dep

