This commit is contained in:
Joakim
2026-10-17 16:53:33 +02:00
parent a9b30e68a6
commit 653c17b982

View File

@@ -1,26 +1,26 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
# ============================================================
# HedgeDoc JOE - user local installer
#
# Installs everything below ~/.local and does not modify
# the system installation of joe.
#
# After installation the source tree may be removed.
# ============================================================
# ============================================================================
# Installation paths
#
# Source tree:
# $SCRIPT_DIR
#
# Installed files:
# ~/.local/bin/
# ~/.local/lib/hedgedoc-joe/
#
# Nothing runtime-critical is left in the source tree.
# ============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ------------------------------------------------------------
# Installation locations
# ------------------------------------------------------------
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/lib/hedgedoc-joe}"
INSTALLED_JOE="$INSTALL_DIR/joe"
INSTALLED_COMMON="$INSTALL_DIR/hd_common"
INSTALLED_HDC="$INSTALL_DIR/hd-cli"
INSTALLED_HD_JOE="$INSTALL_DIR/hd-joe"
VENV_DIR="$INSTALL_DIR/venv"
@@ -29,512 +29,362 @@ JOE_WRAPPER="$BIN_DIR/joe"
HDC_WRAPPER="$BIN_DIR/hdc"
HD_JOE_WRAPPER="$BIN_DIR/hd-joe"
# Built JOE binary
BUILT="$SCRIPT_DIR/joe-patch/build/joe-4.8/joe/joe"
NOTES_DIR="${NOTES_DIR:-$HOME/notes}"
SYNC_INTERVAL="${SYNC_INTERVAL:-30}"
# ------------------------------------------------------------
# Options
# ------------------------------------------------------------
SERVICE_NAME="hdc-sync.service"
SERVICE_PATH="$HOME/.config/systemd/user/$SERVICE_NAME"
FORCE=0
RESTORE=0
NO_SERVICE=0
usage() {
cat <<EOF
Usage: $0 [options]
Options:
--force Replace existing installation
--install-dir DIR Installation directory
--bin-dir DIR User executable directory
-h, --help Show this help
# ============================================================================
# Arguments
# ============================================================================
Default installation:
while [ $# -gt 0 ]; do
Binaries:
$HOME/.local/bin/
Runtime:
$HOME/.local/lib/hedgedoc-joe/
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--restore)
RESTORE=1
--force)
FORCE=1
shift
;;
--no-service)
NO_SERVICE=1
shift
;;
--bin-dir)
BIN_DIR="$2"
JOE_WRAPPER="$BIN_DIR/joe"
HDC_WRAPPER="$BIN_DIR/hdc"
HD_JOE_WRAPPER="$BIN_DIR/hd-joe"
shift 2
;;
--install-dir)
[[ $# -ge 2 ]] || {
echo "ERROR: --install-dir requires an argument" >&2
exit 1
}
INSTALL_DIR="$2"
INSTALLED_JOE="$INSTALL_DIR/joe"
INSTALLED_HDC="$INSTALL_DIR/hd-cli"
INSTALLED_HD_JOE="$INSTALL_DIR/hd-joe"
VENV_DIR="$INSTALL_DIR/venv"
shift 2
;;
--notes-dir)
NOTES_DIR="$2"
shift 2
;;
--sync-interval)
SYNC_INTERVAL="$2"
--bin-dir)
[[ $# -ge 2 ]] || {
echo "ERROR: --bin-dir requires an argument" >&2
exit 1
}
BIN_DIR="$2"
shift 2
;;
-h|--help)
cat <<EOF
Usage: $0 [OPTIONS]
Install:
$0
Options:
--bin-dir DIR
User executable directory.
Default: ~/.local/bin
--install-dir DIR
Permanent installation directory.
Default: ~/.local/lib/hedgedoc-joe
--notes-dir DIR
Notes directory for the sync service.
Default: ~/notes
--sync-interval N
Sync interval in seconds.
Default: 30
--no-service
Do not install the user systemd service.
--restore
Remove the user-local installation.
-h, --help
Show this help.
No sudo is required.
The system installation of joe is never modified.
EOF
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
echo "ERROR: Unknown option: $1" >&2
usage >&2
exit 1
;;
esac
done
# Recalculate paths if --install-dir / --bin-dir changed.
INSTALLED_JOE="$INSTALL_DIR/joe"
INSTALLED_COMMON="$INSTALL_DIR/hd_common"
INSTALLED_HDC="$INSTALL_DIR/hd-cli"
INSTALLED_HD_JOE="$INSTALL_DIR/hd-joe"
VENV_DIR="$INSTALL_DIR/venv"
# ============================================================================
# Never run as root
# ============================================================================
JOE_WRAPPER="$BIN_DIR/joe"
HDC_WRAPPER="$BIN_DIR/hdc"
HD_JOE_WRAPPER="$BIN_DIR/hd-joe"
if [ "$(id -u)" = "0" ]; then
echo "ERROR: Do not run install.sh with sudo." >&2
echo "This is a per-user installation." >&2
# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------
die() {
echo "ERROR: $*" >&2
exit 1
fi
# ============================================================================
# Restore
# ============================================================================
restore() {
echo "==> Removing HedgeDoc JOE installation"
# Stop/remove user service first.
if command -v systemctl >/dev/null 2>&1; then
systemctl --user disable --now "$SERVICE_NAME" \
>/dev/null 2>&1 || true
rm -f "$SERVICE_PATH"
systemctl --user daemon-reload \
>/dev/null 2>&1 || true
fi
# Remove our launchers only if they are ours.
for launcher in \
"$JOE_WRAPPER" \
"$HDC_WRAPPER" \
"$HD_JOE_WRAPPER"
do
if [ -f "$launcher" ] &&
grep -q '# hedgedoc-joe-installer' "$launcher" 2>/dev/null
then
rm -f "$launcher"
echo " Removed $launcher"
fi
done
# Remove complete installed application.
if [ -d "$INSTALL_DIR" ]; then
rm -rf "$INSTALL_DIR"
echo " Removed $INSTALL_DIR"
fi
echo
echo "==> Restore complete"
echo
echo "The system joe was never modified."
}
info() {
echo "==> $*"
}
if [ "$RESTORE" = 1 ]; then
restore
exit 0
# ------------------------------------------------------------
# Check source tree
# ------------------------------------------------------------
info "Checking source tree"
[[ -d "$SCRIPT_DIR/hd-cli" ]] ||
die "Missing source directory: $SCRIPT_DIR/hd-cli"
[[ -d "$SCRIPT_DIR/hd-joe" ]] ||
die "Missing source directory: $SCRIPT_DIR/hd-joe"
[[ -d "$SCRIPT_DIR/hd_common" ]] ||
die "Missing source directory: $SCRIPT_DIR/hd_common"
[[ -f "$SCRIPT_DIR/hd_common/requirements.txt" ]] ||
die "Missing: $SCRIPT_DIR/hd_common/requirements.txt"
[[ -f "$SCRIPT_DIR/hd-cli/requirements.txt" ]] ||
die "Missing: $SCRIPT_DIR/hd-cli/requirements.txt"
[[ -f "$SCRIPT_DIR/hd-joe/requirements.txt" ]] ||
die "Missing: $SCRIPT_DIR/hd-joe/requirements.txt"
[[ -x "$BUILT" ]] ||
die "Built JOE binary not found:
$BUILT
Run ./build.sh first."
# ------------------------------------------------------------
# Existing installation
# ------------------------------------------------------------
if [[ -e "$INSTALL_DIR" ]]; then
if [[ "$FORCE" -ne 1 ]]; then
die "Installation already exists:
$INSTALL_DIR
Use --force to replace it."
fi
info "Removing existing installation"
rm -rf "$INSTALL_DIR"
fi
# ------------------------------------------------------------
# Create directories
# ------------------------------------------------------------
# ============================================================================
# Verify source tree
# ============================================================================
if [ ! -x "$BUILT" ]; then
echo "ERROR: Patched joe binary not found:"
echo
echo " $BUILT"
echo
echo "Run ./build.sh first."
exit 1
fi
if [ ! -f "$SCRIPT_DIR/hd-joe/joe_wrapper.py" ]; then
echo "ERROR: hd-joe source not found:"
echo " $SCRIPT_DIR/hd-joe"
exit 1
fi
if [ ! -f "$SCRIPT_DIR/hd-joe/requirements.txt" ]; then
echo "ERROR: hd-joe requirements not found."
exit 1
fi
if [ ! -f "$SCRIPT_DIR/hd-cli/requirements.txt" ]; then
echo "ERROR: hd-cli requirements not found."
exit 1
fi
# ============================================================================
# Display installation
# ============================================================================
echo
echo "=============================================="
echo " HedgeDoc JOE user installation"
echo "=============================================="
echo
echo "Source:"
echo " $SCRIPT_DIR"
echo
echo "Installation:"
echo " $INSTALL_DIR"
echo
echo "Executables:"
echo " $JOE_WRAPPER"
echo " $HDC_WRAPPER"
echo " $HD_JOE_WRAPPER"
echo
# ============================================================================
# Create installation directory
# ============================================================================
echo "==> Creating installation directory"
info "Creating installation directories"
mkdir -p "$INSTALL_DIR"
mkdir -p "$BIN_DIR"
# ------------------------------------------------------------
# Install JOE
# ------------------------------------------------------------
# ============================================================================
# Install patched JOE
# ============================================================================
info "Installing patched JOE"
echo "==> Installing patched joe"
install -m 0755 "$BUILT" "$INSTALLED_JOE"
cp "$BUILT" "$INSTALLED_JOE"
chmod 755 "$INSTALLED_JOE"
echo " $INSTALLED_JOE"
# ============================================================================
# ------------------------------------------------------------
# Install Python applications
#
# Copy the source code into the permanent installation directory.
# After this point the runtime does NOT depend on SCRIPT_DIR.
# ============================================================================
# ------------------------------------------------------------
echo "==> Installing Python applications"
rm -rf "$INSTALLED_HDC"
rm -rf "$INSTALLED_HD_JOE"
info "Installing Python applications"
cp -a "$SCRIPT_DIR/hd_common" "$INSTALLED_COMMON"
cp -a "$SCRIPT_DIR/hd-cli" "$INSTALLED_HDC"
cp -a "$SCRIPT_DIR/hd-joe" "$INSTALLED_HD_JOE"
# ============================================================================
# ------------------------------------------------------------
# Python virtual environment
# ============================================================================
# ------------------------------------------------------------
echo "==> Creating Python virtual environment"
rm -rf "$VENV_DIR"
info "Creating Python virtual environment"
python3 -m venv "$VENV_DIR"
echo "==> Installing Python dependencies"
PYTHON="$VENV_DIR/bin/python3"
PIP="$PYTHON -m pip"
"$VENV_DIR/bin/python3" -m pip install \
--quiet \
--upgrade pip
info "Upgrading pip"
"$VENV_DIR/bin/python3" -m pip install \
"$PYTHON" -m pip install --upgrade pip
# ------------------------------------------------------------
# Python dependencies
# ------------------------------------------------------------
info "Installing Python dependencies"
"$PYTHON" -m pip install \
-r "$INSTALLED_COMMON/requirements.txt" \
-r "$INSTALLED_HDC/requirements.txt" \
-r "$INSTALLED_HD_JOE/requirements.txt"
# ============================================================================
# joe wrapper
# ------------------------------------------------------------
# Verify Python installation
#
# This wrapper contains ONLY permanent installation paths.
# It does not reference SCRIPT_DIR.
# ============================================================================
# Both of these paths are included so the installed programs
# work whether they import modules as:
#
# hd_common.hd_core
#
# or directly as:
#
# hd_core
# ------------------------------------------------------------
echo "==> Installing joe wrapper"
PYTHONPATH_VALUE="$INSTALL_DIR:$INSTALLED_COMMON"
info "Testing Python installation"
PYTHONPATH="$PYTHONPATH_VALUE" \
"$PYTHON" -c '
import hd_common.hd_core
print(" hd_common: OK")
'
# ------------------------------------------------------------
# Install wrappers
# ------------------------------------------------------------
info "Installing command wrappers"
cat > "$JOE_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
set -e
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HD_JOE/joe_wrapper.py" "\$@"
INSTALL_DIR="$INSTALL_DIR"
VENV_DIR="$VENV_DIR"
export PYTHONPATH="\$INSTALL_DIR:\$INSTALL_DIR/hd_common\${PYTHONPATH:+:\$PYTHONPATH}"
exec "\$INSTALL_DIR/joe" "\$@"
EOF
chmod 755 "$JOE_WRAPPER"
chmod 0755 "$JOE_WRAPPER"
# ============================================================================
# hdc launcher
# ============================================================================
echo "==> Installing hdc"
cat > "$HDC_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
set -e
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HDC/hdc" "\$@"
INSTALL_DIR="$INSTALL_DIR"
VENV_DIR="$VENV_DIR"
export PYTHONPATH="\$INSTALL_DIR:\$INSTALL_DIR/hd_common\${PYTHONPATH:+:\$PYTHONPATH}"
exec "\$VENV_DIR/bin/python3" \
"\$INSTALL_DIR/hd-cli/hd_cli.py" "\$@"
EOF
chmod 755 "$HDC_WRAPPER"
chmod 0755 "$HDC_WRAPPER"
# ============================================================================
# hd-joe launcher
# ============================================================================
echo "==> Installing hd-joe"
cat > "$HD_JOE_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
set -e
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HD_JOE/hd_joe_agent.py" "\$@"
INSTALL_DIR="$INSTALL_DIR"
VENV_DIR="$VENV_DIR"
export PYTHONPATH="\$INSTALL_DIR:\$INSTALL_DIR/hd_common\${PYTHONPATH:+:\$PYTHONPATH}"
exec "\$VENV_DIR/bin/python3" \
"\$INSTALL_DIR/hd-joe/joe_wrapper.py" "\$@"
EOF
chmod 755 "$HD_JOE_WRAPPER"
chmod 0755 "$HD_JOE_WRAPPER"
# ------------------------------------------------------------
# Final verification
# ------------------------------------------------------------
# ============================================================================
# PATH information
# ============================================================================
info "Verifying installation"
case ":$PATH:" in
*":$BIN_DIR:"*)
;;
*)
echo
echo "NOTE: $BIN_DIR is not currently in PATH."
echo
echo "Add this to your shell configuration:"
echo
echo " export PATH=\"$BIN_DIR:\$PATH\""
echo
;;
esac
[[ -x "$JOE_WRAPPER" ]] ||
die "joe wrapper was not installed"
[[ -x "$HDC_WRAPPER" ]] ||
die "hdc wrapper was not installed"
# ============================================================================
# User systemd service
# ============================================================================
[[ -x "$HD_JOE_WRAPPER" ]] ||
die "hd-joe wrapper was not installed"
install_sync_service() {
[[ -x "$INSTALLED_JOE" ]] ||
die "JOE binary was not installed"
if ! command -v systemctl >/dev/null 2>&1; then
echo "==> systemctl not found -- skipping sync service."
return 0
fi
[[ -d "$INSTALLED_COMMON" ]] ||
die "hd_common was not installed"
mkdir -p "$(dirname "$SERVICE_PATH")"
[[ -d "$INSTALLED_HDC" ]] ||
die "hd-cli was not installed"
cat > "$SERVICE_PATH" <<EOF
[Unit]
Description=hdc sync --watch -- continuous HedgeDoc local folder sync
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=10
[[ -d "$INSTALLED_HD_JOE" ]] ||
die "hd-joe was not installed"
[Service]
Type=simple
ExecStart=$HDC_WRAPPER sync --dir $NOTES_DIR --watch --interval $SYNC_INTERVAL
Restart=on-failure
RestartSec=15
[[ -x "$VENV_DIR/bin/python3" ]] ||
die "Python virtual environment was not installed"
[Install]
WantedBy=default.target
EOF
# Test that Python can actually see the installed tree.
PYTHONPATH="$PYTHONPATH_VALUE" \
"$PYTHON" -c '
import hd_common.hd_core
print(" Python runtime: OK")
'
echo "==> Installed user systemd service:"
echo " $SERVICE_PATH"
if ! systemctl --user daemon-reload 2>/dev/null; then
echo
echo "NOTE: Could not contact the user systemd session."
echo "The service file was installed but not started."
echo
echo "Run later:"
echo
echo " systemctl --user daemon-reload"
echo " systemctl --user enable --now $SERVICE_NAME"
return 0
fi
systemctl --user enable "$SERVICE_NAME" \
>/dev/null 2>&1 || true
if [ -d "$NOTES_DIR/.hd" ]; then
systemctl --user restart "$SERVICE_NAME" \
>/dev/null 2>&1 || \
systemctl --user start "$SERVICE_NAME" \
>/dev/null 2>&1 || true
echo "==> Sync service enabled"
echo " Notes: $NOTES_DIR"
echo " Interval: ${SYNC_INTERVAL}s"
else
echo "==> Sync service installed but not started."
echo " $NOTES_DIR/.hd does not exist yet."
fi
# Test JOE itself.
"$INSTALLED_JOE" --version >/dev/null 2>&1 || {
echo "WARNING: JOE --version test failed"
}
if [ "$NO_SERVICE" = 1 ]; then
echo "==> --no-service: skipping systemd service"
else
echo "==> Installing user systemd service"
install_sync_service
fi
# ============================================================================
# Final verification
# ============================================================================
echo
echo "==> Verifying installation"
if [ ! -x "$INSTALLED_JOE" ]; then
echo "ERROR: Installed joe is missing."
exit 1
fi
if [ ! -x "$JOE_WRAPPER" ]; then
echo "ERROR: joe launcher is missing."
exit 1
fi
if [ ! -x "$HDC_WRAPPER" ]; then
echo "ERROR: hdc launcher is missing."
exit 1
fi
if [ ! -x "$HD_JOE_WRAPPER" ]; then
echo "ERROR: hd-joe launcher is missing."
exit 1
fi
if [ ! -x "$VENV_DIR/bin/python3" ]; then
echo "ERROR: Python virtual environment is missing."
exit 1
fi
# ============================================================================
# ------------------------------------------------------------
# Done
# ============================================================================
# ------------------------------------------------------------
echo
echo "=============================================="
echo " Installation complete"
echo "=============================================="
echo
echo "Installed files:"
echo
echo " $INSTALL_DIR"
echo
echo "Commands:"
echo
echo " $JOE_WRAPPER"
echo " $HDC_WRAPPER"
echo " $HD_JOE_WRAPPER"
echo
echo "Patched JOE:"
echo
echo " $INSTALLED_JOE"
echo
echo "Python:"
echo
echo " $VENV_DIR"
echo
echo "The installation is independent of:"
echo
echo " $SCRIPT_DIR"
echo
echo "You can now remove the source directory."
echo
echo "System joe has NOT been modified."
echo
echo "To remove this installation later:"
echo
echo " $0 --restore"
echo
cat <<EOF
============================================================
Installation complete
============================================================
Runtime:
$INSTALL_DIR
Commands:
$JOE_WRAPPER
$HDC_WRAPPER
$HD_JOE_WRAPPER
Installed components:
JOE:
$INSTALLED_JOE
Common:
$INSTALLED_COMMON
HedgeDoc CLI:
$INSTALLED_HDC
HedgeDoc JOE:
$INSTALLED_HD_JOE
Python environment:
$VENV_DIR
No system files were modified.
No sudo was required.
The installation is independent of:
$SCRIPT_DIR
You can therefore remove the source tree after verifying
the installation.
If ~/.local/bin is not already in PATH, add:
export PATH="$HOME/.local/bin:\$PATH"
============================================================
EOF