#!/bin/bash

##########################################################################
# Wird von ini.sh getriggert
# json parsen und entsprechende Skripte triggern
# Der Server liefert exklusiv entweder
# - Mediaplayer-Konfiguration
# - GDS-Panel-Konfiguration
# - Token oder Passwort
# - action:vpn zur Etablierung eines VPN, falls noch keine besteht 
# - action:ssh zur Etablierung eines Reversetunnels, falls noch keiner besteht
# - action:reboot Neustart des Systems
# In Abhängigkeit des Inhaltes werden unterschiedliche Prozesse getriggert
##########################################################################

JSON="$(jq -r 'keys[0]' /home/snowtv/Downloads/tmp.json)"
JSON1="$(jq -r '.action' /home/snowtv/Downloads/tmp.json)"
JSONERR="$(jq -r '.error' /home/snowtv/Downloads/tmp.json)"
CFGGDS="$(jq -r '.cfgGDS' /home/snowtv/Downloads/tmp.json)"

if [ "$JSON" == "cfgCore" ]; then
	# neue Konfiguration in Temp-File schreiben und validieren
	jq -r '.[]' /home/snowtv/Downloads/tmp.json > /tmp/mc.cfg.new
	if [ -s /tmp/mc.cfg.new ]; then
		cp /home/snowtv/bin/mc.cfg /home/snowtv/bin/mc.cfg.old
		mv /tmp/mc.cfg.new /home/snowtv/bin/mc.cfg
		# Validierung nach dem Schreiben
		if [ "$(/home/snowtv/bin/ckmccfg.sh)" != "1" ]; then
			echo "$(date)  ERROR: neue mc.cfg ungültig – Rollback auf mc.cfg.old, cfg_pending gesetzt" | tee -a /var/log/stv.log
			cp /home/snowtv/bin/mc.cfg.old /home/snowtv/bin/mc.cfg
			touch /home/snowtv/bin/cfg_pending
		else
			rm -f /home/snowtv/bin/cfg_pending
			exec /home/snowtv/bin/200.sh
		fi
	else
		echo "$(date)  ERROR: cfgCore leer oder ungültig – mc.cfg nicht überschrieben, cfg_pending gesetzt" | tee -a /var/log/stv.log
		rm -f /tmp/mc.cfg.new
		touch /home/snowtv/bin/cfg_pending
	fi

elif [ "$JSON" = "cfgGDS" ]; then
	if [ ! -f "/home/snowtv/bin/.gds_installed" ]; then
		echo "$(date)  WARN: PANELTYPE=gds empfangen, aber kein GDS-Setup vorhanden (.gds_installed fehlt)" | tee -a /var/log/stv.log
	else
		snmpset localhost "$CFGGDS"
		echo "$(date)  INFO: Write new thresholds to GDS Panel" | tee -a /var/log/stv.log
	fi
	#sudo -u snowtv echo "{}" | sudo -u snowtv tee /home/snowtv/Downloads/tmp.json

elif [ "$JSON" = "tokenID" ]; then
	echo "$(date)  INFO: neues Token geschrieben;Monitor-cfg hochgeladen"  | tee -a /var/log/stv.log
	jq -r '.tokenID' /home/snowtv/Downloads/tmp.json > /home/snowtv/bin/token
	# SIM-Info hochladen sobald Token verfügbar (Erstaktivierung / Token-Erneuerung)
	/home/snowtv/bin/simupl.sh &
	#sudo -u snowtv echo "{}" | sudo -u snowtv tee /home/snowtv/Downloads/tmp.json
	#sudo -u snowtv /home/snowtv/bin/gdstshupl2s.sh

elif [ "$JSON" = "action" ] && [ "$JSON1" == "vpn" ]; then
	#sudo -u snowtv echo "{}" | sudo -u snowtv tee /home/snowtv/Downloads/tmp.json
	exec /home/snowtv/bin/ovp3.sh

elif [ "$JSON" = "action" ] && [ "$JSON1" == "ssh" ]; then
	#sudo -u snowtv echo "{}" | sudo -u snowtv tee /home/snowtv/Downloads/tmp.json
	exec /home/snowtv/bin/sshcon.sh

elif [ "$JSON" = "action" ] && [ "$JSON1" == "reboot" ]; then
	echo "$(date)  INFO: System-Reboot, calling reboot.sh"  | tee -a /var/log/stv.log
	sleep 1 && /home/snowtv/bin/reboot.sh

elif [ "$JSON" = "action" ] && [ "$JSON1" == "diag" ]; then
	echo "$(date)  INFO: Diagnosepanel aktiviert" | tee -a /var/log/stv.log
	/home/snowtv/bin/diag.sh &

elif [ "$JSON" = "action" ] && [ "$JSON1" == "diag_off" ]; then
	echo "$(date)  INFO: Diagnosepanel deaktiviert" | tee -a /var/log/stv.log
	touch /tmp/diag_stop

elif [ "$JSON" = "error" ] && [ "$JSONERR" = "token does not match" ]; then
	echo "$(date)  ERROR: token does not match" | tee -a /var/log/stv.log
	/home/snowtv/bin/err_token.sh
else
	exit 1

fi

exit 0
