#!/usr/bin/python3
import importlib
import pkgutil
import sys

import ernie

usage = """usage: ernie command
commands are:
"""
rc = {}


def fmt(s):
    tok = s.split("\n")
    cols = ["\t", "\t\t"]
    sp = ["-- ", "   "]
    r = ""
    for i, t in enumerate(tok):
        r += cols[i > 0]
        if i > 0:
            r += sp[i > 1]
        r += t + "\n"
    return r


if __name__ == "__main__":
    for i, mod, ispkg in pkgutil.iter_modules(ernie.__path__):
        if not ispkg:
            m = importlib.import_module(".." + mod, package="ernie.__init__")
            rc[mod] = m.main
            usage += fmt(m.__doc__)

    if len(sys.argv) < 2:
        ernie.die(usage)

    sys.argv.pop(0)
    cmd = sys.argv[0]
    if cmd not in rc.keys():
        ernie.die(usage)
    sys.exit(rc[cmd]())
