#!/usr/bin/python3
"""Run security update commands and record their completed outcome."""

import datetime
import fcntl
import hashlib
import json
import os
from pathlib import Path
import re
import shlex
import subprocess
import sys
import tempfile
import threading

STATE = Path("/var/lib/turnkey-security-updates")
RUNTIME = Path("/run/turnkey-security-updates")
SECURITY_SOURCES = "/etc/apt/sources.list.d/security.sources.sources"
LOGFILE = Path("/var/log/inithooks/secupdates.log")
INSTALL_COMMAND = "/usr/sbin/turnkey-install-security-updates"
CRON_HOOK = Path("/etc/cron-apt/config.d/0-update")
CRON_CONFIG = Path("/etc/cron-apt/config")
CRON_SCHEDULE = Path("/etc/cron.d/cron-apt")
HOOK_TEXT = '''if [ -x /usr/lib/inithooks/bin/security-updates-status ]; then
export TURNKEY_SECURITY_APT_COMMAND="$APTCOMMAND"
export TURNKEY_SECURITY_CRON_PID="$$"
export TURNKEY_SECURITY_CRON_ID="$$-$(date +%s)"
export TURNKEY_SECURITY_ACTIONDIR="$ACTIONDIR"
export TURNKEY_SECURITY_ACTIONCONFDIR="$ACTIONCONFDIR"
export TURNKEY_SECURITY_CONFIG="$CONFIG"
APTCOMMAND=/usr/lib/inithooks/bin/security-updates-status
fi
'''
SUMMARY = re.compile(r"^(\d+) upgraded, (\d+) newly installed, \d+ to remove and \d+ not upgraded")


def now():
    return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")


def valid_time(value):
    if not isinstance(value, str) or not re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", value):
        return False
    try:
        datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
        return True
    except ValueError:
        return False


def process_identity(pid):
    stat = Path(f"/proc/{pid}/stat").read_text()
    fields = stat[stat.rfind(")") + 2:].split()
    if fields[0] == "Z":
        raise ValueError("process exited")
    return {"boot_id": Path("/proc/sys/kernel/random/boot_id").read_text().strip(),
            "owner_pid": pid, "owner_start_ticks": int(fields[19])}


def write_json(path, value):
    path.parent.mkdir(parents=True, exist_ok=True)
    fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=".status-")
    try:
        with os.fdopen(fd, "w") as stream:
            json.dump(value, stream)
            stream.write("\n")
        os.chmod(tmp, 0o644)
        os.replace(tmp, path)
    finally:
        if os.path.exists(tmp):
            os.unlink(tmp)


def change_status(attempt, result, installed=None):
    STATE.mkdir(mode=0o755, parents=True, exist_ok=True)
    with (STATE / "lock").open("a") as lock:
        fcntl.flock(lock, fcntl.LOCK_EX)
        # Each stream has its own active attempt, so an interactive retry
        # cannot overwrite or prevent the scheduled outcome being recorded.
        daily = attempt["attempt_id"].startswith("cron:")
        paths = [STATE / "daily.json", STATE / "status.json"] if daily else [STATE / "status.json"]
        for path in paths:
            try:
                previous = json.loads(path.read_text())
            except (OSError, ValueError):
                previous = {}
            if not isinstance(previous, dict):
                previous = {}
            if result != "running" and previous.get("attempt_id") != attempt["attempt_id"]:
                continue
            if result == "success" and previous.get("result") != "running":
                continue
            record = {"schema_version": 1, "last_attempt": attempt["last_attempt"],
                      "last_check": previous.get("last_check") if valid_time(previous.get("last_check")) else None,
                      "last_success": previous.get("last_success") if valid_time(previous.get("last_success")) else None,
                      "result": result,
                      "last_install": previous.get("last_install") if valid_time(previous.get("last_install")) else None,
                      "installed": installed, "attempt_id": attempt["attempt_id"]}
            if "monitor_signature" in attempt:
                record["monitor_signature"] = attempt["monitor_signature"]
            if result in ("failed", "unknown"):
                record["failure_stage"] = attempt.get("stage")
            if result == "running":
                record.update(attempt["owner"])
            if result == "success":
                record["last_success"] = now()
                if installed:
                    record["last_install"] = record["last_success"]
            write_json(path, record)


def record_check(attempt):
    try:
        with (STATE / "lock").open("a") as lock:
            fcntl.flock(lock, fcntl.LOCK_EX)
            paths = [STATE / "daily.json", STATE / "status.json"] if attempt["attempt_id"].startswith("cron:") else [STATE / "status.json"]
            for path in paths:
                try:
                    value = json.loads(path.read_text())
                    if isinstance(value, dict) and value.get("attempt_id") == attempt["attempt_id"]:
                        value["last_check"] = now()
                        write_json(path, value)
                except (OSError, ValueError, TypeError, KeyError):
                    pass
    except (OSError, ValueError, TypeError, KeyError):
        print("Security update status could not be recorded.", file=sys.stderr)


def invoke_cron(command, *, warnings=None, truncate=False):
    # Keep the full failure output for an administrator without exposing
    # repository URLs or other configuration to unprivileged readers.
    fd = None
    try:
        STATE.mkdir(mode=0o755, parents=True, exist_ok=True)
        fd = os.open(STATE / "daily.log", os.O_WRONLY | os.O_CREAT | (os.O_TRUNC if truncate else os.O_APPEND), 0o600)
        os.fchmod(fd, 0o600)
        log = os.fdopen(fd, "w" if truncate else "a")
    except OSError:
        if fd is not None:
            os.close(fd)
        return invoke(command, warnings=warnings) if warnings is not None else invoke(command)
    try:
        return invoke(command, log=log, warnings=warnings, log_errors=True)
    finally:
        try:
            log.close()
        except OSError:
            pass


def invoke(command, log=None, warnings=None, log_errors=False):
    count = None
    log_lock = threading.Lock()
    log_failed = False

    def log_line(line):
        nonlocal log_failed
        with log_lock:
            if log is None or log_failed:
                return
            try:
                log.write(line)
                log.flush()
            except (OSError, ValueError):
                # A full or failed log filesystem must not stop APT or leave
                # either child pipe undrained. Continue forwarding its output.
                log_failed = True

    with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                          text=True, errors="replace", bufsize=1) as process:
        def copy_errors():
            for line in process.stderr:
                sys.stderr.write(line)
                sys.stderr.flush()
                if log_errors:
                    log_line(line)
                if warnings is not None and ("Failed to fetch" in line or "Some index files failed to download" in line):
                    warnings.append(line)

        reader = threading.Thread(target=copy_errors)
        reader.start()
        for line in process.stdout:
            sys.stdout.write(line)
            sys.stdout.flush()
            log_line(line)
            match = SUMMARY.match(line)
            if match:
                count = int(match[1]) + int(match[2])
            if warnings is not None and ("Failed to fetch" in line or "Some index files failed to download" in line):
                warnings.append(line)
        code = process.wait()
        reader.join()
    return code, count


def attempt(owner, identifier):
    return {"attempt_id": identifier, "last_attempt": now(), "owner": process_identity(owner)}


def record(current, result, installed=None):
    try:
        change_status(current, result, installed)
    except (OSError, ValueError, TypeError, KeyError, IndexError):
        print("Security update status could not be recorded.", file=sys.stderr)


def record_setup_skip():
    """Record an explicit first-boot choice without changing daily history."""
    try:
        current = attempt(os.getpid(), "setup:" + str(os.getpid()) + ":" + now())
        change_status(current, "running")
        change_status(current, "skipped")
    except (OSError, ValueError, TypeError, KeyError, IndexError):
        print("Security update status could not be recorded.", file=sys.stderr)


def record_manual_success(current, completed):
    """Keep the latest completed manual run even if another attempt started."""
    try:
        if (not valid_time(current.get("last_attempt")) or not valid_time(completed)
                or current["last_attempt"] > completed or completed > now()):
            raise ValueError("invalid completion time")
        STATE.mkdir(mode=0o755, parents=True, exist_ok=True)
        with (STATE / "lock").open("a") as lock:
            fcntl.flock(lock, fcntl.LOCK_EX)
            path = STATE / "manual-success.json"
            try:
                previous = json.loads(path.read_text())
                if (isinstance(previous, dict) and type(previous.get("schema_version")) is int
                        and previous["schema_version"] == 1
                        and previous.get("result") == "success"
                        and str(previous.get("attempt_id", "")).startswith("manual:")
                        and valid_time(previous.get("last_attempt"))
                        and valid_time(previous.get("last_success"))
                        and previous["last_attempt"] <= previous["last_success"] <= now()
                        and previous["last_success"] >= completed):
                    return
            except (OSError, ValueError, TypeError):
                pass
            write_json(path, {"schema_version": 1, "result": "success",
                              "last_attempt": current["last_attempt"],
                              "last_success": completed,
                              "attempt_id": current["attempt_id"]})
    except (OSError, ValueError, TypeError, KeyError):
        # Recording an outcome must not change the update command's result.
        print("Security update status could not be recorded.", file=sys.stderr)


def security_upgrade(args):
    if any(arg in ("-s", "--simulate", "--just-print", "--dry-run", "--recon",
                   "--no-act", "-d", "--download-only", "--print-uris")
           or (re.fullmatch(r"-[A-Za-z]+", arg) and any(flag in arg[1:] for flag in ("s", "d")))
           for arg in args):
        return False
    options = {}
    iterator = iter(args)
    for arg in iterator:
        if arg in ("-o", "--option"):
            setting = next(iterator, "")
        elif arg.startswith("--option="):
            setting = arg[len("--option="):]
        elif arg.startswith("-o") and len(arg) > 2:
            setting = arg[2:]
        else:
            continue
        key, separator, value = setting.partition("=")
        if separator:
            options[key.lower()] = value
    parts = options.get("dir::etc::sourceparts")
    return (options.get("dir::etc::sourcelist") == SECURITY_SOURCES
            and not any(options.get(key, "").lower() not in ("", "false", "0", "no")
                        for key in ("apt::get::simulate", "apt::get::download-only", "apt::get::print-uris"))
            and (parts == "/dev/null" or
                 (parts == "nonexistent" and not Path("/etc/apt/nonexistent").exists())))


def security_sources_available():
    try:
        contents = Path(SECURITY_SOURCES).read_text()
    except (OSError, UnicodeError):
        return False
    for stanza in re.split(r"\n\s*\n", contents):
        fields = {}
        for line in stanza.splitlines():
            key, separator, value = line.partition(":")
            if separator and not line[0].isspace():
                fields[key.lower()] = value.strip()
        if (fields.get("enabled", "yes").lower() != "no"
                and "deb" in fields.get("types", "").split()
                and fields.get("uris") and fields.get("suites")):
            return True
    return False


def update_covers_security(args, source_options=None):
    # A custom APT configuration or source override can exclude the security
    # sources from the index refresh. Leave its outcome unmonitored.
    if source_options is None and os.environ.get("APT_CONFIG"):
        return False
    if any(
        arg in ("-c", "--config-file")
        or arg.startswith(("--config-file=", "-c"))
        or any(key in arg.lower() for key in (
            "dir::etc::sourcelist", "dir::etc::sourceparts",
            "dir::etc::main", "dir::etc::parts", "dir::etc=", "dir=",
        ))
        for arg in args
    ):
        return False
    try:
        result = subprocess.run(["apt-config", *(source_options or []), "dump"], capture_output=True,
                                text=True, timeout=5, check=True)
    except (OSError, subprocess.SubprocessError):
        return False
    settings = {key.lower(): value for key, value in
                re.findall(r'^([^\s]+) "([^"]*)";$', result.stdout, re.MULTILINE)}
    expected_sources = {
        "dir": "/", "dir::etc": "etc/apt",
        "dir::etc::sourcelist": "sources.list",
        "dir::etc::sourceparts": "sources.list.d",
    }
    if source_options is not None:
        # The source paths are absolute. APT RootDir can still redirect them
        # and the package database, so do not certify an alternate root.
        if settings.get("rootdir", "") not in ("", "/"):
            return False
        expected_sources.pop("dir::etc")
        expected_sources["dir::etc::sourcelist"] = SECURITY_SOURCES
        expected_sources["dir::etc::sourceparts"] = "/dev/null"
    return (all(settings.get(key) == expected for key, expected in expected_sources.items()) and not any(
        settings.get(key, "").lower() not in ("", "false", "0", "no")
        for key in ("apt::get::simulate", "apt::get::download-only", "apt::get::print-uris")
    ))


def supported_cron_actions():
    actions = Path(os.environ.get("TURNKEY_SECURITY_ACTIONDIR", "/etc/cron-apt/action.d"))
    configs = Path(os.environ.get("TURNKEY_SECURITY_ACTIONCONFDIR", "/etc/cron-apt/config.d"))
    try:
        for path in actions.iterdir():
            if not path.is_file() or path.name.startswith(".") or path.name.endswith("~"):
                continue
            # Even an empty action can load per-action settings that replace
            # the command used by later actions.
            if path.name != "0-update" and (configs / path.name).exists():
                return False
            lines = [line.split("#", 1)[0].strip() for line in path.read_text().splitlines()]
            lines = [line for line in lines if line]
            if path.name == "0-update":
                if len(lines) != 1 or shlex.split(lines[0])[0] != "update":
                    return False
            elif path.name == "5-install":
                if (len(lines) != 2 or not lines[0].startswith("autoclean ")
                        or not security_upgrade(shlex.split(lines[1]))
                        or shlex.split(lines[1])[0] not in ("upgrade", "dist-upgrade")):
                    return False
            elif lines:
                return False
        return ((actions / "0-update").is_file() and (actions / "5-install").is_file()
                and security_sources_available())
    except (OSError, ValueError):
        return False


def cron_monitor_signature():
    if (os.environ.get("TURNKEY_SECURITY_CONFIG", str(CRON_CONFIG)) != str(CRON_CONFIG)
            or os.environ.get("TURNKEY_SECURITY_ACTIONDIR", "/etc/cron-apt/action.d") != "/etc/cron-apt/action.d"
            or os.environ.get("TURNKEY_SECURITY_ACTIONCONFDIR", "/etc/cron-apt/config.d") != "/etc/cron-apt/config.d"):
        return ""
    if CRON_HOOK.read_text() != HOOK_TEXT:
        return ""
    paths = [CRON_HOOK, CRON_CONFIG, CRON_SCHEDULE,
             Path("/etc/cron-apt/action.d/0-update"),
             Path("/etc/cron-apt/action.d/3-download"),
             Path("/etc/cron-apt/action.d/5-install")]
    digest = hashlib.sha256()
    for path in paths:
        digest.update(str(path).encode() + b"\0")
        try:
            digest.update(path.read_bytes())
        except FileNotFoundError:
            digest.update(b"<missing>")
        digest.update(b"\0")
    return digest.hexdigest()


def cron(args):
    original = os.environ.get("TURNKEY_SECURITY_APT_COMMAND", "/usr/bin/apt-get").split()
    job = os.environ.get("TURNKEY_SECURITY_CRON_ID", "")
    owner = os.environ.get("TURNKEY_SECURITY_CRON_PID", "")
    if not re.fullmatch(r"[0-9]+-[0-9]+", job) or not owner.isdigit():
        return invoke(original + args)[0]
    runfile = RUNTIME / (job + ".json")
    action = next((arg for arg in args if arg in ("update", "upgrade", "dist-upgrade", "autoclean")), "")
    if action == "update":
        if original != ["/usr/bin/apt-get"] or not supported_cron_actions() or not update_covers_security(original + args):
            try:
                current = attempt(int(owner), "cron:" + job)
                current["monitor_signature"] = cron_monitor_signature()
                change_status(current, "running")
                change_status(current, "unknown")
            except (OSError, ValueError, TypeError, IndexError):
                print("Security update status could not be recorded.", file=sys.stderr)
            return invoke(original + args)[0]
        try:
            runfile.unlink(missing_ok=True)
            current = attempt(int(owner), "cron:" + job)
            current["monitor_signature"] = cron_monitor_signature()
            change_status(current, "running")
        except (OSError, ValueError, TypeError, IndexError):
            print("Security update status could not be recorded.", file=sys.stderr)
            current = None
        warnings = []
        code, _ = (invoke_cron(original + args, warnings=warnings, truncate=True)
                   if current else invoke(original + args, warnings=warnings))
        if current:
            current["stage"] = "repository refresh"
        if current and code:
            record(current, "failed")
        elif current and warnings:
            record(current, "failed")
        elif current:
            record_check(current)
            try:
                write_json(runfile, current)
            except (OSError, ValueError, TypeError):
                record(current, "unknown")
        return code
    if action == "autoclean":
        code, _ = invoke_cron(original + args) if runfile.exists() else invoke(original + args)
        if code:
            try:
                current = json.loads(runfile.read_text())
                current["stage"] = "package cleanup"
                if isinstance(current, dict) and current.get("owner") == process_identity(int(owner)):
                    record(current, "failed")
                    runfile.unlink(missing_ok=True)
            except (OSError, ValueError, TypeError, IndexError):
                pass
        return code
    if action in ("upgrade", "dist-upgrade") and security_upgrade(original[1:] + args) and update_covers_security([]):
        try:
            current = json.loads(runfile.read_text())
            if not isinstance(current, dict) or current.get("owner") != process_identity(int(owner)):
                current = None
            elif (previous := json.loads((STATE / "daily.json").read_text())).get("result") != "running" or previous.get("attempt_id") != current.get("attempt_id"):
                current = None
        except (OSError, ValueError, TypeError, IndexError, AttributeError):
            current = None
        code, count = invoke_cron(original + args) if current else invoke(original + args)
        if current:
            current["stage"] = "security update installation"
            record(current, "success" if code == 0 else "failed", count if code == 0 else None)
            try:
                runfile.unlink(missing_ok=True)
            except OSError:
                pass
        return code
    return invoke(original + args)[0]


def manual():
    try:
        current = attempt(os.getpid(), "manual:" + str(os.getpid()) + ":" + now())
        change_status(current, "running")
    except (OSError, ValueError, TypeError, IndexError):
        print("Security update status could not be recorded.", file=sys.stderr)
        current = None
    logfile = LOGFILE
    logfile.parent.mkdir(parents=True, exist_ok=True)
    env = dict(os.environ, DEBIAN_FRONTEND="noninteractive", LC_ALL="C")
    os.environ.update(env)
    security_options = ["-o", "Dir::Etc::sourceparts=/dev/null",
                        "-o", "Dir::Etc::sourcelist=" + SECURITY_SOURCES]
    sources_available = security_sources_available() and update_covers_security([], security_options)
    # Refresh the same repositories the upgrade uses. Keep indexes belonging
    # to other repositories for ordinary APT commands and scheduled updates.
    refresh = ["apt-get", "update", *security_options, "-o", "APT::Get::List-Cleanup=false"]
    commands = [
        ["dpkg", "--force-confdef", "--force-confold", "--configure", "-a"],
        refresh,
        ["apt-get", "autoclean", "-y"],
        ["apt-get", "dist-upgrade", "-y", "-o", "APT::Get::Show-Upgraded=true",
         *security_options,
         "-o", "DPkg::Options::=--force-confdef", "-o", "DPkg::Options::=--force-confold"],
    ]
    try:
        fd = os.open(logfile, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
        try:
            os.fchmod(fd, 0o600)
        except OSError:
            os.close(fd)
            raise
        with os.fdopen(fd, "a") as log:
            log.write("[security-updates] Manual run " + now() + "\n")
            log.flush()
            incomplete = []
            for command in commands:
                code, count = invoke(command, log,
                                     incomplete if command == refresh else None,
                                     log_errors=True)
                if code or incomplete:
                    if current:
                        current["stage"] = ("repository refresh" if command == refresh
                                            else "package configuration" if command[0] == "dpkg"
                                            else "security update installation")
                        record(current, "failed")
                    return code or 1
                if current and command == refresh and sources_available:
                    record_check(current)
            if not sources_available:
                reason = "Could not verify the configured security update sources. Check the APT security repository configuration."
                print(reason, file=sys.stderr)
                log.write(reason + "\n")
                log.flush()
        if current:
            completed = now()
            record(current, "unknown" if incomplete or not sources_available else "success",
                   count if not incomplete and sources_available else None)
            if sources_available:
                record_manual_success(current, completed)
        # A custom or missing security source cannot establish a reliable
        # outcome even if APT itself completed successfully.
        return 0 if sources_available else 1
    except (OSError, subprocess.SubprocessError):
        if current:
            record(current, "failed")
        raise


def status_file(filename):
    try:
        value = json.loads((STATE / filename).read_text())
        if (not isinstance(value, dict) or type(value.get("schema_version")) is not int
                or value.get("schema_version") != 1
                or value.get("result") not in ("running", "success", "failed", "unknown", "skipped")
                or not valid_time(value.get("last_attempt"))
                or (value.get("last_success") is not None and not valid_time(value["last_success"]))
                or (value.get("last_check") is not None and not valid_time(value["last_check"]))
                or (value.get("last_install") is not None and not valid_time(value["last_install"]))
                or (value.get("result") == "success" and not valid_time(value.get("last_success")))
                or (value.get("installed") is not None
                    and (type(value["installed"]) is not int or value["installed"] < 0))):
            raise ValueError("invalid status")
        current_time = now()
        if (value["last_attempt"] > current_time
                or (value.get("last_success") and value["last_success"] > current_time)
                or (value.get("last_check") and value["last_check"] > current_time)
                or (value.get("last_install") and value["last_install"] > current_time)
                or (value["result"] == "success" and value["last_success"] < value["last_attempt"])):
            raise ValueError("invalid status timestamps")
    except (OSError, ValueError, TypeError):
        value = {"schema_version": 1, "last_attempt": None, "last_check": None, "last_success": None, "last_install": None,
                 "result": "unknown", "installed": None}
    if value.get("result") == "running":
        try:
            owner = process_identity(int(value["owner_pid"]))
            if any(owner[key] != value.get(key) for key in owner):
                value["result"] = "interrupted"
        except (OSError, ValueError, KeyError, TypeError, IndexError):
            value["result"] = "interrupted"
    if str(value.get("attempt_id", "")).startswith("cron:"):
        try:
            # The recorded outcome describes the completed run. Unrelated
            # changes such as mail settings can alter the current signature
            # without changing what happened during that run.
            if (not value.get("monitor_signature")
                    or not cron_monitor_signature()
                    or not supported_cron_actions()
                    or not update_covers_security([])):
                value["result"] = "unknown"
        except OSError:
            value["result"] = "unknown"
    return {"schema_version": 1, "last_attempt": value.get("last_attempt"),
            "last_check": value.get("last_check"),
            "last_success": value.get("last_success"), "result": value["result"],
            "last_install": value.get("last_install"),
            "installed": value.get("installed") if value["result"] == "success" else None,
            "reboot_required": True if Path("/run/reboot-required").exists() else None,
            "failure_stage": value.get("failure_stage") if value["result"] in ("failed", "unknown") else None,
            "source": ("setup" if str(value.get("attempt_id", "")).startswith("setup:") else
                       "manual" if str(value.get("attempt_id", "")).startswith("manual:") else
                       "daily" if str(value.get("attempt_id", "")).startswith("cron:") else None)}


def status():
    value = status_file("status.json")
    daily = status_file("daily.json")
    manual = status_file("manual-success.json")
    if manual.get("source") != "manual" or manual.get("result") != "success":
        manual = {"result": "unknown", "last_attempt": None, "last_success": None}
    value["daily"] = {key: daily.get(key) for key in ("result", "last_attempt", "last_success", "failure_stage")}
    value["manual"] = {key: manual.get(key) for key in ("result", "last_attempt", "last_success")}
    return value


def show_status(as_json):
    value = status()
    if as_json:
        print(json.dumps(value, sort_keys=True))
        return
    daily = value["daily"]
    print("Daily security updates:", daily["result"])
    if daily["last_attempt"]:
        print("Last daily attempt:", daily["last_attempt"])
    if daily["last_success"]:
        print("Last successful daily run:", daily["last_success"])
    if daily["failure_stage"]:
        print("Failed stage:", daily["failure_stage"])
    if daily["result"] in ("failed", "interrupted"):
        print("Daily output: /var/lib/turnkey-security-updates/daily.log")
        try:
            lines = (STATE / "daily.log").read_text(errors="replace").splitlines()[-20:]
            if lines:
                print("Recent output:")
                for line in lines:
                    print("  " + "".join(c if c.isprintable() else "?" for c in line)[:500])
        except OSError:
            print("Run with sudo to read the daily output.")
        print("Retry: sudo security-updates")
    print("Most recently started update:", value["result"])
    if value["manual"]["result"] == "success":
        print("Last successful manual run:", value["manual"]["last_success"])
    if value["source"] == "manual" and value["result"] in ("failed", "unknown"):
        if value["failure_stage"]:
            print("Manual update failed during:", value["failure_stage"])
        elif value["result"] == "unknown":
            print("Manual update outcome could not be verified.")
        print("Manual update output:", LOGFILE)
        try:
            lines = LOGFILE.read_text(errors="replace").splitlines()
            markers = [i for i, line in enumerate(lines) if line.startswith("[security-updates] Manual run ")]
            if markers:
                print("Recent manual output:")
                for line in lines[markers[-1] + 1:][-20:]:
                    print("  " + "".join(c if c.isprintable() else "?" for c in line)[:500])
        except OSError:
            print("Run with sudo to read the manual update output.")
    if value["last_attempt"]:
        print("Last attempt:", value["last_attempt"])
    if value["last_check"]:
        print("Last successful index refresh:", value["last_check"])
    if value["last_success"]:
        print("Last successful check:", value["last_success"])
    if value["last_install"]:
        print("Last installation:", value["last_install"])
    if value["installed"] is not None:
        print("Packages installed:", value["installed"])
    if value["reboot_required"]:
        print("Reboot required")


if __name__ == "__main__":
    try:
        name = Path(sys.argv[0]).name
        if name == "security-updates":
            if not sys.argv[1:]:
                os.execv(INSTALL_COMMAND, [INSTALL_COMMAND])
            if sys.argv[1:] not in (["--status"], ["--json"]):
                sys.exit("Usage: security-updates [--status|--json]")
            show_status(sys.argv[1:] == ["--json"])
        elif name == "turnkey-security-updates-status":
            if sys.argv[1:] not in ([], ["--json"]):
                sys.exit("Usage: turnkey-security-updates-status [--json]")
            show_status(sys.argv[1:] == ["--json"])
        else:
            if sys.argv[1:] == ["--mark-skipped"]:
                record_setup_skip()
            else:
                sys.exit(manual() if sys.argv[1:] == ["--manual"] else cron(sys.argv[1:]))
    except (OSError, ValueError, subprocess.SubprocessError) as error:
        print(f"security update status: {error}", file=sys.stderr)
        sys.exit(1)
