#!/bin/bash

cd /etc/realtime/

rm MQTT-conf_realtime.md5
rm MQTT_realtime.md5

if [ $# = 1 ]; then

	wget -q $1MQTT_realtime.py

	if [ $? = 0 ] ; then
		echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.py downloaded succesfully" >> /var/log/realtime.log 
	else
	 	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.py not downloaded with reason <$?>" >> /var/log/realtime.log
		echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - realtime.service restart" >> /var/log/realtime.log

		rm MQTT_realtime.py.*
		#systemctl start realtime.service
		exit 1
	fi

	wget -q $1MQTT_realtime.md5

	if [ $? = 0 ] ; then
		echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.md5 downloaded succesfully" >> /var/log/realtime.log
	else 
		echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.md5 not downloaded with reason <$?>" >> /var/log/realtime.log
		
		rm MQTT_realtime.md5
		rm MQTT_realtime.py.*
		#systemctl start realtime.service
		exit 1
	fi
fi

wget -q $1MQTT-conf_realtime.json

if [ $? = 0 ] ; then
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.json downloaded succesfully" >> /var/log/realtime.log
else 
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.json not downloaded with reason <$?>" >> /var/log/realtime.log
	
	rm MQTT-conf_realtime.json.*
	if [ $# = 1 ]; then
		rm MQTT_realtime.md5
		rm MQTT_realtime.py.*
		exit 1
	fi
fi

wget -q $1MQTT-conf_realtime.md5

if [ $? = 0 ] ; then
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.md5 downloaded succesfully" >> /var/log/realtime.log
else 
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.md5 not downloaded with reason <$?>" >> /var/log/realtime.log
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - realtime.service restart" >> /var/log/realtime.log

	rm MQTT-conf_realtime.md5
	rm MQTT-conf_realtime.json.*
	if [ $# = 1 ]; then
		rm MQTT_realtime.md5
		rm MQTT_realtime.py.*
		exit 1
	fi
fi


if [ $# = 1 ]; then
	MQTT_computed=`md5sum MQTT_realtime.py.1 | sed -e 's/\s.*$//'`
	MQTT_downloaded=`cat MQTT_realtime.md5 | sed -e 's/\s.*$//'`
fi

conf_computed=`md5sum MQTT-conf_realtime.json.1 | sed -e 's/\s.*$//'`
conf_downloaded=`cat MQTT-conf_realtime.md5 | sed -e 's/\s.*$//'`

if [ "$conf_computed" = "$conf_downloaded" ] ; then
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.json file verified!" >> /var/log/realtime.log
	mv /etc/realtime/MQTT-conf_realtime.json.1 /etc/realtime/MQTT-conf_realtime.json

	if [ $# = 1 ]; then
		if [ "$MQTT_computed" = "$MQTT_downloaded" ] ; then
			echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.py file verified!" >> /var/log/realtime.log
			mv /etc/realtime/MQTT_realtime.py.1 /etc/realtime/MQTT_realtime.py
		else
			echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT_realtime.py file corrupted!" >> /var/log/realtime.log
			rm /etc/realtime/MQTT_realtime.py.1
			exit 2
		fi
	fi

	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - restart realtime.service" >> /var/log/realtime.log
	systemctl restart realtime.service	

else
	echo "$(date +"%F %H:%M:%S,%3N") - MQTT_update.sh - MQTT-conf_realtime.json file corrupted!" >> /var/log/realtime.log
	rm /etc/realtime/MQTT-conf_realtime.json.1
	exit 2
fi
