summaryrefslogtreecommitdiffstats
path: root/core/modules/system-check/data/opt/openslx/scripts/systemd-system_check
blob: d03a8e525e2d1f51df5d6066cf63699d2227808f (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
#!/bin/bash
#
# Kind of a generic message generation thing. Hooks in /opt/openslx/system-check/hooks.d
# are passed the location of the lightdm warning file on execution. They can either
# ignore it (e.g. for remote logging purposes) or just append tags to that file.
# The tags are defined in /opt/openslx/system-check/tags and will be replaced
# by their localized version automatically.

. /opt/openslx/bin/slx-tools

declare -rg root_dir="/opt/openslx/system-check"
generate_messages() {
	local lang f hook_dir lang_dir dm_warning_tmp dm_warning_file
	hook_dir="${root_dir}/hooks.d"
	lang_dir=

	# determine lang. Ignore current environment to make sure we use the system setting
	for f in "/etc/default/locale" "/etc/environment"; do
		lang="$( unset LANG; . "$f" &> /dev/null; [ -n "$LANG" ] && echo "${LANG:0:2}" || echo "${LANGUAGE}" )"
		if [ -d "${root_dir}/lang/${lang}" ]; then
			lang_dir="${root_dir}/lang/${lang}"
			break
		fi
	done
	[ -z "$lang_dir" ] && lang_dir="${root_dir}/lang/en"

	# Determine which file to put final messages into
	dm_warning_file="$( \
		awk -F'=' '$1 == "greeter-message-file" {print $2}' \
		/etc/lightdm/qt-lightdm-greeter.conf /etc/lightdm/qt-lightdm-greeter.conf.d/* \
		| tail -n 1)"
	[ -z "$dm_warning_file" ] && dm_warning_file="/run/hw-warnings.log"
	dm_warning_tmp="$(mktemp)"

	# Run hooks that will generate all the warnings
	if [ ! -d "$hook_dir" ]; then
		echo "Missing '$hook_dir' - dev failure?"
		return 1
	fi
	for file in "$hook_dir"/*; do
		[ -s "$file" ] || continue
		[ -x "$file" ] || continue
		"$file" &
	done > "$dm_warning_tmp"
	wait
	# post-process, dm_warning_tmp contains just tags now
	# check in /opt/openslx/messages/{lang,tags}
	local blacklist="${root_dir}/blacklist"
	local color do_contact
	while read -r tag rest; do
		old_IFS="$IFS"
		IFS='|' tag_with_params=($tag)
		IFS="$old_IFS"
		# blacklisted?
		grep -qFx "${tag_with_params[0]}" "$blacklist" && continue
		# "meta" info?
		[ -s "${root_dir}/tags/${tag_with_params[0]}" ] && \
			. "${root_dir}/tags/${tag_with_params[0]}"
		print_tag "${color:-000000}" "${tag_with_params[@]}" \
			>> "$dm_warning_file"
		# contact?
		[ -n "$contact" ] && do_contact="yes"
	done < "$dm_warning_tmp"

	# add contact footer if needed
	local support_tag="slx-contact-support"
	if [ -n "$do_contact" ]; then
		[ -s "${root_dir}/tags/${support_tag}" ] && \
			. "${root_dir}/tags/${support_tag}"
		print_tag "${color:-000000}" "${support_tag}" \
			>> "$dm_warning_file"
	fi
}

# print_tag <prefix> <file> <vars...>
print_tag() {
	[ "$#" -ge 2 ] || return 1
	regex_imatch "$1" '^[0-9a-f]{6}$' || return 1
	local prefix="$1"
	shift
	# since we fully pass the parsed tag as array, the first param
	# is the tag name, the rest are the variables to substitute %i% with.
	local tag="$1"
	shift
	# get localized message for this tag
	local file="${lang_dir}/${tag}"
	if [ ! -f "$file" ]; then
		# fallback
		echo "000000" "Missing translation for '$tag' in '$lang_dir'."
		return 1
	fi
	while read -r line; do
		line_cur="$line"
		# go over vars and replace %i% with them
		local count=1
		while [ "$#" -ne 0 ]; do
			line_cur="${line_cur//%${count}%/${1}}"
			shift
			(( count++ ))
		done
		echo "$prefix" "$line_cur"
	done < "$file"
}

generate_messages
exit 0