#!/usr/bin/env python
#
# Copyright 2006-2009 by Purdue Research Foundation, West Lafayette, IN 47906.
# All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License, version 2.1 as published by the Free Software Foundation.

import sys
import os

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

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

file=open("/etc/hubzero/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)

