#!/usr/local/bin/python
import string,sys

if len(sys.argv) > 1 and sys.argv[1] == "-c":
  iam = "committers"
  title = "Apache Committers"
else:
  iam = "projects"
  title = "Apache Committers by Project Modules"

sys.stdout = open(iam+'.html', 'w')

# populate dictionary fullname from passwd file
fullname = {}
for line in open('/etc/passwd','r').readlines():
  data = string.split(line,':')
  # handle bogusness of comments in passwd
  if len(data) > 5:
    fullname[data[0]] = string.split(data[4],',')[0]

# Now populate the members array
members = {}
for line in open('members.dat','r').readlines():
  data = string.split(line)
  members[data[0]] = data[0]

# populate dictionary module from avail file
# tally tallies the no. of CVS modules the user is a member of
module = {}
module["cvs-committers"] = []
tally = {}
for line in open('/home/cvs/CVSROOT/avail','r').readlines():
  data = string.split(line,'|')
  if data[0]=='avail':
    for name in string.split(data[2][0:-1], ','):
      if iam == 'projects':
        if not module.has_key(name): module[name]=[]
      for id in string.split(data[1], ','):
        if iam == 'projects':
          if not id in module[name]: module[name].append(id)
        else:
          if not id in module["cvs-committers"]: module["cvs-committers"].append(id)
        if not tally.has_key(id):
          tally[id] = 1
        else:
          tally[id] = tally[id] + 1

# front matter

print '<html>'
print '<head>'
print '<title>' + title + '</title>'
print '</head>'
print '<body bgcolor="#ffffff" text="#000000" link="#525D76">'
print '<a href="http://www.apache.org">'
print '<img border="0" '
print ' src="http://www.apache.org/images/asf-logo.gif">'
print '</a>'
print '<h1>' + title + '</h1>'
print '<table cellspacing="4" width="100%" border="0">'
print '<tr>'
print '<td colspan="2">'
print '<hr size="1" noshade="">'
print '</td>'
print '</tr>'
print '<tr>'
print '<td nowrap="true" valign="top">'
print '<h2>Project Modules</h2>'

# produce an index

modules = module.keys()
modules.sort()

cols=5
rows=(len(modules)+cols-1)/cols
print '<table border="0">'
for row in range(0,rows):
  print '<tr>'
  for col in range(0,cols):
    try:
      name=modules[row+col*rows]
      print '<td><a href="#' + name + '">' + name + '</a></td>'
    except:
      break
  print '</tr>'
print '</table>'

# produce a sorted list of committers to each project

print '<hr size="1" noshade="" />'
for name in modules:
  print '<h2 id="' + name + '">' + name + '</h2>'
  print '<table border="1">'
  module[name].sort()
  for id in module[name]:
    if fullname.has_key(id):
      print '  <tr>'
      print '    <td>',
      if members.has_key(id):
         print '<b>' + id + '</b>',
      else:
         print id,
      print '</td>'
      print '    <td>',
      if members.has_key(id):
         print '<b>' + fullname[id] + ' </b>',
      else:
         print fullname[id],
      print '</td>'
      print '  </tr>'
  print '</table>'

# closing
 
print '</table>'
print '</body>'
print '</html>'
