#!/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_HOMES_HOMENAME=${OMV_NETATALK_AFP_HOMES_HOMENAME:-"\$u's home"}
OMV_NETATALK_AFP_HOMES_BASEDIRREGEX=${OMV_NETATALK_AFP_HOMES_BASEDIRREGEX:-"/home"}
OMV_NETATALK_AFP_HOMES_FILEPERM=${OMV_NETATALK_AFP_HOMES_FILEPERM:-"0600"}
OMV_NETATALK_AFP_HOMES_DIRECTORYPERM=${OMV_NETATALK_AFP_HOMES_DIRECTORYPERM:-"0700"}
OMV_NETATALK_AFP_HOMES_UMASK=${OMV_NETATALK_AFP_HOMES_UMASK:-"0077"}

[ "$(omv_config_get "//services/afp/enable")" = "0" ] && exit 0

basedirregex="${OMV_NETATALK_AFP_HOMES_BASEDIRREGEX}"
if [ "$(omv_config_get "//system/usermanagement/homedirectory/enable")" = "1" ]; then
	sfref=$(omv_config_get "//system/usermanagement/homedirectory/sharedfolderref")
	basedirregex=$(omv_get_sharedfolder_path "${sfref}")
fi

xmlstarlet sel -t -m "//services/afp" \
  -i "homesenable[. = '1']" \
    -n \
    -o "[Homes]" -n \
    -o "home name = ${OMV_NETATALK_AFP_HOMES_HOMENAME}" -n \
    -o "file perm = ${OMV_NETATALK_AFP_HOMES_FILEPERM}" -n \
    -o "directory perm = ${OMV_NETATALK_AFP_HOMES_DIRECTORYPERM}" -n \
    -o "umask = ${OMV_NETATALK_AFP_HOMES_UMASK}" -n \
    -o "basedir regex = ${basedirregex}" -n \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc >> ${OMV_NETATALK_AFP_CONFIG}
