#!/bin/bash

source /etc/monithor-client/service.cfg

Exit () {

        [[ "$1" == "root" ]] && echo "You have to be root ..." 

        exit
}

if [[ $(id -u) != "0" ]]
        then
                Exit root
fi

if [[ "$1" == "start" ]]
	then
		[[ -f /tmp/endmonitor ]] && rm -f /tmp/endmonitor
		rm /tmp/Monitor* /tmp/webmonithor &> /dev/null

		TotalConfig=$(ls -1 /etc/monithor-client/conf/ | grep .*\.cfg| wc -l)
		for Conf in $(ls -1 /etc/monithor-client/conf/ | grep .*\.cfg)
			do
				screen -dmS ${Conf} bash /usr/local/bin/monithor-client.d/MoniThor ${Conf}
			done
		screen -dmS webserver bash /usr/local/bin/monithor-client.d/MoniThorWebServer  ${WebPort} $TotalConfig
fi

if [[ "$1" == "stop" ]]
then
		killall python3 &> /dev/null
		rm /tmp/Monithor* &> /dev/null
		touch /tmp/endmonitor

fi

