#!/bin/sh

set -efu

if [ -z "$AUTOPKGTEST_TMP" ]; then
    AUTOPKGTEST_TMP=tmp
    mkdir -p ${AUTOPKGTEST_TMP}
fi

export XDG_CONFIG_HOME=$AUTOPKGTEST_TMP/.config
export XDG_DATA_HOME=$AUTOPKGTEST_TMP/.local/share
export XDG_CACHE_HOME=$AUTOPKGTEST_TMP/.cache
export XDG_RUNTIME_DIR=$AUTOPKGTEST_TMP/runtime
mkdir -p $XDG_RUNTIME_DIR

(Xvfb :5 -screen 0 1600x1200x24 -ac -noreset -v -fbdir $AUTOPKGTEST_TMP/ >/dev/null 2>&1 &)

export DISPLAY=:5
export XAUTHORITY=/dev/null

cd $AUTOPKGTEST_TMP
mkdir -p home
cp /usr/bin/tracecompass .
sed -i "s/1.8/1.8 \-Dorg.eclipse.swt.internal.SessionManagerDBus.disable \-Duser.home=.\/home/g" tracecompass
./tracecompass &
sleep 10
pid=`pgrep java`
echo $pid
win=`xdotool search --sync --onlyvisible --all --pid $pid --name 'Trace Compass'`
echo "win=$win"
name=`xdotool getwindowname $win | xargs`
kill -9 $pid
echo "name=$name"
if [ "$name" = "Trace Compass" ]; then
echo "Test OK"
return 0
else
echo "name=$name"
echo "Test Failed"
return 1
fi
