#!/usr/bin/env bash
# ********* Starting configuration *********
while getopts ":m:p:s:xh"  option; do
case "${option}" in
m) HOME_DIR=${OPTARG};;
p) PROXY_URL=${OPTARG};;
s) SERVER_ID=${OPTARG};;
x) SKIP_SERVICE="true";;
k) KEYSTORE_TYPE=${OPTARG};;  # keystore type
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 "-p,       SOCKS5 proxy server URL"
    echo "          Example: -p socks5://user:password@socks5-server:1080"
    echo "-k,       specify keystore type (e.g, JKS or PKCS12)"
    echo " "
    exit 0
    ;;
esac
done
shift "$((OPTIND-1))"

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/pid-file

installServiceAgent() {
    # Configuring otel collector as a service
    SERVICE_UNIT_FILE="/lib/systemd/system/am-otel-collector.service"
    DIRECTORY_WATCHER_SERVICE_UNIT_FILE="/lib/systemd/system/am-directory-watcher.service"
    
    [[ -f /bin/systemctl ]] && SYSTEMCTL_EXEC=/bin/systemctl || SYSTEMCTL_EXEC=/usr/bin/systemctl
    if [[ ! -d "$SERVICE_UNIT_FILE" ]]; then
        echo "Copying otel collector service configuration"
        sudo cp ${AM_HOME}/config/am-otel-collector.template.service $SERVICE_UNIT_FILE
        sudo sed -i '' -e "s/\$muleHome\\$/${HOME_DIR//\//\\/}/g" $SERVICE_UNIT_FILE > /dev/null 2>&1
        sudo sed -i '' -e "s/\$javaHome\\$/${JAVA_HOME//\//\\/}/g" $SERVICE_UNIT_FILE > /dev/null 2>&1
        GOMEMLIMIT=26MiB
        sudo sed -i '' -e "s/\$gomemlimit\\$/${GOMEMLIMIT//\//\\/}/g" $SERVICE_UNIT_FILE > /dev/null 2>&1
    fi

    # Process DirectoryWatcher service configuration
    if [[ ! -d "$DIRECTORY_WATCHER_SERVICE_UNIT_FILE" ]]; then
        echo "Copying directory watcher service configuration"
        sudo cp ${AM_HOME}/config/am-directory-watcher.template.service $DIRECTORY_WATCHER_SERVICE_UNIT_FILE
        sudo sed -i '' -e "s/\$muleHome\\$/${HOME_DIR//\//\\/}/g" $DIRECTORY_WATCHER_SERVICE_UNIT_FILE > /dev/null 2>&1
        
        # Replace javaHome placeholder if JAVA_HOME is available
        if [[ -n "$JAVA_HOME" ]]; then
            sudo sed -i '' -e "s/\$javaHome\\$/${JAVA_HOME//\//\\/}/g" $DIRECTORY_WATCHER_SERVICE_UNIT_FILE > /dev/null 2>&1
            echo "DirectoryWatcher service configured with JAVA_HOME: $JAVA_HOME"
        else
            echo "Warning: DirectoryWatcher service installed but JAVA_HOME not set"
        fi
    fi

    # Create pid file to run as a service in sudo directory
    createOtelCollectorPidFile true /etc/otel-collector
    createDirectoryWatcherPidFile true /etc/directory-watcher
    sudo systemctl daemon-reload
}

configureProxyUrl() {
    # Check and write the proxy URL file if PROXY_URL or HTTPS_PROXY is set
    local proxy_value="${PROXY_URL:-$HTTPS_PROXY}"
    if [[ -n "$proxy_value" ]]; then
        printf "%s\n" "$proxy_value" > "${AM_HOME}/config/proxy.url"
        printf "Set the proxy to %s.\n" "$proxy_value"
    fi
}

configureAgent() {
    echo "Configuring agent..."
    if (type -p java) || ([[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]); then
        java -jar ${AM_HOME}/lib/agent-configurator.jar ${HOME_DIR}
    else
        echo "Error pushing configuration because Java is not installed"
    fi
}

rm -f ${AM_HOME}/config/pipelines/*.yml > /dev/null 2>&1
if [[ -z "$SERVER_ID" ]]; then
    rm ${AM_HOME}/config/server.id > /dev/null 2>&1
else
    touch ${AM_HOME}/config/server.id > /dev/null 2>&1
    echo "$SERVER_ID" > "${AM_HOME}/config/server.id"
fi

# Create pid file in home
createOtelCollectorPidFile false ${AM_HOME}
createDirectoryWatcherPidFile false ${AM_HOME}
configureProxyUrl
configureAgent

if [[ -z "$SKIP_SERVICE" ]]; then
    if [[ "$OSTYPE" != "darwin"* ]]; then
        read -p "Do you want to use monitoring as a service? [y|n] " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            echo "Installing Anypoint Monitoring OpenTelemetry Collector as a service..."
            installServiceAgent
        fi
        echo "Next Steps:"
        echo "1. Restart your Mule server completely to ensure all configurations are loaded properly"
        echo "2. After restarting Mule, run the setup script to start monitoring:"
        echo "   $AM_HOME/bin/setup"
    fi
else
  echo "Skipping service installation prompt. Anypoint Monitoring OpenTelemetry Collector will run as a standalone process."
fi

exit 0
