# EasySpotter Arch install scriptlet. Ported from easysdr/debian/{postinst,prerm,postrm} (the same scripts
# packaging/build_deb.py ships), the way packaging/build_rpm.py ports them: adduser/addgroup become
# useradd/groupadd, "configure" becomes install-or-upgrade, and there is no "purge" (settings survive a
# removal). EasySDR is bundled in EasySpotter, so its system user, owner record and service are set up here.

_rtl_ids="0bda:2832 0bda:2838 0413:6680 0413:6f0f 0458:707f 0ccd:00a9 0ccd:00b3 0ccd:00b4 0ccd:00b5 0ccd:00b7 0ccd:00b8 0ccd:00b9 0ccd:00c0 0ccd:00c6 0ccd:00d3 0ccd:00d7 0ccd:00e0 1554:5020 15f4:0131 15f4:0133 185b:0620 185b:0650 185b:0680 1b80:d393 1b80:d394 1b80:d395 1b80:d397 1b80:d398 1b80:d39d 1b80:d3a4 1b80:d3a8 1b80:d3af 1b80:d3b0 1d19:1101 1d19:1102 1d19:1103 1d19:1104 1f4d:a803 1f4d:b803 1f4d:c803 1f4d:d286"

_rtl_udev_retrigger() {
    # RTL-SDR dongles plugged in before this install get the udev rule's access now (without a replug):
    # a change event re-applies permissions for each known RTL-SDR USB ID; nothing is reset.
    if [ -S /run/udev/control ] && command -v udevadm >/dev/null; then
        udevadm control --reload-rules >/dev/null 2>&1 || true
        for id in $_rtl_ids; do
            udevadm trigger --action=change --subsystem-match=usb \
                --attr-match=idVendor="${id%:*}" --attr-match=idProduct="${id#*:}" >/dev/null 2>&1 || true
        done
    fi
}

post_install() {
    getent group easysdr >/dev/null || groupadd -r easysdr
    getent passwd easysdr >/dev/null || useradd -r -g easysdr -d /var/lib/easysdr -M -s /usr/bin/nologin -c "EasySDR web SDR" easysdr
    mkdir -p /etc/easysdr
    if [ ! -s /etc/easysdr/owner ]; then
        owner=${SUDO_USER:-}
        if [ -z "$owner" ] && [ -n "${PKEXEC_UID:-}" ]; then owner=$(getent passwd "$PKEXEC_UID" | cut -d: -f1); fi
        if [ -z "$owner" ] && command -v loginctl >/dev/null; then owner=$(loginctl list-sessions --no-legend 2>/dev/null | awk '$4 == "seat0" { print $3; exit }'); fi
        if [ -n "$owner" ] && [ "$owner" != root ]; then echo "$owner" > /etc/easysdr/owner; fi
    fi
    # EasySpotter first install: sharing waits, off, for the launcher's first start, whose one-time notice
    # switches it on. A station that already has settings keeps them.
    if [ ! -e /var/lib/easysdr/easysdr.json ]; then
        mkdir -p /var/lib/easysdr
        printf '{"sharing": false, "sharing_pending": true}\n' > /var/lib/easysdr/easysdr.json
        chown -R easysdr:easysdr /var/lib/easysdr
        chmod 700 /var/lib/easysdr
        chmod 600 /var/lib/easysdr/easysdr.json
    fi
    if [ -d /run/systemd/system ]; then
        systemctl daemon-reload || true
        systemctl enable easysdr.service >/dev/null 2>&1 || true
        systemctl restart easysdr.service || true
    fi
    if [ ! -s /etc/easysdr/owner ]; then
        echo "EasySDR: no station owner set. Put your user name in /etc/easysdr/owner, then: systemctl restart easysdr" >&2
    fi
    _rtl_udev_retrigger
}

post_upgrade() {
    post_install
}

pre_remove() {
    if [ -d /run/systemd/system ]; then
        systemctl disable --now easysdr.service >/dev/null 2>&1 || true
    fi
}

post_remove() {
    # Arch has no "purge": /etc/easysdr and /var/lib/easysdr stay for a reinstall (remove them by hand).
    if [ -d /run/systemd/system ]; then systemctl daemon-reload >/dev/null 2>&1 || true; fi
}
