#!/bin/bash

CHECKSUM=$1
REG_STATUS=$2

SIP_STATE_FILE=/run/sip.state
ULBIN=/usr/local/bin

stdbuf -o0 echo -e "# md5sum of SIP.config being used
CONFIG_CSUM=${CHECKSUM}
# Registration failure codes e.g (0 -> No failure, 1 -> Failure, 2 -> Authorization failure etc.)
REGISTRATION_FAILURE=${REG_STATUS}
" > $SIP_STATE_FILE

if [ "${REG_STATUS}" == "0" ]; then
	${ULBIN}/led_controller status green on
else
	device_ip=(`ip addr show primary | sed -n -e '/127\.0\.0\.1/d;s/\s*inet\s\+\(\([[:digit:]]\+\.\)\+[[:digit:]]\+\).*/\1/p'`)
	if [ -z "${device_ip[0]}" ]; then
		${ULBIN}/led_controller status red on yes
	elif [ "`${ULBIN}/led_controller status blue state`" == "none" ]; then
		${ULBIN}/led_controller status green on yes
	fi
fi
