#!/bin/sh

# sanity-check
#
# Verify that the Apertis system basically works.
#
# Please add more basic checks here as you run into situations where they fail

# Copyright © 2015-2016 Collabora Ltd.
#
# SPDX-License-Identifier: MPL-2.0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

set -e

if [ "$(id -u)" != 0 ]; then
    echo "This test must be run as root" >&2
    exit 1
fi

pkglibdir="$(cd "$(dirname $0)/.." && pwd)"
common="${pkglibdir}/common"

failed=0

fail () {
    failed=1
    tag="$1"
    shift
    echo "TEST_RESULT:fail:$tag:$*" >&2
}

set -x

# This is just for information, so we ignore the exit status. Minimal
# images might legitimately not have these tools.
systemd-cgls -al --no-pager || :
pstree -lpu || :

getent passwd root || fail getent-passwd-root
getent passwd user || fail getent-passwd-user

env || fail lava-env
"$common/run-test-in-systemd" --name=system-env "$common/sanity-check-env" system || :
"$common/run-test-in-systemd" --name=user-env --user=user "$common/sanity-check-env" user || :

id || fail lava-id
"$common/run-test-in-systemd" --name=system-id id || :
"$common/run-test-in-systemd" --name=user-id --user=user id || :

test -S /run/dbus/system_bus_socket || fail system-bus-is-up
test -d /run/user/$(id -u user)/systemd || fail user-session-is-up
test -S /run/user/$(id -u user)/bus || fail user-bus-is-up

test -e /etc/debian_version || fail has-debian_version
cat /etc/os-release || fail os-release
grep -E '^NAME=("?)Apertis\1$' /etc/os-release || fail os-release-name
grep -E '^ID=("?)apertis\1$' /etc/os-release || fail os-release-id

if ! test -S /run/user/$(id -u user)/wayland-0; then
    "$common/run-test-in-systemd" --user=user --name=x11-works -- xterm -e /bin/true
fi

set +x  # remaining commands run are uninteresting

if test "x$failed" != "x0"; then
    echo "Failed, see above for details" >&2
    exit "$failed"
fi

echo "# Sanity check successful"
exit 0

# vim:set sw=4 sts=4 et: