#!/usr/bin/env python2.7
import runpy
import os
import sys

try:
    import moke
except ImportError:
    sys.exit("moke: *** Not properly installed.  Stop.")

if __name__ == "__main__":
    sys.path.insert(0, os.getcwd())
    if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]):
        mokefile = sys.argv.pop(1)
    elif os.path.exists("mokefile.py"):
        mokefile = "mokefile.py"
    else:
        sys.exit("moke: *** No task specified and no mokefile.py found.  Stop.")
    # run mokefile and make it thing it is executed
    ret =  runpy.run_path(mokefile, run_name="__main__")
    gottask = any([isinstance(obj, moke.core.task) for obj in ret.itervalues()])
    if not gottask:
        sys.exit("moke: *** No task specified in %s.  Stop." % mokefile)
