#!/cygdrive/c/python33/python
import pdb
import subprocess
# Program to generate html pages from a set of pictures

# read in the template html files used for the various pages
# returns a string
def get_html (filename):
    html = open(filename).read()
    return html

space10 = '          '

# input the list of pics to process
filename = input('file to process: ')
filename = filename.rstrip('\n')
inp = open(filename)
# input the base name of the .shtml
htmlbase = input('base name for html output: ')
htmlbase = htmlbase.rstrip('\n')

# get html templates
top = get_html('top.html')
top1 = get_html('top1.html')
end = get_html('end.html')
pic = get_html('pic.html')

		# count the number of lines in the file to know how many pages I will be needing
numl = sum(1 for line in open(filename))
numpage = (numl - 1)/39.
if numl % numpage:
	numpage = int(numpage+1)

		# cycle through the input lines
onp = open(filename)
		# read first line which contains the directory to the data files
line = inp.readline()
line = line.rstrip('\n')
aline = line.split('\t')
datadir = aline[0]
maintitle = aline[1]

jj = 0		# tracks number of pictures in a row
pagnum = 1	# Page being worked on
count = 1	# Picture number being processed
pcount = 1	# number of pictures being processed within a page, greater than 39 go to next page

		# now cycle through the rest of the lines creating the html.  The maximum number of pictures
		# per html file is 39
for line in inp:
	line = line.rstrip('\n')
	aline = line.split('\t')
	if len(aline) == 1:
		aline.append('')
	if pcount > 39:
		# put ending html in open file, close it
		outfile.write (space10 + '</tr>\n')
		outfile.write (space10 + '</table>\n')
		outfile.write (space10 + '</div>\n')
		#pdb.set_trace()
		for i in range (1, numpage+1):
			if i == 1 and pagnum > 1:
				astring = '<a href="' + htmlbase + str(pagnum-1) + '.shtml">' + '&#8592; Previous  '
				outfile.write (space10 + astring)
			astring = '<a href="wedding' + str(i) + '.shtml">' + str(i)
			if (i == pagnum):
				astring = '<a href="' +htmlbase + str(i) + '.shtml"><font color="red">' + str(i) + '</font>'
			outfile.write (space10 + astring)
			if i == numpage and pagnum < numpage:
				astring = '<a href="' +htmlbase + str(pagnum+1) + '.shtml">' + '  Next &#8594;'
				outfile.write (space10 + astring)
		outfile.writelines(end)
		outfile.close()
		#pdb.set_trace()
		pagnum += 1
		pcount = 1
		jj = 0
	if pcount == 1:
		# open file for output
		outf = htmlbase + str(pagnum) + '.shtml'
		outfile = open(outf, 'w')
		top1 = top1.format(atitle=maintitle)
		ptop = top + top1
		outfile.writelines(ptop)
		outfile.write (space10 + '<tr>\n')
		
		# now print the picture
	if jj > 2:
			outfile.write (space10 + '</tr>\n')
			outfile.write (space10 + '<tr>\n')
			jj = 0
	filen = datadir + aline[0]
	filesm = datadir + 'small/' + aline[0]
	fileful = datadir + 'orig/' + aline[0]
	linkful = '<br> <a href="' + fileful + '">(Full sized Pic.)</a>'
	pict = aline[0].replace('.JPG','')
	#pdb.set_trace()
	if aline[1]:
		title = str(count) + ', ' + pict + ' - ' + aline[1] + linkful
	else:
		title = str(count) + ', ' + pict + linkful
	oline = pic.format(pic=filen, pic_sm=filesm, caption=title)
	outfile.write (space10 + oline)
	jj += 1
	pcount += 1
	count += 1
outfile.write (space10 + '</tr>\n')
outfile.write (space10 + '</table>\n')
outfile.write (space10 + '</div>\n')
for i in range (1, numpage+1):
	if i == 1 and pagnum > 1:
		astring = '<a href="' + htmlbase + str(pagnum-1) + '.shtml">' + '&#8592; Previous  '
		outfile.write (space10 + astring)
	astring = '<a href="' + htmlbase + str(i) + '.shtml">' + str(i)
	if (i == pagnum):
		astring = '<a href="' + htmlbase + str(i) + '.shtml"><font color="red">' + str(i) + '</font>'
	outfile.write (space10 + astring)
outfile.writelines(end)
outfile.close()
