#!/bin/bash

PACKAGE_MONIT_VERSION="5.25.1"
#URL_MONIT="http://mmonit.com/monit/dist/monit-$PACKAGE_MONIT_VERSION.tar.gz"
URL_MONIT="https://mmonit.com/monit/dist/monit-5.25.1.tar.gz"
#URL_MONIT="http://hub.nvpn.eu:8000/x86_64/monit-$PACKAGE_MONIT_VERSION.tar.gz"
basename=`basename $URL_MONIT`
SYS_VERSION=`sed -e 's/.*\([0-9]\)\.[0-9].*/\1/g' /etc/redhat-release`
SYSTEM=`uname -i`

# version dependent assignements
if [ $SYS_VERSION -lt 6 ]; then
	CENTOS_RPMBUILD_ROOT="/usr/src/redhat"
	MODPROBE_PATH="/etc/modprobe.conf"
	RC_LOCAL="/etc/rc.local"
else
	CENTOS_RPMBUILD_ROOT="/root/rpmbuild"
	MODPROBE_PATH="/etc/modprobe.d/modprobe.conf"
	RC_LOCAL="/etc/rc.d/rc.local"
fi

service monit stop
/bin/mkdir /tmp/monit_backup ; /bin/cp -Rp /etc/monitrc /tmp/monit_backup/ ; /bin/cp -Rp /etc/monit.d/monit-* /tmp/monit_backup/
yum erase monit -y

wget $URL_MONIT -O $CENTOS_RPMBUILD_ROOT/SOURCES/$basename
rpmbuild -tb $CENTOS_RPMBUILD_ROOT/SOURCES/$basename
rpm -ivh $CENTOS_RPMBUILD_ROOT/RPMS/$SYSTEM/monit-$PACKAGE_MONIT_VERSION*.rpm
mkdir -p /var/lib/monit

/bin/cp -Rp /tmp/monit_backup/monitrc /etc/ ; /bin/cp -Rp /tmp/monit_backup/monit-* /etc/monit.d/
service monit restart
