#!/usr/bin/env bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

: "${ALERTMANAGER_VERSION:=0.27.0}"

: "${NODE_EXPORTER_VERSION:=1.8.2}"

: "${PROMETHEUS_VERSION:=2.53.1}"

set -o errexit -o nounset -o pipefail

which curl dirname gzip realpath tar xargs 1>/dev/null

readonly SELF=$(realpath "$0" | xargs dirname) && cd "$SELF/"

readonly ALERTMANAGER_URL="https://github.com/prometheus/alertmanager/releases/download/v$ALERTMANAGER_VERSION/alertmanager-$ALERTMANAGER_VERSION.linux-amd64.tar.gz"
readonly ALERTMANAGER_URL_ARM64="https://github.com/prometheus/alertmanager/releases/download/v$ALERTMANAGER_VERSION/alertmanager-$ALERTMANAGER_VERSION.linux-arm64.tar.gz"

readonly NODE_EXPORTER_URL="https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERSION/node_exporter-$NODE_EXPORTER_VERSION.linux-amd64.tar.gz"
readonly NODE_EXPORTER_URL_ARM64="https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERSION/node_exporter-$NODE_EXPORTER_VERSION.linux-arm64.tar.gz"

readonly PROMETHEUS_URL="https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz"
readonly PROMETHEUS_URL_ARM64="https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-arm64.tar.gz"

install -d "$SELF/alertmanager/" "$SELF/alertmanager.arm64/"
curl -fsSL "$ALERTMANAGER_URL" | tar -xzf- --strip-components=1 -C "$SELF/alertmanager/"
curl -fsSL "$ALERTMANAGER_URL_ARM64" | tar -xzf- --strip-components=1 -C "$SELF/alertmanager.arm64/"

install -d "$SELF/node_exporter/" "$SELF/node_exporter.arm64/"
curl -fsSL "$NODE_EXPORTER_URL" | tar -xzf- --strip-components=1 -C "$SELF/node_exporter/"
curl -fsSL "$NODE_EXPORTER_URL_ARM64" | tar -xzf- --strip-components=1 -C "$SELF/node_exporter.arm64/"

install -d "$SELF/prometheus/" "$SELF/prometheus.arm64/"
curl -fsSL "$PROMETHEUS_URL" | tar -xzf- --strip-components=1 -C "$SELF/prometheus/"
curl -fsSL "$PROMETHEUS_URL_ARM64" | tar -xzf- --strip-components=1 -C "$SELF/prometheus.arm64/"
