#!/usr/bin/sh
# -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*-
# vim:set noet ts=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2018-2024 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This test runs /usr/bin/ibus-daemon after install ibus


PROGNAME=`basename $0`
TEST_CASE_DIR="/usr/share/installed-tests"
MODULE_SH='ibus-desktop-testing-module'
MODULE_SH_PATH="/usr/libexec/$MODULE_SH"
AUTOSTART_DESKTOP_DIR="/usr/share/ibus/tests"
TEST_LOG="test-suite.log"
TESTING_RUNNER="default"


if test -f $MODULE_SH_PATH ; then
  . $MODULE_SH_PATH
elif test -f $(dirname $0)/$MODULE_SH ; then
  . $(dirname $0)/$MODULE_SH
else
  echo "Not found $MODULE_SH"
  exit 1
fi


usage()
{
    command=""
    for element in $SESSION_COMMAND; do
        if test x"$element" = x; then
            continue
        fi
        if echo "$element" | grep -q -E "^-" ; then
            continue
        fi
        command="$element"
    done
    $ECHO -e \
"This test runs /usr/bin/ibus-daemon after install ibus\n"                     \
"$PROGNAME [OPTIONS…]\n"                                                       \
"\n"                                                                           \
"OPTIONS:\n"                                                                   \
"-h, --help                       This help\n"                                 \
"-v, --version                    Show version\n"                              \
"-b, --builddir=BUILDDIR          Set the BUILDDIR\n"                          \
"-s, --srcdir=SOURCEDIR           Set the SOURCEDIR\n"                         \
"-c, --no-graphics                Use Xvfb instead of Xorg or Wayland\n"       \
"-V, --verbose                    Verbose log for ibus-daemon\n"               \
"-d, --session=SESSION            Run SESSION. The default is ${command}.\n"   \
"                                 Suffix '-with-dbus' can run SESSION with dbus session.\n" \
"                                 E.g. --session=${command}-with-dbus\n"       \
"-l, --lang=LANG                  Run SESSION with LANG\n"                     \
"-t, --tests=\"TESTS...\"           Run TESTS programs which is separated by space\n" \
"-r, --runner=RUNNER              Run TESTS programs with a test RUNNER.\n"    \
"                                 RUNNDER = 'gnome' or 'default'.\n"           \
"                                 'default' is an embedded runner.\n"          \
"-i, --no-systemd                 Run gnome-seesion directly withoout systemd\n" \
"                                 and login manager for legacy GNOME and this\n" \
"                                 mode does not support Wayland.\n" \
"-T, --timeout=TIMEOUT            Set timeout (default TIMEOUT is 300 sec).\n" \
"-o, --output=OUTPUT_FILE         Output the log to OUTPUT_FILE\n"             \
"                                 default is $TEST_LOG\n"                      \
"-O, --result=RESULT_FILE         Output the result to RESULT_FILE\n"          \
"                                 default is stdout\n"                         \
"-S, --screendump=DUMP_FILE       Output the screen to DUMP_FILE ('STDOUT' can be stdout)\n" \
"-D, --delete-tests               Delete test enviroments and user $TEST_USER\n" \
""
}


main()
{
    parse_args "$@"
    init_session
    run_dbus_daemon 2>>$TEST_LOG 1>>$TEST_LOG
    run_session 2>>$TEST_LOG 1>>$TEST_LOG
    save_screen
    run_test_suite
    finit
}


# Need to enclose $@ with double quotes not to split the array.
main "$@"
