#! /usr/bin/python

import os, string, sys, HTML, Documents

if len(sys.argv) < 5:

    print "Usage: " + sys.argv[0] + " title thumbnail_size smallimage_size row_length files ..."
    print "  make a web page from the listed image files."

else:

    title     = sys.argv[1]
    thumbsize = int(sys.argv[2])
    smallsize = int(sys.argv[3])
    rowlen    = int(sys.argv[4])    
    
    doc = Documents.Document()
    doc.append(HTML.TITLE(title))
    bod = HTML.BODY(bgcolor="white", text="black")
    bod.append(HTML.CENTER(HTML.H1(title)))
    #bod.append("<br>If you click on a thumbnail, a larger picture will appear.  If you click on 'ORIGINAL', a much larger picture will probably appear.<br><br><br>");

    table = HTML.TABLE(border=0, cellpadding=10)
    cnt = 0
    current_row = HTML.TR()
    for i in range(5,len(sys.argv)):
        if cnt % rowlen == 0 and cnt != 0:
            table.append(current_row)
            current_row = HTML.TR()

        image  = sys.argv[i]
        if (image[-3:] == "avi" and image[-5:] != "*.avi") or (image[-3:] == "mpg" and image[-5:] != "*.mpg"):
            current_row.append(HTML.TD(
                "MOVIE",HTML.BR(),
                HTML.A(image, href=image)))
            cnt = cnt + 1
        elif image[-3:] == "jpg" and image[-5:] != "*.jpg":
            thumb = "thumb_" + image
            small = "small_" + image
            desc = image[0:string.find(image,".")] + ".txt"
            os.system("cp "+image+" "+thumb)
            os.system("cp "+image+" "+small)
            cmd = "mogrify -geometry x"+str(thumbsize)+ " " + thumb;
            os.system(cmd)
            cmd = "mogrify -geometry x"+str(smallsize)+ " " + small;
            os.system(cmd)
            if os.path.exists(desc):
                description = image + ": " + open(desc).read()
            else:
                description = image
            #idstring = os.popen("idstring "+image).read()[1:-1]
            #if idstring != "Created with The GIMP":
            #    description = description + " (" + idstring + ")"
            current_row.append(HTML.TD(
                HTML.A(HTML.IMG(src=thumb),href=small), 
                HTML.BR(), 
                HTML.A("ORIGINAL", href=image),
                HTML.BR(),
                description))
	    cnt = cnt + 1

    while cnt % rowlen != 0:
        current_row.append(HTML.TD("&nbsp"))
        cnt = cnt + 1
        
    table.append(current_row)
    bod.append(HTML.CENTER(table))
    doc.append(bod)

    print doc
