#!/bin/sh
#
# Init script for Maliit
#
### BEGIN INIT INFO
# Provides:             maliit-server
# Required-Start:       $remote_fs dbus
# Required-Stop:        $remote_fs dbus
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Maliit IM daemon
# Description:          This init script starts the maliit daemon software
#                       used on the maemo platform.
### END INIT INFO

PROGRAM=maliit-server
DAEMON=/usr/bin/$PROGRAM
NAME=maliit
DESC="Maliit IM Context Framework server"
INITFILE=/etc/init.d/$NAME
DSMETOOL=/usr/sbin/dsmetool
OPTIONS="-software -use-self-composition -graphicssystem raster"

# abort if simple command fails
set -e

# abort if no executable exists
test -x $DAEMON

# only use dsmetool if it exists
test -x $DSMETOOL || USE_DSMETOOL="no"

# source init functions
. /lib/lsb/init-functions

start() {
  if [ -h /targets/links/scratchbox.config ]; then
    echo "SCRATCHBOX: you need to start $NAME manually"
  elif [ "$USE_DSMETOOL" = "no" ]; then
    start-stop-daemon --start --background --quiet --exec $1 -- $2
  else
    $DSMETOOL -U user -G users -f "$1 $2"
  fi
}

stop()
{
  if [ -h /targets/links/scratchbox.config ]; then
    echo "SCRATCHBOX: you need to stop $NAME manually"
  elif [ "$USE_DSMETOOL" = "no" ]; then
    start-stop-daemon --stop --oknodo --quiet --exec $1
  else
    $DSMETOOL -U user -G users -k "$1 $2"
  fi
}

case "$1" in
  start)
        log_begin_msg "Starting $DESC..."
	export QT_IM_MODULE=Maliit
	start $DAEMON "$OPTIONS"
        log_end_msg $?
        ;;

  stop)
        log_begin_msg "Stopping $DESC..."
	stop $DAEMON "$OPTIONS"
	unset QT_IM_MODULE
        log_end_msg $?
        ;;

  restart|force-reload)
        $0 stop
        sleep 1
        $0 start
        ;;
  *)
        N=/etc/osso-af-init/$NAME
        log_success_msg "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
