#!/usr/bin/env python

# Copyright (C) 2010  Aldo Cortesi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys, argparse, os
from libmproxy import proxy, console, cmdline, version
from libmproxy.console import palettes


if __name__ == '__main__':
    parser = argparse.ArgumentParser(usage = "%(prog)s [options]")
    parser.add_argument('--version', action='version', version=version.NAMEVERSION)
    cmdline.common_options(parser)
    parser.add_argument("--debug", dest="debug", default=False, action="store_true")
    parser.add_argument(
        "--palette", type=str, default="dark",
        action="store", dest="palette",
        help="Select color palette: " + ", ".join(palettes.palettes.keys())
    )


    group = parser.add_argument_group(
                        "Filters",
                        "See help in mitmproxy for filter expression syntax."
                    )
    group.add_argument(
        "-i", "--intercept", action="store",
        type = str, dest="intercept", default=None,
        help = "Intercept filter expression."
    )
    options = parser.parse_args()

    config = proxy.process_proxy_options(parser, options)

    if options.no_server:
        server = proxy.DummyServer(config)
    else:
        try:
            server = proxy.ProxyServer(config, options.port, options.addr)
        except proxy.ProxyServerError, v:
            print >> sys.stderr, "mitmproxy:", v.args[0]
            sys.exit(1)

    try:
        opts = console.Options(**cmdline.get_common_options(options))
    except cmdline.OptionException, v:
        parser.error(v.message)
    opts.intercept = options.intercept
    opts.debug = options.debug
    opts.palette = options.palette

    spec = ""
    for i in ["LANG", "LC_CTYPE", "LC_ALL"]:
        spec += os.environ.get(i, "").lower()
    if "utf" not in spec:
        print >> sys.stderr, "Error: mitmproxy requires a UTF console environment."
        print >> sys.stderr, "Set your LANG enviroment variable to something like en_US.UTF-8"
        sys.exit(1)

    m = console.ConsoleMaster(server, opts)
    try:
        m.run()
    except KeyboardInterrupt:
        pass


