#!/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-2015 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_AVAHIDAEMON_OWNCLOUDSERVICE=${OMV_AVAHIDAEMON_OWNCLOUDSERVICE:-"/etc/avahi/services/owncloud.service"}

# Announce this service via DNS-SD?
rm -f ${OMV_AVAHIDAEMON_OWNCLOUDSERVICE}
[ "$(omv_config_get "//services/owncloud/enable")" != "1" -o "$(omv_config_get "//services/zeroconf/services/service[id='owncloud']/enable")" != "1" ] && exit 0

# Create '/etc/avahi/services/http.service' file.
xmlstarlet sel -t \
  -o "<?xml version=\"1.0\" standalone=\"no\"?>" -n \
  -o "<!DOCTYPE service-group SYSTEM \"avahi-service.dtd\">" -n \
  -o "<service-group>" -n \
  -m "//services/owncloud" \
	  -v "concat('  <name replace-wildcards=\"yes\">',//services/zeroconf/services/service[id='owncloud']/name,'</name>')" -n \
	  -o "  <service>" -n \
	  -o "    <type>_https._tcp</type>" -n \
	  -v "concat('    <port>',port,'</port>')" -n \
	  -o "  </service>" -n \
  -b \
  -o "</service-group>" \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_AVAHIDAEMON_OWNCLOUDSERVICE}
