#!/bin/sh

### BEGIN INIT INFO
# Provides: datadog-agent-data-plane
# Short-Description: Start and stop datadog agent data plane
# Description: Datadog Agent Data Plane
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO

. /lib/lsb/init-functions

ETC_DIR="/etc/datadog-agent"
INSTALL_DIR="/opt/datadog-agent"
AGENTPATH="$INSTALL_DIR/embedded/bin/agent-data-plane"
PIDFILE="$INSTALL_DIR/run/agent-data-plane.pid"
AGENT_ARGS="run --config $ETC_DIR/datadog.yaml --pidfile $PIDFILE"
AGENT_USER="dd-agent"
NAME="datadog-agent-data-plane"
DESC="Datadog Agent Data Plane"



if [ ! -x $AGENTPATH ]; then
	echo "$AGENTPATH not found. Exiting $NAME"
	exit 0
fi

if [ -r "/etc/default/${NAME}" ]; then
	. "/etc/default/${NAME}"
fi


#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --test --quiet --pidfile $PIDFILE --user $AGENT_USER --startas $AGENTPATH > /dev/null \
		|| return 1
	start-stop-daemon --start --background --chuid $AGENT_USER --quiet --pidfile $PIDFILE --user $AGENT_USER --startas $AGENTPATH -- \
		$AGENT_ARGS \
		|| return 2
	# Add code here, if necessary, that waits for the process to be ready
	# to handle requests from services started subsequently which depend
	# on this one.  As a last resort, sleep for some time.
}


#
# Start the agent and wait for it to be up
#
start_and_wait()
{
	log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
		*)
			# check if the agent is running once per second for 5 seconds
			retries=5
			while [ $retries -gt 1 ]; do
				start-stop-daemon --start --test --quiet --pidfile $PIDFILE --user $AGENT_USER --startas $AGENTPATH
				if [ "$?" -eq "1" ]; then
					# We've started up successfully. Exit cleanly
					log_end_msg 0
					return 0
				else
					retries=$(($retries - 1))
					sleep 1
				fi
			done
			# After 5 tries the agent didn't start. Report an error
			log_end_msg 1

		;;
	esac
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	start-stop-daemon --stop --quiet --retry=30 --pidfile $PIDFILE --user $AGENT_USER --exec $AGENTPATH
	RETVAL="$?"
	rm -f $PIDFILE
	return $RETVAL
}

#
# Stop the agent and wait for it to be down
#
stop_and_wait()
{
	log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) log_end_msg 0 ;;
		*) log_end_msg 1 ;; # Failed to stop
	esac
}

# Action to take
case "$1" in
	start)
		if init_is_upstart; then
			exit 1
		fi
		if [ "$DATADOG_ENABLED" = "no" ]; then
			echo "Disabled via /etc/default/$NAME. Exiting."
			exit 0
		fi

		start_and_wait

		;;

	stop)
		if init_is_upstart; then
			exit 0
		fi

		stop_and_wait

		;;

	status)
		status_of_proc -p $PIDFILE $AGENTPATH $NAME && exit 0 || exit $?
		;;

	restart|force-reload)
		if init_is_upstart; then
			exit 1
		fi

		echo "Restarting $DESC"

		stop_and_wait
		start_and_wait

		;;

	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|status}"
		exit 1
		;;
esac

exit $?
