#! /bin/sh

NAME=prosody

# This must match the pidfile field in
# /etc/prosody/prosody.cfg.lua

PIDDIR=/var/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid

case "$1" in
  start)
    printf "Starting $NAME: "
    mkdir -p $PIDDIR
    chown $NAME:$NAME $PIDDIR
    start-stop-daemon -S -q -o -x /usr/bin/prosody -c $NAME
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;
  stop)
    printf "Stopping $NAME: "
    # The standard method of identifying the name doesn't
    # work as the process name is lua. So use the pidfile
    # which is created by the service itself as a match
    # criteria when stopping the service
    start-stop-daemon -K -q -o -p "$PIDFILE"
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;
  restart|reload)
    echo "Restarting $NAME: "
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}" >&2
    exit 1
    ;;
esac

exit 0
