#!/usr/bin/python

HOMEDIR = "/home"				# The directory containing home directories to scan
IMAGESCALE = "300x200"  			# The size to scale incoming images to
THUMBSCALE = "100x75"				# The size to scale thumbs to
INDEXFILE = "index.csv" 			# The index file to look for
IMAGEMAGICK_CONVERT = "/usr/bin/convert" 	# The imagemagick "convert" binary
SMTP_HOST = "localhost"				# The SMTP server for sending failed message
FROM_ADDRESS = "failure-notice@example.com"	# The address failure messages come from
SEND_FAILURE_NOTICE = True			# Whether we are sending failure messages
SITE_NAME = "www.example.com"			# The site

"""
	petindex, scans FTP home directories for
	new data files. Run as part of a cron job or something.

	Does the following steps:
	
	1. Do we have an index.csv file?, if so continue.
	2. Delete all animal records in the database for this shelter
	3. Insert the CSV records into the database for this shelter
	4. Remove the index.csv file
	5. Scan the directory for images
	6. If an image isn't referred to in the database for that shelter, 
	   remove it.
	7. If the image *is* referred to in the database, scale it
	   (this is harmless and does nothing for already scaled images)

	If an error occurs at any point creating database records from
	the index file, the indexer will abandon processing for that
	homedir/shelter and move to the next.
	
	This file is covered by the GNU GPL v2

"""
import os
import os.path
import db
import smtplib

# Loop through home directories
for shelterid in os.listdir(HOMEDIR):

	print "Checking for shelter id: " + shelterid

	# Grab directory content for each home directory
	try:
		dircontent = os.listdir(HOMEDIR + os.path.sep + shelterid)
	except:
		# It's not a directory - move on
		continue
	
	# index datafile exists?
	if dircontent.__contains__(INDEXFILE):
		print "Got " + INDEXFILE

		homeindexfile = HOMEDIR + os.path.sep + shelterid + os.path.sep + INDEXFILE

		# Remove all animal records for this shelter
		print "Removing animal records for shelter " + shelterid
		db.executeQuery("DELETE FROM ANIMAL WHERE SHELTERID='%s'" % shelterid)

		# Parse and insert CSV records
		f = open(homeindexfile, "r")
		
		# Grab the field order and sanitise
		fieldorder = f.readline()
		fieldorder.replace(";", "_")
		
		# Make the INSERT clause from first line header. 
		head = "INSERT INTO ANIMAL (" + fieldorder + ") VALUES ("

		# Line counter as we process
		curline = 0
		try:

			# Go through each line and write an insert query
			for line in f.readlines():
				curline = curline + 1

				# Sanitize in case of SQL injection attacks
				line = line.replace(";", "_")
				line = line.replace("'", "_")

				# Substitute \\n for a real \n
				line = line.replace("\\n", "\n");
				
				# Substitute quotes for apostrophes ready for INSERT
				line = line.replace("\"", "'")

				# Create the query
				sql = head + line + ")"

				# Send it to the server
				print "Create: " + sql
				db.executeQuery(sql)

		except:
			# Close the file since we're going to skip to the next shelter
			f.close()

			# Might as well delete it as well to stop multiple error emails
			# being sent
			os.remove(homeindexfile)
			
			# If we're not sending failure emails, just skip
			if not SEND_FAILURE_NOTICE:
				break
			
			# Look up the email address for the shelter ID
			d = runQuery("SELECT EMAIL FROM SHELTER WHERE ID = '" + shelterid + "'");
			
			# If we don't have an email address, just skip, we can't do anything
			if len(d) == 0:
				break
			if d[0][0].strip() == "":
				break
				
			# Send the email
			to = d[0][0]
			subject = SITE_NAME + " failure notice"
			body = """
				There was a problem parsing your index file here on the server
				at %s

				The error occurred on line %s of the file. Please fix it and
				reupload.

				This is an automated message - PLEASE DO NOT REPLY.
				"""
			msg = "From: " + FROM_ADDRESS + "\r\nTo: " + to + "\r\nSubject: " + subject + "\r\n\r\n" + body

			# Send the email
			try:
				smtp = smtplib.SMTP()
				smtp.connect(SMTP_HOST, 25)
				smtp.sendmail(FROM_ADDRESS, to, msg)
				smtp.quit()
			except:
				# We've tried, we're just going to have to give up now
				pass
			
			# Skip to the next shelter
			break

		# Clean up
		f.close()

		# Remove the index file
		os.remove(homeindexfile)

		for img in os.listdir(HOMEDIR + os.path.sep + shelterid):

			# Drop out if it's not an image file
			if not img.endswith(".jpg") and not img.endswith(".jpeg"):
				continue
		
			imagefile = HOMEDIR + os.path.sep + shelterid + os.path.sep + img

			# If it's a thumbnail, check on the main image
			if img.endswith(".thm.jpg"):
				img = img[0:len(img)-8]
		
			# Compare images in home directory and remove any that
			# aren't in the database for this shelter
			d = db.runQuery("SELECT IMAGEPATH FROM ANIMAL WHERE SHELTERID='%s' AND IMAGEPATH='%s'" % (shelterid, img))
			if len(d) == 0:
				print "Removing redundant image: " + imagefile
				os.remove(imagefile)
			else:
			
				# Don't bother scaling thumbnails that might already be there
				if imagefile.endswith(".thm.jpg"):
					continue
			
				# Scale any images left
				print "Scaling: " + imagefile + " to " + IMAGESCALE
				os.system(IMAGEMAGICK_CONVERT + " " + imagefile + " " + "-resize " + IMAGESCALE + " " + imagefile)
				# Make a thumbnail
				print "Thumbnail: " + imagefile + ".thm.jpg" + " to " + THUMBSCALE
				os.system(IMAGEMAGICK_CONVERT + " " + imagefile + " " + "-resize " + THUMBSCALE + " " + imagefile + ".thm.jpg")




