#!/usr/bin/env python
import argparse, sys, os, imp

def build_site(dir, **args):
    "This is what will be called when the user runs `cytoplasm build`"
    import cytoplasm
    cytoplasm.build(dir)

def init_site(site, **args):
    "This is the function called when you run `cytoplasm init something`"
    sites = {
            "bare": "git://github.com/startling/cytoplasm-bare.git",
            "cytoplasm-site": "git://github.com/startling/cytoplasm-site.git",
    }
    os.system("git clone %s ." %(sites[site]))
    os.system("git submodule init")
    os.system("git submodule update")

def serve_site(port, rebuild, **args):
    "This is the function called when you run `cytoplasm serve`: serve the site in the build_dir."
    import cytoplasm.server
    cytoplasm.server.serve(port, rebuild)

def test_cytoplasm(**args):
    "This is the function called when you run `cytoplasm test`; test"
    import nose
    # Get the package directory where Cytoplasm lives
    _, cytoplasm_directory, _ = imp.find_module("cytoplasm")
    # Change directory to there; this is silly but I can't figure out how else to do it.
    os.chdir(cytoplasm_directory)
    # run the nose tests.
    nose.run()

class Parser(argparse.ArgumentParser):
    "A custom parser class with more helpful error messages."
    def error(self, message): 
        sys.stderr.write('error: %s\n' % message) 
        self.print_help()
        sys.exit(2) 


# Make an argparse parser, using the class above.
parser = Parser(description='a static blog compiler.', add_help=True)
# (subparsers, aka subcommands)
subparsers = parser.add_subparsers(title="Subcommands")
# "build" subparser, i.e. build the site
build = subparsers.add_parser('build', help='Build the cytoplasm site in the working directory.')
# Associate the build function, above, with the subcommand `build`.
build.set_defaults(func=build_site)
build.add_argument("dir", type=str, help="The source directory of your site; defaults to cwd.", 
        action="store", nargs="?", default=".")
# "init" subparse
init = subparsers.add_parser('init', help='Clone a repository to work from (requires Git).')
init.set_defaults(func=init_site)
init.add_argument('site', choices=["bare", "cytoplasm-site"], help="The site you want to clone.")
# serve subparser
serve = subparsers.add_parser('serve', help="Serve your site with Python's built-in server.")
serve.set_defaults(func=serve_site)
serve.add_argument("port", type=int, help="The port to serve on; defaults to 8080.", action="store",
        nargs="?", default=8080)
serve.add_argument('-r', '--rebuild', help="Re-build the site automatically when changes are made.",
        action="store_true")
# test subparser
serve = subparsers.add_parser('test',
        help="Test Cytoplasm with the built-in tests. (Requires nose).")
serve.set_defaults(func=test_cytoplasm)
# Parse the arguments from the command line.
args = parser.parse_args()
# run the functions associated with the arguments.
args.func(**vars(args))
