#!/cygdrive/c/python33/python
import pdb
import subprocess

# read in the template html files used for the various pages
# returns a string
def get_html (filename):
    html = open(filename).read()
    return html

# input the directory in pics directory to process
#dir = input('base directory to process: ')
space10 = '          '
dir = '2013_09_28'
ndir = '/cygdrive/c/htdocs/birds/Pictures/' + dir 
# list the .jpg/.JPG files in the specified directory
lss = str(subprocess.check_output(['ls', ndir]), encoding='utf8')
files = lss.split('\n')

# get html templates
top = get_html('top.html')
end = get_html('end.html')
pic = get_html('pic.html')

# open file for output
outfile = open('mypics.shtml', 'w')

outfile.writelines(top)
outfile.write (space10 + '<tr>\n')
jj = 0
num = 1
for i in files:
	if '.jpg' in i or '.JPG' in i:
		if jj > 2:
				outfile.write (space10 + '</tr>\n')
				outfile.write (space10 + '<tr>\n')
				jj = 0
		filen = 'Pictures/' + dir +'/' + i
		line = pic.format(pic=filen, caption=num)
		outfile.write (space10 + line)
		jj += 1
		num += 1
outfile.write (space10 + '</tr>\n')
outfile.writelines(end)
outfile.close()
#pdb.set_trace()
