#!/usr/bin/env bash

set -e

PROMETHEUS_VERSION="2.0.0-alpha.3"

BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Download and install Prometheus Server
ARM_VERSION=v7
if $(uname -m | grep -Eq ^armv6); then
    ARM_VERSION=v6
elif ! [ -z $(uname -m | grep aarch64)]; then
    ARM_VERSION=64
fi
mkdir "$BASE_DIR/tmp"
wget "https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-arm${ARM_VERSION}.tar.gz" -O "$BASE_DIR/tmp/prometheus.tar.gz"
tar xvfz "$BASE_DIR/tmp/prometheus.tar.gz" -C "$BASE_DIR/tmp" --strip 1
mv "$BASE_DIR/tmp/prometheus.yml" "$BASE_DIR/tmp/prometheus.yml.orig"
CJDNS_IPV6=$(sudo grep -m 1 '"ipv6"' /etc/cjdroute.conf | awk '{ print $2 }' | sed 's/[",]//g')
cat "$BASE_DIR/tmp/prometheus.yml.orig" | sed "s|localhost:9090|[${CJDNS_IPV6}]:9100|" > "$BASE_DIR/tmp/prometheus.yml"
sudo cp -r "$BASE_DIR/tmp" /opt/prometheus
rm -rf "$BASE_DIR/tmp"

# Configure systemd to start prometheus-server.service on system boot
sudo cp "$BASE_DIR/prometheus-server.service" /lib/systemd/system/prometheus-server.service
sudo systemctl daemon-reload
sudo systemctl enable prometheus-server.service
sudo systemctl start prometheus-server.service
