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

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

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")

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)
# "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"], help="The site you want to clone.")
# Parse the arguments from the command line.
args = parser.parse_args()
# run the functions associated with the arguments.
args.func(**vars(args))
