#!/bin/sh
# Start the GUI and check that every QML module it imports is actually
# installed.  A missing QML module dependency does not break the build, only
# the installed application (see #1089101, #1142265), so it has to be caught
# by running the real thing.
set -e

: "${AUTOPKGTEST_TMP:=$(mktemp -d)}"
export HOME="$AUTOPKGTEST_TMP"
export XDG_RUNTIME_DIR="$AUTOPKGTEST_TMP/run"
mkdir -p "$XDG_RUNTIME_DIR"
chmod 700 "$XDG_RUNTIME_DIR"

# llvmpipe is enough for a smoke test, and no camera is needed to build the UI.
export QT_QUICK_BACKEND=software
export LIBGL_ALWAYS_SOFTWARE=1

log="$AUTOPKGTEST_TMP/webcamoid.log"

rc=0
xvfb-run -a --server-args='-screen 0 1024x768x24' \
    timeout -k 5 20 webcamoid --new-instance > "$log" 2>&1 || rc=$?

cat "$log"

# 124 means timeout(1) had to stop it, i.e. it was still running happily.
if [ "$rc" -ne 0 ] && [ "$rc" -ne 124 ]; then
    echo "ERROR: webcamoid exited with status $rc" >&2
    exit 1
fi

if grep -Eq 'is not installed|failed to load component' "$log"; then
    echo "ERROR: the QML engine reported missing modules or components" >&2
    exit 1
fi
