#!/usr/bin/env python
from pathlib import Path
from ursus import build, lint
from ursus.config import config
from ursus.server import serve, serve_async
from ursus.utils import import_module_or_path
import argparse
import logging
import sys


if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog="ursus", description="Static site generator", epilog="Made with ❤️ in Berlin")
    parser.add_argument("action", nargs="?", default="build", choices=("build", "lint"), help="Action to perform.")
    parser.add_argument(
        "-c",
        "--config",
        help="Path to a Python config file or module. The `config` variable will be imported from that file.",
    )
    parser.add_argument(
        "-f",
        "--fast",
        action="store_true",
        help="Fast rebuilds. Prefer faster page rebuilds to completeness. Related pages might not be reloaded.",
    )
    parser.add_argument(
        "--level",
        default="INFO",
        help="Log level for ursus lint (INFO, WARNING, ERROR). Errors below this level are ignored.",
    )
    parser.add_argument(
        "-s",
        "--serve",
        dest="port",
        nargs="?",
        const=80,
        default=None,
        help="Start a static file server, and serve the generated website on the given port. The default port is 80.",
    )
    parser.add_argument(
        "-w", "--watch", action="store_true", help="Regenerate files when <content_path> and <templates_path> change."
    )
    parser.add_argument(
        "files",
        type=Path,
        nargs="*",
        help="Only lint the given file(s). The paths must be relative to the <content_path>. For example, 'blog/first-post.md'.",
    )
    args = parser.parse_args()

    if args.config:
        import_module_or_path(args.config)
    elif Path("./ursus_config.py").exists():
        import_module_or_path("ursus_config.py")

    if args.fast:
        config.fast_rebuilds = True

    logging.basicConfig(**config.logging)

    if args.config:
        logging.info(f"Loaded config from {args.config}")
    elif Path("./ursus_config.py").exists():
        logging.info("Loaded config from ./ursus_config.py")
    else:
        logging.warning("No config supplied. Using default config.")

    if args.port:
        if args.watch:
            serve_async(args.port)
        else:
            serve(args.port)

    logging.info(f"Content path: {str(config.content_path)}")
    if args.action == "build":
        logging.info(f"Templates path: {str(config.templates_path)}")
        logging.info(f"Output path: {str(config.output_path)}")
        try:
            build(args.watch)
        except:
            logging.exception("Could not generate site")
            sys.exit(1)
    elif args.action == "lint":
        for file_to_lint in args.files:
            absolute_path = config.content_path / file_to_lint
            if not absolute_path.exists():
                raise ValueError(f"{file_to_lint} does not exist in content path ({config.content_path})")
        lint(files_to_lint=args.files or None, min_level=getattr(logging, args.level))
