def _main():
    import atexit
    import readline

    path = os.path.expanduser("~/.pyhistory")  # noqa: PTH111
    if os.path.exists(path):  # noqa: PTH110
        readline.read_history_file(path)

    if platform == "darwin":
        history = os.path.expanduser(os.environ["PYTHONHISTFILE"])  # noqa: PTH111
        # history = os.path.join(
        #     os.path.expanduser(os.environ["XDG_STATE_HOME"]), "python_history",
        # )
    else:
        # if hasattr(sys, "__interactivehook__"):
        # disable ~/.python_history
        del sys.__interactivehook__

    try:
        import __builtin__
    except ImportError:
        import builtins as __builtin__

    setattr(__builtin__, "true", True)
    setattr(__builtin__, "false", False)
    setattr(__builtin__, "null", None)

    def save_history():
        try:
            import readline
        except ImportError:
            sys.stdout.write("Module readline not available.\n")
        else:
            import rlcompleter  # noqa: F401
            readline.write_history_file(path)
            readline.parse_and_bind("tab: complete")
            readline.parse_and_bind(r"\C-a: beginning-of-line")
            readline.parse_and_bind(r"\C-e: end-of-line")
            atexit.register(save_history)

if __name__ == "__main__":
    _main()
# ----------------------------------------------------------------------------
# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Set an environment variable to point to it:  "export PYTHONSTARTUP=foo"
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the full
# path to your home directory.

# Default imports for easier repl usage
# import glob, fnmatch, re, os.path, pathlib, sys, subprocess

import atexit
import os
import readline
from pathlib import Path


def _main():
    """Read and write history from state file."""
    # check PYTHON_HISTORY for future-compatibility with Python 3.13
    if history := os.environ.get("PYTHON_HISTORY"):
        history = Path(history)
    elif state_home := os.environ.get("XDG_STATE_HOME"):
        state_home = Path(state_home)
    else:
        state_home = Path.home() / ".local" / "state"

    history: Path = history or state_home / "python_history"

    # https://github.com/python/cpython/issues/105694
    if not history.is_file():
        readline.write_history_file(str(history))
        # breaks on macos + python3 without this.

    readline.read_history_file(history)
    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind(r"\C-a: beginning-of-line")
    readline.parse_and_bind(r"\C-e: end-of-line")
    atexit.register(readline.write_history_file, history)

if __name__ == "__main__":
    _main()
