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

case $(uname -m) in
	arm*)
	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 '
	;;
	aarch64)
	export CFLAGS='-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72   '
	export CXXFLAGS='-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72  '
	;;
esac
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS

P=man-db
V=2.11.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget xz libtool intltool libpipeline-dev \
gdbm-dev groff db-dev "
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

su -c "/usr/local/bin/wget -nc \
http://download.savannah.nongnu.org/releases/$P/$SRC.tar.xz " $USER
tar xvf $SRC*xz
cd $SRC 
./configure --prefix=/usr/local --disable-automatic-update --disable-setuid \
--sysconfdir=/usr/local/etc --disable-cache-owner --disable-manual \
--with-systemdtmpfilesdir=no --with-systemdsystemunitdir=no
make -j4 # 1M15
make install-strip DESTDIR=/tmp/$P
cd /tmp

# locale
#########
mkdir -p $P-locale/usr/local/share/man
mv $P/usr/local/share/locale $P-locale/usr/local/share/
mv $P/usr/local/share/man/it $P-locale/usr/local/share/man/

# dev needed to compile man-pages - but man-pages are any arch
rm -rf $P/usr/local/lib/man-db/*.la 

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

# TCZ them
#######
LIST="$P $P-locale"
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:          man-db-locale.tcz
Description:    locales
Version:        2.11.2
Author:         Colin Watson 
Original-site:  http://savannah.nongnu.org/projects/man-db
Copying-policy: GPL v3
Size:           312K		
Extension_by:   aus9 
Tags:           locale man-db man 
Comments:       for non-english users untested.
                Contains Italian man pages

Change-log:     2023/06/23 v 2.11.2 on 14.x 
Current:        2023/06/23   '  > $P-locale.tcz.info
echo 'man-db.tcz ' > $P-locale.tcz.dep

echo 'Title:          man-db.tcz
Description:    man page viewer 
Version:        2.11.2
Author:         Colin Watson 
Original-site:  http://savannah.nongnu.org/projects/man-db
Copying-policy: GPL v3
Size:           272K
Extension_by:   aus9 
Tags:           man-db man pages
Comments:       terminal software to read man pages 
                $ man <man-page-name>
 
                WARNING setting setuid for user=man has not been compiled so 
                $ mandb -t (and some others) will fail. Most people will only
                want to read man pages and not create them here. IMHO
               
Change-log:     2023/06/23 v 2.11.2 on 14.x 
Current:        2023/06/23  ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # delete duplicates TCBs
readelf -d $P/usr/local/sbin/accessdb | grep 'NEEDED'
readelf -d $P/usr/local/libexec/man-db/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/$P/*.so | grep 'NEEDED'
# [libmandb-2.11.2.so] this TCE
# [libman-2.11.2.so] "
# [libgdbm.so.6] gdbm
# [libpipeline.so.1] libpipeline
ldd $P/usr/local/bin/* | grep # check pcre/libffi/openssl
ldd $P/usr/local/sbin/* # check pcre/libffi/openssl 
ldd $P/usr/local/lib/$P/*.so # check pcre/libffi/openssl 
echo 'gdbm.tcz
libpipeline.tcz
groff.tcz 
less.tcz ' > $P.tcz.dep

# submitqc --libs rm -rf *.zsync


