#! /usr/bin/env python

import sys, os, string, stat, time

def processjpeg(jpegpath):
    haveThumb = " No Thumb"
    entryname = os.path.split(jpegpath)[1]
    thumbName = os.path.split(jpegpath)[0]+'/thumbs/'+entryname
    # print thumbName
    if os.path.isfile(thumbName):
        haveThumb = "<a href=\"%s\">Has Thumb</a>" % (thumbName)
    number = 0
    try:
        fstat = os.stat(jpegpath)
        number = fstat[stat.ST_SIZE]/1000	# add in the file's size
    except os.error, exc:
        sys.stderr.write("stat error %s:%s\n" % (jpegpath, exc))
    print "%12s %4d %s <a href=\"%s\">%s</a> %s" % (entryname, number, haveThumb, jpegpath, entryname, jpegpath)

def processhtml(jpegpath):
    number = 0
    try:
        fstat = os.stat(jpegpath)
        number = fstat[stat.ST_SIZE]/1000	# add in the file's size
    except os.error, exc:
        sys.stderr.write("stat error %s:%s\n" % (jpegpath, exc))
    print "(HTML)       %4d <a href=\"%s\">%s</a>" % (number, jpegpath, jpegpath)

def processother(type, path):
    number = 0
    try:
        fstat = os.stat(path)
        number = fstat[stat.ST_SIZE]/1000	# add in the file's size
    except os.error, exc:
        sys.stderr.write("stat error %s:%s\n" % (path, exc))
    print "(%s)        %4d <a href=\"%s\">%s</a>" % (type, number, path, path)

def processdir(arg, dir, names):
    randoms = 0
    things = 0
    subdirs = 0
    dirname = os.path.split(dir)[1]
    if dirname == 'thumbs':
        return
    if dirname == 'code':
        return
    if dirname == 'private':
        return
    if dirname[0] == '.':
        print "found XVPICS "+os.path.abspath(dir)
        return
    for filename in names:
        ext = os.path.splitext(filename)[1]
        fullname = os.path.join(dir,filename)
        if ext == '.htm' or ext == '.html' or ext == '.HTM':
            things = things+1
            processhtml(fullname)
        elif ext == '.jpg' or ext == '.gif' or ext == 'png':
            things = things+1
            processjpeg(fullname)
        elif ext == '.txt':
            things = things+1
            processother("TXT", fullname)
        elif ext == '.pdf':
            things = things+1
            processother("PDF", fullname)
        elif ext == '.doc':
            things = things+1
            processother("DOC", fullname)
        elif ext == '.rtf':
            things = things+1
            processother("RTF", fullname)
        elif os.path.isdir(fullname):
            subdirs = subdirs+1
        else:
            randoms = randoms+1
    print "(DIR) %s has %d files, %d subdirs and %d randoms" % (dir, things, subdirs, randoms)

def main():
    print "<html><head>"
    print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"
    print "<meta name=\"Author\" content=\"YON - Jan C. Hardenbergh\">"
    print "<meta name=\"GENERATOR\" content=\"Emacs & Python & Yo momma\">"
    print "<title>index of files</title></head><body><pre>"
    print time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(time.time()))
    
    inputdirs = sys.argv[1:]

    for dir in inputdirs:
        os.path.walk(dir,processdir,0)

    print "</pre></body></html>"


if __name__ == '__main__':
    main()
