#!/usr/bin/python
#
# Copyright (C) 2005, TUBITAK/UEKAE
#
# 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 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

import sys

import gettext
__trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext

from pisi.cli.pisicli import PisiCLI

def handle_exception(exception, value, tb):
    import traceback
    import exceptions

    import pisi.ui
    import pisi.context as ctx
    from pisi.xmlext import XmlError

    ui = pisi.cli.CLI()
    show_traceback = False
    if exception == exceptions.KeyboardInterrupt:
        ui.error(_("\nKeyboardInterrupt: Exiting...\n"))
        sys.exit(1)
    elif exception == XmlError:
        ui.error(str(value))
        sys.exit(1)
    elif exception == pisi.Error:
        ui.error(_("Fatal Error:"))
        show_traceback = ctx.options and ctx.options.debug
    elif exception == pisi.Exception:
        show_traceback = True
    else:
        # For any other exception (possibly Python exceptions) show
        # the traceback!
        show_traceback = True

    ui.error("%s" %exception)
    ui.error("%s\n" %value)

    if show_traceback:
        ui.error(_("""
Internal PISI Error:
Please file a bug report! (http://bugs.uludag.org.tr)
"""))
        ui.info(_("Traceback:"))
        traceback.print_tb(tb)

    sys.exit(1)

if __name__ == "__main__":

    sys.excepthook = handle_exception

    cli = PisiCLI()
    cli.run_command()
