~mil/sxmo-utils

ref: dbc3b1ab509aab5f1db72c5cece2ee2a6eb521e8 sxmo-utils/scripts/core/sxmo_open.sh -rwxr-xr-x 7.9 KiB
dbc3b1abStacy Harper Allow the sendnewtextmenu to be used with a number as arg 1 year, 4 months ago
                                                                                
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

MIMEAPPS="${XDG_CONFIG_HOME:-$HOME/.config}/mimeapps.list"
DESKTOPS_CACHED_MIMEAPPS="${XDG_CONFIG_HOME:-$HOME/.config}/desktops.mimeapps.list"
DESKTOP_DIRS="/usr/share/sxmo/applications/|/usr/share/applications/|/usr/local/share/applications/|${XDG_DATA_HOME:-$HOME/.local/share}/applications/"
attached=
debug=
TERMCMD="${TERMCMD:-st -e}"

# This will convert a mimeapps.list to a parsable mapping
# Lines with multiple mimetype will be splitted
# The mapping format is something as
# added;image/jpeg;bar.desktop
# added;image/jpeg;baz.desktop
# added;video/H264;bar.desktop
# default;image/jpeg;foo.desktop
# removed;video/H264;baz.desktop
mimeapps_to_desktop_mapping() {
	[ ! -f "$1" ] && return
	awk '
		/^\[Default Applications\]$/ { kind = "default" }
		/^\[Added Associations\]$/ { kind = "added" }
		/^\[Removed Associations\]$/ { kind = "removed" }
		/=/ {
			split($0, mime_desktops, "=")
			split(mime_desktops[2], desktops, ";")

			for ( key in desktops ) {
				print kind ";" mime_desktops[1] ";" desktops[key]
			}
		}
	' "$1"
}

# This will generate the added mapping from all destkop entries in a dir
get_mimeapps_entries_from_desktop_dir() {
	for desktop_path in "$1"/*.desktop; do
		grep --include "*.desktop" '^MimeType=' "$desktop_path" \
		| tr ';' '\n' \
		| cut -d= -f2 \
		| xargs -i{} printf "%s=%s\n" "{}" "$(basename "$desktop_path")"
	done
}

# Build and save the desktop mimeapps mapping if necessary
prepare_desktop_mimeapps_mapping_cache() {
	last_desktop_modif_date="$(printf %s "$DESKTOP_DIRS" | tr '|' '\n' | while read -r desktop_dir; do
		for desktop_file in "$desktop_dir"/*.desktop; do
			stat -c %Y "$desktop_file"
		done
	done | sort -r | head -n1)"

	if [ -r "$DESKTOPS_CACHED_MIMEAPPS" ]; then
		desktop_cache_modif_date="$(stat -c %Y "$DESKTOPS_CACHED_MIMEAPPS")"
		if [ "$last_desktop_modif_date" -gt "$desktop_cache_modif_date" ]; then
			rm "$DESKTOPS_CACHED_MIMEAPPS"
		fi
	fi

	if [ ! -r "$DESKTOPS_CACHED_MIMEAPPS" ]; then
		IFS='|'
		echo "[Added Associations]" > "$DESKTOPS_CACHED_MIMEAPPS"
		for desktop_dir in $DESKTOP_DIRS; do
			get_mimeapps_entries_from_desktop_dir "$desktop_dir"
		done >> "$DESKTOPS_CACHED_MIMEAPPS"
	fi
}

find_desktop_path() {
	desktop_name="$1"

	IFS='|'
	for desktop_dir in $DESKTOP_DIRS; do
		if [ -r "$desktop_dir/$desktop_name" ]; then
			realpath "$desktop_dir/$desktop_name"
			return
		fi
	done
}

# This will take the full maping and simplify it which means
# * strip added entries with removed ones
# * put default entries first
# * remove duplicates lines
# following this format
# image/jpeg;foo.desktop
# image/jpeg;bar.desktop
# image/jpeg;baz.desktop
simplify_mapping() {
	awk '
		function contains(array, value) {
			for (key in array) {
				if (array[key] == value)
					return 1
			}
			return 0
		}

		/^$/ {next}

		/^default;/ { kind = "default" }
		/^added;/ { kind = "added" }
		/^removed;/ { kind = "removed" }

		{ gsub(kind ";", "", $0) }

		kind == "default" { default_entries[NR]=$0 }
		kind == "added" { added_entries[NR]=$0 }
		kind == "removed" { removed_entries[NR]=$0 }

		END {
			for (key in default_entries) {
				print default_entries[key]
			}
			for (key in added_entries) {
				entry = added_entries[key]
				if (contains(removed_entries, entry)) { continue }
				if (contains(default_entries, entry)) { continue }
				print entry
			}
		}
	' /dev/stdin
}

get_mimeapps_mapping() {
	prepare_desktop_mimeapps_mapping_cache

	printf '%s\n%s' \
		"$(mimeapps_to_desktop_mapping "$MIMEAPPS")" \
		"$(mimeapps_to_desktop_mapping "$DESKTOPS_CACHED_MIMEAPPS")" \
		| simplify_mapping
}

filter_matching_desktops() {
	mime_type="$1"

	grep "^$mime_type;" /dev/stdin \
		| cut -d";" -f2
}

curl_mime_type() {
	curl --head -fsw '%{content_type}' "$1" | tail -n1 | sed 's|;.*||'
}

extension_mime_type() {
	[ ! -r "/etc/mime.types" ] && return # require mailcap

	ext="${1##*.}"
	grep "\t.*$ext" /etc/mime.types | awk '{print $1}'
}

get_mime_type() {
	# Seems like an x-scheme
	if echo "$1" | grep -q '^.\+:'; then
		if echo "$1" | grep -q '^https\?:'; then
			mime_type="$(curl_mime_type "$1")"
		fi

		[ -z "$mime_type" ] && mime_type="x-scheme-handler/$(echo "$1" | grep -o '^\w\+')"
	else
		# is readable
		if [ -r "$1" ]; then
			mime_type="$(file -b --mime-type "$1")"
		else
			echo "This file does not exists?" > /dev/stderr
			exit 1
		fi
	fi

	# we try to be more precise then
	if [ "application/octet-stream" = "$mime_type" ]; then
		new_mime_type="$(extension_mime_type "$1")"
		[ -n "$new_mime_type" ] && mime_type="$new_mime_type"
	elif [ "inode/symlink" = "$mime_type" ]; then
		real_path="$(realpath "$1")"
		mime_type="$(get_mime_type "$real_path")"
	fi

	echo "$mime_type"
}

execute() {
	if [ -n "$debug" ]; then
		echo "The final command to execute is:" > /dev/stderr
		echo "$@" > /dev/stderr
		exit 0
	fi

	eval "$@"
}

fetch_file() {
	if [ -n "$debug" ]; then
		echo "The file would be downloaded with '$*'" > /dev/stderr
		return
	fi

	eval "$@"
}

build_command() {
	exec="$1"
	shift

	IFS="
"
	file_paths="$*"
	unset IFS

	if echo "$exec" | grep -q '%f\|%F'; then # We must retrieve distant files
		file_paths="$(
			echo "$file_paths" | while read -r file_path; do
				local_file_path="$(mktemp --suffix=".$(basename "$file_path")")"
				if echo "$file_path" | grep -q '^https\?://'; then
					fetch_file curl -so "$local_file_path" "$file_path"
				else
					echo "$file_path"
					continue
				fi
				echo "$local_file_path"
			done
		)"
		exec="$(echo "$exec" | sed -e 's|%f|%u|' -e 's|%F|%U|')"
	fi

	file_paths="$(echo "$file_paths" | awk '{print "\"" $0 "\""}')"

	command=
	if echo "$exec" | grep -q '%u'; then # handle url file one by one
		exec="$(echo "$exec" | sed 's|%u|"%s"|')"
		if [ "true" = "$terminal" ]; then
			command="$(echo "$file_paths" | xargs printf "$TERMCMD $exec & " | sed 's| $||')"
		else
			command="$(echo "$file_paths" | xargs printf "$exec & " | sed -e 's|^ ||' -e 's| $||')"
		fi
	elif echo "$exec" | grep -q '%U'; then # handle url file grouped
		exec_before="$(echo "$exec" | awk -F' %U' '{print $1}')"
		exec_after="$(echo "$exec" | awk -F'%U ' '{$1=""; print $0}')"
		command="$exec_before $(echo "$file_paths" | xargs printf '"%s" ')${exec_after:+ $exec_after }&"
		if [ "true" = "$terminal" ]; then
			command="$TERMCMD $command"
		fi
	else
		command="$exec $(echo "$file_paths" | xargs printf '"%s" ')&"
		if [ "true" = "$terminal" ]; then
			command="$TERMCMD $command"
		fi
	fi

	echo "$command"
}

run_desktop() {
	desktop="$1"
	shift

	desktop_path="$(find_desktop_path "$desktop")"
	if [ -z "$desktop_path" ]; then
		echo "We can't find the desktop file \"$desktop\"" > /dev/stderr
		return
	fi

	desktop_content="$(cat "$desktop_path")"
	exec="$(echo "$desktop_content" | grep '^Exec=' | sed 's|^Exec=||' | head -n1)"
	terminal="$(echo "$desktop_content" | grep '^Terminal=' | cut -d= -f2)"
	[ -z "$terminal" ] && terminal="false"

	command="$(build_command "$exec" "$@")"

	if [ -n "$attached" ]; then
		command="$(echo "$command" | sed 's| &|;|g')"
	fi

	if [ -n "$debug" ]; then
		echo "Exec is \"$exec\"" > /dev/stderr
		echo "Terminal is \"$terminal\"" > /dev/stderr
		echo "Command is \"$command\"" > /dev/stderr
		exit 0
	fi

	eval "$command"

	exit 0
}

run() {
	mime_type="$(get_mime_type "$1")"

	if [ -z "$mime_type" ]; then
		echo "We failed to find the mime_type for \"$1\"" > /dev/stderr
		exit 1
	fi

	desktops="$(get_mimeapps_mapping | filter_matching_desktops "$mime_type")"

	if [ -n "$debug" ]; then
		echo "The mime type is \"$mime_type\"" > /dev/stderr
		echo "The matching desktops are:" > /dev/stderr
		echo "$desktops" > /dev/stderr
	fi

	if [ -z "$desktops" ]; then
		echo "There is no matching desktop to open \"$mime_type\"" > /dev/stderr
		exit 1
	fi

	echo "$desktops" | while read -r desktop; do
		run_desktop "$desktop" "$@"
	done
}

if [ "-d" = "$1" ]; then
	debug="1"
	shift
fi

if [ "-a" = "$1" ]; then
	attached="1"
	shift
fi

if [ $# -gt 0 ]; then
	run "$@"
fi