Files
klipper-macros/install.sh
T

107 lines
3.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Install klipper-macros on a Klipper host (Raspberry Pi etc.)
#
# cd ~ && git clone https://mangavalk.com/mangavalk/klipper-macros.git
# ~/klipper-macros/install.sh
#
# What it does (safe to run again, Moonraker runs it after every update):
# 1. links extras/*.py into ~/klipper/klippy/extras/
# 2. links macros/ to ~/printer_data/config/klipper-macros and scripts/
# to ~/printer_data/config/klipper-macros-scripts (editable in the web UI)
# 3. makes the shell scripts executable
# 4. adds an [update_manager klipper-macros] section to moonraker.conf
# (once, with a backup of moonraker.conf)
#
# It does not edit printer.cfg; see README.md for the include line.
#
# ~/klipper-macros/install.sh --uninstall remove the links again
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
KLIPPER_DIR="${KLIPPER_DIR:-$HOME/klipper}"
CONFIG_DIR="${CONFIG_DIR:-$HOME/printer_data/config}"
MOONRAKER_CONF="$CONFIG_DIR/moonraker.conf"
LINK_NAME="$CONFIG_DIR/klipper-macros"
SCRIPTS_LINK="$CONFIG_DIR/klipper-macros-scripts"
EXTRAS_DIR="$KLIPPER_DIR/klippy/extras"
ORIGIN="$(git -C "$REPO_DIR" remote get-url origin 2>/dev/null || echo "https://mangavalk.com/mangavalk/klipper-macros.git")"
die() { echo "ERROR: $*" >&2; exit 1; }
[[ -d $EXTRAS_DIR ]] || die "Klipper not found at $KLIPPER_DIR (set KLIPPER_DIR=...)"
[[ -d $CONFIG_DIR ]] || die "config folder not found at $CONFIG_DIR (set CONFIG_DIR=...)"
if [[ ${1:-} == --uninstall ]]; then
for py in "$REPO_DIR"/extras/*.py; do
target="$EXTRAS_DIR/$(basename "$py")"
if [[ -L $target && $(readlink -f "$target") == "$(readlink -f "$py")" ]]; then
rm "$target" && echo "removed $target"
fi
done
for link in "$LINK_NAME" "$SCRIPTS_LINK"; do
if [[ -L $link ]]; then rm "$link" && echo "removed $link"; fi
done
echo "Remove [include klipper-macros/...] from printer.cfg and the"
echo "[update_manager klipper-macros] section from moonraker.conf yourself,"
echo "then restart the Klipper service."
exit 0
fi
# 1. Klipper modules
for py in "$REPO_DIR"/extras/*.py; do
target="$EXTRAS_DIR/$(basename "$py")"
if [[ -e $target && ! -L $target ]]; then
mv "$target" "$target.bak" && echo "existing $target moved to $target.bak"
fi
ln -sfn "$py" "$target"
echo "linked $target"
done
# Klipper's update manager would report the links as untracked files
exclude="$KLIPPER_DIR/.git/info/exclude"
if [[ -f $exclude ]]; then
for py in "$REPO_DIR"/extras/*.py; do
line="klippy/extras/$(basename "$py")"
grep -qxF "$line" "$exclude" || echo "$line" >> "$exclude"
done
fi
# 2. Macros and scripts, visible and editable in Mainsail/Fluidd
for pair in "macros:$LINK_NAME" "scripts:$SCRIPTS_LINK"; do
src="$REPO_DIR/${pair%%:*}" link="${pair#*:}"
if [[ -e $link && ! -L $link ]]; then
die "$link exists and is not a link; move it away first"
fi
ln -sfn "$src" "$link"
echo "linked $link"
done
# 3. Scripts
find "$REPO_DIR/macros" "$REPO_DIR/scripts" -name "*.sh" -exec chmod +x {} +
chmod +x "$REPO_DIR/install.sh"
# 4. Moonraker update manager
if [[ -f $MOONRAKER_CONF ]] && ! grep -q '^\[update_manager klipper-macros\]' "$MOONRAKER_CONF"; then
cp "$MOONRAKER_CONF" "$MOONRAKER_CONF.bak.$(date +%Y%m%d_%H%M%S)"
cat >> "$MOONRAKER_CONF" <<EOF
[update_manager klipper-macros]
type: git_repo
path: $REPO_DIR
origin: $ORIGIN
primary_branch: main
managed_services: klipper
install_script: install.sh
EOF
echo "added [update_manager klipper-macros] to moonraker.conf"
echo "restart Moonraker to see it in the update manager"
fi
cat <<EOF
Done. Next steps (see README.md):
1. add to printer.cfg, after [include AFC/*.cfg]:
[include klipper-macros/klipper-macros.cfg]
and remove the old includes of the same macro files
2. restart the Klipper SERVICE (not just RESTART), so the new Python
modules are loaded: sudo systemctl restart klipper
EOF