#!/usr/local/bin/python


import sys, os, time

if len(sys.argv) > 1:
  g= sys.argv[1]
else:
  g="GammaSpectrometry"

r =[]
idx = open(g + os.path.sep+  "index.html","w")
def iw(s):
  idx.write(s + "\r\n")
  r.append(s)

ln = """<a href="New.html">Same index, but new file first</a>"""
lo = """<a href="index.html">Same index, but old file first</a>"""

def head(gg,ll):
  return """<html><body>
<h1>%s group archive index</h1>
<ul>
<li>
%s
</li>
<li>
<a href="../%s.zip">A zip file of all messages in individual HTML file</a>
</li>
<hr>
""" % (gg, ll, gg)

idx.write(head(g,ln))

i = 1
if '-' in g:
  ia = g.rfind('-')
  i = int(g[ia+1:]+'0000') 
  if i <= 0: i = 1

xc = 0

while 1:
  try:
    s = open(g + os.path.sep+ str(i) + ".html").read()
  except:
    i = i + 1
    xc = xc + 1
    if xc > 300: break
    continue

  xc = 0
  title = ""
  author = ""
  senttime = ""

  ia = s.find('data-subject=') + 14
  if ia >= 0:
    ib = s.find('">', ia)
    if ib >= 0:
      title = s[ia:ib]

  ia = s.find('class="author', ib) + 14
  ia = s.find('">',ia) + 2
  if ia >= 0:
    ib = s.find('</', ia)
    if ib >= 0:
      author = s[ia:ib]

  ia = s.find('alt="Message sent time">', ib) + 24
  if ia >= 0:
    ib = s.find('</', ia)
    if ib >= 0:
      senttime = s[ia:ib]

  iw('''<a href="%d.html">%d.html</a>  %s : by %s  ---  %s<br>''' % (i, i, title,author, senttime))
  i = i + 1

#  break

foot = """<hr>
<a href="../">Back to Yahoo group Archive list</a> ... <a href="../../EnglishDocuments.html">Back to English Documents</a>
</ul><br><br>
</body></html>\r\n"""

idx.write(foot)
idx.close()


idxn = open(g + os.path.sep+  "New.html","w")
def iwn(s):
  idxn.write(s + "\r\n")

iwn(head(g,lo))
for s in r[::-1]:
  iwn(s)

iwn(foot)
idxn.close()

print "Done", i, xc
