#!/bin/bash

ULBIN=/usr/local/bin
server_ip=$($ULBIN/find_tftp_server)
mac=$(sudo ifconfig eth0 | grep 'HWaddr' | awk '{print $5}' | sed -e 's/://g')
temp_path="/run"
actions=(msgupdate checkin-status)
action=${1:-msgupdate}

event_msgupdate() {
	cmd="pushd ${temp_path} > /dev/null;$ULBIN/get_file_from_server ${server_ip} msg/${mac}\.msg;[ -f ${mac}\.msg ] && mv ${mac}\.msg message;popd > /dev/null"
	systemd-run --unit display-update-msgupdate --nice 0 --no-block --service-type=idle bash -c "${cmd}" > /dev/null
}

event_checkin-status() {
	cmd="pushd ${temp_path} > /dev/null;$ULBIN/get_file_from_server ${server_ip} checkin_status;popd > /dev/null"
	systemd-run --unit display-update-checkin --nice 0 --no-block --service-type=idle bash -c "${cmd}" > /dev/null
}

if [ "${actions[*]/${action}/}" = "${actions[*]}" ]; then
	logger -t $0 "Invalid parameters - $action"
	exit 1
else
	eval event_${action}
fi

exit 0

