#!/bin/bash

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - support@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 <http://www.gnu.org/licenses/>.
#

# skip event name

shift

# check if engine used is rsync

backup=$1
[ "$(/sbin/e-smith/db backups gettype "${backup}")" == "rsync" ] || exit 0

# if rsync then fix permissions

if [[ -d "/var/lib/asterisk/" && $(stat -c %U:%G "/var/lib/asterisk/") != "asterisk:asterisk" ]]; then
  /bin/chown asterisk:asterisk /var/lib/asterisk
  /bin/chown -R asterisk:asterisk /var/lib/asterisk/moh
  /bin/chown -R asterisk:asterisk /var/lib/asterisk/sounds
  /bin/chown -R asterisk:asterisk /var/spool/asterisk
fi

# if logs are included in the backup then fix their permissions too

if [[ "$(/sbin/e-smith/config getprop backup-data IncludeLogs)" == "enabled" ]]; then
  if [[ -d "/var/log/asterisk" ]]; then
    /bin/chown -R asterisk:asterisk /var/log/asterisk
  fi
    
  if [[ -d "/var/log/pbx/" ]]; then
    /bin/chown -R asterisk:asterisk /var/log/pbx
  fi
fi
exit 0
