#!/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}"

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"
    WATCHER_SERVICE_UNIT_FILE="/lib/systemd/system/am-otel-collector-watcher.service"
    WATCHER_PATH_UNIT_FILE="/lib/systemd/system/am-otel-collector-watcher.path"
    [[ -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
    fi
    if [[ ! -d "$WATCHER_SERVICE_UNIT_FILE" ]]; then
        echo "Copying otel collector watcher service configuration"
        sudo cp ${AM_HOME}/config/am-otel-collector-watcher.template.service $WATCHER_SERVICE_UNIT_FILE
        sudo sed -i '' -e "s/\$systemctlExec\\$/${SYSTEMCTL_EXEC//\//\\/}/g" $WATCHER_SERVICE_UNIT_FILE > /dev/null 2>&1
    fi
    if [[ ! -d "$WATCHER_PATH_UNIT_FILE" ]]; then
        echo "Copying otel collector watcher path configuration"
        sudo cp ${AM_HOME}/config/am-otel-collector-watcher.template.path $WATCHER_PATH_UNIT_FILE
        sudo sed -i '' -e "s/\$muleHome\\$/${HOME_DIR//\//\\/}/g" $WATCHER_PATH_UNIT_FILE > /dev/null 2>&1
    fi
    # Create pid file to run as a service in sudo directory
    createPidFile true /etc/otel-collector
    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 ${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
createPidFile 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
            installServiceAgent
        fi
    fi
else
  echo "Monitoring is not used as a service"
fi

exit 0
