#!/bin/bash

# This is a bare-metal to Nagios Core build
# Designed strictly for Raspberry Pi Zero 2 W
# EverWatch Global Nagios World Conference 2026
# Contact: Eric Loyd <eloyd@everwatch.global>

do_msg() {
  msg="$1"
  shift
  toilet -f future "$msg" -F metal $*
}
do_done() {
  do_msg "Done." -F border
}
do_finished() {
  do_msg "All Done!" -F border -F gay -F border
}

# Get me a banner first
echo "Installing *toilet* so you can see this stuff more easily..."
sudo apt update
sudo apt install -y toilet

# Pre-preqs
do_msg "Installing pre-reqs"
sudo apt update && sudo apt dist-upgrade -y
sudo apt install -y autoconf gcc libperl-dev make apache2 php php-gd libgd-dev \
	libapache2-mod-php libssl-dev daemon unzip wget libtool libxml2-dev \
	libjansson-dev libdbi-dev libsqlite3-dev libcjson-dev
do_done

# Make Nagios
do_msg "Installing Nagios Core"
sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -aG nagcmd nagios
sudo usermod -aG nagcmd www-data
cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.5.12.tar.gz
tar -xzf nagios-4.5.12.tar.gz && rm nagios-4.5.12.tar.gz
cd nagios-4.5.12
./configure --with-command-group=nagcmd
make all
sudo make install
sudo make install-commandmode
sudo make install-init
sudo make install-config
sudo make install-webconf
# Nagios password (nagiosadmin/nagiosadmin)
sudo /bin/sh -c 'echo "nagiosadmin:\$apr1\$peYer4Fi\$KL9/8Ya8Zy6TOBtlmBZuP/" > /usr/local/nagios/etc/htpasswd.users'
do_done

# Nagios plugins
do_msg "Installing Nagios plugins"
cd /tmp
wget https://nagios-plugins.org/download/nagios-plugins-2.4.11.tar.gz
tar -xzf nagios-plugins-2.4.11.tar.gz && rm nagios-plugins-2.4.11.tar.gz
cd nagios-plugins-2.4.11
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
sudo make install

# Final configs
sudo systemctl enable apache2
sudo systemctl enable nagios
sudo systemctl start apache2
sudo systemctl start nagios
sudo a2enmod cgi
sudo systemctl restart apache2
do_done

### Installing NCPA and the like, getting the box to monitor itself via NCPA
# Install check_ncpa (C code from beta test)
do_msg "Installing NCPA"
arch=$(uname -m)
file="check_ncpa.${arch}"
if [ -r "$file" ]; then
  echo "Will install $file, since it's here..."
  echo "==> Installing ${file} to /usr/local/nagios/libexec..."
  sudo install -d /usr/local/nagios/libexec
  sudo install -m 0755 ${file} /usr/local/nagios/libexec/check_ncpa
  echo "==> Installation complete!"
  echo "Installed to: /usr/local/nagios/libexec/check_ncpa"
else
  echo ""
  echo "### IMPORTANT:"
  echo "### You must extract, compile, and install the check_ncpa_c.2.Mar-05-26.tgz beta code to /usr/local/nagios/libexec"
  echo "### Failure to do so will result in service check failures that may be hard to diagnose."
  echo ""
fi
### Verify
/usr/local/nagios/libexec/check_ncpa --version
### Output: check_ncpa.c, Version 1.2.4

# Install NCPA Agent
cd ~/NWC2026-Fusion
sudo apt install ./ncpa_3.3.1-1_armhf.deb
### Verify
/usr/local/nagios/libexec/check_ncpa -H localhost -t mytoken -M system/version
### Output: OK: Version was #1 SMP Raspbian 1:6.12.75-1+rpt1 (2026-03-11) | 'version'=#1 SMP Raspbian 1:6.12.75-1+rpt1 (2026-03-11);;;
do_done

# Get localhost monitored via NCPA
do_msg "Monitor thyself via NCPA"
cd /usr/local/nagios/etc/
sudo mkdir EWG && sudo chown nagios:nagios EWG
sudo /bin/sh -c 'grep -q "cfg_dir=/usr/local/nagios/etc/EWG" nagios.cfg && echo "cfg_dir line already added." || echo "cfg_dir=/usr/local/nagios/etc/EWG" >> nagios.cfg'
cd EWG
sudo tar xvfz ~/NWC2026-Fusion/EWG_configs.tar.gz && sudo rm EWG_configs.tar.gz
sudo sed -i -e "s/localhost:NCPA/${HOSTNAME}/g" *.cfg
### Verify
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
### Output: Things look okay - No serious problems were detected during the pre-flight check
do_done

sudo systemctl restart nagios
do_finished
