#!/bin/sh

# Copyright (C) 2009 Nokia Corporation
# Copyright (C) 2009 Collabora Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

cd "@abs_top_builddir@/tests/twisted/tools"

G_MESSAGES_DEBUG=all
export G_MESSAGES_DEBUG
ulimit -c unlimited
exec > missioncontrol-$$.log 2>&1
ln -f missioncontrol-$$.log missioncontrol.log

if test -z "$MC_ACCOUNT_DIR"; then
        echo "MC_ACCOUNT_DIR must be set"
        exit 1
fi

if test -n "$MISSIONCONTROL_TEST_VALGRIND"; then
        G_DEBUG="$G_DEBUG,gc-friendly"
        export G_DEBUG
        G_SLICE=always-malloc
        export G_SLICE
	MISSIONCONTROL_WRAPPER="valgrind --leak-check=full --num-callers=20"
	MISSIONCONTROL_WRAPPER="$MISSIONCONTROL_WRAPPER --track-origins=yes"
	MISSIONCONTROL_WRAPPER="$MISSIONCONTROL_WRAPPER -v"
	MISSIONCONTROL_WRAPPER="$MISSIONCONTROL_WRAPPER --suppressions=@abs_top_srcdir@/tests/twisted/tools/valgrind.supp"
	MISSIONCONTROL_WRAPPER="$MISSIONCONTROL_WRAPPER --gen-suppressions=all"
	MISSIONCONTROL_WRAPPER="$MISSIONCONTROL_WRAPPER --show-reachable=yes"
elif test -n "$MISSIONCONTROL_TEST_REFDBG"; then
        if test -z "$REFDBG_OPTIONS" ; then
                REFDBG_OPTIONS="btnum=10"
                export REFDBG_OPTIONS
        fi
        if test -z "$MISSIONCONTROL_WRAPPER" ; then
                MISSIONCONTROL_WRAPPER="refdbg"
        fi
elif test -n "$MISSIONCONTROL_TEST_BACKTRACE"; then
        MISSIONCONTROL_WRAPPER="gdb -x @abs_top_srcdir@/tools/run_and_bt.gdb"
fi

# The bus-daemon that is activating us doesn't know it's also the system bus
DBUS_SYSTEM_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS"
export DBUS_SYSTEM_BUS_ADDRESS

if test "z$MC_EXECUTABLE" = z; then
        MC_EXECUTABLE=@abs_top_builddir@/tests/twisted/mc-debug-server
fi

if test -f "$MC_ACCOUNT_DIR/gnome-keyring-env"; then
        cat "$MC_ACCOUNT_DIR/gnome-keyring-env"
        . "$MC_ACCOUNT_DIR/gnome-keyring-env"
        export GNOME_KEYRING_CONTROL GNOME_KEYRING_PID MC_KEYRING_NAME SSH_AUTH_SOCK
fi

GSETTINGS_SCHEMA_DIR=@abs_top_builddir@/data
export GSETTINGS_SCHEMA_DIR

exec @abs_top_builddir@/libtool --mode=execute \
        $MISSIONCONTROL_WRAPPER \
        $MC_EXECUTABLE
