#!/usr/bin/env python3
"""Harmless child process for factory stop tests."""

from __future__ import annotations

import signal
import sys


def main() -> None:
    order_file = sys.argv[1]
    token = sys.argv[2]
    mode = sys.argv[3]

    def on_term(_signum, _frame) -> None:
        with open(order_file, "a", encoding="utf-8") as handle:
            handle.write(f"{token}\n")
        raise SystemExit(0)

    if mode == "ignore-term":
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        signal.signal(signal.SIGINT, signal.SIG_IGN)
    else:
        signal.signal(signal.SIGTERM, on_term)
        signal.signal(signal.SIGINT, on_term)

    try:
        sys.stdin.read(1)
    except OSError:
        pass


if __name__ == "__main__":
    main()
