#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2017 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_TFTPD_DEFAULT=${OMV_TFTPD_DEFAULT:-"/etc/default/tftpd-hpa"}
OMV_TFTPD_USERNAME=${OMV_TFTPD_USERNAME:-"tftp"}
OMV_TFTPD_OPTIONS=${OMV_TFTPD_OPTIONS:-"--secure"}

# Workaround for bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=544089
if ! omv_is_ipv6_enabled; then
	OMV_TFTPD_OPTIONS="--ipv4 ${OMV_TFTPD_OPTIONS}"
fi

# Create '/etc/default/tftpd-hpa' file
xmlstarlet sel -t -m "//services/tftp" \
	-o "TFTP_OPTIONS=\"${OMV_TFTPD_OPTIONS}" \
	-v "concat(' --retransmit ',retransmit)" \
	-i "allownewfiles[. = '1']" -o " --create" -b \
	-i "string-length(blocksize) > 0" -v "concat(' --blocksize ',blocksize)" -b \
	-i "string-length(extraoptions) > 0" -v "concat(' ',extraoptions)" -b \
	-o "\"" -n \
	-o "TFTP_USERNAME=\"${OMV_TFTPD_USERNAME}\"" -n \
	-o "TFTP_DIRECTORY=\"" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "\"" -n \
	-v "concat('TFTP_ADDRESS=\"0.0.0.0:',port,'\"')" \
	${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_TFTPD_DEFAULT}
