#!/bin/sh
### BEGIN INIT INFO
# Provides:          etcd
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: etcd daemon
# Description:       etcd - highly-available key value store
### END INIT INFO

NAME=etcd
DAEMON=/usr/bin/$NAME
DAEMON_USER=etcd
DAEMON_ARGS=""

# Exit if executable is not installed
[ -x $DAEMON ] || exit 0

set -a
ETCD_NAME="$(hostname)"
ETCD_DATA_DIR="/var/lib/etcd/default"
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
set +a

RETRY=TERM/30/KILL/5

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

[ -f /lib/lsb/init-functions ] || exit 1
# Define LSB log_* functions.
. /lib/lsb/init-functions

case "$1" in
    start)
        log_action_begin_msg "Starting $NAME"
        start-stop-daemon --start --exec $DAEMON \
          --chuid $DAEMON_USER --background -- $DAEMON_ARGS
        log_action_end_msg $?
    ;;
    debug)
        start-stop-daemon --start --exec $DAEMON --chuid $DAEMON_USER -- $DAEMON_ARGS
    ;;
    stop)
        log_action_begin_msg "Stopping $NAME"
        start-stop-daemon --stop --exec $DAEMON --retry=$RETRY
        log_action_end_msg $?
    ;;
    status)
        ## return status 0 if process is running.
        status_of_proc "$DAEMON" "$NAME"
    ;;
    restart|force-reload)
        $0 stop
        $0 start
    ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
        exit 1
    ;;
esac
