#!/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-2012 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/>.

# Documentation/Howto:
# http://www.ibm.com/developerworks/linux/library/l-lpic1-v3-104-4/index.html

set -e

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

xmlstarlet sel -t -m "//system/storage/filesystem/quota" \
  -v "concat(uuid,' ',fsuuid)" \
  -i "position() != last()" -n -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc |
  while read uuid fsuuid; do
	  # Get the device file from the given filesystem UUID.
	  devicefile=$(blkid -U ${fsuuid}) || true
	  if [ -n "${devicefile}" ]; then
		  # Turn filesystem quotas off.
		  quotaoff -gu ${devicefile}
		  # Check if there exists any user or group quota, otherwise it is not
		  # necessary to turn on quota for this filesystem.
		  quotaon=$(xmlstarlet sel -t -m "//system/storage/filesystem/quota[uuid='${uuid}']" \
			-i "count(usrquota) > 0 or count(grpquota) > 0" -o "1" -b \
			-i "count(usrquota) = 0 and count(grpquota) = 0" -o "0" -b \
			${OMV_CONFIG_FILE})
		  [ ${quotaon} -eq 0 ] && continue;
		  # Create the quota files.
		  quotacheck -cugnv ${devicefile}
		  # Set the user quotas.
		  xmlstarlet sel -t -m "//system/storage/filesystem/quota[uuid='${uuid}']/usrquota" \
			-v "concat(name,' ',bsoftlimit,' ',bhardlimit,' ',isoftlimit,' ',ihardlimit)" \
			-i "position() != last()" -n -b \
			${OMV_CONFIG_FILE} | xmlstarlet unesc | setquota -b -u ${devicefile}
		  # Set the group quotas.
		  xmlstarlet sel -t -m "//system/storage/filesystem/quota[uuid='${uuid}']/grpquota" \
			-v "concat(name,' ',bsoftlimit,' ',bhardlimit,' ',isoftlimit,' ',ihardlimit)" \
			-i "position() != last()" -n -b \
			${OMV_CONFIG_FILE} | xmlstarlet unesc | setquota -b -g ${devicefile}
		  # Turn filesystem quotas on.
		  quotaon -gu ${devicefile}
	  fi

	  # Set the user quotas
#	  xmlstarlet sel -t -m "//system/storage/filesystem/quota[uuid='${uuid}']/usrquota" \
#		-v "concat(userref,' ',bsoftlimit,' ',bhardlimit,' ',isoftlimit,' ',ihardlimit)" \
#		-i "position() != last()" -n -b \
#		${OMV_CONFIG_FILE} | xmlstarlet unesc |
#		while read userref bsoftlimit bhardlimit isoftlimit ihardlimit; do
#			# Get the user name.
#			name=$(omv_config_get "//system/usermanagement/users/user[uuid='${userref}']/name")
#			# Set the quota for the given user.
#			setquota -u ${name} ${bsoftlimit} ${bhardlimit} ${isoftlimit} ${ihardlimit} ${devicefile}
#		done
	  # Set the group quotas
#	  xmlstarlet sel -t -m "//system/storage/filesystem/quota[uuid='${uuid}']/grpquota" \
#		-v "concat(groupref,' ',bsoftlimit,' ',bhardlimit,' ',isoftlimit,' ',ihardlimit)" \
#		-i "position() != last()" -n -b \
#		${OMV_CONFIG_FILE} | xmlstarlet unesc |
#		while read groupref bsoftlimit bhardlimit isoftlimit ihardlimit; do
#			# Get the group name.
#			name=$(omv_config_get "//system/usermanagement/groups/group[uuid='${groupref}']/name")
#			# Set the quota for the given group.
#			setquota -g ${name} ${bsoftlimit} ${bhardlimit} ${isoftlimit} ${ihardlimit} ${devicefile}
#		done
  done
