aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/run_onchange_after_deploy-tb-eer.sh.tmpl
blob: 27f1fef42839ac47db1ac3fda8dab33aff2bf77a (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
#!/bin/sh
# Bridge the External Editor Revived native messaging host into the
# org.mozilla.thunderbird flatpak. The host binary stays installed via
# pacman (`external-editor-revived`); we relocate the manifest into the
# sandbox and replace its binary path with a wrapper that re-enters the
# host via flatpak-spawn.
#
# Idempotent. Re-runs on script changes or whenever the host-side manifest
# content changes.
#
# host manifest hash: {{ output "sh" "-c" "for p in $HOME/.nix-profile/lib/mozilla/native-messaging-hosts/external_editor_revived.json /usr/lib/mozilla/native-messaging-hosts/external_editor_revived.json /usr/lib/thunderbird/native-messaging-hosts/external_editor_revived.json; do [ -f \"$p\" ] && sha256sum \"$p\" && break; done; true" | sha256sum }}
set -eu

TB_APP=org.mozilla.thunderbird
MANIFEST_NAME=external_editor_revived.json
WRAPPER_NAME=external_editor_revived.sh

# Locate the EER binary. nixpkgs is the preferred source (see nix/host.nix)
# because it has no thunderbird dep; the AUR package is the fallback for
# hosts where nix isn't set up yet.
HOST_BINARY=
for candidate in \
  "$HOME/.nix-profile/bin/external-editor-revived" \
  /usr/lib/external-editor-revived/external-editor-revived \
  /usr/bin/external-editor-revived; do
  if [ -x "$candidate" ]; then
    HOST_BINARY=$candidate
    break
  fi
done

flatpak info --user "$TB_APP" >/dev/null 2>&1 || exit 0

# Locate the host-side manifest. Different packagings (nix, AUR variants)
# use different install dirs; check the common ones.
HOST_MANIFEST=
for candidate in \
  "$HOME/.nix-profile/lib/mozilla/native-messaging-hosts/$MANIFEST_NAME" \
  /usr/lib/mozilla/native-messaging-hosts/$MANIFEST_NAME \
  /usr/lib/thunderbird/native-messaging-hosts/$MANIFEST_NAME \
  /usr/lib64/mozilla/native-messaging-hosts/$MANIFEST_NAME; do
  if [ -f "$candidate" ]; then
    HOST_MANIFEST=$candidate
    break
  fi
done
[ -n "$HOST_MANIFEST" ] || {
  echo "tb-eer: external-editor-revived manifest not found on host; install via nix (host.nix) or skip TB native editor." >&2
  exit 0
}

[ -n "$HOST_BINARY" ] || {
  echo "tb-eer: external-editor-revived binary not found on host; install via nix (host.nix) or skip TB native editor." >&2
  exit 0
}

# Allow flatpak-spawn --host from inside the sandbox.
flatpak override --user --talk-name=org.freedesktop.Flatpak "$TB_APP"

# In-sandbox path the manifest will reference. TB looks for user manifests
# at $HOME/.mozilla/native-messaging-hosts/ from inside its sandbox; from
# outside that maps to ~/.var/app/$TB_APP/.mozilla/native-messaging-hosts/.
SANDBOX_NMH_DIR="$HOME/.mozilla/native-messaging-hosts"
HOST_NMH_DIR="$HOME/.var/app/$TB_APP/.mozilla/native-messaging-hosts"
mkdir -p "$HOST_NMH_DIR"

# Wrapper that re-enters the host to invoke the real binary.
WRAPPER_HOST_PATH="$HOST_NMH_DIR/$WRAPPER_NAME"
WRAPPER_SANDBOX_PATH="$SANDBOX_NMH_DIR/$WRAPPER_NAME"
cat >"$WRAPPER_HOST_PATH" <<EOF
#!/bin/sh
exec flatpak-spawn --host "$HOST_BINARY" "\$@"
EOF
chmod +x "$WRAPPER_HOST_PATH"

# Rewrite the manifest's "path" field to point at the wrapper as seen from
# inside the sandbox.
TARGET_MANIFEST="$HOST_NMH_DIR/$MANIFEST_NAME"
if command -v jq >/dev/null 2>&1; then
  jq --arg p "$WRAPPER_SANDBOX_PATH" '.path = $p' "$HOST_MANIFEST" >"$TARGET_MANIFEST.tmp"
  mv "$TARGET_MANIFEST.tmp" "$TARGET_MANIFEST"
else
  # Fallback: simple sed on the "path": "..." line. Brittle if the file
  # ever becomes minified or contains escaped quotes — jq is preferred.
  escaped=$(printf '%s' "$WRAPPER_SANDBOX_PATH" | sed 's/[\/&]/\\&/g')
  sed -E "s|(\"path\"[[:space:]]*:[[:space:]]*\")[^\"]*(\")|\1$escaped\2|" \
    "$HOST_MANIFEST" >"$TARGET_MANIFEST.tmp"
  mv "$TARGET_MANIFEST.tmp" "$TARGET_MANIFEST"
fi

echo "tb-eer: External Editor Revived bridged into $TB_APP."