#!/usr/bin/python
#
# @package      hubzero-mw-service
# @file         set_quotas
# @author       Rick Kennell <kennell@purdue.edu>
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @author       John Rosheck <jrosheck@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2015 HUBzero Foundation, LLC.
#
# 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 HUBzero Foundation, LLC.
#

import sys
import os

classes={}
users={}
hub_homedir='/home'

#=============================================================================
# Load the configuration and override the variables above.
#=============================================================================
try:
  execfile('/etc/mw-service/mw-service.conf')
except IOError:
    pass

file=open("/etc/mw-service/quota.conf")
while 1:
  line=file.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


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

for user in list:
  if user.startswith("."):
    continue

  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("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)

