This commit is contained in:
Joakim
2026-10-15 19:48:43 +02:00
parent 0e0febffda
commit 2e9a9d3b36
2 changed files with 209 additions and 62 deletions

74
.gitignore vendored Normal file
View File

@@ -0,0 +1,74 @@
# Built joe lives here (build.sh clones + builds into this)
joe-patch/build/
# install.sh's venv
.venv/
__pycache__/
*.pyc
# ============================================================
# Build output
# ============================================================
joe-patch/build/
*.o
*.a
*.so
*.la
*.lo
# Autotools temporary files
autom4te.cache/
config.log
config.status
stamp-h1
# Generated build files that should NOT be committed
.deps/
*.d
# ============================================================
# Python
# ============================================================
.venv/
__pycache__/
*.py[cod]
*$py.class
# ============================================================
# Python tooling
# ============================================================
.pytest_cache/
.mypy_cache/
.ruff_cache/
.coverage
htmlcov/
# ============================================================
# Local configuration / secrets
# ============================================================
.env
.env.*
!.env.example
# ============================================================
# Editor / OS garbage
# ============================================================
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store
# ============================================================
# Runtime / local application data
# ============================================================
*.log

View File

@@ -3,6 +3,10 @@
# resolves to, installs Python dependencies into a venv, and links # resolves to, installs Python dependencies into a venv, and links
# hdc/hd-joe onto PATH. # hdc/hd-joe onto PATH.
# #
# The compiled joe binary is copied out of the source/build tree into
# ~/.local/lib/hedgedoc-joe (or PREFIX_LIB if overridden), so the
# installation does not depend on the build directory remaining present.
#
# Usage: ./install.sh [--joe-target PATH] [--bin-dir DIR] # Usage: ./install.sh [--joe-target PATH] [--bin-dir DIR]
# ./install.sh --restore [--joe-target PATH] [--bin-dir DIR] # ./install.sh --restore [--joe-target PATH] [--bin-dir DIR]
# sudo ./install.sh --joe-only [--joe-target PATH] # sudo ./install.sh --joe-only [--joe-target PATH]
@@ -10,7 +14,7 @@
# --joe-target PATH Where to install patched joe (default: wherever # --joe-target PATH Where to install patched joe (default: wherever
# 'joe' currently resolves on PATH, or # 'joe' currently resolves on PATH, or
# <bin-dir>/joe if there isn't one yet). # <bin-dir>/joe if there isn't one yet).
# --bin-dir DIR Where to link hd and hd-joe (default: ~/.local/bin). # --bin-dir DIR Where to install hdc and hd-joe (default: ~/.local/bin).
# --joe-only Only back up + install the joe binary; skip the # --joe-only Only back up + install the joe binary; skip the
# venv/pip/launcher setup entirely. Use this (with # venv/pip/launcher setup entirely. Use this (with
# sudo, if needed) when the joe target directory # sudo, if needed) when the joe target directory
@@ -18,8 +22,9 @@
# run the plain form (no sudo) separately for # run the plain form (no sudo) separately for
# everything else. # everything else.
# --restore Undo: put the original joe back from its backup, # --restore Undo: put the original joe back from its backup,
# remove the hdc/hd-joe links, disable and remove # remove the hdc/hd-joe launchers, disable and remove
# the sync service, exit. # the sync service, and remove the installed patched
# joe copy.
# --notes-dir DIR What the sync service watches (default: ~/notes, # --notes-dir DIR What the sync service watches (default: ~/notes,
# same default hdc itself uses). # same default hdc itself uses).
# --sync-interval N Seconds between polls for the sync service # --sync-interval N Seconds between polls for the sync service
@@ -34,17 +39,24 @@
# root creates them owned by root, under root's $HOME, invisible to you # root creates them owned by root, under root's $HOME, invisible to you
# afterward. If joe's install location needs root, use --joe-only for just # afterward. If joe's install location needs root, use --joe-only for just
# that part (with sudo) and run the rest as yourself. # that part (with sudo) and run the rest as yourself.
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
BUILT="$SCRIPT_DIR/joe-patch/build/joe-4.8/joe/joe" BUILT="$SCRIPT_DIR/joe-patch/build/joe-4.8/joe/joe"
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}" BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
PREFIX_LIB="${PREFIX_LIB:-$HOME/.local/lib/hedgedoc-joe}"
INSTALLED_JOE="$PREFIX_LIB/joe"
NOTES_DIR="${NOTES_DIR:-$HOME/notes}" NOTES_DIR="${NOTES_DIR:-$HOME/notes}"
SYNC_INTERVAL="${SYNC_INTERVAL:-30}" SYNC_INTERVAL="${SYNC_INTERVAL:-30}"
# Defined here, unconditionally, not inside install_python_and_launchers(): # Defined here, unconditionally, not inside install_python_and_launchers():
# install_joe() (possibly run alone, under sudo, via --joe-only, before the # install_joe() (possibly run alone, under sudo, via --joe-only, before the
# venv exists yet) needs this path too, to write it into the joe wrapper. # venv exists yet) needs this path too, to write it into the joe wrapper.
VENV_DIR="$SCRIPT_DIR/.venv" VENV_DIR="$SCRIPT_DIR/.venv"
JOE_TARGET="" JOE_TARGET=""
RESTORE=0 RESTORE=0
JOE_ONLY=0 JOE_ONLY=0
@@ -54,17 +66,42 @@ SERVICE_PATH="$HOME/.config/systemd/user/$SERVICE_NAME"
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--restore) RESTORE=1; shift ;; --restore)
--joe-only) JOE_ONLY=1; shift ;; RESTORE=1
--no-service) NO_SERVICE=1; shift ;; shift
--joe-target) JOE_TARGET="$2"; shift 2 ;; ;;
--bin-dir) BIN_DIR="$2"; shift 2 ;; --joe-only)
--notes-dir) NOTES_DIR="$2"; shift 2 ;; JOE_ONLY=1
--sync-interval) SYNC_INTERVAL="$2"; shift 2 ;; shift
;;
--no-service)
NO_SERVICE=1
shift
;;
--joe-target)
JOE_TARGET="$2"
shift 2
;;
--bin-dir)
BIN_DIR="$2"
shift 2
;;
--notes-dir)
NOTES_DIR="$2"
shift 2
;;
--sync-interval)
SYNC_INTERVAL="$2"
shift 2
;;
-h|--help) -h|--help)
sed -n '2,34p' "$0" | sed 's/^# \{0,1\}//' sed -n '2,44p' "$0" | sed 's/^# \{0,1\}//'
exit 0 ;; exit 0
*) echo "Unknown argument: $1" >&2; exit 1 ;; ;;
*)
echo "Unknown argument: $1" >&2
exit 1
;;
esac esac
done done
@@ -72,6 +109,7 @@ if [ -z "$JOE_TARGET" ]; then
JOE_TARGET="$(command -v joe 2>/dev/null || true)" JOE_TARGET="$(command -v joe 2>/dev/null || true)"
[ -z "$JOE_TARGET" ] && JOE_TARGET="$BIN_DIR/joe" [ -z "$JOE_TARGET" ] && JOE_TARGET="$BIN_DIR/joe"
fi fi
BACKUP="${JOE_TARGET}.pre-hedgedoc" BACKUP="${JOE_TARGET}.pre-hedgedoc"
install_joe() { install_joe() {
@@ -80,24 +118,32 @@ install_joe() {
exit 1 exit 1
fi fi
echo "==> Installing compiled joe to $INSTALLED_JOE"
mkdir -p "$PREFIX_LIB"
if ! cp "$BUILT" "$INSTALLED_JOE"; then
echo "Could not install $BUILT -> $INSTALLED_JOE" >&2
return 1
fi
chmod 755 "$INSTALLED_JOE"
echo "==> Installed patched joe binary: $INSTALLED_JOE"
mkdir -p "$(dirname "$JOE_TARGET")" 2>/dev/null || true mkdir -p "$(dirname "$JOE_TARGET")" 2>/dev/null || true
# Back up whatever ORIGINAL, non-hd joe was at this path -- once. If # Back up whatever ORIGINAL, non-hd joe was at this path -- once.
# what's there is already one of our own wrappers (re-running # If what's there is already one of our own wrappers, there's nothing
# install.sh), there's nothing genuine left to preserve; regenerating # genuine left to preserve.
# the wrapper below is cheap and always safe to redo. if [ -e "$JOE_TARGET" ] &&
# ! grep -q "# hd-installer-generated" "$JOE_TARGET" 2>/dev/null; then
# Checked explicitly (not left to `set -e`) because this function runs
# as `install_joe || JOE_OK=0` in the main flow -- bash disables -e for
# every command inside a function called as part of an && / || list,
# not just its last one, so a failed cp here would otherwise fall
# through silently to the next step instead of being caught.
if [ -e "$JOE_TARGET" ] && ! grep -q "# hd-installer-generated" "$JOE_TARGET" 2>/dev/null; then
if [ -e "$BACKUP" ]; then if [ -e "$BACKUP" ]; then
echo "==> Backup already exists at $BACKUP (leaving it -- it's your original, pre-hd, joe)" echo "==> Backup already exists at $BACKUP (leaving it -- it's your original, pre-hd, joe)"
else else
if ! cp "$JOE_TARGET" "$BACKUP" 2>/dev/null; then if ! cp "$JOE_TARGET" "$BACKUP" 2>/dev/null; then
echo "$(dirname "$JOE_TARGET") isn't writable by $(whoami) -- can't even back up the existing joe there." >&2 echo "$(dirname "$JOE_TARGET") isn't writable by $(whoami) -- can't back up the existing joe there." >&2
echo "Run just this part with sudo:" >&2 echo "Run just this part with sudo:" >&2
echo " sudo $0 --joe-only --joe-target $JOE_TARGET" >&2 echo " sudo $0 --joe-only --joe-target $JOE_TARGET" >&2
echo "then run $0 again WITHOUT sudo to finish (venv, hdc, hd-joe)." >&2 echo "then run $0 again WITHOUT sudo to finish (venv, hdc, hd-joe)." >&2
@@ -115,11 +161,11 @@ install_joe() {
return 1 return 1
fi fi
# joe itself is now a wrapper, not the raw binary: it routes hd-managed # joe itself is now a wrapper, not the raw binary:
# notes to hd-joe (so you can never accidentally edit a note's cache # - HedgeDoc-managed notes are routed to hd-joe.
# file directly on disk, bypassing HedgeDoc) and everything else to the # - Everything else is handled by the installed patched joe binary.
# real compiled binary, which stays at $BUILT rather than being copied #
# -- see hd-joe/joe_wrapper.py for the actual decision logic. # The compiled binary lives at $INSTALLED_JOE, outside the source tree.
if ! cat > "$JOE_TARGET" <<WRAPEOF 2>/dev/null if ! cat > "$JOE_TARGET" <<WRAPEOF 2>/dev/null
#!/usr/bin/env bash #!/usr/bin/env bash
# hd-installer-generated # hd-installer-generated
@@ -130,10 +176,12 @@ WRAPEOF
echo " sudo $0 --joe-only --joe-target $JOE_TARGET" >&2 echo " sudo $0 --joe-only --joe-target $JOE_TARGET" >&2
return 1 return 1
fi fi
chmod 755 "$JOE_TARGET" chmod 755 "$JOE_TARGET"
echo "==> Installed joe wrapper -> $JOE_TARGET" echo "==> Installed joe wrapper -> $JOE_TARGET"
echo " (hd-managed notes route to hd-joe automatically; everything else" echo " Patched joe binary -> $INSTALLED_JOE"
echo " still goes to the real patched joe, unchanged)" echo " Source/build tree can now be removed independently."
return 0 return 0
} }
@@ -146,40 +194,37 @@ install_python_and_launchers() {
exit 1 exit 1
fi fi
# A plain `pip install --user` fails outright on modern Debian/Ubuntu
# (PEP 668 "externally-managed-environment"), and the suggested escape
# hatch (--break-system-packages) is pip's own last resort, not a
# default a script should reach for quietly. A dedicated venv
# sidesteps the problem entirely and needs no activation -- the
# launcher scripts below call its python3 directly.
echo "==> Setting up a virtual environment at $VENV_DIR" echo "==> Setting up a virtual environment at $VENV_DIR"
python3 -m venv "$VENV_DIR" python3 -m venv "$VENV_DIR"
"$VENV_DIR/bin/pip" install --quiet --upgrade pip "$VENV_DIR/bin/pip" install --quiet --upgrade pip
"$VENV_DIR/bin/pip" install \ "$VENV_DIR/bin/pip" install \
-r "$SCRIPT_DIR/hd-cli/requirements.txt" \ -r "$SCRIPT_DIR/hd-cli/requirements.txt" \
-r "$SCRIPT_DIR/hd-joe/requirements.txt" -r "$SCRIPT_DIR/hd-joe/requirements.txt"
echo "==> Installing launchers into $BIN_DIR" echo "==> Installing launchers into $BIN_DIR"
mkdir -p "$BIN_DIR" mkdir -p "$BIN_DIR"
# Wrapper scripts, not symlinks: they pin the venv's interpreter
# explicitly, so hdc/hd-joe work whether or not the venv is activated,
# and from any cwd. The marker comment is how --restore recognizes
# these as ours to remove, rather than deleting an unrelated file that
# happened to be named the same.
cat > "$BIN_DIR/hdc" <<WRAPEOF cat > "$BIN_DIR/hdc" <<WRAPEOF
#!/usr/bin/env bash #!/usr/bin/env bash
# hd-installer-generated # hd-installer-generated
exec "$VENV_DIR/bin/python3" "$SCRIPT_DIR/hd-cli/hdc" "\$@" exec "$VENV_DIR/bin/python3" "$SCRIPT_DIR/hd-cli/hdc" "\$@"
WRAPEOF WRAPEOF
cat > "$BIN_DIR/hd-joe" <<WRAPEOF cat > "$BIN_DIR/hd-joe" <<WRAPEOF
#!/usr/bin/env bash #!/usr/bin/env bash
# hd-installer-generated # hd-installer-generated
exec "$VENV_DIR/bin/python3" "$SCRIPT_DIR/hd-joe/hd_joe_agent.py" "\$@" exec "$VENV_DIR/bin/python3" "$SCRIPT_DIR/hd-joe/hd_joe_agent.py" "\$@"
WRAPEOF WRAPEOF
chmod +x "$BIN_DIR/hdc" "$BIN_DIR/hd-joe" chmod +x "$BIN_DIR/hdc" "$BIN_DIR/hd-joe"
case ":$PATH:" in case ":$PATH:" in
*":$BIN_DIR:"*) ;; *":$BIN_DIR:"*)
;;
*) *)
echo echo
echo "NOTE: $BIN_DIR isn't on your PATH. Add to your shell rc:" echo "NOTE: $BIN_DIR isn't on your PATH. Add to your shell rc:"
@@ -194,12 +239,10 @@ WRAPEOF
install_sync_service() { install_sync_service() {
if [ "$(id -u)" = "0" ]; then if [ "$(id -u)" = "0" ]; then
# Same reasoning as install_python_and_launchers(): a systemd
# --user service belongs to the invoking user's session. Enabling
# it as root would enable it for root, not you -- silently useless.
echo "Refusing to set up the sync service as root -- re-run without sudo." >&2 echo "Refusing to set up the sync service as root -- re-run without sudo." >&2
return 1 return 1
fi fi
if ! command -v systemctl >/dev/null 2>&1; then if ! command -v systemctl >/dev/null 2>&1; then
echo "==> systemctl not found -- skipping the background sync service." echo "==> systemctl not found -- skipping the background sync service."
echo " Run 'hdc sync --dir $NOTES_DIR --watch' yourself (or via cron/tmux/screen)" echo " Run 'hdc sync --dir $NOTES_DIR --watch' yourself (or via cron/tmux/screen)"
@@ -208,6 +251,7 @@ install_sync_service() {
fi fi
mkdir -p "$(dirname "$SERVICE_PATH")" mkdir -p "$(dirname "$SERVICE_PATH")"
cat > "$SERVICE_PATH" <<UNITEOF cat > "$SERVICE_PATH" <<UNITEOF
[Unit] [Unit]
Description=hdc sync --watch -- continuous HedgeDoc <-> local folder sync Description=hdc sync --watch -- continuous HedgeDoc <-> local folder sync
@@ -225,6 +269,7 @@ RestartSec=15
[Install] [Install]
WantedBy=default.target WantedBy=default.target
UNITEOF UNITEOF
echo "==> Wrote $SERVICE_PATH" echo "==> Wrote $SERVICE_PATH"
if ! systemctl --user daemon-reload 2>/tmp/hdc-systemctl-err; then if ! systemctl --user daemon-reload 2>/tmp/hdc-systemctl-err; then
@@ -236,7 +281,9 @@ UNITEOF
rm -f /tmp/hdc-systemctl-err rm -f /tmp/hdc-systemctl-err
return 0 return 0
fi fi
rm -f /tmp/hdc-systemctl-err rm -f /tmp/hdc-systemctl-err
systemctl --user enable "$SERVICE_NAME" >/dev/null 2>&1 systemctl --user enable "$SERVICE_NAME" >/dev/null 2>&1
if [ -d "$NOTES_DIR/.hd" ]; then if [ -d "$NOTES_DIR/.hd" ]; then
@@ -250,7 +297,9 @@ UNITEOF
} }
# --- restore ------------------------------------------------------------- # --- restore -------------------------------------------------------------
if [ "$RESTORE" = 1 ]; then if [ "$RESTORE" = 1 ]; then
if [ -f "$BACKUP" ]; then if [ -f "$BACKUP" ]; then
if cp "$BACKUP" "$JOE_TARGET" 2>/dev/null; then if cp "$BACKUP" "$JOE_TARGET" 2>/dev/null; then
echo "Restored: $BACKUP -> $JOE_TARGET" echo "Restored: $BACKUP -> $JOE_TARGET"
@@ -261,28 +310,48 @@ if [ "$RESTORE" = 1 ]; then
else else
echo "No backup at $BACKUP -- nothing to restore." >&2 echo "No backup at $BACKUP -- nothing to restore." >&2
fi fi
for name in hdc hd-joe; do for name in hdc hd-joe; do
link="$BIN_DIR/$name" link="$BIN_DIR/$name"
if [ -f "$link" ] && grep -q "# hd-installer-generated" "$link" 2>/dev/null; then
rm "$link" && echo "Removed $link" if [ -f "$link" ] &&
grep -q "# hd-installer-generated" "$link" 2>/dev/null; then
rm "$link"
echo "Removed $link"
fi fi
done done
if [ -f "$SERVICE_PATH" ] && command -v systemctl >/dev/null 2>&1; then
if [ -f "$SERVICE_PATH" ] &&
command -v systemctl >/dev/null 2>&1; then
systemctl --user disable --now "$SERVICE_NAME" >/dev/null 2>&1 || true systemctl --user disable --now "$SERVICE_NAME" >/dev/null 2>&1 || true
rm -f "$SERVICE_PATH" rm -f "$SERVICE_PATH"
systemctl --user daemon-reload 2>/dev/null || true systemctl --user daemon-reload 2>/dev/null || true
echo "Disabled and removed $SERVICE_NAME" echo "Disabled and removed $SERVICE_NAME"
fi fi
if [ -f "$INSTALLED_JOE" ]; then
rm -f "$INSTALLED_JOE"
echo "Removed installed patched joe: $INSTALLED_JOE"
fi
if [ -d "$PREFIX_LIB" ] && [ -z "$(ls -A "$PREFIX_LIB" 2>/dev/null)" ]; then
rmdir "$PREFIX_LIB"
fi
exit 0 exit 0
fi fi
# --- joe-only -------------------------------------------------------------- # --- joe-only ------------------------------------------------------------
if [ "$JOE_ONLY" = 1 ]; then if [ "$JOE_ONLY" = 1 ]; then
install_joe install_joe
exit $? exit $?
fi fi
# --- full install ---------------------------------------------------------- # --- full install --------------------------------------------------------
install_python_and_launchers install_python_and_launchers
if [ "$NO_SERVICE" = 1 ]; then if [ "$NO_SERVICE" = 1 ]; then
@@ -298,13 +367,17 @@ JOE_OK=1
install_joe || JOE_OK=0 install_joe || JOE_OK=0
echo echo
if [ "$JOE_OK" = 1 ]; then if [ "$JOE_OK" = 1 ]; then
echo "Done. joe at $JOE_TARGET has HedgeDoc support (only active when" echo "Done. joe at $JOE_TARGET has HedgeDoc support (only active when"
echo "JOE_HEDGE_SOCKET is set -- stock behavior otherwise; your original" echo "JOE_HEDGE_SOCKET is set -- stock behavior otherwise; your original"
echo "is backed up at $BACKUP)." echo "is backed up at $BACKUP)."
echo "Installed patched joe binary: $INSTALLED_JOE"
else else
echo "hd and hd-joe are set up. Patched joe is NOT installed yet -- see" echo "hd and hd-joe are set up. Patched joe is NOT installed yet -- see"
echo "the sudo command printed above to finish that part." echo "the sudo command printed above to finish that part."
fi fi
echo echo
echo "Restore the original joe, remove hdc/hd-joe, and stop the sync service: $0 --restore" echo "Restore the original joe, remove hdc/hd-joe, and stop the sync service: $0 --restore"