_provision_pearl_greeter() {
    systemd-sysusers /usr/lib/sysusers.d/pearl-greeter.conf || return 1
    systemd-tmpfiles --create /usr/lib/tmpfiles.d/pearl-greeter.conf || return 1
}

# The appearance sync socket must be live on every install and upgrade, not only
# fresh ones: users coming from packages without the socket, or whose socket
# never started, would otherwise silently fall back to pkexec, which cannot
# prompt when Pearl runs outside a logind session scope. Activate only when it
# is missing so an already running socket is never disturbed.
_enable_greeter_sync() {
    if systemctl is-enabled --quiet pearl-greeter-sync.socket &&
        systemctl is-active --quiet pearl-greeter-sync.socket; then
        return 0
    fi
    systemctl enable --now pearl-greeter-sync.socket 2>/dev/null ||
        echo '==> Activate appearance sync later: sudo systemctl enable --now pearl-greeter-sync.socket'
}

_install_greetd_config() {
    local root=${1:-/}
    local config="${root%/}/etc/greetd/config.toml"
    # greetd or the administrator owns existing configuration, including wrappers.
    # -L also preserves dangling symlinks instead of writing through them.
    if [[ -e "$config" || -L "$config" ]]; then
        echo '==> Preserved existing /etc/greetd/config.toml.'
        echo '    Pearl template: /usr/share/pearl-greeter/greetd.toml'
        return 0
    fi
    local state="${root%/}/var/lib/pearl-greeter-setup"
    install -dm700 -- "$state" || return 1
    # Discard any stale ownership record before creating a new configuration.
    rm -f -- "$state/created-greetd.toml" || return 1
    install -Dm644 -- "${root%/}/usr/share/pearl-greeter/greetd.toml" "$config" || return 1
    install -m600 -- "$config" "$state/created-greetd.toml" || return 1
    echo '==> Installed Pearl Greeter configuration in /etc/greetd/config.toml (VT7).'
}

post_install() {
    _provision_pearl_greeter || return 1
    _install_greetd_config || return 1
    /usr/lib/pearl/pearl-greeter-setup enable || return 1
    _enable_greeter_sync
    echo '==> Pearl Greeter service is selected for the next boot using the retained greetd configuration.'
    echo '    The running session was not restarted. Review the session command to select Pearl Greeter.'
    echo '    Configuration: /etc/pearl/greeter.json and /etc/greetd/config.toml'
    echo '    Restore the previous boot setup: sudo /usr/lib/pearl/pearl-greeter-setup restore'
}

post_upgrade() {
    # Only supply defaults when no configuration exists, including legacy installs.
    # Keep disabled services and later display-manager choices unchanged.
    _provision_pearl_greeter || return 1
    _install_greetd_config || return 1
    _enable_greeter_sync
    echo '==> Pearl Greeter updated; existing greetd configuration and backups are preserved.'
    echo '    Password entry is now enabled before Sign in by default.'
    echo '    If your preserved /etc/pearl/greeter.json has password_first: false, set it to true for one-action password login.'
    echo '    Keep false only for custom PAM stacks that request another credential before the password.'
}

pre_remove() {
    /usr/lib/pearl/pearl-greeter-setup restore
}
