#!/usr/bin/python
import os, sys, inspect,logging

shortcuts = {} 
def loadCfgs(path):
    global shortcuts
    shortcut = open(os.path.join(path,'shortcut.cfg'),'r')
    for line in shortcut:
        if line.find('#') != 0:
            parts = line.rstrip().split("=")
            if len(parts) == 2:
                shortcuts[parts[0]] = parts[1]

def getRealCmd(cmd):
    global shortcuts
    if cmd in shortcuts:
        return shortcuts[cmd]
    else:
        return cmd

def main():
    pwd = os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])
    ppd = os.path.dirname(pwd)
    if ppd not in sys.path:
        sys.path.insert(0, ppd)
    # use this if you want to include modules from a subforder
    modules = os.path.abspath(os.path.join(os.path.realpath(pwd),"modules"))
    if modules not in sys.path:
        sys.path.insert(0, modules)
    loadCfgs(pwd)

    cmd = "help" if len(sys.argv) <= 1 else getRealCmd(sys.argv[1])

    try:
        method = sys.argv[2]
    except:
        method = "help"

    try:
        module = __import__(cmd)
        try:
            try:
                func = getattr(module, method)
            except:
                logging.exception("fuck2")
                module.process(*sys.argv[2:])
                return
            func(*sys.argv[3:]) # or func()
        except:
            logging.exception("module error")
    except:
        logging.exception("unknown")
        print "This module is not implemented yet."
        __import__("help").help()

main()
