#!/bin/sh
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @author    OpenMediaVault Plugin Developers <plugins@omv-extras.org>
# @copyright Copyright (c) 2009-2013 Volker Theile
# @copyright Copyright (c) 2013-2024 OpenMediaVault Plugin Developers
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.

if [ $# -ne 3 ]; then
  echo "Wrong number of arguments passed."
  echo "${0} [path] [dirperms] [clearacls]"
  exit 1
fi

# logging location
logDir="/var/log/"
logFile="${logDir}/omv-resetperms.log"

_log()
{
  msg=${1}
  echo "[$(date +'%Y-%m-%d %H:%M:%S%z')] [resetperms] ${msg}" | tee -a ${logFile} >&2
}

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

export LANG=C

usergrp=${OMV_USERMGMT_DEFAULT_GROUP:-"users"}

path="${1}"
dirperms="${2}"
clearacl="${3}"

case ${dirperms} in
  "700") fileperms="600";;
  "750") fileperms="640";;
  "770") fileperms="660";;
  "755") fileperms="644";;
  "775") fileperms="664";;
  "777") fileperms="666";;
  *)     fileperms="644";;
esac

_log "path :: ${path}"
_log "dir perms :: ${dirperms}"
_log "fil eperms :: ${fileperms}"
_log "clear acls :: ${clearacl}"
_log "user group :: ${usergrp}"

# Removes all ACLs and sticky bits
if [ "${clearacl}" = "1" ]; then
  _log "Clearing ACLs ..."
  setfacl -Rb "${path}"
  _log "ACLs cleared."
fi

# Make root:users owner of the shared folder and all subdirs
_log "Changing owner to root:users ..."
chown -R root:${usergrp} "${path}"
_log "Ownership reset."

# For all folders that are inside that folder (recursively) and the folder itself
_log "Change directory permissions to ${dirperms} ..."
find "${path}" -type d -exec chmod "${dirperms}" {} +
_log "Directory permissions reset."

# For all files that are inside that folder (recursively)
_log "Change file permissions to ${fileperms} ..."
find "${path}" -type f -exec chmod "${fileperms}" {} +
_log "File permissions reset."

_log "Reset complete."

exit 0
