#!/bin/bash
set -e

ABS_BUILDDIR='@ABS_BUILDDIR@'
ABS_SRCDIR='@ABS_SRCDIR@'

if [ "${PHOSH_VALGRIND}" = 1 ]; then
   echo "Running phosh under valgrind"
   WRAPPER="valgrind -v              \
           --tool=memcheck           \
           --leak-check=full         \
           --leak-resolution=high    \
           --num-callers=40          \
           --log-file=vgdump         \
           --suppressions=${ABS_SRCDIR}/data/valgrind.suppressions \
           --suppressions=/usr/share/glib-2.0/valgrind/glib.supp \
   "
elif [ "${PHOSH_GDB}" = 1 ]; then
    echo "Running phosh under gdb"
    WRAPPER="gdb --args"
elif [ "${PHOSH_AUTO_GDB}" = 1 ]; then
    echo "Running phosh under gdb"
    WRAPPER="gdb -batch -ex run -ex bt --args"
fi

SCHEMAS=(
         "${ABS_BUILDDIR}/data:"
         "${ABS_BUILDDIR}/plugins/launcher-box:"
         "${ABS_BUILDDIR}/plugins/pomodoro-quick-setting"
         "${ABS_BUILDDIR}/plugins/ticket-box:"
         "${ABS_BUILDDIR}/plugins/upcoming-events:"
        )
GSETTINGS_SCHEMA_DIR=$(echo "${SCHEMAS[*]}" | tr -d ' ')${GSETTINGS_SCHEMA_DIR}
export GSETTINGS_SCHEMA_DIR

[ -n "${G_MESSAGES_DEBUG}" ] || G_MESSAGES_DEBUG=all
export G_MESSAGES_DEBUG

if [ -z "${GSETTINGS_BACKEND}" ]; then
  # Make sure we don't mess with the systems gsettings:
  export GSETTINGS_BACKEND=memory
fi

# Ensure that we don't filter out wanted app launcher, i.e. p-m-s or g-c-c
export XDG_CURRENT_DESKTOP="Phosh:GNOME"

set -x
exec ${WRAPPER} "${ABS_BUILDDIR}/src/phosh" -U $@
