#!/bin/sh
""":"
case "$0" in
    # Inside the app: always this bundle's Python, never a home inherited from another one.
    *.app/Contents/Resources/*) bundle=${0%%.app/Contents/Resources/*}.app
                                export PYTHONHOME="$bundle/Contents/Resources/python" ;;
esac
exec "$PYTHONHOME/bin/python3" -I -S -B "$0" "$@"
": """
__doc__ = """Archive: put old notes away, never delete them (brief 0117; design nothing-destroyed §3.3).

    bin/archive PATH...     each a note in the life folder, by its path there
                            (domains/people/2026-01-02-lunch.md)

Inside a run Argoni started, each note moves into the run's own folder (ARGONI_ARCHIVE_IN)
under the folder it came from; the runner files it into that folder's archive/ when the
run ends. Run by hand, it moves straight into <folder>/archive/. Only a plain file with one
name moves, never through a link, never from me/, log/, inbox/, an archive/ or a fenced
place, never over a note already archived under that name. Nothing is ever deleted: this
is how the weekly review's roll-up (and anything else) takes a note out of a folder.

Exit status: 0 every note moved; 2 usage; 3 one or more refused (each named; the others moved).
Standard library only.
"""
import os
import sys

sys.dont_write_bytecode = True  # no __pycache__ beside the engine's scripts
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import boxes  # noqa: E402
import lifecfg  # noqa: E402


def main(argv):
    if len(argv) < 2 or any(a.startswith("-") for a in argv[1:]):
        print("usage: bin/archive PATH...", file=sys.stderr)
        return 2
    life = lifecfg.root()
    code = 0
    for rel in argv[1:]:
        if os.path.isabs(rel):
            real = os.path.realpath(os.path.dirname(rel))
            top = os.path.realpath(life)
            rel = os.path.relpath(os.path.join(real, os.path.basename(rel)), top) \
                if real == top or real.startswith(top + os.sep) else "../" + rel
        try:
            print("archived %s -> %s" % (rel, boxes.archive(rel, life)))
        except (boxes.Refused, OSError) as e:
            print("archive: REFUSED, %s" % e, file=sys.stderr)
            code = 3
    return code


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