#!/usr/bin/env python
import re
import sys
from subprocess import call


fabfile = None
try:
    import littlechef
    fabfile = re.sub(r'\.pyc$', '.py', littlechef.__file__)
except ImportError:
    # Fallback
    import os
    from distutils.sysconfig import get_python_lib
    fabfile = get_python_lib() + "/littlechef.py"
    if not os.path.exists(fabfile):
        fabfile = fabfile.replace('/usr/lib/', '/usr/local/lib/')
        if not os.path.exists(fabfile):
            fabfile = __file__.replace('EGG-INFO/scripts/cook', '') + 'littlechef.py'
            if not os.path.exists(fabfile):
                sys.exit("There was an installation error. Couldn't find littlechef.py, try reinstalling again")
    sys.path.insert(0, os.path.dirname(fabfile))
    import littlechef

# Act on some arguments and pass others to fabric
args = ['fab', '-f', fabfile]
if len(sys.argv) > 1:
    for arg in sys.argv[1:]:
        if arg == '-v' or arg == '-V' or arg == '--version':
            print('littlechef %s' % littlechef.version)
            sys.argv.remove(arg)
            sys.exit(0)
        else:
            args.append(arg)
else:
    sys.exit("No order given. Type 'cook -l' for a list of orders\n")

# Call fabric with the given arguments
call(args)
