#!/usr/bin/python

from rha.command import runs

groups = ["music", "wrestle", "physics", "emperors", "governor", "games"]
user_hash = {
	'elvis': ["music", "wrestle", "physics", "emperors" ],
	'blondie': ["music", ],
	'prince': ["music", ],
	'madonna': ["music", ],
	'ventura': ["governor", "wrestle", ],
	'hogan': ["wrestle", ],
	'pataki': ["governor", ],
	'einstein': ["physics", ],
	'maxwell': ["physics", ],
	'julius': ["emperors", ],
	'nero': ["emperors", ],
	'alice': ["music", "games", ],
	'bob': [],
}

import grp

ghash = {}
for g in groups:
	print "checking group %s." % g
	try: ghash[g] = grp.getgrnam(g)[3]
	except KeyError,e: 
		c = runs("/usr/sbin/groupadd -r %s" % g)
		if c: raise "bad groupadd: " + c.dump()
		ghash[g] = []

import pwd, string
for u,glist in user_hash.items():
	print "checking user %s." % u
	try: pwd.getpwnam(u)
	except KeyError, e:
		if glist: g = "-G %s " % string.join(glist, ",")
		else: g = ""
		c = runs("/usr/sbin/useradd %s %s" % (g,u))
		if c: raise "bad useradd: " + c.dump()
		for g in glist: ghash[g].append(u)

	for g in glist: 
		if u not in ghash[g]:
			c = runs("/usr/sbin/usermod -G %s %s" % (g,u))
			if c: raise "bad usermod: " + c.dump()
