#!/usr/bin/python3
# coding: utf-8
import sys
import os
import argparse
import logging
import json
import time

log = logging.getLogger()

def main():
    parser = argparse.ArgumentParser(description="Static site generator.")

    subparsers = parser.add_subparsers(help="sub-command help", dest="command")

    from staticsite.check import Check
    Check.make_subparser(subparsers)

    from staticsite.build import Build
    Build.make_subparser(subparsers)

    from staticsite.serve import Serve
    Serve.make_subparser(subparsers)

    from staticsite.new import New
    New.make_subparser(subparsers)

    from staticsite.edit import Edit
    Edit.make_subparser(subparsers)

    args = parser.parse_args()
    if args.command is None:
        parser.print_help()
    else:
        handler = args.handler(args)
        res = handler.run()
        if res is not None:
            sys.exit(res)


if __name__ == "__main__":
    from staticsite.commands import CmdlineError
    try:
        main()
    except CmdlineError as e:
        print(e, file=sys.stderr)
        sys.exit(1)
