#!/usr/local/bin/python

import subprocess
import os
import stat
import time

numcols=5

def getrange(date):
	year=int(date[0:4])
	month=int(date[4:6])
	day=int(date[6:8])
	start=time.mktime((year,month,day,0,0,0,0,0,0))
	end=time.mktime((year,month,day,23,59,59,0,0,0))
	return (start,end)


def makeday(date):
	(start,end)=getrange(date)
#	print "date: %s  %d to %d"%(date,start,end)
	col=1
	toppath="~tjohnson/"
	day=[]
	for j in names:
		if ((names[j]>start) and (names[j]<end)):
			day.append(j)
	
	if (len(day)>0):
		day.sort()
		print "<table border=1><tr>"
		for j in day:
			jsp=j.split("/")
			mediumpath=toppath+"/".join(jsp[6:])
			thumbpath=toppath+"/".join(jsp[6:-1])+"/thumbs/"+jsp[-1]
			print "<td><a href=\"/%s\" target=\"_new\"><img src=\"/%s.jpg\"></a>"%(mediumpath,thumbpath)
			print "</td>"
			if (col>numcols):
				print "</tr><tr>"
				col=0
			col=col+1
		print "</tr></table>"


imagetop="/home/bobdbob/mm/protius/stills/lab"


kid=subprocess.Popen("grep -i lumonics %s/*/desc.txt"%(imagetop), shell=True, stdout=subprocess.PIPE)

(list,foo)=kid.communicate()

names={}

for i in list.splitlines():
	(name,desc)=i.split(":")
	date=name.split("/")[-2]
	dir="/".join(name.split("/")[:-1])
	
#	print "%s   %s"%(date,dir)

	for j in os.listdir(dir):
		st=os.stat(dir+"/"+j)
		if (stat.S_ISREG(st.st_mode) and ( (j[-3:]=="jpg") or (j[-3:]=="JPG"))):
			names[dir+"/"+j]=st.st_ctime


f=open("notes.txt")

for line in f:
	if (line.find("IMAGES:")==0):
		makeday(line[7:])
	else:
		print line,
