############################################################
# Michael Wallick:  This is a quick python script to       #
# display thumbnail images of jpeg pictures (and rename    #
# them - to prevent collisions) in an HTML page.           #
#							   #
# useage (from command prompt in folder where you want the #
# page to go:  python makepage.py			   #
# run this program every time you add files to the folder  #
#							   #
# You must have ImageMagick's convert in your home 	   #
# directory inorder to run this script (to generate preview#
# pictures						   #
#							   #
# This script may be redistrubuted and modified provided   #
# this message remains more or lesse intact.  This 	   #		   
# mesage may be changed to reflect changes made to the     #
# script.  Please leave credit to the original author      #
# (Michael Wallick)					   #
############################################################

import os

tmppage = open("index.html", "w")
tmppage.write("<h1>Page is currently being rebuilt, please try again in a few minutes</H1>")
tmppage.close()

pics = os.listdir(os.getcwd()) 
webpage = open("index.html", "w")

webpage.write("<HTML>\n<TITLE>Michael's Photo Album</TITLE>\n")

webpage.write("<BODY>\n")
webpage.write("Click a picture to see a larger view.  Note: Individual pictures will take a while to load!<br><br>\n")

templist = []

for i in range(len(pics)):
	if pics[i][-6:] == "tn.jpg":
		os.remove(pics[i])

pics = os.listdir(os.getcwd())

for i in range(len(pics)):
	pic = pics[i].lower()
	if pic[-3:] == "jpg":
		#nme = "%05d.jpg"%(i)	
		webpage.write('<a href="%s"><img src="%s-tn.jpg"></a>\n'%(pics[i],pics[i][:-4]))
		#os.rename(pics[i], nme)
                print('convert -size 75x50 "%s" "%s-tn.jpg"'%(pics[i], pics[i][:-4]))
		os.system('convert -size 75x50 "%s" "%s-tn.jpg"'%(pics[i], pics[i][:-4]))
	else :
		templist.append(pics[i])

webpage.write("<br>\n")
webpage.write("Other Files<br>\n")
for i in range(len(templist)):
	if templist[i][-2:] != "py" and templist[i][-4:]!="html" and templist[i]!="convert" and templist[i]!=".libs" and templist[i][-3:]!=".db":
		webpage.write('<a href="%s">%s</a><br>\n'%(templist[i],templist[i]))

#		nme = "%05d-other_file.%s"%(i,templist[i][-3:])
#		webpage.write('<a href="%s">%s</a><br>\n'%(nme, nme))
#		os.rename(templist[i], nme)

webpage.write('\n<br><br>This page was created by Michael Wallick\'s <a href="makepage.py"</a>"Make Page"</a> Python Script<br><br>\n')
webpage.write('<a href="http://www.python.org">Python Webpage</a><br><br>')

webpage.write("</HTML>")

webpage.close()
