#Download glib-2.76.5.tar.xz from https://download.gnome.org/sources/glib
#Download libffi-3.4.4.tar.gz from https://sourceware.org/libffi/
#Download https://github.com/PhilipHazel/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.bz2
#Download https://www.efficios.com/files/babeltrace/babeltrace2-2.0.5.tar.bz2
#Download pcre2_10.42-2_patch.zip from https://wrapdb.mesonbuild.com/v2/pcre2_10.42-2/get_patch


BASEPATH=/tmp/$USER/babeltrace-3
mkdir -p $BASEPATH
cp libffi-3.4.4.tar.gz $BASEPATH
cp glib-2.76.5.tar.xz $BASEPATH
cp  babeltrace2-2.0.5.tar.bz2 $BASEPATH
cp pcre2-10.42.tar.bz2 $BASEPATH
cp pcre2_10.42-2_patch.zip $BASEPATH

module add python/3.9.5
module add ninja/
module add gcc/9.3.0

#First time
export https_proxy=
pip3 install --index-url https://arm.sero.gic.ericsson.se/artifactory/api/pypi/proj-exilis-3pp-pypi-local/simple     --extra-index-url https://arm.sero.gic.ericsson.se/artifactory/api/pypi/proj-exilis-dev-pypi-local/simple     --extra-index-url https://arm.seli.gic.ericsson.se/artifactory/api/pypi/pypi-remote/simple --target $HOME/python-modules meson
pip3 install --index-url https://arm.sero.gic.ericsson.se/artifactory/api/pypi/proj-exilis-3pp-pypi-local/simple     --extra-index-url https://arm.sero.gic.ericsson.se/artifactory/api/pypi/proj-exilis-dev-pypi-local/simple     --extra-index-url https://arm.seli.gic.ericsson.se/artifactory/api/pypi/pypi-remote/simple --target $HOME/pythonmodules ninja

export PYTHONPATH="$HOME/pythonmodules:$HOME/python-modules:$PYTHONPATH"
export PATH="$HOME/pythonmodules/bin:$HOME/python-modules/bin:$PATH"
export CC=/app/vbuild/SLED11-x86_64/gcc/9.3.0/bin/gcc
export CXX=/app/vbuild/SLED11-x86_64/gcc/9.3.0/bin/gcc

#verify successful installation:
which ninja
which meson

cd "$BASEPATH"
tar xvf libffi-3.4.4.tar.gz
cd libffi-3.4.4/
env CFLAGS="-fPIC" ./configure --prefix="$BASEPATH/libffi-3.4.4-bin" && make install
cd ..
export PKG_CONFIG_PATH="$BASEPATH/libffi-3.4.4-bin/lib/pkgconfig:$PKG_CONFIG_PATH"

tar xvf glib-2.76.5.tar.xz
mkdir glib-2.76.5/subprojects/packagecache/
cp pcre2-10.42.tar.bz2 glib-2.76.5/subprojects/packagecache/.
cp pcre2_10.42-2_patch.zip glib-2.76.5/subprojects/packagecache/.

patch glib-2.76.5/meson.build ~/moshell/commonjars/src/lttng/meson.build.patch
cd glib-2.76.5/
meson setup --prefix "$BASEPATH/glib-2.76.5-install" _build
meson compile -C _build
meson install -C _build
cd ..

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$BASEPATH/glib-2.76.5-install/lib64/pkgconfig/"
tar xvf babeltrace2-2.0.5.tar.bz2
cd babeltrace2-2.0.5
env CFLAGS="-g -O2 -I$BASEPATH/glib-2.76.5-install/include -I$BASEPATH/glib-2.76.5-install/lib64/glib-2.0/include" LDFLAGS="-Wl,-rpath=$BASEPATH/glib-2.76.5-install/lib64 -L$BASEPATH/glib-2.76.5-install/lib64" LD_LIBRARY_PATH=$BASEPATH/glib-2.76.5-install/lib64 LIBRARY_PATH=$BASEPATH/glib/glib-2.76.5-install/lib64 ./configure --enable-built-in-plugins --disable-debug-info --disable-shared && make
cd ..
#Then copy ./src/cli/babeltrace2.bin to moshell/commonjars/lttng/lin64/babeltrace2 and strip it. Also do "ldd" on that file to check the related glib .so files , strip them and include them in the moshell/commonjars/lttng/lin64 folder as well