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

# no loading any locales please
# env: can't execute 'bash': No such file or directory
###############################
# etc/passwd changed sh to bash----add bash to boot list
#################################
# Export variables needed for compilation
# ninja error exception handling disabled, use '-fexceptions' to enable
# ninja error error: 'dynamic_cast' not permitted with '-fno-rtti'
CFLAGS="-march=i486 -mtune=i686 -Os -pipe   " 
CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fexceptions "
echo $CFLAGS && echo $CXXFLAGS

P=vte
P1=$P-2.91
V=0.70.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="bash ninja-py3.9 compiletc submitqc cmake meson gtk3-dev libglade-dev pcre2-dev  "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

rm -rf /usr/local/bin/python3
ln -s /usr/local/bin/python3.9 /usr/local/bin/python3
python3.9 -m ensurepip --upgrade
# At time of write installs pip-20.2.3
python3.9 -m pip install dataclasses


# 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://gitlab.gnome.org/GNOME/$P/-/archive/$V/$SRC.zip  " $USER 
unzip $SRC.zip
cd $SRC
# ninja error for icu-uc so disabled 
# ninja error FAILED: bindings/gir/Vte-3.91.gir   note three.91 not two.91 disable gir 

mkdir build && cd build # debugg below is correct spelling
meson setup --prefix=/usr/local -D_b_symbolic_functions=false -Ddebugg=false \
-Ddocs=false -Dgnutls=true -Dgtk3=true -Dgtk4=false  -Dicu=false \
-D_systemd=false -Dvapi=false -Dglade=true -Dstrip=true -Dgir=false
# Program python3 (dataclasses) found: YES made above
##################
# Version:      0.70.2
# C compiler:   gcc (version 12.2.0)
# C++ compiler: gcc (version 12.2.0)
# Coverage:     false
# Debug:        false
# A11y:         true
# Docs:         false
# FRIBIDI:      true
# GNUTLS:       true
# GTK+ 3.0:     true
# GTK+ 4.0:     false
# ICU:          false
# GIR:          false
# systemd:      false
# Glade:        true
# Vala:         false
# Prefix:       /usr/local
# strip         : true
# _b_symbolic_functions: false
# _systemd      : false
# debugg        : false
# docs          : false
# gir           : false
# vapi          : false

ninja # 30s
ninja test # 11/11 pass in 1M
ninja install # DESTDIR fails
cd /tmp

# locale
######
mkdir -p $P1-locale/usr/local/share
mv /usr/local/share/locale $P1-locale/usr/local/share

# dev
#####
mkdir -p $P1-dev/usr/local/lib/pkgconfig
mkdir -p $P1-dev/usr/local/include
mv /usr/local/include/$P1 $P1-dev/usr/local/include
mv /usr/local/lib/pkgconfig/$P1.pc $P1-dev/usr/local/lib/pkgconfig

# main 
####
mkdir -p $P1/usr/local/share/doc/$P1
echo 'GPL v3 ' > $P1/usr/local/share/doc/$P1/COPYING
mkdir -p $P1/usr/local/lib
mkdir -p $P1/usr/local/libexec
mkdir -p $P1/usr/local/etc/profile.d
mkdir -p $P1/usr/local/bin
mv /usr/local/bin/$P1 $P1/usr/local/bin
mv /usr/local/etc/profile.d/vte* $P1/usr/local/etc/profile.d
mv /usr/local/lib/libvte-2.91* $P1/usr/local/lib
mv /usr/local/libexec/vte* $P1/usr/local/libexec

# TCZ them
###########
LIST2="$P1 $P1-dev $P1-locale"
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

# info files 
######
echo 'Title:          vte-2.91-dev.tcz
Description:    dev files
Version:        0.70.2
Author:         Gnome vte team
Original-site:  https://gitlab.gnome.org/GNOME/vte
Copying-policy: GPL v3
Size:           20K	
Extension_by:   aus9
Tags:           terminal vte 
Comments:       Development files

Change-log:     2023/08/03 v 0.70.2 on 14.x 
Current:        2023/08/03'  > $P1-dev.tcz.info
echo 'vte-2.91.tcz
gtk3-dev.tcz
libglade-dev.tcz 
pcre2-dev.tcz ' > $P1-dev.tcz.dep

echo 'Title:          vte-2.91-locale.tcz
Description:    locales 
Version:        0.70.2
Author:         Gnome vte team
Original-site:  https://gitlab.gnome.org/GNOME/vte
Copying-policy: GPL v3
Size:           88K
Extension_by:   aus9
Tags:           locales vte
Comments:       locales not tested by me

Change-log:     2023/08/03 v 0.70.2 on 14.x 
Current:        2023/08/03' > $P1-locale.tcz.info
echo 'vte-2.91.tcz' > $P1-locale.tcz.dep

echo 'Title:          vte-2.91.tcz
Description:    Virtual Terminal Emulator
Version:        0.70.2
Author:         Gnome vte team
Original-site:  https://gitlab.gnome.org/GNOME/vte
Copying-policy: GPL v3
Size:           368K
Extension_by:   aus9
Tags:           terminal gtk3
Comments:       May conflict with vte-gtk3 TCE

Change-log:     2023/08/03 v 0.70.2 on 14.x 
Current:        2023/08/03' > $P1.tcz.info


readelf -d $P1/usr/local/bin/$P1 | grep 'NEEDED'
#[libvte-2.91.so.0] this TCE
#[libgio-2.0.so.0][libgobject-2.0.so.0][libglib-2.0.so.0] glib2 cairo gtk3 
#[libpango-1.0.so.0]    pango gtk3
#[libgtk-3.so.0][libgdk-3.so.0] gtk3
#[libcairo-gobject.so.2]cairo pango gtk3
#[libcairo.so.2]     cairo pango gtk3
#[libpangocairo-1.0.so.0] pango ..gtk3
#[libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 gtk3 
readelf -d $P1/usr/local/lib/* | grep 'NEEDED' # extras
#[libfribidi.so.0]fribidi pango gtk3
#[libgnutls.so.30]   gnutls3.6
#[libpcre2-8.so.0]   pcre2
#[libatk-1.0.so.0]  atk...gtk3

echo 'gtk3.tcz
gnutls3.6.tcz 
pcre2.tcz '  $P1.tcz.dep 


# reverse etc/passwd and bash in bootlist and full reboot please
      



