-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
197 lines (171 loc) · 6.86 KB
/
install.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
#!/usr/bin/env bash
#################################################
###### AUTOMATED INSTALL AND UPDATE SCRIPT ######
#################################################
# Written by yomgui1 & Frix_x
# @version: 1.0
# CHANGELOG:
# v1.0: first version of the script to allow a peaceful install and update ;)
# Where the user Klipper config is located (ie. the one used by Klipper to work)
KLIPPER_PATH="${HOME}/klipper"
KLIPPER_CONFIG_PATH="${HOME}/klipper_config"
USER_CONFIG_PATH="$(realpath -e ${HOME}/printer_data/config)"
# Where to clone Frix-x repository config files (read-only and keep untouched)
FRIX_CONFIG_PATH="${HOME}/frix-x_config"
# Where to clone ERCF-Software-V3 repository config files (read-only and keep untouched)
ERCF_SOFTWARE_V3_PATH="${HOME}/ERCF-Software-V3"
# Path used to store backups when updating (backups are automatically dated when saved inside)
BACKUP_PATH="${HOME}/frix-x_config_backups"
set -eu
export LC_ALL=C
function prompt_yn() {
while true; do
read -p "$@ (y/n)?" yn
case "${yn}" in
Y|y|Yes|yes)
echo "y"
break;;
N|n|No|no)
echo "n"
break;;
*)
echo "Please answer yes or no.";;
esac
done
}
INSTALL_ERCF=0
while getopts e arg; do
case $arg in
e) INSTALL_ERCF=1;;
esac
done
# Step 1: Verify that the script is not run as root and Klipper is installed
function preflight_checks {
if [ "$EUID" -eq 0 ]; then
echo "This script must not be run as root"
exit -1
fi
if [ "$(sudo systemctl list-units --full -all -t service --no-legend | grep -F 'klipper.service')" ]; then
echo "Klipper service found! Continuing..."
else
echo "Klipper service not found, please install Klipper first!"
exit -1
fi
if [ ! -d "${KLIPPER_PATH}" ]; then
echo -e "${ERROR}Klipper home directory (${KLIPPER_PATH}) not found."
exit -1
fi
if [ ! -d "${KLIPPER_CONFIG_PATH}" ]; then
if [ ! -d "${USER_CONFIG_PATH}" ]; then
echo -e "Klipper config directory (${KLIPPER_CONFIG_PATH} or ${USER_CONFIG_PATH}) not found."
exit -1
fi
KLIPPER_CONFIG_PATH="${USER_CONFIG_PATH}"
fi
}
# Step 2: Check if the git configs folders exist (or download it) if necessary
function check_download {
local frixtemppath frixreponame ercfrepopath ercfreponame
frixtemppath="$(dirname ${FRIX_CONFIG_PATH})"
frixreponame="$(basename ${FRIX_CONFIG_PATH})"
ercfrepopath="$(dirname ${ERCF_SOFTWARE_V3_PATH})"
ercfreponame="$(basename ${ERCF_SOFTWARE_V3_PATH})"
if [ ! -d "${FRIX_CONFIG_PATH}" ]; then
echo "Downloading Frix-x configuration folder..."
if git -C $frixtemppath clone https://github.com/Benoitone/klipper-voron-v3.git $frixreponame; then
chmod +x ${FRIX_CONFIG_PATH}/install.sh
echo "Download complete!"
else
echo "Download of Frix-x configuration git repository failed!"
exit -1
fi
else
echo "Frix-x git repository folder found locally!"
fi
if [ "${INSTALL_ERCF}" -eq 1 ]; then
echo
yn=$(prompt_yn "Do you have an ERCF and do you want to use ERCF Software V3?")
case $yn in
y)
if [ ! -d "${ERCF_SOFTWARE_V3_PATH}" ]; then
echo "Downloading ERCF Software V3 configuration folder..."
if git -C $ercfrepopath clone https://github.com/moggieuk/ERCF-Software-V3.git $ercfreponame; then
echo "Download complete!"
else
echo "Download of ERCF Software V3 configuration git repository failed!"
exit -1
fi
else
echo "ERCF Software V3 git repository folder found locally!"
fi
;;
n)
;;
esac
fi
}
# Step 3: Backup the old Klipper configuration
function backup_config {
mkdir -p ${BACKUP_DIR}
if [ -f "${USER_CONFIG_PATH}/.VERSION" ]; then
echo "Frix-x configuration already in use: only a backup of the custom user cfg files is needed"
find ${USER_CONFIG_PATH} -type f -regex '.*\.\(cfg\|conf\|VERSION\)' | xargs mv -t ${BACKUP_DIR}/ 2>/dev/null
else
echo "New installation detected: a full backup of the user config folder is needed"
cp -fa ${USER_CONFIG_PATH} ${BACKUP_DIR}
fi
echo "Backup done in: ${BACKUP_DIR}"
}
# Step 4: Put the new configuration files in place to be ready to start
function install_config {
echo "Installation of the last Frix-x Klipper configuration files"
mkdir -p ${USER_CONFIG_PATH}
# Symlink Frix-x config folders (read-only git repository) to the user's config directory
for dir in config macros scripts moonraker; do
ln -fsn ${FRIX_CONFIG_PATH}/$dir ${USER_CONFIG_PATH}/$dir
done
# Copy custom user's config files from the last backup to restore them to their config directory (or install templates if it's a first install)
if [ -f "${BACKUP_DIR}/.VERSION" ]; then
echo "Update done: restoring user config files now!"
find ${BACKUP_DIR} -type f -regex '.*\.\(cfg\|conf\)' | xargs cp -ft ${USER_CONFIG_PATH}/
else
echo "New installation detected: default config templates will be set in place!"
cp -fa ${FRIX_CONFIG_PATH}/user_templates/* ${USER_CONFIG_PATH}/
fi
# CHMOD the scripts to be sure they are all executables (Git should keep the modes on files but it's to be sure)
chmod +x ${FRIX_CONFIG_PATH}/install.sh
for file in graph_vibrations.py plot_graphs.sh; do
chmod +x ${FRIX_CONFIG_PATH}/scripts/$file
done
# Create the config version tracking file in the user config directory
git -C ${FRIX_CONFIG_PATH} rev-parse HEAD > ${USER_CONFIG_PATH}/.VERSION
}
# Step 5: Put the ercf plugin files in place to be ready to use if ERCF Software V3
function link_ercf_plugin() {
if [ "${INSTALL_ERCF}" -eq 1 ]; then
if [ -d "${ERCF_SOFTWARE_V3_PATH}" ]; then
echo -e "Linking ercf extension to Klipper..."
if [ -d "${KLIPPER_PATH}/klippy/extras" ]; then
for file in `cd ${ERCF_SOFTWARE_V3_PATH}/extras ; ls *.py`; do
ln -sf "${ERCF_SOFTWARE_V3_PATH}/extras/${file}" "${KLIPPER_PATH}/klippy/extras/${file}"
done
else
echo -e "ERCF modules not installed because Klipper 'extras' directory not found!"
fi
fi
fi
}
# Step 6: restarting Klipper
function restart_klipper {
echo "Restarting Klipper..."
sudo systemctl restart klipper
}
BACKUP_DIR="${BACKUP_PATH}/config_$(date +'%Y%m%d%H%M%S')"
# Run steps
preflight_checks
check_download
backup_config
install_config
link_ercf_plugin
restart_klipper
echo "Everything is ok, Frix-x config installed and up to date!"