Files
klipper-macros/install.sh
T

98 lines
3.5 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
# 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
chmod +x "$REPO_DIR"/macros/*.sh "$REPO_DIR"/scripts/*.sh "$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: [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