#!/usr/bin/python
"""Bunch of utility functions for administrating the tool. These include--

adding a new team/player
updating a team or player
"""

import os, sys, string
from gagz.bugz import konst, player, team

#
# addteam teamname "Full Descriptive Name"
#
def AddUserOrTeam(name, fullname):
    """Add a team to the top level site """
    userdir = konst.kSiteInstallationDir + name
    if not os.access(userdir, os.F_OK):
        print 'creating %s' % userdir
        os.makedirs(userdir)
    print 'writing initial .data file'
    f = open(userdir + '/.data', 'w')
    f.write("""# Auto generated! DO NOT EDIT!\n[DEFAULT]\nname:%s\n""" 
        % fullname)
    f.close()

def AddTeam(name, fullname):
    AddUserOrTeam(name, fullname)
    print 'creating sym link for index.cgi'
    os.system('ln -s ../groupindex.cgi %s/index.cgi' % (konst.kSiteInstallationDir + name))


#
# adduser.py username "Full Name"
#
def AddUser(username, fullname, teem='.'):
    """Add a user to the directory"""
    userdir = teem + '/' + username
    if not os.access(userdir, os.F_OK):
        print 'creating', userdir
        os.makedirs(userdir)
        print 'writing initial .data file'
        f = open(userdir + '/.data', 'w')
        f.write("""# Auto generated! DO NOT EDIT!\n[DEFAULT]\nname:%s\n""" 
            % fullname)
        f.close()
    print 'creating sym link for index.cgi'
    os.system('ln -s ../../userindex.cgi %s/index.cgi' % userdir)
    t = team.Team(datadir=teem+'/')
    print 'adding %s to team %s' % (username, t.name)
    if username not in t.team: t.team.append(username)
    t.saveHistory = 0
    t.save()
    print 'all done!'

