#!/bin/sh
source "$RUNES_INTERFACE_DIR/RUNES/functions.sh"

interface=$RUNES_INTERFACE_DIR/$RUNES_RUNE_NAME

bootpath=$RUNES_CONFIG_PATH

#eval $(runes_cmdline param_ tftpboot)


if [ ! -d "$bootpath" ];then
	gprintf_failure "No boot directory found"
	exit 1
fi


eval $(runes_interface ipv4_ LOCAL IPv4Network getNetworkInterfacesList)
# ipv4_Interfaces


# 最初の1つを使う
netdev=$(echo $ipv4_Interfaces | cut -d ' ' -f 1)

if [ -z "$netdev" ];then
	gprintf_failure "No network interface was found."
	exit 1
fi


eval $(runes_interface netdev_ LOCAL IPv4Network getInterfaceInfo $netdev)
# netdev_IPAddress=
# netdev_NetworkAddress=
# netdev_BcastAddress=


if [ -f "$bootpath/template.in" ];then
	sed -e "s|@@IPAddress@@|$netdev_IPAddress|g" \
	$bootpath/template.in > $bootpath/template
fi

rm -rf $interface
mkdir $interface

cp -f ./interface $interface/
cp -f ./bin/tftpd $interface/
$interface/tftpd -l -s $bootpath
if [ $? -eq 0 ];then
	gprintf_success "TFTP boot server successfully started"
else
	gprintf_failure "Failed to start TFTP boot server"
fi

