#!/usr/bin/env python

import sys

from focus import environment, errors

def main(argv):
    try:
        # spin up the environment
        io = environment.IOStream(inputs=sys.stdin, outputs=sys.stdout,
                                  errors=sys.stderr)
        env = environment.Environment(args=argv, io=io)
        env.load()

        try:
            environment.CLI().execute(env)
            return 0

        except errors.HelpBanner as exc:
            io.write(unicode(exc))
            return exc.code

    except errors.FocusError as exc:
        io.error(unicode(exc))
        return exc.code

    except Exception as exc:
        io.error(u"Unexpected error occurred: {0}".format(exc))
        return 2

if __name__ == '__main__':
    code = main(sys.argv[1:])
    sys.exit(code)
