#!/bin/bash

if [ $# -lt 1 ]; then
	echo "Please provide debian version (7, 8, 9, 10, 11, 12)"
	exit 1
fi

version=$1

if [ ! -d debian-$version ]; then
	echo "This version does not exist yet"
	exit 1
fi

echo "Copying binaries"
cp -rp debian-$version/nagios /usr/local/
echo "Copying extra plugins"
cp -rp ../extra/* /usr/local/nagios/libexec/

echo "Creating user and group"
groupadd -r nagios
useradd -r -g nagios -d /usr/local/nagios/ nagios

echo "Settings system and rights"
echo >> /etc/services
echo '# Nagios services' >> /etc/services
echo 'nrpe    5666/tcp' >> /etc/services
mkdir /usr/local/nagios/var && chown -R nagios:nagios /usr/local/nagios/var
chown -R nagios:nagios /usr/local/nagios/libexec
chown -R nagios:nagios /usr/local/nagios/etc
cp sudoers.d/nrpe /etc/sudoers.d/nrpe

echo "Creating daemon scripts"
if test -f "debian-$version/nrpe.service"; then
    cp debian-$version/nrpe.service /lib/systemd/system/nrpe.service
fi

if test -f "debian-$version/nrpe.init"; then
    cp debian-$version/nrpe.init /etc/init.d/
fi

echo "*****************************************************************************"
echo "              Setup finished"
echo "   Please create a config file in /usr/local/nagios/etc/nrpe.cfg"
echo "   You can start nrpe using /etc/init.d/nrpe start or systemctl start nrpe"
echo "*****************************************************************************"
