#!/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-2012 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_TFTPDTIMEOUT=${OMV_TFTPD_TFTPDTIMEOUT:-"300"}
OMV_TFTPD_MCASTPORT=${OMV_TFTPD_MCASTPORT:-"1758"}
OMV_TFTPD_MCASTADDR=${OMV_TFTPD_MCASTADDR:-"239.239.239.0-255"}
OMV_TFTPD_MCASTTTL=${OMV_TFTPD_MCASTTTL:-"1"}
OMV_TFTPD_MAXTHREAD=${OMV_TFTPD_MAXTHREAD:-"100"}
OMV_TFTPD_VERBOSE=${OMV_TFTPD_VERBOSE:-"5"}
OMV_TFTPD_USERNAME=${OMV_TFTPD_USERNAME:-"tftp"}
OMV_TFTPD_OPTIONS=${OMV_TFTPD_OPTIONS:-"--secure"}

# Create '/etc/default/tftpd-hpa' file
xmlstarlet sel -t -m "//services/tftp" \
  -o "TFTP_OPTIONS=&quot;${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 "&quot;" -n \
  -o "TFTP_USERNAME=&quot;${OMV_TFTPD_USERNAME}&quot;" -n \
  -o "TFTP_DIRECTORY=&quot;" ${OMV_XMLSTARLET_GET_SHAREDFOLDER_PATH} -o "&quot;" -n \
  -v "concat('TFTP_ADDRESS=&quot;0.0.0.0:',port,'&quot;')" \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_TFTPD_DEFAULT}
