Files
thd/install.sh
Joakim a9b30e68a6 N/A
2026-10-17 16:40:15 +02:00

541 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
# ============================================================================
# Installation paths
#
# Source tree:
# $SCRIPT_DIR
#
# Installed files:
# ~/.local/bin/
# ~/.local/lib/hedgedoc-joe/
#
# Nothing runtime-critical is left in the source tree.
# ============================================================================
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/lib/hedgedoc-joe}"
INSTALLED_JOE="$INSTALL_DIR/joe"
INSTALLED_HDC="$INSTALL_DIR/hd-cli"
INSTALLED_HD_JOE="$INSTALL_DIR/hd-joe"
VENV_DIR="$INSTALL_DIR/venv"
JOE_WRAPPER="$BIN_DIR/joe"
HDC_WRAPPER="$BIN_DIR/hdc"
HD_JOE_WRAPPER="$BIN_DIR/hd-joe"
BUILT="$SCRIPT_DIR/joe-patch/build/joe-4.8/joe/joe"
NOTES_DIR="${NOTES_DIR:-$HOME/notes}"
SYNC_INTERVAL="${SYNC_INTERVAL:-30}"
SERVICE_NAME="hdc-sync.service"
SERVICE_PATH="$HOME/.config/systemd/user/$SERVICE_NAME"
RESTORE=0
NO_SERVICE=0
# ============================================================================
# Arguments
# ============================================================================
while [ $# -gt 0 ]; do
case "$1" in
--restore)
RESTORE=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)
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"
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
exit 0
;;
*)
echo "Unknown argument: $1" >&2
exit 1
;;
esac
done
# ============================================================================
# Never run as root
# ============================================================================
if [ "$(id -u)" = "0" ]; then
echo "ERROR: Do not run install.sh with sudo." >&2
echo "This is a per-user installation." >&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."
}
if [ "$RESTORE" = 1 ]; then
restore
exit 0
fi
# ============================================================================
# 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"
mkdir -p "$INSTALL_DIR"
mkdir -p "$BIN_DIR"
# ============================================================================
# Install patched JOE
# ============================================================================
echo "==> Installing patched 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"
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"
python3 -m venv "$VENV_DIR"
echo "==> Installing Python dependencies"
"$VENV_DIR/bin/python3" -m pip install \
--quiet \
--upgrade pip
"$VENV_DIR/bin/python3" -m pip install \
-r "$INSTALLED_HDC/requirements.txt" \
-r "$INSTALLED_HD_JOE/requirements.txt"
# ============================================================================
# joe wrapper
#
# This wrapper contains ONLY permanent installation paths.
# It does not reference SCRIPT_DIR.
# ============================================================================
echo "==> Installing joe wrapper"
cat > "$JOE_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HD_JOE/joe_wrapper.py" "\$@"
EOF
chmod 755 "$JOE_WRAPPER"
# ============================================================================
# hdc launcher
# ============================================================================
echo "==> Installing hdc"
cat > "$HDC_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HDC/hdc" "\$@"
EOF
chmod 755 "$HDC_WRAPPER"
# ============================================================================
# hd-joe launcher
# ============================================================================
echo "==> Installing hd-joe"
cat > "$HD_JOE_WRAPPER" <<EOF
#!/usr/bin/env bash
# hedgedoc-joe-installer
exec "$VENV_DIR/bin/python3" \
"$INSTALLED_HD_JOE/hd_joe_agent.py" "\$@"
EOF
chmod 755 "$HD_JOE_WRAPPER"
# ============================================================================
# PATH information
# ============================================================================
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
# ============================================================================
# User systemd service
# ============================================================================
install_sync_service() {
if ! command -v systemctl >/dev/null 2>&1; then
echo "==> systemctl not found -- skipping sync service."
return 0
fi
mkdir -p "$(dirname "$SERVICE_PATH")"
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
[Service]
Type=simple
ExecStart=$HDC_WRAPPER sync --dir $NOTES_DIR --watch --interval $SYNC_INTERVAL
Restart=on-failure
RestartSec=15
[Install]
WantedBy=default.target
EOF
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
}
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