#!/usr/bin/python2
# -*- coding: utf-8 -*-

import os
import sys

# try to load a virtualenv from the PYTHON_VIRTUALENV environment
# variable, or from the directory "venv"
try:
    venv = os.getenv("PYTHON_VIRTUALENV", "venv") + "/bin/activate_this.py"
    if sys.version_info.major == 2:
        execfile(venv, dict(__file__=venv))
    if sys.version_info.major == 3:
        with open(venv, 'rb') as f:
            exec(compile(f.read(), venv, 'exec'), dict(__file__=venv))
except IOError:
    pass

import argparse
from werkzeug.contrib.profiler import ProfilerMiddleware
from flasfka import app

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="flasfka")
    parser.add_argument(
        "-d", "--debug", action="store_true", help="start in debug mode",
        default=False
        )
    parser.add_argument(
        "-p", "--port", action="store", type=int, help="port number",
        default=5000
        )
    parser.add_argument(
        "-o", "--profile", action="store_true", help="do profiling",
        default=False
        )
    parser.add_argument(
        "-t", "--test", action="store_true", help="launch the test suite",
        default=False
        )
    args = parser.parse_args()

    if args.test:
        import flasfka
        import unittest
        unittest.main(module="flasfka.tests",
                      argv=["flasfka-serve"], verbosity=2)

    if args.profile:
        app.config["PROFILE"] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])

    app.run(debug=args.debug, host="127.0.0.1", port=args.port)
