forked from julianschill/klipper-led_effect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-led_effect.sh
executable file
·203 lines (180 loc) · 5.13 KB
/
install-led_effect.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Force script to exit if an error occurs
set -e
KLIPPER_PATH="${HOME}/klipper"
KLIPPER_SERVICE_NAME=klipper
SYSTEMDDIR="/etc/systemd/system"
MOONRAKER_CONFIG_DIR="${HOME}/printer_data/config"
RESTART_SERVICE=1
UPDATE_MOONRAKER=1
IGNORE_ROOT=0
VENV_PATH="${HOME}/klippy-env"
usage(){ echo "Usage: $0 [-k <klipper path>] [-c <moonraker config path>] [-s klipper-service-name]" 1>&2; exit 1; }
args=$(getopt -a -o k:c:s:uh --long klipper:,moonraker:,klipper-service-name:,uninstall,help,no-moonraker,no-service,ignore-root,venv: -- "$@")
# shellcheck disable=SC2181
if [[ $? -gt 0 ]]; then
usage
fi
eval set -- "${args}"
while :; do
case $1 in
-k | --klipper)
shift
KLIPPER_PATH=$1
shift
;;
-s | --klipper-service-name)
shift
KLIPPER_SERVICE_NAME=$1
shift
;;
-c | --moonraker)
shift
MOONRAKER_CONFIG_DIR=$1
shift
;;
--venv)
shift
VENV_PATH=$1
shift
;;
--no-moonraker)
UPDATE_MOONRAKER=0
shift
;;
--no-service)
RESTART_SERVICE=0
shift
;;
--ignore-root)
IGNORE_ROOT=1
shift
;;
-u | --uninstall)
UNINSTALL=1
shift
;;
-h | --help)
usage
;;
--)
shift
break
;;
*)
echo >&2 Unsupported option: "$1"
usage
;;
esac
done
# Fall back to old directory for configuration as default
if [ $UPDATE_MOONRAKER -ne 0 ] && [ ! -d "${MOONRAKER_CONFIG_DIR}" ]; then
echo "\"$MOONRAKER_CONFIG_DIR\" does not exist. Falling back to "${HOME}/klipper_config" as default."
MOONRAKER_CONFIG_DIR="${HOME}/klipper_config"
fi
# Find SRCDIR from the pathname of this script
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/ && pwd )"
# Verify Klipper has been installed
check_klipper()
{
if [ "$(sudo systemctl list-units --full -all -t service --no-legend | grep -F "$KLIPPER_SERVICE_NAME.service")" ]; then
echo "Klipper service found with name "$KLIPPER_SERVICE_NAME"."
else
echo "[ERROR] Klipper service with name "$KLIPPER_SERVICE_NAME" not found, please install Klipper first or specify name with -s."
exit -1
fi
}
check_folders()
{
if [ ! -d "$KLIPPER_PATH/klippy/extras/" ]; then
echo "[ERROR] Klipper installation not found in directory \"$KLIPPER_PATH\". Exiting"
exit -1
fi
echo "Klipper installation found at $KLIPPER_PATH"
if [ $UPDATE_MOONRAKER -ne 0 ] && [ ! -f "${MOONRAKER_CONFIG_DIR}/moonraker.conf" ]; then
echo "[ERROR] Moonraker configuration not found in directory \"$MOONRAKER_CONFIG_DIR\". Exiting"
exit -1
fi
echo "Moonraker configuration found at $MOONRAKER_CONFIG_DIR"
}
# Link extension to Klipper
link_extension()
{
echo -n "Linking extension to Klipper... "
ln -sf "${SRCDIR}/packages/led_effect/led_effect/" "${KLIPPER_PATH}/klippy/extras/led_effect"
echo "[OK]"
}
# Restart moonraker
restart_moonraker()
{
echo -n "Restarting Moonraker... "
sudo systemctl restart moonraker
echo "[OK]"
}
# Add updater for led_effect to moonraker.conf
add_updater()
{
echo -e -n "Adding update manager to moonraker.conf... "
update_section=$(grep -c '\[update_manager led_effect\]' ${MOONRAKER_CONFIG_DIR}/moonraker.conf || true)
if [ "${update_section}" -eq 0 ]; then
echo -e "\n" >> ${MOONRAKER_CONFIG_DIR}/moonraker.conf
while read -r line; do
echo -e "${line}" >> ${MOONRAKER_CONFIG_DIR}/moonraker.conf
done < "$PWD/file_templates/moonraker_update.txt"
echo -e "\n" >> ${MOONRAKER_CONFIG_DIR}/moonraker.conf
echo "[OK]"
restart_moonraker
else
echo -e "[update_manager led_effect] already exists in moonraker.conf [SKIPPED]"
fi
}
restart_klipper()
{
echo -n "Restarting Klipper... "
sudo systemctl restart $KLIPPER_SERVICE_NAME
echo "[OK]"
}
start_klipper()
{
echo -n "Starting Klipper... "
sudo systemctl start $KLIPPER_SERVICE_NAME
echo "[OK]"
}
stop_klipper()
{
echo -n "Stopping Klipper... "
sudo systemctl stop $KLIPPER_SERVICE_NAME
echo "[OK]"
}
uninstall()
{
if [ -d "${KLIPPER_PATH}/klippy/extras/led_effect" ]; then
echo -n "Uninstalling... "
rm -f "${KLIPPER_PATH}/klippy/extras/led_effect"
echo "[OK]"
echo "You can now remove the [update_manager led_effect] section in your moonraker.conf and delete this directory. Also remove all led_effect configurations from your Klipper configuration."
else
echo "led_effect not found in \"${KLIPPER_PATH}/klippy/extras/\". Is it installed?"
echo "[FAILED]"
fi
}
# Helper functions
verify_ready()
{
if [ "$EUID" -eq 0 ]; then
echo "[ERROR] This script must not run as root. Exiting."
exit 1
fi
}
# Run steps
[ $IGNORE_ROOT -ne 0 ] && verify_ready
[ $RESTART_SERVICE -eq 1 ] && check_klipper
check_folders
[ $RESTART_SERVICE -eq 1 ] && stop_klipper
if [ ! $UNINSTALL ]; then
link_extension
[ $UPDATE_MOONRAKER -eq 1 ] && add_updater
else
uninstall
fi
[ $RESTART_SERVICE -eq 1 ] && start_klipper