#!/usr/bin/env python
#
# @package      hubzero-mw2-file-service
# @file         set_quotas
# @copyright    Copyright (c) 2016-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Based on prior work by Richard L. Kennell and Nicholas Kisseberth
#
# Copyright (c) 2016-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#
# Deployment information: this script should only be found on file servers.

import sys
import os
from hubzero.mw.constants import QUOTA_PATH, SERVICE_CONFIG_FILE

classes={}
users={}
hub_homedir = ''  # This should be set in SERVICE_CONFIG_FILE, and be something like /home/hubname

#=============================================================================
# Load the configuration and override the variables above.
#=============================================================================
try:
  execfile(SERVICE_CONFIG_FILE)
except IOError:
  print "Unable to read configuration file, exiting."
  print "The configuration file '%s' needs to exist" % (SERVICE_CONFIG_FILE)
  sys.exit(1)


qfile = open(QUOTA_PATH)
while 1:
  line = qfile.readline()
  if line == '':
    break

  line=line.strip()
  try:
    line=line[0:line.index('#')]
  except:
    pass
  arr=line.split()

  if len(arr) == 0:
    continue

  if arr[0] == 'class':
    if len(arr) != 6:
      print "Error with line: %s" % line
      sys.exit(1)

    classes[arr[1]] = [ int(arr[2]), int(arr[3]), int(arr[4]), int(arr[5]) ]

  elif arr[0] == 'user':
    if len(arr) != 3:
      print "Error with line: %s" % line
      sys.exit(1)

    users[arr[1]] = arr[2]

  else:
    print "Error with line: %s" % line

#for c in classes:
#  print "class %s" % c

#for u in users:
#  print "user %s" % u
print "set_quotas is deprecated, please use the web-based mechanism instead.  Comment out this line and the following one to use this script anyway, which may result in quotas inconsistent with the ones displayed in the web interface, being set."
sys.exit(1)

ulist = sys.argv
ulist.pop(0)
if len(ulist) == 0:
  ulist = os.listdir(hub_homedir)

for user in ulist:
  if user.startswith("."):
    continue
  if user == "aquota.user":
      continue

  # let the script work with usernames containing uppercase letters.  Uncomment for strict lowercase usernames.
  #user = user.lower()
  c='default'
  try:
    c=users[user]
  except:
    pass

  if c == 'ignore':
    continue

  info=[0,0,0,0]
  try:
    info=classes[c]
  except:
    print "Unknown class, %s, for user %s" % (c,user)
    continue

  status = os.system("/usr/sbin/setquota -a %s %d %d %d %d" % (user, info[0], info[1], info[2], info[3]))
  if status != 0:
    print "error: %s %d %d %d %d" % (user, info[0], info[1], info[2], info[3])

sys.exit(0)

