# src/main.py
# Parses and calls commands

from ctf.commands import basic_group
from ctf.cli_forensics import forensics_group
from ctf.cli_steg import steg_group
from ctf.cli_analyse import analyse_cmd
from ctf.cli_helpers import flag_cmd
from ctf.helpers import FlagDetectorStream, detector_state
import click
import sys

# {{{ cli
@click.group()
def cli(): pass

cli.add_command(forensics_group)
cli.add_command(basic_group)
cli.add_command(flag_cmd)
cli.add_command(steg_group)
cli.add_command(analyse_cmd)
# }}}

# {{{ main
def main():
    from ctf.config import load_config
    config = load_config("/home/venus/code/ctf/config.toml")
    flag_format = config.get("Competition", {}).get("flag_format", "")
    
    if flag_format:
        detector = FlagDetectorStream(sys.stdout, flag_format)
        sys.stdout = detector
        detector_state["active"] = detector
        try:
            cli()
        finally:
            sys.stdout = detector.original_stream
            detector_state["active"] = None
    else:
        cli()
# }}}

if __name__ == "__main__":
    main()
