#!/usr/bin/env python

desc="""
Plot data from a HAPI server.

    List options and usage:
        hapiplotserver -h

Version: 0.0.4
"""

import sys
import optparse

from hapiplotserver.config import config
from hapiplotserver.main import hapiplotserver

# Entry point for command line.
if __name__ == "__main__":

    # This is needed in order for test-virtualenv Makefile tests to work.
    # Without it, urlrequest silently fails when 'make test-virtualenv' is run.
    # However, if one does
    #   source env/bin/activate
    #   hapiplotserver --port 5001 --workers 4 --loglevel debug &
    #   ... Execute failing curl request on command line
    # the curl request works. So there is something about the Makefile environment
    # that is screwing things up.
    # TODO: When test_hapiplotserver.sh is converted to a python script, see if this is
    # needed anymore.
    if hasattr(sys, 'real_prefix'):
        from hapiclient.util import urlopen
        res = urlopen('http://google.com/')

    conf = config()
    parser = optparse.OptionParser(add_help_option=False)

    parser.add_option('-h', '--help', dest='help', action='store_true', help='show this help message and exit')

    parser.add_option("--port", help="Server port " + "[%s]" % conf['port'], default=conf['port'])
    parser.add_option("--cachedir", help="Cache directory " + "[%s]" % conf['cachedir'], default=conf['cachedir'])
    parser.add_option("--loglevel", help="Log level " + "(error, default, debug) [%s]" % conf['loglevel'], default=conf['loglevel'])
    parser.add_option("--threaded", type="int", help="Run each request in separate thread (0=no or 1=yes) " + "[%s]" % int(conf['threaded']), default=int(conf['threaded']))
    parser.add_option("--usecache", type="int", help="Use cached data and images (0=no or 1=yes) " + "[%s]" % int(conf['usecache']), default=int(conf['usecache']))
    parser.add_option("--workers", type="int", help="# of Gunicorn workers. If > 0, Gunicorn is used and 'threaded' option ignored. " + "[%s]" % int(conf['workers']), default=int(conf['workers']))

    (options, args) = parser.parse_args()

    if options.help:
        parser.print_help()
        print(desc)
        sys.exit(0)

    opts = {'port': options.port,
            'cachedir': options.cachedir,
            'usecache': bool(options.usecache),
            'loglevel': options.loglevel,
            'threaded': bool(options.threaded),
            'workers': int(options.workers)
            }
    hapiplotserver(**opts)


















