#!/bin/bash

# Default versions
DEFAULT_PROMETHEUS_VERSION="3.1.0"
DEFAULT_NODE_EXPORTER_VERSION="1.8.2"

# Function to display menu and get user choices
function show_menu() {
  echo "Please select the software you would like to install (multiple selections allowed):"
  echo "1. Prometheus"
  echo "2. Node Exporter"
  echo "3. Grafana"
  echo "4. Exit"
  read -p "Enter your choice(s) as a comma-separated list (e.g., 1,2,3) [default: 4]: " choice
}

# Function to display progress bar
function show_progress {
  local total=$1
  local current=$2
  local percent=$((current * 100 / total))
  printf "\r[%-70s] %d%%" "$(printf '#%.0s' $(seq 1 $percent))" "$percent"
}

# Function to download and install Prometheus
function install_prometheus() {
  read -p "Enter the desired Prometheus version (e.g., v2.41.0) [default: $DEFAULT_PROMETHEUS_VERSION]: " PROMETHEUS_VERSION
  PROMETHEUS_VERSION=${PROMETHEUS_VERSION:-$DEFAULT_PROMETHEUS_VERSION}

  echo "Chosen Prometheus version: $PROMETHEUS_VERSION"
  DOWNLOAD_DIR="/tmp/prometheus-install"
  PROMETHEUS_USER="prometheus"
  PROMETHEUS_GROUP="prometheus"
  PROMETHEUS_DIR="/usr/local/bin/prometheus"
  CONFIG_DIR="/etc/prometheus"
  DATA_DIR="/var/lib/prometheus"

  echo "Creating download directory..."
  mkdir -p $DOWNLOAD_DIR

  echo "Downloading Prometheus binary..."
  cd $DOWNLOAD_DIR
  wget https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz

  echo "Extracting Prometheus binary..."
  tar xvfz prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz

  echo "Copying Prometheus binary to $PROMETHEUS_DIR..."
  cp prometheus-$PROMETHEUS_VERSION.linux-amd64/prometheus $PROMETHEUS_DIR
  echo "Copying promtool binary to /usr/local/bin"
  sudo cp prometheus-$PROMETHEUS_VERSION.linux-amd64/promtool /usr/local/bin

  echo "Creating Prometheus configuration file in $CONFIG_DIR..."
  cat <<EOF > $CONFIG_DIR/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
EOF

  echo "Creating $PROMETHEUS_USER user and group..."
  sudo groupadd --system $PROMETHEUS_GROUP
  sudo useradd -s /sbin/nologin --system -g $PROMETHEUS_GROUP $PROMETHEUS_USER

  echo "Setting ownership of directories and files..."
  sudo chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP $CONFIG_DIR $DATA_DIR
  sudo chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP /usr/local/bin/prometheus
  sudo chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP /usr/local/bin/node_exporter

  echo "Creating systemd service for Prometheus..."
  cat <<EOF | sudo tee /etc/systemd/system/prometheus.service > /dev/null
[Unit]
Description=Prometheus Time Series Database
Wants=network-online.target
After=network-online.target

[Service]
User=$PROMETHEUS_USER
Group=$PROMETHEUS_GROUP
Type=simple
ExecStart=/usr/local/bin/prometheus \
  --config.file=$CONFIG_DIR/prometheus.yml \
  --storage.tsdb.path=$DATA_DIR

Restart=always
[Install]
WantedBy=default.target
EOF

  echo "Reloading systemd daemon..."
  sudo systemctl daemon-reload
  echo "Enabling Prometheus service..."
  sudo systemctl enable prometheus.service
  echo "Starting Prometheus service..."
  sudo systemctl start prometheus.service
  echo "Checking status of Prometheus service..."
  systemctl status prometheus.service
}

# Function to download and install Node Exporter
function install_node_exporter() {
  read -p "Enter the desired Node Exporter version (e.g., v1.6.0) [default: $DEFAULT_NODE_EXPORTER_VERSION]: " NODE_EXPORTER_VERSION
  NODE_EXPORTER_VERSION=${NODE_EXPORTER_VERSION:-$DEFAULT_NODE_EXPORTER_VERSION}

  echo "Chosen Node Exporter version: $NODE_EXPORTER_VERSION"
  DOWNLOAD_DIR="/tmp/node-exporter-install"
  PROMETHEUS_USER="prometheus"
  PROMETHEUS_GROUP="prometheus"
  BINARY_DIR="/usr/local/bin"

  echo "Creating download directory..."
  mkdir -p $DOWNLOAD_DIR

  echo "Downloading Node Exporter binary..."
  cd $DOWNLOAD_DIR
  wget https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERSION/node_exporter-$NODE_EXPORTER_VERSION.linux-amd64.tar.gz

  echo "Extracting Node Exporter binary..."
  tar xvfz node_exporter-$NODE_EXPORTER_VERSION.linux-amd64.tar.gz

  echo "Copying Node Exporter binary to $BINARY_DIR..."
  sudo cp node_exporter-$NODE_EXPORTER_VERSION.linux-amd64/node_exporter $BINARY_DIR

  echo "Creating systemd service for Node Exporter..."
  cat <<EOF | sudo tee /etc/systemd/system/node_exporter.service > /dev/null
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=$PROMETHEUS_USER
Group=$PROMETHEUS_GROUP
Type=simple
ExecStart=/usr/local/bin/node_exporter

Restart=always
[Install]
WantedBy=default.target
EOF

  echo "Reloading systemd daemon..."
  sudo systemctl daemon-reload
  echo "Enabling Node Exporter service..."
  sudo systemctl enable node_exporter.service
  echo "Starting Node Exporter service..."
  sudo systemctl start node_exporter.service
  echo "Checking status of Node Exporter service..."
  systemctl status node_exporter.service
}

# Function to install Grafana
function install_grafana() {
  echo "Installing Grafana using the official PPA..."

  echo "Importing Grafana repository keyring..."
  wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

  echo "Adding Grafana repository..."
  echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

  echo "Updating package list..."
  sudo apt-get update

  echo "Installing Grafana..."
  sudo apt-get install -y grafana

  echo "Enabling Grafana service..."
  sudo systemctl enable grafana-server
  echo "Starting Grafana service..."
  sudo systemctl start grafana-server
  echo "Checking status of Grafana service..."
  systemctl status grafana-server
}

# Prompt for sudo password and validate it
sudo -v

# Main script execution
show_menu

# Convert the input choice to an array
IFS=',' read -r -a choices <<< "$choice"

# Execute selected installations with progress bar
for i in "${!choices[@]}"; do
  case ${choices[$i]} in
    1)
      echo "Installing Prometheus..."
      install_prometheus
      ;;
    2)
      echo "Installing Node Exporter..."
      install_node_exporter
      ;;
    3)
      echo "Installing Grafana..."
      install_grafana
      ;;
    4)
      echo "Exiting..."
      exit 0
      ;;
    *)
      echo "Invalid choice: ${choices[$i]}. Skipping."
      ;;
  esac
done

echo "Installation completed successfully!"