#!/usr/bin/env python3

import os, sys, re
from itertools import chain
from pathlib import Path
from typing import Optional

def eprint(*args, **kwargs):
    print(*args, **kwargs, file=sys.stderr)

def main() -> int:
    # Really cheap command-line parsing
    args = sys.argv[1:]
    if len(args) > 1:
        eprint(
            "Usage:",
            Path(sys.argv[0]).name,
            "[PAGE_ID]",
        )
        eprint("If PAGE_ID is specified, only the ID of that page is returned.")
        return 1

    if (ACW_WIKI_DIR := os.getenv("ACW_WIKI_DIR")) is None:
        eprint("ACW_WIKI_DIR is not set; please set it with the path of your wiki")
        return 1
    ACW_WIKI_DIR = Path(ACW_WIKI_DIR)

    for file in ACW_WIKI_DIR.glob("*.wiki"):
        page_id = file.stem

        if len(args) == 1:
            wanted_id = args[0]
            if page_id != wanted_id:
                continue

            with file.open("r") as f:
                tags = get_tags(f)
                print(page_id, " ".join(tags))
                return 0
        else:
            with file.open("r") as f:
                tags = get_tags(f)
                print(page_id, " ".join(tags))

    # With an ID specified, we only get here if the specific page wasn't found
    if len(args) == 1:
        return 1

    return 0

RE_HEADER_LINE = re.compile(r"^%:([\w.]+) (.*)$")
RE_SPACED_TAGS = re.compile(r"[^\s]+")

def get_tags(handle) -> list[str]:
    try:
        # Let's check for empty lines before the header.
        while True:
            # `next` can be used to iterate over lines in a file
            line = next(handle).strip()
            if line != "":
                break
    except StopIteration:
        # We reached the end of the file without finding the header!
        # Let's return an empty list then.
        return []

    # At this point we still have the value in `line` because of python's weird scoping rules.
    # To avoid code duplication let's just chain it back with the file lines iterator.
    for line in chain([line], handle):
        line = line.strip() # Stripping the line again yay.

        # Check if it's a header line.
        # If it's not, we reached the end of the header and thus there's no need to continue reading the file.
        if (m := RE_HEADER_LINE.match(line)) is None:
            break

        # At this point in the code we have an actual header line, so let's get the data and check if it's what we want.
        attr, val = m.groups()
        if attr == "tags":
            return RE_SPACED_TAGS.findall(val)

    # If we got here, it means the header didn't have the `tags` line, so let's just return an empty list.
    return []

if __name__ == "__main__":
    sys.exit(main())
