#!/usr/bin/env bash
# ********* Starting configuration *********
while getopts m:l:h  option
do
case "${option}"
in
m) HOME_DIR=${OPTARG};;
h)
    echo "DESCRIPTION: Install Anypoint Monitoring OpenTelemetry Collector"
    echo "Usage: $0 [options]"
    echo " "
    echo "options:"
    echo "-h,       show help"
    echo "-m,       set home directory - defaults to MULE_HOME"
    echo " "
    exit 0
    ;;
esac
done

if [[ -z "$HOME_DIR" || ! -d "$HOME_DIR" ]]; then
    if [[ -d "$MULE_HOME" ]]; then
        HOME_DIR=$MULE_HOME
    else
        cd $(dirname $0)
        cd ../..
        HOME_DIR=`pwd`
    fi
fi

echo "MULE_HOME is set to ${HOME_DIR}"

AM_HOME=${HOME_DIR}/am
source ${AM_HOME}/bin/tools/serviceHelper

if [[ "$OSTYPE" == "darwin"* ]]; then
    source ${AM_HOME}/bin/tools/tool-osx
else
    source ${AM_HOME}/bin/tools/tool-linux
fi

check_both_status

echo "Clearing artifacts..."
rm -fr ${AM_HOME}/config/prospectors
rm -fr ${AM_HOME}/config/certs
rm -f ${AM_HOME}/config/proxy.url
rm -f ${HOME_DIR}/server-plugins/mule-agent-plugin/lib/mule-agent-dias-*.jar 1> /dev/null 2>&1
rm -f ${HOME_DIR}/server-plugins/mule-agent-plugin/lib/analytics-metrics-collector-*.jar 1> /dev/null 2>&1
rm -f ${HOME_DIR}/server-plugins/mule-agent-plugin/lib/modules/mule-agent-dias-*.jar 1> /dev/null 2>&1
rm -f ${HOME_DIR}/server-plugins/mule-agent-plugin/lib/modules/analytics-metrics-collector-*.jar 1> /dev/null 2>&1
rm -f ${HOME_DIR}/plugins/mule-agent-plugin/lib/modules/mule-agent-dias-*.jar 1> /dev/null 2>&1
rm -f ${HOME_DIR}/plugins/mule-agent-plugin/lib/modules/analytics-metrics-collector-*.jar 1> /dev/null 2>&1

if isOtelCollectorServiceInstalled; then
    echo "Stopping OtelCollector and DirectoryWatcher service..."
    ${AM_HOME}/bin/am-service stop
    ${AM_HOME}/bin/am-service disable
    echo "Removing OtelCollector and DirectoryWatcher service files..."
    sudo rm -f "/lib/systemd/system/am-otel-collector.service"
    sudo rm -f "/lib/systemd/system/am-directory-watcher.service"
    echo "Removing OtelCollector and DirectoryWatcher PID files..."
    sudo rm -f "/etc/otel-collector/am-agent.pid"
    sudo rm -f "/etc/directory-watcher/directory-watcher.pid"
    echo "Reloading systemd services..."
    sudo systemctl daemon-reload
    sudo systemctl reset-failed
else
    echo "Stopping OtelCollector process..."
    ${AM_HOME}/bin/am stop
    echo "Removing OtelCollector PID file..."
    rm -f "${AM_HOME}/am-agent.pid"
   
    # Stop DirectoryWatcher process if running
    echo "Stopping DirectoryWatcher..."
    if [[ -f "${AM_HOME}/directory-watcher.pid" ]]; then
        PID=$(cat "${AM_HOME}/directory-watcher.pid")
        if pgrep -F "${AM_HOME}/directory-watcher.pid" > /dev/null 2>&1; then
            echo "Killing DirectoryWatcher process (PID: $PID)"
            kill -TERM $PID > /dev/null 2>&1
            sleep 2
            if pgrep -F "${AM_HOME}/directory-watcher.pid" > /dev/null 2>&1; then
                echo "Force killing DirectoryWatcher process (PID: $PID)"
                kill -KILL $PID > /dev/null 2>&1
            fi
        fi
        rm -f "${AM_HOME}/directory-watcher.pid"
        echo "DirectoryWatcher stopped"
    else
        echo "DirectoryWatcher PID file not found"
    fi
fi
exit 0
