-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubspace-sync
More file actions
executable file
·154 lines (131 loc) · 4.83 KB
/
Copy pathsubspace-sync
File metadata and controls
executable file
·154 lines (131 loc) · 4.83 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
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
#!/usr/bin/env bash
set -euo pipefail
SUBSPACE_DIR="/subspace"
BIN_DIR="$SUBSPACE_DIR"
if [ "$EUID" -ne 0 ]; then
echo "Synchronizing subspaces requires superuser privileges."
exit 1
fi
if [ -n "${SUDO_USER:-}" ]; then
REAL_HOME="$(getent passwd "$SUDO_USER" | cut -d: -f6)"
else
REAL_HOME="${HOME:-/root}"
fi
DESKTOP_DIR="$REAL_HOME/.local/share/applications"
MANIFEST="$SUBSPACE_DIR/.manifest"
mkdir -p "$DESKTOP_DIR" "$(dirname "$MANIFEST")"
if [ -f "$MANIFEST" ]; then
while IFS= read -r file; do rm -f "$file"; done < "$MANIFEST"
fi
rm -f "$BIN_DIR"/subspace-*-*
find "$DESKTOP_DIR" -name 'subspace-*.desktop' -delete 2>/dev/null || true
declare -A plain_claimed
find_binary() {
local subspace="$1" name="$2"
for dir in "usr/bin" "usr/local/bin" "usr/sbin" "sbin"; do
local path="$SUBSPACE_DIR/$subspace/$dir/$name"
if [ -x "$path" ]; then
echo "$path"
return 0
fi
done
return 1
}
for subspace_dir in "$SUBSPACE_DIR"/*/; do
[ -d "$subspace_dir" ] || continue
subspace=$(basename "$subspace_dir")
[[ "$subspace" == .* ]] && continue
[ ! -d "$subspace_dir/usr" ] && continue
echo "$subspace"
declare -A binaries
for bindir in "usr/bin" "usr/local/bin" "usr/sbin" "sbin"; do
[ -d "$subspace_dir/$bindir" ] || continue
while IFS= read -r -d '' binary; do
app=$(basename "$binary")
binaries["$app"]="$binary"
done < <(find "$subspace_dir/$bindir" -maxdepth 1 -type f -executable -print0 2>/dev/null)
done
if [ -d "$subspace_dir/usr/share/applications" ]; then
while IFS= read -r -d '' desktop; do
appname=$(basename "$desktop" .desktop)
exec_line=$(grep -m1 '^Exec=' "$desktop" 2>/dev/null || true)
[ -z "$exec_line" ] && continue
exec_cmd=$(echo "$exec_line" | sed 's/^Exec=//' | awk '{print $1}')
exec_bin=$(basename "$exec_cmd")
[ -z "$exec_bin" ] && continue
bin_path=""
if [ -n "${binaries[$exec_bin]:-}" ]; then
bin_path="${binaries[$exec_bin]}"
else
bin_path=$(find_binary "$subspace" "$exec_bin") || true
fi
[ -z "$bin_path" ] && continue
container_path="${bin_path#$subspace_dir}"
wrapper_name="subspace-${subspace}-${exec_bin}"
wrapper_path="$BIN_DIR/$wrapper_name"
cat > "$wrapper_path" << WRAPPER
#!/bin/bash
for term in konsole xterm gnome-terminal kitty alacritty; do
if type -p "\$term" &>/dev/null; then
exec "\$term" -e bash -c '
if type -p doas &>/dev/null; then
exec doas /subspace/subspace-run $subspace $container_path "\$@"
elif type -p sudo &>/dev/null; then
exec sudo /subspace/subspace-run $subspace $container_path "\$@"
else
exec /subspace/subspace-run $subspace $container_path "\$@"
fi
' _ "\$@"
fi
done
echo "No terminal emulator found" >&2
exit 1
WRAPPER
chmod +x "$wrapper_path"
echo " wrapper: $wrapper_name"
desktop_target="$DESKTOP_DIR/subspace-${subspace}-${appname}.desktop"
sed \
-e "s|^Exec=[^ ]*|Exec=$wrapper_path|" \
-e "/^TryExec=/d" \
-e "/^Name\[/! s/^Name=\(.*\)/Name=\1 ($subspace)/" \
"$desktop" > "$desktop_target"
echo "X-Subspace=$subspace" >> "$desktop_target"
echo " desktop: subspace-${subspace}-${appname}.desktop"
if [ -z "${plain_claimed[$exec_bin]:-}" ]; then
if ! command -v "$exec_bin" &>/dev/null; then
plain_claimed["$exec_bin"]="$subspace:$container_path"
fi
fi
done < <(find "$subspace_dir/usr/share/applications" -maxdepth 1 -name '*.desktop' -type f -print0 2>/dev/null)
fi
done
echo "plain wrappers"
: > "$MANIFEST"
for bin_name in "${!plain_claimed[@]}"; do
IFS=':' read -r subspace container_path <<< "${plain_claimed[$bin_name]}"
target="$BIN_DIR/$bin_name"
if [ ! -f "$target" ]; then
cat > "$target" << WRAPPER
#!/bin/bash
for term in konsole xterm gnome-terminal kitty alacritty; do
if type -p "\$term" &>/dev/null; then
exec "\$term" -e bash -c '
if type -p doas &>/dev/null; then
exec doas /subspace/subspace-run $subspace $container_path "\$@"
elif type -p sudo &>/dev/null; then
exec sudo /subspace/subspace-run $subspace $container_path "\$@"
else
exec /subspace/subspace-run $subspace $container_path "\$@"
fi
' _ "\$@"
fi
done
echo "No terminal emulator found" >&2
exit 1
WRAPPER
chmod +x "$target"
echo "$target" >> "$MANIFEST"
echo " $bin_name -> $subspace"
fi
done
echo "Done."