#!/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 # 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" 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 [[ -L $LINK_NAME ]] && rm "$LINK_NAME" && echo "removed $LINK_NAME" 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 if [[ -e $LINK_NAME && ! -L $LINK_NAME ]]; then die "$LINK_NAME exists and is not a link; move it away first" fi ln -sfn "$REPO_DIR/macros" "$LINK_NAME" echo "linked $LINK_NAME" # 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" <