#!/usr/bin/env python

from __future__ import print_function
from __future__ import unicode_literals

import sys

from libogr import *

try:
    c = Command(sys.argv[1:])
    c.run()

except ScriptNotExecutable as e:
    print(e)
    exit(-1)
except ScriptNotFound as e:
    print(e)
    exit(-2)
except EngineDirError as e:
    print(e)
    exit(-3)

# If everything went as expected
if c.return_value == 0:
    print(c.output,end='')
else:
    print(c.errput,end='')
    script_help = get_help(c.script_name)
    exception_label = 'exception'
    if exception_label in script_help:
        exceptions = script_help[exception_label]
        exception_number = c.return_value - 1
        if 0 <= exception_number < len(exceptions):
            description = exceptions[exception_number]
        else:
            description = 'Unknown exception.'
    else:
        description = 'Unknown exception.'

    print("The exception %d was raised in %s: %s" % (c.return_value,
        c.script_name, description))

exit(c.return_value)
