#!/bin/bash

ULBIN=/usr/local/bin
SERVER_ADDR=${2:-$($ULBIN/find_tftp_server)}
mac=${3:-$(sudo ifconfig eth0 | grep 'HWaddr' | awk '{print $5}' | sed -e 's/://g')}
FILE_TO_GET="$mac.cfg"
#echo "Fetching $FILE_TO_GET from $SERVER_ADDR"

read OUTPUT < <(sudo tftp $SERVER_ADDR <<< "get $FILE_TO_GET")

if [ "${OUTPUT/*Received*/received}" = "received" -o -z "${OUTPUT/*>}" ]; then
	mv $FILE_TO_GET /etc/nyquist/SIP.config
	exit 0
fi

[ -f $FILE_TO_GET ] && unlink $FILE_TO_GET
exit 1
#echo "Done"
