#!/bin/bash
### BEGIN INIT INFO
# Provides:          yandex-bugbounty-internal
# Required-Start:    rsyslog rabbitmq-server $network $remote_fs
# Required-Stop:     rsyslog rabbitmq-server $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Yandex bugbounty internal.
# Description:       Yandex bugbounty internal.
#                    This script delegates commands to supervisor process.
### END INIT INFO

SUPERVISORCTL="supervisorctl -c /etc/supervisor/supervisord.conf"

PYTHON_PATH="/etc/yandex/bugbounty-internal"
export PYTHONPATH="$PYTHON_PATH:$PYTHONPATH"

RETVAL=0

set -e

install -d -m 775 -o www-data -g www-data /var/run/yandex/bugbounty-internal /var/run/yandex/bugbounty-internal/celery /var/run/yandex/bugbounty-internal/celeryb /var/run/yandex/bugbounty-internal/celerycam

start_bugbounty () {
    $SUPERVISORCTL reread
    $SUPERVISORCTL update
    $SUPERVISORCTL start bugbounty:*
}

stop_bugbounty() {
    $SUPERVISORCTL stop bugbounty:*
}

restart_bugbounty() {
    stop_bugbounty
    start_bugbounty
}

case "$1" in
    start)
        start_bugbounty
        ;;
    stop)
        stop_bugbounty
        ;;
    restart)
        restart_bugbounty
        ;;
    status)
        $SUPERVISORCTL status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}" >&2
        RETVAL=1
        ;;
esac

exit $RETVAL
