#!/bin/bash
BOGEN_DIR=/opt/bogen
PRODUCT_NAME=appliance
ULBIN=/usr/local/bin

NC_FILE=$BOGEN_DIR/$PRODUCT_NAME/network.config

trim() {
    local var="$*"
    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
    echo -n "$var"
}


TFTP_ADDR=$(trim $(grep "TFTP Server" $NC_FILE | cut -f2 -d ":"))
if [ "$TFTP_ADDR" == "" ]; then
	# No TFTP Server defined in network.config, check the DHCP lease 
	TFTP_ADDR=$($ULBIN/get_dhcp_lease_info tftp-server-name)
fi


if [ "$TFTP_ADDR" == "" ]; then
	# No TFTP Server defined anywhere, return a warning 
	echo "WARNING: No TFTP Server address is defined, using default configuration"
	echo "<br>"
	exit 0
fi

echo $TFTP_ADDR

:<<'END'
TFTP_ADDR=$($ULBIN/get_dhcp_lease_info tftp-server-name)

if [ "$TFTP_ADDR" == "" ]; then
	#echo "No TFTP Address found, looking in network.config"
	TFTP_ADDR=$(trim $(grep "TFTP Server" $NC_FILE | cut -f2 -d ":"))
	echo $TFTP_ADDR
else
	echo $TFTP_ADDR
fi
END
