#!/bin/bash

#
# Copyright (C) 2018 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer 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.
#
# NethServer 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 NethServer.  If not, see COPYING.
#

. /usr/libexec/nethserver/api/lib/helper_functions

# Read JSON from stdin and set the record
data=$(cat /dev/stdin)
vh=$(echo $data | jq -r '.props.VirtualHost')
td=$(echo $data | jq -r '.props.TrustedDomains | join(",")')
wn=$(echo $data | jq -r '.props.Wellknown')

/sbin/e-smith/config setprop nextcloud VirtualHost "$vh" TrustedDomains "$td" Wellknown "$wn"

if [ $? -gt 0 ]; then
    invalid_error
fi

/sbin/e-smith/signal-event -j nethserver-nextcloud-save
if [ $? -gt 0 ]; then
    error "EventFailed" "See /var/log/messages"
else
    success
fi
