#!/usr/bin/python3
"""Launcher for VolnOS Setup (the pre-install screen, runs before gnome-initial-setup).

Installed to /usr/bin/volnos-preinstall and started by the gnome-initial-setup
wrapper on first boot. It checks the SYSTEM stamp on /data BEFORE importing GTK and
exits 0 immediately if pre-install already ran (so the wrapper falls straight through
to the real wizard on later boots / freshly-OTA'd slots).

Exit codes (the wrapper relies on these):
    0   pre-install finished (or was already done) -> launch gnome-initial-setup
    !0  pre-install was cancelled/failed -> the wrapper aborts the wizard launch

  volnos-preinstall            run if not yet completed (the wrapper behaviour)
  volnos-preinstall --force    show it again regardless of the stamp (for testing)

Also runnable from a source checkout via:
    PYTHONPATH=partA-remix/preinstall ./partA-remix/preinstall/bin/volnos-preinstall --force
"""
import os
import sys

# Prefer the installed location; fall back to the source tree for development.
_INSTALL_DIR = "/usr/lib/volnos-preinstall"
if os.path.isdir(os.path.join(_INSTALL_DIR, "volnos_preinstall")):
    sys.path.insert(0, _INSTALL_DIR)
else:
    sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

from volnos_preinstall import preinstall_is_done  # noqa: E402

if __name__ == "__main__":
    if "--force" not in sys.argv:
        if preinstall_is_done():
            sys.exit(0)
    from volnos_preinstall.application import main  # imported lazily (pulls in GTK)
    sys.exit(main())
