#!/bin/bash
### BEGIN INIT INFO
# Provides:          marathon
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Init for Apache Mesos
# Description:       Cluster-wide init and control system for Apache Mesos
### END INIT INFO
set -ue

NAME="marathon"
DESC="marathon"

. /lib/lsb/init-functions

PID=/var/run/marathon.pid

start() {
  start-stop-daemon --start --background --quiet \
                    --pidfile "$PID" --make-pidfile \
                    --exec /usr/bin/marathon -- --jar /usr/share/java/marathon-runnable.jar
}

stop() {
  start-stop-daemon --stop --quiet --pidfile "$PID"
}

case "${1-}" in
  start)
    echo -n "Starting $DESC: "
    start
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    stop
    rm -f "$PID"
    echo "$NAME."
    ;;
  restart)
    echo -n "Restarting $DESC: "
    stop
    sleep 1
    start
    echo "$NAME."
    ;;
  status)
    status_of_proc -p "$PID" "$NAME" "$NAME"
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}" >&2
    exit 1
    ;;
esac
