#!/usr/bin/python
import os, sys, glob, time, tempfile, shutil

output_filename = 'index.html'

file_list = []
tmp_filename = tempfile.mkstemp()[1]
table_flag = False

path = os.getcwd()
if sys.argv[1:]:
	if (sys.argv[1:][0] == '-t'):
		table_flag = True
	else:
		path = sys.argv[1:][0]

if (os.path.exists(output_filename) == True):
	print "%s already exist! aborted..." % output_filename
	sys.exit(-1)

f_out = open(tmp_filename, "w")

for dir in glob.glob(path):
	for item in glob.glob(dir + '/*.*'):
		#  0     1    2    3      4    5    6     7      8      9
 		# (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
		item_stats = os.stat(item)
		item_lmdate = time.localtime(item_stats[8])
		item_size = item_stats[6]
		item_tuple = item, item_size, item_lmdate
		file_list.append(item_tuple)

file_list.sort()

f_out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f_out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n")
f_out.write("\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\n")
f_out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n")
f_out.write("<head>\n")
f_out.write("\t<title>Files</title>\n")
f_out.write("\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n")
f_out.write("\t<style type=\"text/css\">\n")
f_out.write("\t\ttd { border: 1px solid #000; padding: 6px; }\n")
f_out.write("\t\ta:link, a:visited, a:hover, a:active { color: #026; text-decoration: underline; }\n")
f_out.write("\t</style>\n")
f_out.write("</head>\n\n")
f_out.write("<body style=\"background-color: #fff\">\n\n")
f_out.write("\t<table style=\"border: 1px solid #000; border-collapse: collapse; width: 60%;\">\n")
f_out.write("\t<tr style=\"background-color: #000; color: #FFF; font-weight: bold;\">\n")
f_out.write("\t\t<td style=\"width: 80%;\">Name</td>\n")
f_out.write("\t\t<td>Size</td>\n")
if (table_flag == True):
	f_out.write("\t\t<td>Last modified</td>\n")
f_out.write("\t</tr>\n")

n = 0
for file in file_list:
	dir, file_name = os.path.split(file[0])
	file_size = str(file[1]) + '&nbsp;bytes'
	if (n % 2):
		f_out.write("\t<tr>\n")
	else:
		f_out.write("\t<tr style=\"background-color: #eee;\">\n")
	f_out.write("\t\t<td><a href=\"%s\">%s</a></td>\n" % (file_name, file_name))
	f_out.write("\t\t<td>%s</td>\n" % (file_size))
	if (table_flag == True):
		file_date = time.strftime("%d-%b-%Y&nbsp;-&nbsp;%H:%M:%S", file[2])
		f_out.write("\t\t<td>%s</td>\n" % (file_date))
	f_out.write("\t</tr>\n")
	n = n + 1

f_out.write("\t</table>\n\n")
f_out.write("</body>\n")
f_out.write("</html>\n\n")
f_out.write("<!-- http://clay.ll.pl -->\n")

f_out.close()

shutil.move(tmp_filename, os.getcwd()+ "/" + output_filename)

# 10.12.2007


