summarylogtreecommitdiffstats
path: root/crossmacro.install
blob: 32a6d644da2031bc1afb37673f4c18b911e4e99f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
_crossmacro_load_uinput() {
    # Best effort: make uinput available immediately for the daemon.
    # Persistent boot-time loading is handled by /usr/lib/modules-load.d/crossmacro.conf.
    if command -v modprobe >/dev/null 2>&1; then
        modprobe uinput >/dev/null 2>&1 || true
    fi
}

_crossmacro_has_cmd() {
    command -v "$1" >/dev/null 2>&1
}

_crossmacro_has_systemd_runtime() {
    [ -d /run/systemd/system ] && _crossmacro_has_cmd systemctl
}

_crossmacro_reload_udev() {
    if _crossmacro_has_cmd udevadm; then
        udevadm control --reload-rules >/dev/null 2>&1 || true
        udevadm settle >/dev/null 2>&1 || true
    fi
}

post_install() {
    if _crossmacro_has_systemd_runtime; then
        echo "Reloading systemd daemon..."
        systemctl daemon-reload >/dev/null 2>&1 || true
    fi

    _crossmacro_reload_udev
    _crossmacro_load_uinput
    if _crossmacro_has_cmd udevadm; then
        udevadm trigger >/dev/null 2>&1 || true
        udevadm settle >/dev/null 2>&1 || true
    fi

    auto_service_status="systemd runtime not detected; skipped auto enable/start"
    if _crossmacro_has_systemd_runtime; then
        if systemctl enable --now crossmacro.service >/dev/null 2>&1; then
            auto_service_status="crossmacro.service enabled and started automatically"
        else
            auto_service_status="auto enable/start failed; run: sudo systemctl enable --now crossmacro.service"
        fi
    fi

    installer_user=""
    if [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then
        installer_user="${SUDO_USER}"
    elif [ -n "${PKEXEC_UID:-}" ] && [ "${PKEXEC_UID}" != "0" ]; then
        installer_user="$(getent passwd "${PKEXEC_UID}" | cut -d: -f1)"
    fi

    if [ -n "$installer_user" ] && getent passwd "$installer_user" >/dev/null 2>&1; then
        usermod -aG crossmacro "$installer_user" >/dev/null 2>&1 || true
    fi

    echo "------------------------------------------------------------------------"
    echo "CrossMacro Daemon installed."
    echo "Service: $auto_service_status"
    echo ""
    if [ -n "$installer_user" ]; then
        echo "1. '$installer_user' was added to 'crossmacro' group (best effort)."
        echo "   If needed, run: sudo usermod -aG crossmacro \$USER"
    else
        echo "1. Add yourself to the 'crossmacro' group to communicate with the daemon:"
        echo "   sudo usermod -aG crossmacro \$USER"
    fi
    echo ""
    echo "2. Log out and log back in for group changes to take effect."
    echo "------------------------------------------------------------------------"
}

post_upgrade() {
    if _crossmacro_has_systemd_runtime; then
        systemctl daemon-reload >/dev/null 2>&1 || true
    fi

    _crossmacro_reload_udev
    _crossmacro_load_uinput
    if _crossmacro_has_cmd udevadm; then
        udevadm trigger >/dev/null 2>&1 || true
        udevadm settle >/dev/null 2>&1 || true
    fi

    # Only restart if service is active
    if _crossmacro_has_systemd_runtime && systemctl is-active --quiet crossmacro.service 2>/dev/null; then
        systemctl try-restart crossmacro.service >/dev/null 2>&1 || true
    fi
}

pre_remove() {
    if ! _crossmacro_has_systemd_runtime; then
        return 0
    fi

    # Only disable if service exists and is enabled/active
    if systemctl is-enabled --quiet crossmacro.service 2>/dev/null || \
       systemctl is-active --quiet crossmacro.service 2>/dev/null; then
        systemctl disable --now crossmacro.service >/dev/null 2>&1 || true
    fi
}

post_remove() {
    if _crossmacro_has_systemd_runtime; then
        systemctl daemon-reload >/dev/null 2>&1 || true
    fi
    _crossmacro_reload_udev
}