#!/bin/bash
# obsolete

stop() {
    echo "STOP ESAME-APPLY-CONFIG"
    #rm /local/esame-machine.conf
    #rm /local/iptables
}


start() {
    . /opt/esame/bin/get-config start
    
    echo "START ESAME-APPLY-CONFIG"

	# configuration profile is changed
	if [[ "$MACHINE_CONFIGS_ID" != "$MACHINE_CONFIGS_ID_PREV" ]]
	then
		# update config_id file
		echo ${MACHINE_CONFIGS_ID} > /local/config_id

		# no exam, set normal target
		if [[ "$MACHINE_CONFIGS_ID" -eq "0" ]]
		then
			# stop some services to revert system files
			/usr/bin/systemctl stop esame-kioskmode
			/usr/bin/systemctl stop esame-kdm-autologin
			rm /local/esame-machine.conf 2>&1
			rm /local/esame-machine.conf.prev 2>&1
			rm /local/iptables 2>&1
			rm /local/iptables.prev 2>&1
			echo "Normal!"
			/usr/bin/systemctl set-default graphical.target
			reboot
		else                
			echo "Exam!"
			/usr/bin/systemctl set-default esame.target
			reboot
		fi
	fi

	if [[ "$MACHINE_CONFIGS_ID" -ne "0" ]]
	then
		# check if parameters are changed
		cmp --silent /local/esame-machine.conf /local/esame-machine.conf.prev
		if [[ $? -ne 0 ]]
		then
			/usr/bin/systemctl daemon-reload
			echo "Configuration parameters changed: restart services... "
			# restart some exam services
			echo -n "USB..."
			/usr/bin/systemctl start esame-usb
			echo " done."
			echo -n "Firefox home..."
			/usr/bin/systemctl start esame-firefox-home
			echo " done."
			echo
			echo "All done."
		fi
	fi
}


args=("$@")

case "$1" in
'start')
	start
	;;
'stop')
	stop
	;;
'restart')
	stop
	start
	;;
*)
	echo "Usage: $0 { start | stop | restart }";
	exit 1;
	;;
esac
exit 0