#!/usr/bin/env bash
# install-grafana.sh
# Grafana — Visualize and alert on metrics, logs, and traces from any data source.
# Target: Ubuntu / Debian. Safe to re-run.
# Source guide: devops-hub/tools/grafana.html

set -euo pipefail

if [[ $EUID -eq 0 ]]; then
  echo "Run this as a regular sudo-capable user, not root." >&2
  exit 1
fi

echo "==> Install prerequisites"
sudo apt-get install -y apt-transport-https software-properties-common wget

echo "==> Add the Grafana GPG key and repo"
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

echo "==> Install Grafana"
sudo apt-get update
sudo apt-get install -y grafana

echo "==> Enable and start the service"
sudo systemctl enable --now grafana-server

echo "==> Done. Grafana installation finished."
