#!/bin/bash

`hash fbterm /usr/libexec/ibus-fbterm-backend dbus-launch ibus-daemon` || exit 1

# ibus-daemon could exit after ibus-fbterm-backend run
# when 'ibus exit' was called.
pkill -9 ibus-daemon

TTY=`tty`

TTY_NUM=${TTY#/dev/tty}
[ ${#TTY} -eq ${#TTY_NUM} ] && TTY_NUM=${TTY#/dev/vc/}

if [ ${#TTY} -eq ${#TTY_NUM} ]; then
	echo "stdin isn't a interactive tty!"
	exit 1
fi

eval `dbus-launch --sh-syntax`

export DISPLAY=:9${TTY_NUM}.0

ibus-daemon -p disable &

fbterm "$@" /usr/libexec/ibus-fbterm-backend

ibus exit
kill $DBUS_SESSION_BUS_PID
