#!/bin/sh
# postinst for velox (local test build). See packaging/README.md.
set -e

NM_MANIFEST_SRC=/usr/lib/mozilla/native-messaging-hosts/com.velox.host.json
NM_MANIFEST_NAME=com.velox.host.json

# Every real (human) user with a home directory — UID range matches Debian's own
# adduser default (login.defs UID_MIN=1000), not just "everyone in /home" (a service
# account can have a home dir too) and not root.
real_users() {
    getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 && $6 != "" {print $1":"$6}'
}

# Per-user native-messaging manifest (ADR 0003 / docs/05 §4.A): needed for BOTH
# deb/tarball and snap Firefox — the system-wide manifest this package also installs at
# $NM_MANIFEST_SRC only covers deb/tarball Firefox, never snap. Runs unconditionally,
# not just when snap is detected: a per-user login.defs-based install can't assume every
# user runs the same Firefox flavour, and dropping a JSON file nobody reads is harmless.
install_per_user_manifests() {
    [ -f "$NM_MANIFEST_SRC" ] || {
        echo "velox: native-messaging host not built yet (nmhost lane not landed) —" \
             "skipping per-user manifest install."
        return 0
    }
    real_users | while IFS=: read -r user home; do
        [ -d "$home" ] || continue
        dest_dir="$home/.mozilla/native-messaging-hosts"
        install -d -o "$user" -g "$user" -m 0755 "$dest_dir" 2>/dev/null || {
            echo "velox: could not create $dest_dir for $user — skipping" >&2
            continue
        }
        cp "$NM_MANIFEST_SRC" "$dest_dir/$NM_MANIFEST_NAME"
        chown "$user:$user" "$dest_dir/$NM_MANIFEST_NAME"
        chmod 0644 "$dest_dir/$NM_MANIFEST_NAME"
    done
}

# docs/07-packaging.md: detect snap Firefox and say so — the extension pairs over
# loopback WebSocket there (ADR 0003), not native messaging from inside the sandbox.
note_snap_firefox() {
    if command -v snap >/dev/null 2>&1 && snap list firefox >/dev/null 2>&1; then
        cat <<'EOF'

velox: Firefox is installed as a snap on this system. The extension will pair with
       veloxd over the loopback WebSocket (ws://127.0.0.1:<port>), not native
       messaging — this is expected (see docs/adr/0003, ADR 0003) and needs no
       action from you.
EOF
    fi
}

case "$1" in
    configure)
        install_per_user_manifests
        note_snap_firefox

        cat <<'EOF'

============================================================================
velox: LOCAL TEST BUILD — read this before pairing the extension
============================================================================
No real pairing-approval UI is built into this package yet (it needs
libdbus-1-dev, which this build's dependencies do not include). veloxd's
only pairing approver in this build auto-rejects every request UNLESS you
set VELOX_PAIR_AUTO=1 in its environment — there is no prompt to accept or
decline; it is all-or-nothing. Do not run this on a machine or account
where you would not want every pairing request accepted automatically.

  systemctl --user edit velox.service
    # add, in the [Service] section:
    #   Environment=VELOX_PAIR_AUTO=1

debhelper's dh_installsystemduser marks velox.service and velox.socket enabled (the
pair together, via velox.service's Also=velox.socket) for your next login session
automatically. For your CURRENT session, start socket activation now yourself — a
root postinst has no live user session to reach:

  systemctl --user daemon-reload
  systemctl --user enable --now velox.socket

See packaging/README.md in the source tree for the rest of this build's
testing-only limitations (placeholder icon, no license chosen yet, no
libveloxcore.so — it is linked statically).
============================================================================
EOF
        ;;
esac

#DEBHELPER#

exit 0
