#!/usr/bin/env sh
BASEDIR=$(dirname $0)
cd "$BASEDIR/.."

PATH=$BASEDIR/../venv/bin:$PATH

SUPERVISOR_NOT_RUNNING="Supervisor is not running"
SUPERVISOR_CONF_FILE='supervisord/supervisord.conf'
SOCK_FILE='supervisord/agent-supervisor.sock'
action=$1

if [ ! -n "$action" ]; then
    action="start"
fi

supervisor_running() {
    # Returns true if the supervisor is running, and false if not.
    # We check if the supervisor is running by checking if
    # SOCK_FILE exists.
    [ -e $SOCK_FILE ]
}

execute_if_supervisor_running() {
    # Executes `supervisord -c $SUPERVISOR_CONF_FILE $*`
    # if the supervisor is running.
    if supervisor_running; then
        supervisorctl -c $SUPERVISOR_CONF_FILE "$*"
        return $?
    else
        echo $SUPERVISOR_NOT_RUNNING
        return 0
    fi
}

check_agent_status() {


    # First, we check if supervisor is running:
    if ! supervisor_running; then
        echo $SUPERVISOR_NOT_RUNNING
        return 1
    else
        # Next, we check if supervisor is running all the datadog processes:

        # Check the number of datadog processes supervisor is
        # currently controlling, and make sure that it's the
        # same as the number of programs specified in the
        # supervisor config file:
        status_output=$(supervisorctl -c $SUPERVISOR_CONF_FILE status)
        datadog_supervisor_processes=$(echo "$status_output" |
                                       grep -v pup |
                                       grep 'datadog-agent' |
                                       grep -c RUNNING)
        supervisor_config_programs=$(grep -v pup $SUPERVISOR_CONF_FILE |
                                     grep -c '\[program:')

        echo "$status_output"
        if [ "$datadog_supervisor_processes" -ne "$supervisor_config_programs" ]; then
            echo "Supervisor is NOT running all child processes"
            return 1
        else
            echo "Supervisor is running all child processes"
            return 0
        fi
    fi
}

case $action in
    start)
        if supervisor_running; then
            echo "Supervisor is already running"
            exit 0
        else
            echo "Starting supervisor"
            supervisord -c $SUPERVISOR_CONF_FILE
            # Since the above command currently runs in the foreground, we don't
            # have to worry about checking the status of the Agent and setting
            # an exit code here.
        fi
        ;;

    stop)
        execute_if_supervisor_running stop all
        exit $?
        ;;

    restart)
        # FIXME: This command won't do anything if the supervisor isn't running.
        # The restart command should just call stop and then start so it doesn't
        # rely on the supervisor running. Once we stop starting the Agent in the
        # foreground we should fix this.
        execute_if_supervisor_running restart all
        exit $?
        ;;

    status)
        check_agent_status
        exit $?
        ;;

    info)
        shift # shift to pass the remaining arguments to agent/agent.py info.
              # Currently only agent.py takes additional arguments
        python agent/agent.py info $@
        RETURN_VALUE=$?
        python agent/dogstatsd.py info
        RETURN_VALUE=$(($RETURN_VALUE || $?))
        python agent/ddagent.py info
        RETURN_VALUE=$(($RETURN_VALUE || $?))
        exit $RETURN_VALUE
        ;;

    configcheck)
        python agent/agent.py configcheck
        exit $?
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|info|status|configcheck}"
        exit 2
        ;;
esac

