#!/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_NETATALK_AFP_CONFIG=${OMV_NETATALK_AFP_CONFIG:-"/etc/netatalk/afp.conf"}
OMV_NETATALK_AFP_MAX_CLIENTS=${OMV_NETATALK_AFP_MAX_CLIENTS:-"20"}
OMV_NETATALK_AFP_MAC_CHARSET=${OMV_NETATALK_AFP_MAC_CHARSET:-"MAC_ROMAN"}
OMV_NETATALK_AFP_UNIX_CHARSET=${OMV_NETATALK_AFP_UNIX_CHARSET:-"LOCALE"}
OMV_NETATALK_AFP_GUEST=${OMV_NETATALK_AFP_GUEST:-"nobody"}
OMV_NETATALK_AFP_UAMLIST=${OMV_NETATALK_AFP_UAMLIST:-"uams_dhx.so,uams_dhx2.so"}
OMV_NETATALK_AFP_SAVEPASSWORD=${OMV_NETATALK_AFP_SAVEPASSWORD:-"no"}

xmlstarlet sel -t -m "//services/afp" \
  -o "[Global]" -n \
  -o "max connections = ${OMV_NETATALK_AFP_MAX_CLIENTS}" -n \
  -o "mac charset = ${OMV_NETATALK_AFP_MAC_CHARSET}" -n \
  -o "unix charset = ${OMV_NETATALK_AFP_UNIX_CHARSET}" -n \
  -o "guest account = ${OMV_NETATALK_AFP_GUEST}" -n \
  -o "uam list = ${OMV_NETATALK_AFP_UAMLIST}" \
  -i "count(shares/share[allowguest = '1']) > 0" -o ",uams_guest.so" -b \
  -i "allowclrtxt[. = '1']" -o ",uams_clrtxt.so" -b \
  -n \
  -o "save password = ${OMV_NETATALK_AFP_SAVEPASSWORD}" -n \
  -i "string-length(extraoptions) > 0" -v extraoptions -n -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc >> ${OMV_NETATALK_AFP_CONFIG}
