# Path: common/httpd.py
# class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
#                          BaseHTTPServer.HTTPServer):
# class BaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#   def url_to_fs_path(self, url):
#   def send_head(self):
# 
# Path: common/util.py
# ANSI_BLUE = '\033[36m'
# ANSI_GREEN = '\033[32m'
# ANSI_RESET = '\033[0;0m'
# class Logger(object):
#   def __init__(self, color):
#   def __call__(self, msg, *args):
# def GetPackageDir():
# def GetUserDir():
# 
# Path: common/spy.py
# class Error(Exception):
# class UsageReporter(object):
# class NullUsageReporter(object):
#   def __init__(self, host_port):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
# def _GetUsageConfig():
# def _ReadAddressFile(address_file=None):
# def GetClientFromConfig(address_file=None):


import optparse
import re
import os
import sys
import threading
import jsontemplate
import templates
from common import httpd
from common import util
from common import spy

  (opts, _) = CreateOptionsParser().parse_args(argv[1:])

  # TODO:
  # pass request handler map
  # - index
  #   - list self.latches ?
  #   - if you click, does it wait?
  # /-/latch.js
  # /-/latch/README.html
  # /-/wait/
  # /-/notify/  -- or do those come on stdin?  or POST?

  # - latch
  # - static
  #   - except this filters self.wfile
  #   - <!-- INSERT LATCH JS -->

  latches = Latches()

  d = os.path.dirname(sys.argv[0])
  path = os.path.join(d, 'latch.js')
  with open(path) as f:
    latch_js = f.read()

  handler_class = LatchRequestHandler
  handler_class.root_dir = opts.root_dir
  handler_class.latches = latches
  handler_class.latch_js = latch_js


# Path: common/httpd.py
# class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
#                          BaseHTTPServer.HTTPServer):
# class BaseRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#   def url_to_fs_path(self, url):
#   def send_head(self):
# 
# Path: common/util.py
# ANSI_BLUE = '\033[36m'
# ANSI_GREEN = '\033[32m'
# ANSI_RESET = '\033[0;0m'
# class Logger(object):
#   def __init__(self, color):
#   def __call__(self, msg, *args):
# def GetPackageDir():
# def GetUserDir():
# 
# Path: common/spy.py
# class Error(Exception):
# class UsageReporter(object):
# class NullUsageReporter(object):
#   def __init__(self, host_port):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
#   def Send(self, msg):
#   def SendDict(self, d):
#   def SendRecord(self, event, d):
# def _GetUsageConfig():
# def _ReadAddressFile(address_file=None):
# def GetClientFromConfig(address_file=None):


# Path: latch/latch.py
import optparse
import re
import os
import sys
import threading
import jsontemplate
import templates
from common import httpd
from common import util
from common import spy


  (opts, _) = CreateOptionsParser().parse_args(argv[1:])

  # TODO:
  # pass request handler map
  # - index
  #   - list self.latches ?
  #   - if you click, does it wait?
  # /-/latch.js
  # /-/latch/README.html
  # /-/wait/
  # /-/notify/  -- or do those come on stdin?  or POST?

  # - latch
  # - static
  #   - except this filters self.wfile
  #   - <!-- INSERT LATCH JS -->

  latches = Latches()

  d = os.path.dirname(sys.argv[0])
  path = os.path.join(d, 'latch.js')
  with open(path) as f:
    latch_js = f.read()

  handler_class = LatchRequestHandler
  handler_class.root_dir = opts.root_dir
  handler_class.latches = latches
  handler_class.latch_js = latch_js


  s = httpd.ThreadedHTTPServer(('', opts.port), handler_class)