#!/usr/bin/env python

import sys
import os
import subprocess
import logging
import json
import signal
import time
import urllib2

def ping(url):

    req = urllib2.Request(url)
    req.get_method = lambda : 'HEAD'

    try:
        urllib2.urlopen(req)
        return True
    except urllib2.HTTPError, e:
        return True
    except Exception, e:
        return False

if __name__ == '__main__':

    import optparse
    opt_parser = optparse.OptionParser()

    opt_parser.add_option('-d', '--data', dest='data', action='store', default=None, help='The path to your Who\'s On First data')
    opt_parser.add_option('--proxy-config', dest='proxy_config', action='store', default=None, help='')
    opt_parser.add_option('--proxy-host', dest='proxy_host', action='store', default='localhost', help='')
    opt_parser.add_option('--proxy-port', dest='proxy_port', action='store', default='1111', help='')

    opt_parser.add_option('--pip-server', dest='pip_server', action='store', default=None, help='')
    opt_parser.add_option('--proxy-server', dest='proxy_server', action='store', default=None, help='')

    opt_parser.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='Be chatty (default is false)')
    options, args = opt_parser.parse_args()

    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    whoami = os.path.abspath(sys.argv[0])
    utils = os.path.dirname(whoami)
    root = os.path.dirname(utils)

    bin = os.path.join(root, "bin")

    pip_server = options.pip_server
    proxy_server = options.proxy_server

    if pip_server == None:
        pip_server = os.path.join(bin, "wof-pip-server")

    if proxy_server == None:
        proxy_server = os.path.join(bin, "wof-pip-proxy")

    # Make sure there is something to work with 

    for path in (options.data, options.proxy_config, pip_server, proxy_server):

        if not os.path.exists(path):
            logging.error("%s does exist" % path)
            sys.exit(1)

    # Parse the spec/config

    try:
        fh = open(options.proxy_config, "r")
        spec = json.load(fh)
    except Exception, e:
        logging.error("failed to open %s, because %s" % options.proxy_config, e)
        sys.exit(1)

    procs = []

    # Do some basic sanity checking on the config

    for target in spec:

        for prop in ('Target', 'Port', 'Meta'):

            if not target.get(prop, False):
                logging.error("Invalid spec (missing %s)" % prop)
                sys.exit(1)

    # Start all the PIP servers

    for target in spec:

        # https://github.com/whosonfirst/go-whosonfirst-pip/issues/26

        pid_dirname = "/var/run"
        pid_basename = "wof-pip-proxy-%s.pid" % target

        pid_file = os.path.join(pid_dirname, pid_basename)

        # PID=`cat /var/run/foo.pid`
        # ps h -p ${PID} | wc -l

        cmd = [ pip_server, "-cors", "-port", str(target['Port']), "-data", options.data, target['Meta'] ]
        logging.debug(cmd)

        proc = subprocess.Popen(cmd)
        procs.append(proc)

        pid = proc.pid

        fh = open(pid_file, "w")
        fh.write(proc.pid)
        fh.close()

    # Wait for the PIP servers to finish indexing

    while True:
        
        pending = False

        for target in spec:

            url = "http://localhost:%s" % target['Port']
            logging.debug("ping %s" % url)

            if not ping(url):
                logging.info("ping for %s failed, waiting" % url)
                pending = True

        if not pending:
            break

        logging.info("pause...")
        time.sleep(1)

    # Now start the proxy server

    cmd = [ proxy_server, "-host", options.proxy_host, "-port", options.proxy_port, "-config", options.proxy_config ]
    logging.debug(cmd)

    proc = subprocess.Popen(cmd)
    procs.append(proc)

    # Now just sit around waiting for a SIGINT

    def signal_handler(signal, frame):

        for p in procs:
            p.terminate()

        raise Exception, "all done"

    signal.signal(signal.SIGINT, signal_handler)

    try:
        while True:
            time.sleep(.5)
    except Exception, e:
        pass

    # Done

    logging.info("all done")
    sys.exit()

@@ -122,16 +122,32 @@
  urllib2
+%0Aimport tempfile
 %0A%0Adef pi
@@ -2737,32 +2737,144 @@
     
-pid_dirname = %22/var/run%22
+# because this typically gets invoked as user 'www-data'%0A        # pid_dirname = %22/var/run%22%0A%0A        pid_dirname = tempfile.gettempdir()
 %0A   
@@ -2924,16 +2924,26 @@
 %25 target
+%5B'Target'%5D
 %0A%0A      
@@ -3289,16 +3289,98 @@
 proc.pid
+%0A        logging.info(%22start %25s pip server with PID %25s%22 %25 (target%5B'Target'%5D, pid))
 %0A%0A      
@@ -3427,17 +3427,17 @@
 ite(
-proc.
+str(
 pid)
+)
 %0A   

make the pid file stuff for the proxy server work