-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·88 lines (72 loc) · 2.47 KB
/
install.sh
File metadata and controls
executable file
·88 lines (72 loc) · 2.47 KB
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
#!/usr/bin/env bash
set -euo pipefail
HELPER_NAME="thinkpad-red-led-helper"
INSTALL_PATH="/usr/local/bin/${HELPER_NAME}"
SUDOERS_FILE="/etc/sudoers.d/thinkpad-red-led"
SERVICE_NAME="thinkpad-red-led-restore.service"
SERVICE_INSTALL_PATH="/etc/systemd/system/${SERVICE_NAME}"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
candidates=(
"${script_dir}/tools/${HELPER_NAME}"
"${HOME}/.local/share/gnome-shell/extensions/thinkpad-red-led@juanmagd.dev/tools/${HELPER_NAME}"
"/usr/share/gnome-shell/extensions/thinkpad-red-led@juanmagd.dev/tools/${HELPER_NAME}"
)
helper_src=""
for path in "${candidates[@]}"; do
if [[ -f "${path}" ]]; then
helper_src="${path}"
break
fi
done
if [[ -z "${helper_src}" ]]; then
echo "Helper not found. Looked in:" >&2
printf ' %s\n' "${candidates[@]}" >&2
exit 1
fi
service_candidates=(
"${script_dir}/tools/${SERVICE_NAME}"
"${HOME}/.local/share/gnome-shell/extensions/thinkpad-red-led@juanmagd.dev/tools/${SERVICE_NAME}"
"/usr/share/gnome-shell/extensions/thinkpad-red-led@juanmagd.dev/tools/${SERVICE_NAME}"
)
service_src=""
for path in "${service_candidates[@]}"; do
if [[ -f "${path}" ]]; then
service_src="${path}"
break
fi
done
if [[ -z "${service_src}" ]]; then
echo "Service file not found. Looked in:" >&2
printf ' %s\n' "${service_candidates[@]}" >&2
exit 1
fi
if ! command -v sudo >/dev/null 2>&1; then
echo "sudo not found. Install sudo or run this as root." >&2
exit 1
fi
target_user="${SUDO_USER:-${USER:-$(id -un)}}"
if [[ -z "${target_user}" ]]; then
echo "Could not determine target user." >&2
exit 1
fi
if [[ ! "${target_user}" =~ ^[a-z_][a-z0-9._-]*$ ]]; then
echo "Refusing to write sudoers for unexpected username: ${target_user}" >&2
exit 1
fi
echo "Installing helper from ${helper_src} to ${INSTALL_PATH}"
sudo install -o root -g root -m 0755 "${helper_src}" "${INSTALL_PATH}"
sudo tee "${SUDOERS_FILE}" >/dev/null <<EOF
${target_user} ALL=(root) NOPASSWD: ${INSTALL_PATH}
EOF
sudo chmod 0440 "${SUDOERS_FILE}"
sudo visudo -c -f "${SUDOERS_FILE}" >/dev/null
if command -v systemctl >/dev/null 2>&1; then
echo "Installing systemd service from ${service_src} to ${SERVICE_INSTALL_PATH}"
sudo install -o root -g root -m 0644 "${service_src}" "${SERVICE_INSTALL_PATH}"
sudo systemctl daemon-reload
sudo systemctl enable "${SERVICE_NAME}"
else
echo "systemctl not found; skipping systemd service install." >&2
fi
echo "Done."
echo "Restart GNOME Shell or disable/enable the extension."