#!/usr/bin/env python
import tornado.web
import tornado.httpserver
import static
import logging
from os.path import dirname, abspath
from tornado.options import define, options
from omen.service import TagListHandler, ImageListHandler, ImageHandler
#, TagHandler, PictureViewerHandler

def config_application():
	static_dir = dirname(abspath(static.__file__))
	content_dir = abspath(options.content)
	application = tornado.web.Application([
		(r"/", tornado.web.RedirectHandler, {"url": "/static/index.html"}),
		(r"/tags", TagListHandler),
		(r"/images", ImageListHandler),
		(r"/images/current", ImageHandler),
		#(r"/tags/(.*)", TagHandler),
		#(r"/pictures/viewer", PictureViewerHandler),
		#(r"/names/(.*?)\/?", NameDeleteHandler),
		#(r"/instances", InstancesHandler),
		#(r"/instances/(.*?)\/?", InstanceIdHandler),
		#(r"/load-balancers", LoadBalancersHandler),
		#(r"/load-balancers/(.*?)\/?", LoadBalancerIdHandler),
		(r"/static/(.*)", NoCacheStaticFileHandler, {"path": static_dir}),
		(r"/content/(.*)", NoCacheStaticFileHandler, {"path": content_dir})
		])
	return application

class NoCacheStaticFileHandler(tornado.web.StaticFileHandler):
	def set_extra_headers(self, path):
		self.set_header("Cache-control", "no-cache")

def main():
	option_parser()
	tornado.options.parse_command_line()
	application = config_application()
	http_server = tornado.httpserver.HTTPServer(application)
	http_server.listen(8080)
	tornado.ioloop.IOLoop.instance().start()

def option_parser():
	pass
	options.define("logfile", default="stderr",
		help=" ".join(('The file to log to, or "stdout" or "stderr"',
		'to log to those special file descriptors.',)))
	options.define("loglevel", default="error",
		help=" ".join(('The level to log at.',
		'Valid levels are: debug, info, warning, error, and critical.',)))
	options.define("content", default="/home/devon/Dropbox/Gaming/",
		help=" ".join(('Location of content for omen.  All paths will be treated',
		'as below this root.',)))

if (__name__ == "__main__"):
	main()
