summaryrefslogtreecommitdiff
path: root/raveos-theme/gnome/theme-data/extensions/installed/ShutdownTimer@deminder/tool/installer.sh
blob: 56d4167c4c7304a8a106da2eab248d37865d4174 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash

# SPDX-FileCopyrightText: 2023 Deminder <tremminder@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Adapted from cpupower extension by:
# Martin Koppehel <psl.kontakt@gmail.com>, Fin Christensen <christensen.fin@gmail.com>

# installer.sh - This script installs a policykit rule for the Shutdown Timer gnome-shell extension.
#
# This file is part of the gnome-shell extension ShutdownTimer@Deminder.


set -e

################################
# EXTENSION SPECIFIC OPTIONS:  #
################################

EXTENSION_NAME="Shutdown Timer"
ACTION_BASE="dem.shutdowntimer"
RULE_BASE="$ACTION_BASE.settimers"
CFC_BASE="shutdowntimerctl"
POLKIT_DIR="polkit"
VERSION=1


EXIT_SUCCESS=0
EXIT_INVALID_ARG=1
EXIT_FAILED=2
EXIT_NEEDS_UPDATE=3
EXIT_NEEDS_SECURITY_UPDATE=4
EXIT_NOT_INSTALLED=5
EXIT_MUST_BE_ROOT=6

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #stackoverflow 59895

export TEXTDOMAINDIR="$DIR/../locale"
export TEXTDOMAIN="ShutdownTimer"
function gtxt() {
    gettext "$1"
}

function polkit_version_greater_than() {
    printf -v versions '%s\n%s' "$(pkaction --version | cut -d' ' -f3)" "$1"
    [[ $versions != "$(sort -V <<< "$versions")" ]]
}

function polkit_action_lookup_program_gets_realpath() {
    polkit_version_greater_than "126"
}

function polkit_supports_standalonerule() {
    polkit_version_greater_than "0.106"
}

function check_support() {
    local polkit_info=", stand-alone polkit rules"
    if polkit_supports_standalonerule; then
        polkit_info+=" available"
        if polkit_action_lookup_program_gets_realpath; then
            polkit_info+=", with realpath program lookup"
        fi
    else
        polkit_info+=" not avaliable"
    fi
    if which rtcwake >/dev/null 2>&1
    then
        echo "rtcwake supported${polkit_info}"
        exit ${EXIT_SUCCESS}
    else
        echo "rtcwake unsupported${polkit_info}"
        exit ${EXIT_FAILED}
    fi
}

function require_root() {
    if [ "${EUID}" -ne 0 ]; then
        echo "This action must be run as root!"
        exit ${EXIT_MUST_BE_ROOT}
    fi
}

function fail() {
    echo "$(gtxt "Failed")${1}" >&2 && exit ${EXIT_FAILED}
}
DEFAULT_SUCCESS_MSG=$(gtxt 'Success')

function success() {
    echo -n "${1:-$DEFAULT_SUCCESS_MSG}"
    echo -e "\U1F7E2"
}



########################
# GENERALIZED SCRIPT:  #
########################

function usage() {
    echo "Usage: installer.sh [options] {supported,install,check,update,uninstall}"
    echo
    echo "Available options:"
    echo "  --tool-user USER   Set the user of the tool (default: \$USER)"
    echo
    exit ${EXIT_INVALID_ARG}
}

if [ $# -lt 1 ]
then
    usage
fi

ACTION=""
TOOL_USER="$USER"
while [[ $# -gt 0 ]]
do
    key="$1"

    # we have to use command line arguments here as pkexec does not support
    # setting environment variables
    case $key in
        --tool-user)
            TOOL_USER="$2"
            shift
            shift
            ;;
        supported|install|check|update|uninstall)
            if [ -z "$ACTION" ]
            then
                ACTION="$1"
            else
                echo "Too many actions specified. Please give at most 1."
                usage
            fi
            shift
            ;;
        *)
            echo "Unknown argument $key"
            usage
            ;;
    esac
done


CFC_DIR="/usr/local/bin"
RULE_DIR="/etc/polkit-1/rules.d"

if polkit_supports_standalonerule; then
    RULE_IN="${DIR}/../${POLKIT_DIR}/10-$RULE_BASE.rules"
else
    RULE_IN="${RULE_IN}.legacy"
    ACTION_IN="${DIR}/../${POLKIT_DIR}/${ACTION_BASE}.policy.in"
fi
TOOL_IN="${DIR}/$CFC_BASE"

TOOL_OUT="${CFC_DIR}/${CFC_BASE}-${TOOL_USER}"
RULE_OUT="${RULE_DIR}/10-${RULE_BASE}-${TOOL_USER}.rules"
ACTION_ID="${RULE_BASE}.${TOOL_USER}"
ACTION_OUT="/usr/share/polkit-1/actions/${ACTION_ID}.policy"

function print_policy_xml() {
    sed -e "s:{{PATH}}:${TOOL_OUT}:g" \
        -e "s:{{ACTION_BASE}}:${ACTION_BASE}:g" \
        -e "s:{{ACTION_ID}}:${ACTION_ID}:g" "${ACTION_IN}"
}

function print_rules_javascript() {
    if [[ "$RULE_IN" == *.legacy ]]; then
        sed -e "s:{{RULE_BASE}}:${RULE_BASE}:g" "${RULE_IN}"
    else
        local tool_out=${TOOL_OUT}
        if polkit_action_lookup_program_gets_realpath; then
                tool_out="$(realpath "${TOOL_OUT}")"
        fi
        sed -e "s:{{TOOL_OUT}}:${tool_out}:g" \
            -e "s:{{TOOL_USER}}:${TOOL_USER}:g" "${RULE_IN}"
    fi

}

if [ "$ACTION" = "supported" ]
then
    check_support
fi


if [ "$ACTION" = "check" ]
then
    require_root
    NOT_FOUND=0
    NEEDS_UPDATE=0
    function check_installed() {
	if [ -f "$1" ]; then
	    echo -n "Found: $1"
	    if cmp --silent "$1" "$2"; then
		echo " (OK)"
	    else
		echo " (needs update)"
		NEEDS_UPDATE=1
	    fi
	else
	    echo "Not found: $1"
	    NOT_FOUND=1
	fi
    }
    check_installed "${TOOL_OUT}" "${TOOL_IN}"
    check_installed "${RULE_OUT}" <(print_rules_javascript)
    if [ "$NEEDS_UPDATE" -gt 0 ]; then
	echo "Your $EXTENSION_NAME installation needs updating!"
	exit ${EXIT_NEEDS_UPDATE}
    fi
    if [ "$NOT_FOUND" -gt 0 ]; then
	echo "Not installed!"
	exit ${EXIT_NOT_INSTALLED}
    fi

    echo "OK"
    exit ${EXIT_SUCCESS}
fi

TOOL_NAME=$(basename ${TOOL_OUT})

if [ "$ACTION" = "install" ]
then
    require_root
    echo -n "$(gtxt 'Installing') ${TOOL_NAME} $(gtxt 'tool')... "
    mkdir -p "${CFC_DIR}"
    install "${TOOL_IN}" "${TOOL_OUT}" || fail
    success

    if [ ! -z "$ACTION_IN" ];then
        echo "$(gtxt 'Using legacy policykit install')..."
        echo -n "$(gtxt 'Installing') $(gtxt 'policykit action')..."
        (print_policy_xml > "${ACTION_OUT}" 2>/dev/null && chmod 0644 "${ACTION_OUT}") || fail
        success
    fi

    echo -n "$(gtxt 'Installing') $(gtxt 'policykit rule')..."
    mkdir -p "${RULE_DIR}"
    (print_rules_javascript > "${RULE_OUT}" 2>/dev/null && chmod 0644 "${RULE_OUT}")  || fail
    success

    exit ${EXIT_SUCCESS}
fi

if [ "$ACTION" = "update" ]
then
    require_root
    "${BASH_SOURCE[0]}" --tool-user "${TOOL_USER}" uninstall || exit $?
    "${BASH_SOURCE[0]}" --tool-user "${TOOL_USER}" install || exit $?

    exit ${EXIT_SUCCESS}
fi

if [ "$ACTION" = "uninstall" ]
then
    require_root
    LEG_CFG_OUT="/usr/bin/shutdowntimerctl-$TOOL_USER"
    if [ -f "$LEG_CFG_OUT" ]
    then
        # remove legacy "tool" install
        echo -n "$(gtxt 'Uninstalling') $(gtxt 'tool')..."
        rm "${LEG_CFG_OUT}" || fail " - $(gtxt 'cannot remove') ${LEG_CFG_OUT}" && success
    fi

    if [ -f "$ACTION_OUT" ]
    then
        # remove legacy "policykit action" install
        echo -n "$(gtxt 'Uninstalling') $(gtxt 'policykit action')..."
        rm "${ACTION_OUT}" || fail " - $(gtxt 'cannot remove') ${ACTION_OUT}" && success
    fi
    LEG_RULE_OUT="/usr/share/polkit-1/rules.d/10-dem.shutdowntimer.settimers.rules"
    if [ -f "$LEG_RULE_OUT" ]
    then
        # remove legacy "policykit action" install
        echo -n "$(gtxt 'Uninstalling') $(gtxt 'policykit rule')..."
        rm "${LEG_RULE_OUT}" || fail " - $(gtxt 'cannot remove') ${LEG_RULE_OUT}" && success
    fi

    function uninstallFile() {
        echo -n "$(gtxt 'Uninstalling') $2... "
        if [ -f "$1" ]
        then
            rm "$1" || fail " - $(gtxt 'cannot remove') $1" && success
        else
            echo "$2 $(gtxt 'not installed at') $1"
        fi
    }

    uninstallFile "${TOOL_OUT}" "${TOOL_NAME} $(gtxt 'tool')"
    uninstallFile "${RULE_OUT}" "$(gtxt 'policykit rule')"

    exit ${EXIT_SUCCESS}
fi

echo "Unknown parameter."
usage