-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
190 lines (155 loc) · 6.99 KB
/
install.sh
File metadata and controls
190 lines (155 loc) · 6.99 KB
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
#!/usr/bin/env bash
#############################################################################################
# #
# Blocks config update installer for RD50 Machines #
# #
#############################################################################################
#############################################################################################
# Based on Klippain, refer to https://github.com/Frix-x/klippain/blob/main/install.sh #
# #
# Which was originally written by yomgui1 & Frix_x All #
#############################################################################################
#############################################################################################
# #
# This script will backup and delete old configs #
# And create symlinks to new ones #
# #
#############################################################################################
umask 022
set -eu # Exit with error if any script return non-zero exit status and if variables are unset
function verify_ready() {
if [ "$EUID" -eq 0 ]; then
echo_error "This script must not be run as root!"
exit 255
fi
}
# Directories needed
USER_CONFIG_PATH="${HOME}/printer_data/config"
BACKUP_PATH="${HOME}/RD50_backup/"
RD50_Klipper_Configs_PATH="${HOME}/RD50-Klipper-Configs"
KLIPPER_PATH="${HOME}/klipper/"
BLOCKS_RD50_CONFIG_BRANCH="main"
Red='\033[0;31m'
Green='\033[0;32m'
Blue='\033[0;34m'
Cyan='\033[0;36m'
Normal='\033[0m'
function echo_info() {
printf "%s\n" "${Blue}$1${Normal}\n"
}
function echo_text() {
printf "%s\n" "${Normal}$1${Cyan}\n"
}
function echo_error() {
printf "%s\n" "${Red}$1${Normal}\n"
}
function echo_ok() {
printf "%s\n" "${Green}$1${Normal}\n"
}
function progress() {
echo -e "\n\n###### $1"
}
# Backup, compress the files and place them in a backup directory
function backup() {
if [ ! -e "${USER_CONFIG_PATH}" ]; then
echo_info "[BACKUP] No previous config found, skipping backup...\n\n"
return 1
fi
## Create directory, specify the new compressed file with the configurations
current_date=$(date +'Backup_%d-%m-%Y_%H_%M_%S')
mkdir -p "${BACKUP_PATH}" # If the directory already exists do not overwrite it.
if [ -d "${BACKUP_PATH}" ]; then
echo_ok "[BACKUP] Directory at: ${BACKUP_PATH}"
else
echo_error "[BACKUP] Error creating directory.... Aborting"
return 1
fi
## Remove all symlink files on the users config path
find "${USER_CONFIG_PATH}" -type l -exec rm -f {} \;
# Compress the config directory
echo_info "Compressing configuration files"
zip -qr "${BACKUP_PATH}/${current_date}.zip" "${USER_CONFIG_PATH}" # TODO Progress bar: | pv -bep -s "$(du -bs "${USER_CONFIG_PATH}")" | awk '{print $1}' >"${BACKUP_PATH}${current_date}.zip"
## Check if the backup file exists
if [ -f "${BACKUP_PATH}${current_date}.zip" ] && [ "$(du -bs "${BACKUP_PATH}${current_date}.zip" | cut -f1)" -gt "0" ]; then
echo_ok "[BACKUP] Backup successful, old user config files on ${BACKUP_PATH}${current_date}.zip \n"
## Wipe old configurations from the klipper config path
echo_info "Wiping old configuration files on the user config directory..."
rm -rf "${USER_CONFIG_PATH:?}"/{*,.*} # Delete all files and folders on this directory and hidden files as well
# Check if the directory is clean
if [ ! "$(ls -A "${USER_CONFIG_PATH}")" ]; then
echo_ok "Successfully Wiped users configurations"
return 0
else
echo_error "Unable to clean the directory"
return 1
fi
else
echo_error "Problem found while trying to wipe the printer config directory, zip file might not have been created... Exiting installation"
return 1
fi
}
function install_config() {
echo_info "[INSTALL] Installing new RD50 BLOCKS printer configuration."
## Check if a config folder exists
if [ -d "${USER_CONFIG_PATH}" ]; then
echo_ok "[INSTALL] User Klipper config path exists. Continuing..."
fi
# Iterate over all files in the update, and create symlinks for them
echo_ok "[INSTALL] adding new configuration files to Klippers printer_data directory"
for dir in *; do
if [ "$(basename "${dir}")" == "printer.cfg" ]; then
continue
fi
if [[ "$(create_symlink "${RD50_Klipper_Configs_PATH}/${dir}" "${USER_CONFIG_PATH}/${dir}")" -eq 1 ]]; then
echo_ok "\t\t [INSTALL] Symlink of ${RD50_Klipper_Configs_PATH}/${dir} created on ${USER_CONFIG_PATH}/${dir}"
else
echo_error "[INSTALL] ERROR: Unable to create symlink of ${RD50_Klipper_Configs_PATH}/${dir} on ${USER_CONFIG_PATH}/${dir}. Exiting installation!!!"
return 1
fi
done
#*# Copy the printer.cfg file to the directory| -f if an existing cannot be opened delete and try again | -n preserve attributes
cp -fp "${RD50_Klipper_Configs_PATH}/printer.cfg" "${USER_CONFIG_PATH}"
return 0
}
function create_symlink() {
local target_file=$1
local link_name=$2
# Create and check id the symlink was successfully created
if [[ "$(sudo ln -fs "${target_file}" "${link_name}")" -eq 0 ]]; then
# echo_ok "Symlink ${target_file} with ${link_name}."
return 0
else
# echo_error "Problem creating symlink of ${target_file} on ${link_name}. Exiting."
return 1
fi
}
function restart_klipper() {
echo_info "Restarting Klipper"
sudo systemctl restart klipper
}
function restart_moonraker() {
echo_info "Restarting Moonraker"
sudo systemctl restart moonraker
}
function install() {
printf "\n================================================\n"
echo_info "Initializing Blocks Configurations installation."
printf "\n================================================\n"
echo_info "Starting backup procedure of current configuration files\n\n"
backup
if [ $? -gt 1 ]; then # Check if the last command was executed with an exit status.
echo_error "[BACKUP] Unsuccessful, exiting Blocks configuration installation."
exit 1
fi
echo_info "Installing new configuration files to the user printer directory"
install_config
restart_klipper
restart_moonraker
# if [ -z "$START" ] || [ "$START" -eq 0 ]; then
# echo_ok "Blocks installation ended successfully."
# else
# echo_error "Fuck"
# fi
}
install
exit 0