BlueGit

ict rusty-teams

Files

rusty-teams / packaging / linux / AppRun

AppRun

feat(packaging): Linux AppImage, deb, rpm and Flatpak
metis Samuel David Merlin Kemper · pushed by metis · 2 weeks ago
228f36c5 History
AppRun 8.7 KiB Download Raw
1#!/bin/sh
2# Rusty Teams AppImage entry point (packaging/linux/AppRun; installed by package.sh).
3#
4# On Wayland the compositor resolves a window's dock icon by taking the window's
5# app_id (rusty-teams) and looking up an installed .desktop file. An AppImage
6# installs nothing, so every AppImage run would otherwise show the generic
7# "Wayland" icon. Best-effort desktop integration: on first run (and whenever the
8# AppImage's path changes) install the .desktop entry and the hicolor icons into
9# $XDG_DATA_HOME, then launch. Every failure here must leave the launch untouched.
10# Opt out with RUSTY_TEAMS_NO_DESKTOP_INTEGRATION=1.
11#
12# The app's own "install to ~/.local/share/rusty-teams" offer writes the same
13# rusty-teams.desktop; an entry whose Exec is not this AppImage counts as a user
14# customization below and keeps its Exec. The app skips that offer when it runs
15# from an AppImage (linux_desktop::is_packaged).
16
17set -u
18
19APPDIR="$(dirname "$(readlink -f "$0")")"
20DESK_ID=rusty-teams
21
22# A damaged snapshot is not evidence that an absent user key was newly added.
23# Treat it like a legacy installation and preserve the existing launcher.
24valid_baseline() {
25 [ -f "$1" ] && [ ! -L "$1" ] && [ -s "$1" ] || return 1
26 awk '
27 /^\[/ { group = $0; if (group == "[Desktop Entry]") sections++; next }
28 /^[ \t]*($|#)/ { next }
29 /^[^= \t][^=]*=/ {
30 name = $0; sub(/=.*/, "", name)
31 k = group SUBSEP name
32 if (seen[k]++) bad = 1
33 if (group == "[Desktop Entry]") {
34 if ($0 == "Type=Application") type = 1
35 if ($0 ~ /^Name=.+$/) title = 1
36 if ($0 ~ /^Exec=".+"$/) executable = 1
37 }
38 next
39 }
40 { bad = 1 }
41 END { exit !(sections == 1 && type && title && executable && !bad) }
42 ' "$1"
43}
44
45integrate() {
46 case "${RUSTY_TEAMS_NO_DESKTOP_INTEGRATION:-}" in
47 1 | true | yes) return 0 ;;
48 esac
49 # The AppImage runtime exports $APPIMAGE; without it (an extracted AppDir) there
50 # is no path to register and nothing to do.
51 appimage="${APPIMAGE:-}"
52 [ -n "$appimage" ] && [ -f "$appimage" ] || return 0
53 appimage="$(readlink -f "$appimage")" || return 0
54 # Exec= is parsed by the desktop: a path holding a quote, backquote, dollar, backslash,
55 # percent or a line break would need escaping (or could add lines), so skip integration.
56 case "$appimage" in
57 *[\"\`\$\\%]* | *'
58'*) return 0 ;;
59 esac
60 home="${HOME:-}"
61 [ -n "$home" ] || return 0
62 data="${XDG_DATA_HOME:-$home/.local/share}"
63 src_desktop="$APPDIR/usr/share/applications/$DESK_ID.desktop"
64 [ -f "$src_desktop" ] || return 0
65
66 # If another integrator already registered a .desktop whose Exec points at this
67 # AppImage, do not write a second launcher; remove our own if one is left over,
68 # and still install the hicolor icons below. $DESKTOPINTEGRATION is not that
69 # signal: the entry sets X-AppImage-Integrate=false, so AppImageLauncher skips the
70 # AppImage and still exports the variable on its binfmt bypass. Treating it as
71 # "already integrated" would leave the menu with no entry.
72 apps="$data/applications"
73 mkdir -p "$apps" 2>/dev/null || return 0
74 desktop="$apps/$DESK_ID.desktop"
75 baseline="$apps/.$DESK_ID.generated"
76 external=0
77 for f in "$apps"/*.desktop; do
78 if [ ! -f "$f" ] || [ "$f" = "$apps/$DESK_ID.desktop" ]; then
79 continue
80 fi
81 if grep '^Exec=' "$f" 2>/dev/null | grep -Fq "$appimage"; then
82 external=1
83 break
84 fi
85 done
86 # AM / AppMan install their own launcher, whose Exec runs a command on
87 # PATH rather than this AppImage, so the check above misses it. AM's author
88 # names the only two places it writes one: system-wide for AM, per user for
89 # AppMan and rootless installs.
90 for f in /usr/local/share/applications/rusty-teams-AM.desktop "$apps/rusty-teams-AM.desktop"; do
91 if [ -f "$f" ]; then
92 external=1
93 break
94 fi
95 done
96 if [ -L "$desktop" ] || [ -L "$baseline" ] ||
97 { [ -e "$desktop" ] && [ ! -f "$desktop" ]; } ||
98 { [ -e "$baseline" ] && [ ! -f "$baseline" ]; }; then
99 # A link (including a dangling one) or special file is not our installation.
100 # Leave both launcher and snapshot alone; icons may still be installed below.
101 :
102 elif [ "$external" = 1 ]; then
103 # Only a byte-identical, valid generated snapshot proves this is ours to
104 # remove. A customized or legacy launcher belongs to the user.
105 if [ -f "$desktop" ] && valid_baseline "$baseline" && cmp -s "$baseline" "$desktop" && [ ! -L "$desktop" ]; then
106 if rm -f "$desktop" 2>/dev/null; then
107 command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1
108 fi
109 fi
110 else
111 # Rewrite Exec to this AppImage (the binary is not on the host PATH) and drop
112 # TryExec, which would hide the entry; everything else is shipped as packaged.
113 tmp="$apps/.$DESK_ID.tmp.$$"
114 esc="$(printf '%s' "$appimage" | sed 's/[\\&|]/\\&/g')"
115 if sed -e "s|^Exec=.*|Exec=\"$esc\"|" -e '/^TryExec=/d' "$src_desktop" 2>/dev/null >"$tmp"; then
116 generated="$tmp.generated"
117 cp "$tmp" "$generated" 2>/dev/null || { rm -f "$tmp"; return 0; }
118 if [ -f "$desktop" ]; then
119 # Three-way merge: update shipped keys only if the user has not changed
120 # them. Keep custom keys, comments and deletions. The snapshot is hidden
121 # and has no .desktop suffix, so desktop menus do not discover it.
122 # For older installs without a snapshot, the current template is the
123 # conservative baseline. An unknown previous executable is left alone.
124 previous="$baseline"
125 bootstrap=0
126 if ! valid_baseline "$previous"; then
127 previous="$tmp.previous"
128 cp "$generated" "$previous" 2>/dev/null || { rm -f "$tmp" "$generated"; return 0; }
129 bootstrap=1
130 fi
131 if ! awk -v bootstrap="$bootstrap" '
132 function key(line) {
133 if (line ~ /^\[/) { group = line; return "" }
134 if (line ~ /^[^#= \t][^=]*=/) {
135 name = line; sub(/=.*/, "", name); return group SUBSEP name
136 }
137 return ""
138 }
139 FNR == 1 { group = "" }
140 FILENAME == ARGV[1] {
141 k = key($0); if (k != "") old[k] = $0
142 if (group == "[Desktop Entry]" && $0 ~ /^Exec=".*"$/)
143 old_exec = substr($0, 6)
144 next
145 }
146 FILENAME == ARGV[2] {
147 k = key($0); if (k != "") { fresh[k] = $0; order[++n] = k }
148 if (group == "[Desktop Entry]" && $0 ~ /^Exec=/)
149 new_exec = substr($0, 6)
150 next
151 }
152 function additions( i, k) {
153 for (i = 1; i <= n; i++) {
154 k = order[i]
155 if (index(k, "[Desktop Entry]" SUBSEP) == 1 && !(k in old) && !(k in seen))
156 print fresh[k]
157 }
158 }
159 {
160 if ($0 ~ /^\[/ && group == "[Desktop Entry]") additions()
161 k = key($0)
162 if (k != "") {
163 seen[k] = 1
164 if (k in old && $0 == old[k]) {
165 if (k in fresh) print fresh[k]
166 next
167 }
168 if (group == "[Desktop Entry]" && name == "Exec") {
169 # Literal substitution, never a regex or shell evaluation: keep
170 # environment wrappers and arguments around the managed path.
171 pos = index($0, old_exec)
172 prefix = substr($0, 1, pos - 1)
173 if (old_exec != "" && pos > 0 && prefix ~ /^Exec=(env[ \t]+([^ \t=]+=[^ \t]*[ \t]+)*)?$/)
174 $0 = substr($0, 1, pos - 1) new_exec substr($0, pos + length(old_exec))
175 }
176 }
177 print
178 }
179 END { if (group == "[Desktop Entry]") additions() }
180 ' "$previous" "$generated" "$desktop" >"$tmp"; then
181 rm -f "$tmp" "$generated"
182 [ "$bootstrap" = 0 ] || rm -f "$previous"
183 return 0
184 fi
185 [ "$bootstrap" = 0 ] || rm -f "$previous"
186 fi
187 if cmp -s "$tmp" "$desktop"; then
188 rm -f "$tmp"
189 else
190 if mv -f "$tmp" "$desktop" 2>/dev/null; then
191 command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1
192 else
193 rm -f "$tmp" "$generated"
194 return 0
195 fi
196 fi
197 mv -f "$generated" "$baseline" 2>/dev/null || rm -f "$generated"
198 else
199 rm -f "$tmp"
200 return 0
201 fi
202
203 fi
204
205 # Icons: per-file compare so an unchanged install costs a handful of cmp calls.
206 src_icons="$APPDIR/usr/share/icons/hicolor"
207 [ -d "$src_icons" ] || return 0
208 icons="$data/icons/hicolor"
209 (cd "$src_icons" && find . -type f) | while IFS= read -r rel; do
210 mkdir -p "$icons/$(dirname "$rel")" 2>/dev/null || continue
211 if ! cmp -s "$src_icons/$rel" "$icons/$rel"; then
212 cp "$src_icons/$rel" "$icons/$rel" 2>/dev/null || true
213 fi
214 done
215}
216
217integrate
218exec "$APPDIR/usr/bin/teams" "$@"