aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/DisableWaybarService.sh
blob: 7750fe4a7aaab4631c43c4532b1e48306ca57fe1 (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env bash
set -u
set -o pipefail

SERVICE_NAME="waybar.service"
ACTION=""
DRY_RUN=false
OPERATION_RESULT="not-run"

RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m"

ICON_OK="✅"
ICON_WARN="⚠️"
ICON_ERR="❌"
ICON_INFO="ℹ️"

usage() {
    cat <<EOF
Usage: $(basename "$0") [OPTIONS]

Disable --user ${SERVICE_NAME} (default action), revert it, or check status.

Options:
  -h, --help      Show this help message and exit
  -r, --revert    Revert by enabling + starting --user ${SERVICE_NAME}
  -d, --dry-run   Print actions without changing anything
  -s, --status    Show current status of --user ${SERVICE_NAME}
EOF
}

info() {
    printf "${BLUE}${ICON_INFO} %s${NC}\n" "$1"
}

ok() {
    printf "${GREEN}${ICON_OK} %s${NC}\n" "$1"
}

warn() {
    printf "${YELLOW}${ICON_WARN} %s${NC}\n" "$1"
}

error() {
    printf "${RED}${ICON_ERR} %s${NC}\n" "$1" >&2
}

fail() {
    error "$1"
    exit 1
}

format_command() {
    printf "%q " "$@"
}

run_cmd() {
    if $DRY_RUN; then
        info "[dry-run] $(format_command "$@")"
        return 0
    fi
    "$@"
}

set_action() {
    local new_action="$1"
    if [[ -n "$ACTION" && "$ACTION" != "$new_action" ]]; then
        fail "Conflicting options: cannot combine '${ACTION}' with '${new_action}'."
    fi
    ACTION="$new_action"
}

require_systemctl() {
    command -v systemctl >/dev/null 2>&1 || fail "systemctl is required but was not found."
}

require_user_manager() {
    if ! systemctl --user show-environment >/dev/null 2>&1; then
        fail "Cannot contact systemd user manager. Ensure your user systemd session is running."
    fi
}

service_load_state() {
    systemctl --user show --property=LoadState --value "${SERVICE_NAME}" 2>/dev/null || true
}

service_exists() {
    local load_state
    load_state="$(service_load_state)"
    [[ -n "${load_state}" && "${load_state}" != "not-found" ]]
}

require_service_exists() {
    if ! service_exists; then
        fail "--user ${SERVICE_NAME} was not found. Exiting."
    fi
}

get_enabled_state() {
    local state
    state="$(systemctl --user is-enabled "${SERVICE_NAME}" 2>/dev/null || true)"
    [[ -n "${state}" ]] || state="unknown"
    printf "%s" "${state}"
}

get_active_state() {
    local state
    state="$(systemctl --user is-active "${SERVICE_NAME}" 2>/dev/null || true)"
    [[ -n "${state}" ]] || state="unknown"
    printf "%s" "${state}"
}

print_state_line() {
    local label="$1"
    local state="$2"
    local color="${BLUE}"
    local icon="${ICON_INFO}"

    case "${state}" in
        loaded|enabled|active)
            color="${GREEN}"
            icon="${ICON_OK}"
            ;;
        activating|deactivating|reloading|inactive|disabled|static|indirect|masked)
            color="${YELLOW}"
            icon="${ICON_WARN}"
            ;;
        failed|not-found|unknown)
            color="${RED}"
            icon="${ICON_ERR}"
            ;;
    esac

    printf "  %-9s: %b%s %s%b\n" "${label}" "${color}" "${icon}" "${state}" "${NC}"
}

print_report() {
    local load_state enabled_state active_state dry_run_state result_color result_icon result_text

    load_state="$(service_load_state)"
    [[ -n "${load_state}" ]] || load_state="unknown"
    enabled_state="$(get_enabled_state)"
    active_state="$(get_active_state)"
    dry_run_state="no"
    $DRY_RUN && dry_run_state="yes"

    case "${OPERATION_RESULT}" in
        success)
            result_color="${GREEN}"
            result_icon="${ICON_OK}"
            result_text="success"
            ;;
        dry-run)
            result_color="${YELLOW}"
            result_icon="${ICON_WARN}"
            result_text="dry-run"
            ;;
        failed)
            result_color="${RED}"
            result_icon="${ICON_ERR}"
            result_text="failed"
            ;;
        *)
            result_color="${BLUE}"
            result_icon="${ICON_INFO}"
            result_text="not-run"
            ;;
    esac

    printf "\n${BLUE}${ICON_INFO} Report for --user %s${NC}\n" "${SERVICE_NAME}"
    print_state_line "LoadState" "${load_state}"
    print_state_line "Enabled" "${enabled_state}"
    print_state_line "Active" "${active_state}"
    printf "  %-9s: %s\n" "Dry-run" "${dry_run_state}"
    printf "  %-9s: %b%s %s%b\n" "Result" "${result_color}" "${result_icon}" "${result_text}" "${NC}"
}

perform_disable() {
    info "Disabling and stopping --user ${SERVICE_NAME}..."
    if run_cmd systemctl --user disable --now "${SERVICE_NAME}"; then
        if $DRY_RUN; then
            OPERATION_RESULT="dry-run"
            warn "Dry-run complete. No changes were made."
        else
            OPERATION_RESULT="success"
            ok "Disabled and stopped --user ${SERVICE_NAME}."
        fi
        return 0
    fi

    OPERATION_RESULT="failed"
    error "Failed to disable --user ${SERVICE_NAME}."
    return 1
}

perform_revert() {
    info "Reverting --user ${SERVICE_NAME} (unmask + enable + start)..."
    if ! run_cmd systemctl --user unmask "${SERVICE_NAME}"; then
        OPERATION_RESULT="failed"
        error "Failed to unmask --user ${SERVICE_NAME}."
        return 1
    fi
    if ! run_cmd systemctl --user enable --now "${SERVICE_NAME}"; then
        OPERATION_RESULT="failed"
        error "Failed to enable/start --user ${SERVICE_NAME}."
        return 1
    fi

    if $DRY_RUN; then
        OPERATION_RESULT="dry-run"
        warn "Dry-run complete. No changes were made."
    else
        OPERATION_RESULT="success"
        ok "Reverted successfully. --user ${SERVICE_NAME} is enabled and started."
    fi
    return 0
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        -r|--revert)
            set_action "revert"
            ;;
        -d|--dry-run)
            DRY_RUN=true
            ;;
        -s|--status)
            set_action "status"
            ;;
        *)
            fail "Unknown option: $1 (use -h or --help)"
            ;;
    esac
    shift
done

[[ -n "${ACTION}" ]] || ACTION="disable"

require_systemctl
require_user_manager
require_service_exists

exit_code=0
case "${ACTION}" in
    disable)
        perform_disable || exit_code=1
        ;;
    revert)
        perform_revert || exit_code=1
        ;;
    status)
        OPERATION_RESULT="success"
        info "Status requested for --user ${SERVICE_NAME}."
        ;;
esac

print_report
exit "${exit_code}"
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage