#!/usr/bin/env bash
# ********* Starting configuration *********

while getopts m:h  option
do
case "${option}"
in
m) HOME_DIR=${OPTARG};;
h)
    echo "DESCRIPTION: Setup 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

# Set home directory if not already set
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}"

# Detect JAVA_HOME if not provided by user
if [[ -z "$JAVA_HOME" ]]; then
    # Try to find Java in common locations
    if command -v java >/dev/null 2>&1; then
        # Java is in PATH, find its installation directory
        JAVA_PATH=$(which java)
        if [[ -L "$JAVA_PATH" ]]; then
            # Resolve symlink
            JAVA_PATH=$(readlink -f "$JAVA_PATH")
        fi
        # Go up two directories from bin/java to get JAVA_HOME
        JAVA_HOME=$(dirname "$(dirname "$JAVA_PATH")")
        echo "Auto-detected JAVA_HOME: $JAVA_HOME"
    else
        echo "Warning: Could not auto-detect JAVA_HOME. Please set JAVA_HOME environment variable."
        echo "Example: export JAVA_HOME=/path/to/java/home"
        JAVA_HOME=""
        exit 1
    fi
else
    echo "Using user-provided JAVA_HOME: $JAVA_HOME"
fi

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

enableService() {
    ${AM_HOME}/bin/am-service enable
}

restartService() {
    ${AM_HOME}/bin/am-service restart
    echo $RESTART_SERVICE_HELP
}

restartStandalone() {
    ${AM_HOME}/bin/am restart -m "$HOME_DIR"
    echo $RESTART_HELP
}

attempts=30
echo $AM_CONFIG_WAIT
while(true)
do
 attempts=$((attempts-1))
 if (( attempts > 0 )); then
     if [[ -e "${AM_HOME}/config/pipelines/otel-config.yml" ]]; then
        echo $AM_CONFIG_READY
        if isOtelCollectorServiceInstalled; then
            # Enables both OTEL Collector and DirectoryWatcher services
            enableService
            # Restarts both OTEL Collector and DirectoryWatcher services
            restartService
            # restartService -> am-service restart -> ExecStart in am-directory-watcher.service 
            echo "Use 'sudo journalctl -u am-otel-collector.service -f' to view OtelCollector service logs"
            echo "Use 'sudo journalctl -u am-directory-watcher.service -f' to view DirectoryWatcher service logs"
            echo "Use 'tail -f ${AM_HOME}/logs/otel-collector.log' to view OtelCollector logs"
            echo "Use 'tail -f ${AM_HOME}/logs/otel-collector.err' to view OtelCollector error logs"
        else
            restartStandalone
            echo "Use 'tail -f ${AM_HOME}/logs/otel-collector.log' to view OtelCollector logs"
            echo "Use 'tail -f ${AM_HOME}/logs/otel-collector.err' to view OtelCollector error logs"
            echo "Use 'tail -f ${AM_HOME}/logs/directorywatcher.log' to view DirectoryWatcher logs"
            echo "Use 'tail -f ${AM_HOME}/logs/directorywatcher.err' to view DirectoryWatcher error logs"
        fi
        break;
     fi
     sleep 10
  else
    echo $AM_CONFIG_NOT_FOUND
    break;
 fi
done

exit 0
