#! /bin/sh

### BEGIN INIT INFO
# Provides:             catblock-daemon
# Required-Start:       $remote_fs $syslog dbus catblock-daemon
# Required-Stop:        $remote_fs $syslog dbus catblock-daemon
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Blacklist daemon
# Description:          This init script starts the alarm daemon software
#                       used on the maemo platform.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/catblock-daemon
NAME=catblock-daemon
DESC="CATblock daemon"
INITFILE=/etc/init.d/$NAME
DSMETOOL=/usr/sbin/dsmetool
DAEMON_OPTS=""

# 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"

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

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

case "$1" in
  start)
    printf "Starting $DESC: $NAME\n"
    start_catblock
    ;;

  stop)
    printf "Stopping: $DESC: $NAME\n"
    stop_catblock
    ;;

  restart|force-reload)
    printf "Restarting $DESC: $NAME\n"
    stop_catblock
    sleep 2
    start_catblock
    ;;

  *)
    printf >&2 "Usage: $INITFILE {start|stop|restart|force-reload}\n"
    exit 1
    ;;
esac

exit 0
