LC_ALL=C

echo -n Checking locale ...
if grep -q ^uk_UA /etc/locale.gen; then
	echo " OK"
else
	echo " UNSET"
	echo Generating locales ...
	echo en_US.UTF-8 UTF-8 > /etc/locale.gen
	echo ru_RU.UTF-8 UTF-8 >> /etc/locale.gen
	echo ru_UA.UTF-8 UTF-8 >> /etc/locale.gen
	echo uk_UA.UTF-8 UTF-8 >> /etc/locale.gen
	locale-gen
	echo DONE
fi

echo -n Checking vm.swappiness ...
if [ -f /etc/sysctl.d/99-swappinness.conf ]; then
        echo " OK"
else
	echo vm.swappiness=10 > /etc/sysctl.d/99-swappinness.conf
        sysctl -w vm.swappiness=10
fi

echo -n "Check OS ver... "
VER=$(grep VERSION_CODENAME /etc/os-release | cut -d= -f2)
case $VER in
    buster)
	    echo "$VER - OK!"
	    MODS="aufs"
	    APPS="ifupdown2 zfs-zed"
	    ;;
	*)
	    echo " $VER - OK!"
	    ;;
esac

echo -n Checking subscription ...
if apt search free-pmx-no-subscription 2>/dev/null | grep -q free-pmx-no-subscription; then
	echo " OK"
else
	SUBSCRIPTION_UNSET=1
	echo " UNSET"
	wget -P /tmp https://dist.free-pmx.org/free-pmx-no-subscription_0.3.3.deb
	dpkg -i /tmp/free-pmx-no-subscription_0.3.3.deb

	apt update
	
        if [ ! -f /usr/sbin/git ]; then
	    apt -y install git chrony libpve-network-perl ethtool lshw net-tools mtr-tiny mc sudo ipmitool $APPS
        fi
	
	apt -y dist-upgrade

	echo DONE
fi

echo -n Checking modules ...

for module in $MODS overlay; do
	modprobe $module
	grep -q $module /etc/modules || echo $module >> /etc/modules
done
echo " DONE"

echo -n Checking targetcli ...
if [ -f /etc/systemd/system/multi-user.target.wants/rtslib-fb-targetctl.service ]; then
	echo " OK"
else
	echo " UNSET"
	apt -y install targetcli-fb
	echo DONE
fi


if [ ! -d /opt/librenms-agent ]; then
	cd /opt/
	git clone https://github.com/librenms/librenms-agent.git
	cd librenms-agent
	cp check_mk_agent /usr/bin/check_mk_agent
	chmod +x /usr/bin/check_mk_agent
	cp check_mk@.service check_mk.socket /etc/systemd/system
	mkdir -p /usr/lib/check_mk_agent/plugins /usr/lib/check_mk_agent/local
	cp /opt/librenms-agent/agent-local/proxmox /usr/lib/check_mk_agent/local/
	systemctl enable check_mk.socket && systemctl start check_mk.socket
fi

echo -n Checking e-mail aliases ...
if grep -q ^root: /etc/aliases; then
	echo " OK"
else
	echo " UNSET"
	echo "root:root@tanhost.com" >> /etc/aliases
	newaliases
	echo DONE
fi

echo -n Checking netdata ...
if [ -d /etc/netdata ]; then
	echo " OK"
else
	echo " UNSET"
	apt -y install netdata
	sed -i 's/bind/#bind/' /etc/netdata/netdata.conf
	usermod -a -G www-data netdata
	systemctl stop netdata
	systemctl disable netdata
	echo DONE
fi

if [ ! -d /etc/snmp ]; then
	curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
	chmod +x /usr/bin/distro
	apt -y install snmpd
	cat << EOF > /etc/snmp/snmpd.conf
com2sec readonly  default         tangram-snmp

group MyROGroup v2c        readonly
view all    included  .1                               80
access MyROGroup ""      any       noauth    exact  all    none   none

syslocation Units, Rack, Datacenter, City, Country [X,Y]
syscontact Admin <root@tanhost.com>

#Distro Detection
extend distro /usr/bin/distro

#Hardware Detection (uncomment to enable)
extend hardware '/bin/cat /sys/devices/virtual/dmi/id/product_name'
extend manufacturer '/bin/cat /sys/devices/virtual/dmi/id/sys_vendor'
#extend serial '/bin/cat /sys/devices/virtual/dmi/id/product_serial'
EOF
	systemctl restart snmpd
fi
