-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall-smuff-sonic-pad.sh
235 lines (215 loc) · 8.21 KB
/
install-smuff-sonic-pad.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
#----------------------------------------------
# SMuFF on Klipper installation script
# for Creality Sonic Pad
#
#----------------------------------------------
# Force script to exit if an error occurs
set -e
# Common runtime vars
HOME_PATH="/mnt/UDISK"
KLIPPER_PATH="/usr/share/klipper"
KLIPPER_CONFIG_PATH="${HOME_PATH}" # old config folder; may be replaced by query_instance()
MOONRAKER_CONFIG="${KLIPPER_CONFIG_PATH}/moonraker.conf" # old config folder; may be replaced by query_instance()
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # source directory taken from the pathname of this script
ICONDIR="${SRCDIR}/KlipperScreen-images" # source directory for KlipperScreen images
DEFAULT_INSTANCE="printer_config" # default single printer instance folder name
LOGS_PATH="${DEFAULT_INSTANCE}/logs" # default path for Klipper logfiles
SMUFF_LOG_FILE="/var/tmp/smuff.log" # default path for SMuFF logfile
KLIPPERSCREEN_PATH="${HOME_PATH}/KlipperScreen" # path for KlipperScreen
KS_THEMES_PATH="${KLIPPERSCREEN_PATH}/styles" # path for KlipperScreen themes
CONFIG_FOLDERS="${KLIPPER_CONFIG_PATH}/printer_config*/" # folder names for printer configs
# Module specific runtime vars
MODULES=("smuff.py" "smuff_core.py") # modules to symlink
CONFIGS=("smuff.cfg" "smuff_menu.cfg" "smuff_menu_ks.cfg" "smuff_runout.cfg") # config-files to copy
SYMLINK="${KLIPPER_PATH}/klippy/extras/" # symlink folder
ICONS=("smuff.svg" "close_lid.svg" "cut_filament.svg" "instance_a.svg" "instance_b.svg" "load_filament.svg" "open_lid.svg" "purge_filament.svg" "reset.svg" "swap_tools.svg" "unjam.svg" "unload_filament.svg" "wipe_nozzle.svg") # image-files to copy
THEMES=("colorized" "material-dark" "material-darker" "material-light" "z-bolt") # default KlipperScreen theme folders
ALREADY_EXISTS="already exists!"
UPDATE_INFO="${ALREADY_EXISTS} Please update its contents manually if needed."
UPDATE_INFO_ICON="${ALREADY_EXISTS} Please copy it manually if needed."
# Some colors for highlighting text
WHITE='\e[0;37m'
CYAN='\e[1;36m'
RED='\e[1;31m'
GREEN='\e[1;32m'
WARNING='\e[41m\e[1;37m'
# List printer instances
parse_instances()
{
printf "\nKlipper Printer Instances found:"
printf "\n--------------------------------\n"
instances=()
ndx=1
default=0
for entry in $(ls -d ${CONFIG_FOLDERS} | cut -f4 -d'/') ; do
printf "${CYAN}%d: %s${WHITE}\n" $ndx $entry
instances+=($entry)
if [ $entry == $DEFAULT_INSTANCE ] ; then
default=$ndx
fi
((ndx+=1))
done
while true
do
printf "\n"
read -e -p "Enter the instance number in which to install SMuFF: " -i "${default}" instance
if [[ $instance > 0 && $instance < $ndx ]] ; then
KLIPPER_CONFIG_PATH="${HOME_PATH}/${instances[$instance-1]}"
MOONRAKER_CONFIG="${KLIPPER_CONFIG_PATH}/moonraker.conf"
LOGS_PATH="${KLIPPER_CONFIG_PATH//_config/_logs}"
break
else
echo -e "${RED}#${instance} is not contained in the list. Try again or use ^C to abort.${WHITE}"
fi
done
}
# Query path to printer instance
query_instance()
{
while true ; do
parse_instances
printf "\nThe target folder will be: ${GREEN}%s${WHITE}.\n" ${KLIPPER_CONFIG_PATH}
read -e -p "Is this correct (y/n)? " -i "y" answer
if [ $answer == "Y" ] || [ $answer == "y" ] ; then
echo -e "${GREEN}Ok.${WHITE}"
break
fi
done
}
# Create symlinks in Klipper 'extras' folder
link_extensions()
{
for mod in ${MODULES[@]} ; do
if [ ! -L ${SYMLINK}${mod} ] && [ -e ${SYMLINK} ] ; then
echo "Creating symlink for file ${mod} in ${SYMLINK} ..."
ln -sf "${SRCDIR}/${mod}" "${SYMLINK}${mod}"
else
echo -e "${RED}Link for '${mod}' already exists and is valid, skipping symlink creation.${WHITE}"
fi
chmod 644 "${SRCDIR}/${mod}"
done
}
# Remove symlinks from Klipper 'extras' folder
unlink_extensions()
{
for mod in ${MODULES[@]} ; do
if [ -L ${SYMLINK}${mod} ] ; then
echo "Removing symlink for file ${mod} in ${SYMLINK} ..."
rm "${SYMLINK}${mod}"
else
echo -e "${RED}Link for '${mod}' does not exist.${WHITE}"
fi
done
}
# Create symlink in printer 'logs' folder
link_logfile()
{
if [ ! -L ${LOGS_PATH}/smuff.log ] ; then
echo "*Created*" > "${SMUFF_LOG_FILE}"
echo "Creating symlink for logfile ..."
ln -sf "${SMUFF_LOG_FILE}" "${LOGS_PATH}/smuff.log"
else
echo -e "${RED}Link for 'smuff.log' already exists and is valid, skipping symlink creation.${WHITE}"
fi
}
# Remove symlink in printer 'logs' folder
unlink_logfile()
{
if [ -L ${LOGS_PATH}/smuff.log ] ; then
echo "Removing symlink for logfile ..."
rm "${LOGS_PATH}/smuff.log"
else
echo -e "${RED}Link for 'smuff.log' does not exist.${WHITE}"
fi
}
# Copy config files into the Klipper configs folder
copy_configs()
{
for cfg in ${CONFIGS[@]} ; do
if [ -e "${KLIPPER_CONFIG_PATH}/${cfg}" ] ; then
echo -e "${CYAN}File '${cfg}' ${UPDATE_INFO}${WHITE}"
else
echo "Copying ${SRCDIR}/${cfg} to ${KLIPPER_CONFIG_PATH}/${cfg} ..."
cp "${SRCDIR}/${cfg}" "${KLIPPER_CONFIG_PATH}/"
chmod 755 "${KLIPPER_CONFIG_PATH}/${cfg}"
fi
done
}
# Copy image files into the KlipperScreen styles folders
copy_images()
{
if [ -e "${KLIPPERSCREEN_PATH}" ] ; then
for theme in ${THEMES[@]} ; do
if [ -e "${KS_THEMES_PATH}/${theme}/" ] ; then
for icon in ${ICONS[@]} ; do
if [ -e "${KS_THEMES_PATH}/${theme}/${icon}" ] ; then
echo -e "${CYAN}File '${icon}' ${UPDATE_INFO_ICON}${WHITE}"
else
echo "Copying ${ICONDIR}/${icon} to ${KS_THEMES_PATH}/${theme}/${icon} ..."
cp "${ICONDIR}/${icon}" "${KS_THEMES_PATH}/${theme}/"
chmod 644 "${KS_THEMES_PATH}/${theme}/${icon}"
fi
done
fi
done
else
echo -e "${CYAN}KlipperScreen folder not found, skipping copying images!${WHITE}"
fi
}
# Delete config files from the Klipper configs folder
delete_configs()
{
for cfg in ${CONFIGS[@]} ; do
if [ -e "${KLIPPER_CONFIG_PATH}/${cfg}" ] ; then
echo "Removing ${KLIPPER_CONFIG_PATH}/${cfg} ..."
rm "${KLIPPER_CONFIG_PATH}/${cfg}"
fi
done
}
# Add updater to moonraker.conf
add_updater()
{
section=$(grep -c '\[update_manager SMuFF\]' ${MOONRAKER_CONFIG} || true)
if [ "${section}" -eq 0 ] ; then
echo "Adding SMuFF update manager section to Moonraker..."
while read -r line; do
echo "${line}" >> "${MOONRAKER_CONFIG}"
done < "${SRCDIR}/moonraker_update_manager.txt"
else
echo -e "${RED}SMuFF update manager section ${UPDATE_INFO}${WHITE}"
fi
}
# Execute steps sequentially
force_uninst=0
if [ -n "$1" ] && [ $1 == "uninstall" ] ; then
printf "\n${WARNING}*** WARNING! This script will remove all SMuFF files. ***${WHITE}\n\n"
printf "\n${CYAN}Do you want to proceed (y/n)? ${WHITE}"
read -e -i "n" answer
if [ $answer == "N" ] || [ $answer == "n" ] ; then
echo -e "${GREEN}Aborted.${WHITE}"
exit -1
fi
force_uninst=1
fi
query_instance
if [ $force_uninst == 1 ] ; then
unlink_extensions
unlink_logfile
delete_configs
cd ${HOME_PATH}
printf "\n${CYAN}Do you want me to delete the installation folder ${SRCDIR} (y/n)? ${WHITE}"
read -e -i "n" answer
if [ $answer == "Y" ] || [ $answer == "y" ] ; then
rm -r -f ${SRCDIR}
rmdir ${SRCDIR}
fi
printf "\n${GREEN}Done.\n\n${CYAN}Don't forget to remove the updater in moonraker.conf manually!${WHITE}\n\n\n"
else
link_extensions
link_logfile
copy_configs
copy_images
add_updater
printf "\n${GREEN}Done.\n\n${CYAN}Don't forget to edit the 'smuff.cfg' file before you restart the firmware!${WHITE}\n\n\n"
fi