#!/usr/bin/env bash

AM_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )"
source ${AM_HOME}/bin/tools/message
PIDFILE_BASE_DIR=/etc/otel-collector
PIDFILE=${PIDFILE_BASE_DIR}/am-agent.pid

source ${AM_HOME}/bin/tools/tool-linux

# Start and enable the service on boot
start() {
    sudo systemctl enable am-otel-collector.service > /dev/null 2>&1
    sudo systemctl start am-otel-collector.service > /dev/null 2>&1
    sudo systemctl enable am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl start am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl enable am-otel-collector-watcher.path > /dev/null 2>&1
    sudo systemctl start am-otel-collector-watcher.path > /dev/null 2>&1
}

# Stop and disable the service on boot
stop() {
    sudo systemctl stop am-otel-collector.service > /dev/null 2>&1
    sudo systemctl stop am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl stop am-otel-collector-watcher.path > /dev/null 2>&1
}

# Restart function - Stops and starts the service
restart() {
    sudo systemctl restart am-otel-collector.service > /dev/null 2>&1
    sudo systemctl restart am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl restart am-otel-collector-watcher.path > /dev/null 2>&1
}

enable() {
    sudo systemctl enable am-otel-collector.service > /dev/null 2>&1
    sudo systemctl enable am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl enable am-otel-collector-watcher.path > /dev/null 2>&1
}

disable() {
    sudo systemctl disable am-otel-collector.service > /dev/null 2>&1
    sudo systemctl disable am-otel-collector-watcher.service > /dev/null 2>&1
    sudo systemctl disable am-otel-collector-watcher.path > /dev/null 2>&1
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    enable)
        enable
    ;;
    disable)
        disable
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0
