#!/usr/bin/env python

import os

cur = os.path.abspath(os.curdir)

def archive_directories():
    for F in os.listdir('.'):
        if os.path.isdir(F):
            cmd = "mv -v %s ../archive/"%F
            print cmd
            os.system(cmd)

def package_name(F):
    i = F.find("-")
    if i == -1:
        return F
    return F[:i]

def archive_all_but_newest(F, D):
    if not os.path.exists(F):
        return
    if not os.path.exists("../archive/"):
        os.makedirs("../archive/")
    name = package_name(F)
    versions = [(os.path.getmtime(m), m) for m in D if os.path.exists(m) and package_name(m) == name]
    versions.sort()
    for G in versions[:-1]:
        print "Archiving %s..."%G[1]
        cmd = "mv %s.* ../archive/"%G[1][:-5]
        print cmd
        os.system(cmd)

def index(dir):
    os.chdir(dir)
    if dir != 'archive':
        archive_directories()
    s = '<html><title>SAGE %s Packages</title>\n<body>\n<h1 align=center><a href="..">SAGE %s Packages</a></h1>\n'%(dir.capitalize(), dir.capitalize())
    if dir == 'experimental':
        s += "<h1 align=center>These are EXPERIMENTAL!  They probably won't work at all for you!  Use at your own risk!  Many of these have *never* been successfully built on any platform!  (But still, if you can figure out how to build them, I'd like to know about it.)   These also my not be available under a GPL-compatible license.</h1>"
    s += "<table width=70% bgcolor=black cellpadding=10 align=center><tr><td bgcolor=white>\n"
    if dir != 'standard':
        s += "To install one of these packages, type e.g., the following at the UNIX shell prompt:"
        s += "<pre>        sage -i db-jones-2005-11-02</pre>"
        s += "Note that the package name contains the version number, and it "
        s += "will be downloaded automatically if necessary."
    else:
        s += "To upgrade to the latest versions of all these packages, at the UNIX shell prompt type:"
        s += "<pre>        sage -upgrade</pre>"
    s += "\n</tr></td></table><br><hr><br>\n"
        
    s += '<table align=center width=100% bgcolor=black cellpadding=10>\n'
    D = [F for F in os.listdir('.') if F[-5:] == ".spkg"]
    D.sort()
    for F in D:
        if dir != 'archive':
            archive_all_but_newest(F, D)
        if not os.path.exists(F):
            continue
        txt = "%s.txt"%F[:-5]
        if os.path.exists(txt):
            O = open(txt).read()
            i = O.find('\n')
            if i != -1:
                desc = O[:i]
                details = O[i+1:]                
            else:
                desc = O
                details = ""
            X = open("%s.html"%F[:-5], 'w')
            t = '<html><title>%s</title>\n<body>\n<h1 align=center><a href="../">SAGE Package</a>: %s</h1>\n'%(F[:-5], F[:-5])
            t += '<hr><h2 align=center><a href="../%s/%s">%s</a>: %s</h2><hr><br>'%(dir, F, F[:-5], desc)
            t += '<table align=center cellpadding=15><tr><td>%s</td></tr></table>'%details
            t += '</body></html>'
            X.write(t)
        else:
            desc = ''
        s += '<tr><td bgcolor=white><a href="%s">%s</a></td><td bgcolor=white><a href="%s.html">%s</a></td></tr>\n'%(F,F[:-5],F[:-5],desc)
    s += '\n</table>\n'
    if os.path.exists('deps'):
        s += '<br><center><a href="deps">Dependency File</a></center>\n'
    s += '\n</body></html>\n'
    open('index.html','w').write(s)
    open('list','w').write('\n'.join([F[:-5] for F in D]))
    
    os.chdir(cur)

index('standard')
index('optional')
index('experimental')
index('archive')
