#!/bin/sh

PVE=/usr/share/pve-manager/js/pvemanagerlib.js
LIB=/usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js
PBS=/usr/share/javascript/proxmox-backup/js/proxmox-backup-gui.js

if [ -f $PVE ]; then
    sed -i.bak "s/hidden: false/hidden: true/g" $PVE
    sed -i.bak -z "s/res === null || res === undefined || \!res || res\n\t\t\t.data.status.toLowerCase() \!== 'active'/false/g" $LIB
    systemctl restart pveproxy
fi

if [ -f $PBS ]; then
    sed -i.bak -z "s/res === null || res === undefined || \!res || res\n\t\t\t.data.status.toLowerCase() \!== 'active'/false/g" $LIB
    sed -i.bak "s/let subStatus = status.toLowerCase() === 'active' ? 2 : 0/let subStatus = status.toLowerCase() === 'active' ? 2 : 2/" $PBS
    systemctl restart proxmox-backup-proxy
fi

if grep -qv GFXPAYLOAD /etc/default/grub; then
	sed -i 's/ vga=791 video=vesafb:ywrap,mtrr//' /etc/default/grub
	echo GRUB_GFXPAYLOAD_LINUX=1024x768x32 >> /etc/default/grub
	echo GRUB_GFXMODE=1024x768x32 >> /etc/default/grub
fi

if [ ! -f /etc/sysctl.d/99-swappinness.conf ]; then
	echo vm.swappiness=10 > /etc/sysctl.d/99-swappinness.conf
	sysctl -w vm.swappiness=10
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

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
	echo "===> apt -y install snmpd"
	cat << EOF
com2sec readonly  default         tangramsnmp

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

syslocation 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
	echo systemctl restart snmpd
fi

[ -d /opt/netdata ] && /opt/netdata/usr/libexec/netdata/netdata-uninstaller.sh -f -y

if [ ! -d /etc/netdata ]; then
	echo "===> apt -y install netdata && sed -i 's/bind/#bind/' /etc/netdata/netdata.conf && usermod -a -G www-data netdata && systemctl restart netdata"
else
	systemctl stop netdata
	systemctl disable netdata
fi

