#!/usr/bin/env bash
#Download glib-2.82.0.tar.xz from https://download.gnome.org/sources/glib
#Download libffi-3.4.5.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.6.tar.bz2
#Download pcre2_10.42-2_patch.zip from https://wrapdb.mesonbuild.com/v2/pcre2_10.42-2/get_patch
#Download elfutils from https://sourceware.org/elfutils/ftp/0.177/elfutils-0.177.tar.bz2

dflag=
pflag=
BASEPATH=/tmp/$USER/babeltrace-1
while getopts dp: name
do
    case $name in
    d)    dflag=1;;
    p)    pflag=1
          BASEPATH="$OPTARG";;
    ?)   printf "Usage: %s: [-a] [-p <build path>] args\n" $0
          exit 2;;
    esac
done


mkdir -p $BASEPATH
cp libffi-3.4.5.tar.gz $BASEPATH
cp glib-2.82.0.tar.xz $BASEPATH
cp babeltrace2-2.0.6.tar.bz2 $BASEPATH
cp pcre2-10.42.tar.bz2 $BASEPATH
cp pcre2_10.42-2_patch.zip $BASEPATH
cp elfutils-0.177.tar.bz2 $BASEPATH

#These may not take on some older servers when run from a script. In that case it is recommended run these before starting the script
module add python/3.9.5
module add ninja/1.9.0
module add gcc/9.3.0
module add git #Required for newer versions of glib

#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/pythonmodules 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 https_proxy=www-proxy.seli.gic.ericsson.se:8080
export http_proxy=www-proxy.seli.gic.ericsson.se:8080

export PYTHONPATH="$HOME/pythonmodules:$HOME/pythonmodules:$PYTHONPATH"
export PATH="$HOME/pythonmodules/bin:$HOME/pythonmodules/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 xf libffi-3.4.5.tar.gz
cd libffi-3.4.5/
env CFLAGS="-fPIC" ./configure --prefix="$BASEPATH/libffi-3.4.5-bin" && make install
cd ..
export PKG_CONFIG_PATH="$BASEPATH/libffi-3.4.5-bin/lib/pkgconfig:$PKG_CONFIG_PATH"

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

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

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$BASEPATH/glib-2.82.0-install/lib64/pkgconfig/"


if [ ! -z "$dflag" ]; then
    tar xf elfutils-0.177.tar.bz2
    cd elfutils-0.177
    env CFLAGS="-Wno-error=unused-result" ./configure --prefix "$BASEPATH/elfutils-0.177-bin" && make install
    cd ..
    export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$BASEPATH/elfutils-0.177-bin/lib/pkgconfig"

    tar xf babeltrace2-2.0.6.tar.bz2
    cd babeltrace2-2.0.6
    patch configure /tmp/epatrek/babeltrace_configure.patch
    export BABELTRACE_TRACE_MINIMAL_LOG_LEVEL=TRACE
    export BABELTRACE_DEV_MODE=1
    env CFLAGS="-g -O0 -I$BASEPATH/glib-2.82.0-install/include -I$BASEPATH/glib-2.82.0-install/lib64/glib-2.0/include -I$BASEPATH/elfutils-0.177-bin/include" LDFLAGS="-Wl,-rpath=$BASEPATH/glib-2.82.0-install/lib64 -L$BASEPATH/elfutils-0.177-bin/lib -L$BASEPATH/glib-2.82.0-install/lib64" LD_LIBRARY_PATH=$BASEPATH/glib-2.82.0-install/lib64 LIBRARY_PATH=$BASEPATH/glib/glib-2.82.0-install/lib64 ./configure --enable-built-in-plugins --disable-shared && make
    cd ..
else
    tar xf babeltrace2-2.0.6.tar.bz2
    cd babeltrace2-2.0.6
    env CFLAGS="-g -O2 -I$BASEPATH/glib-2.82.0-install/include -I$BASEPATH/glib-2.82.0-install/lib64/glib-2.0/include" LDFLAGS="-Wl,-rpath=$BASEPATH/glib-2.82.0-install/lib64 -L$BASEPATH/glib-2.82.0-install/lib64" LD_LIBRARY_PATH=$BASEPATH/glib-2.82.0-install/lib64 LIBRARY_PATH=$BASEPATH/glib/glib-2.82.0-install/lib64 ./configure --enable-built-in-plugins --disable-debug-info --disable-shared && make
    cd ..
fi

#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
