#!/bin/sh

# test DISPLAY
if [ "x$DISPLAY" != "x" ]; then
    number=`echo $DISPLAY | sed 's|\:\([0-9]\+\)\(\..*\)\?|\1|g'`
    if [ "x$number" = "x" ]; then
        exit 1
    fi
else
    number=0
fi

# find dbus-send
DBUS_SEND=`which dbus-send`

if [ ! -x "${DBUS_SEND}" ]; then
    echo "Cannot find dbus-send"
    exit 1
fi

# use dbus-send to check fcitx is running or not
"${DBUS_SEND}" --print-reply --type=method_call --session --dest=org.fcitx.Fcitx-$number /inputmethod org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1

if [ $? = "1" ]; then
    echo "Fcitx seems is not running"
    fcitx
else
    echo "Fcitx is running correctly."
fi
