|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +detect_printer_data() { |
| 4 | + ### Detects all directories in ${HOME} thet end in ´_data´ |
| 5 | + |
| 6 | + local printer_data |
| 7 | + local re='^.*_data/$' |
| 8 | + |
| 9 | + ### Loop over all directories in ${HOME} |
| 10 | + for dir in "${HOME}"/*/; do |
| 11 | + ### If the directory matches, add it to a list |
| 12 | + if [[ ${dir} =~ ${re} ]]; then |
| 13 | + printer_data+=("${dir}") |
| 14 | + fi |
| 15 | + done |
| 16 | + |
| 17 | + ### Return all found directories |
| 18 | + echo "${printer_data[*]}" |
| 19 | +} |
| 20 | + |
| 21 | +add_config_folder() { |
| 22 | + ### Add config folders to the list |
| 23 | + |
| 24 | + local input |
| 25 | + |
| 26 | + ### Auto detect printer_data folders |
| 27 | + data=$(detect_printer_data) |
| 28 | + |
| 29 | + ### Make the detected printer data folders an array |
| 30 | + mapfile -t auto_detected_dirs < <(echo "${data}" | tr ' ' "\n") |
| 31 | + |
| 32 | + ### List auto detected folders |
| 33 | + success_msg "Detected the following config directories:" |
| 34 | + for i in "${!auto_detected_dirs[@]}"; do |
| 35 | + echo -e "$((i + 1))) ${auto_detected_dirs[${i}]}" |
| 36 | + done |
| 37 | + |
| 38 | + ### Loop until user input is valid |
| 39 | + echo -e "${PURPLE}Which folder should be included in the backup?${NC}" |
| 40 | + echo -e "${PURPLE}Choose only one per prompt${NC}" |
| 41 | + echo -e "${PURPLE}'all' for all detected folders${NC}" |
| 42 | + echo -e "${PURPLE}'stop' to stop adding folders${NC}" |
| 43 | + while true; do |
| 44 | + ### Prompt user for input |
| 45 | + read -r -p "$(echo -e "${PURPLE}Add folder: ${NC}")" -i "all" -e input |
| 46 | + ### Validate user input |
| 47 | + case ${input} in |
| 48 | + [0-9]*) |
| 49 | + ### Add directory to list |
| 50 | + if [[ ${input} -le ${#auto_detected_dirs[@]} ]]; then |
| 51 | + IFS="%" |
| 52 | + if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${auto_detected_dirs[$((input - 1))]}${IFS} ]]; then |
| 53 | + warning_msg "Selected folder already exists in config" |
| 54 | + unset IFS |
| 55 | + else |
| 56 | + unset IFS |
| 57 | + CONFIG_FOLDER_LIST+=("${auto_detected_dirs[$((input - 1))]}") |
| 58 | + success_msg "Added ${auto_detected_dirs[$((input - 1))]}" |
| 59 | + fi |
| 60 | + else |
| 61 | + ### Invalid input |
| 62 | + deny_action |
| 63 | + fi |
| 64 | + ;; |
| 65 | + stop) |
| 66 | + ### Stop adding direcories |
| 67 | + break |
| 68 | + ;; |
| 69 | + all) |
| 70 | + for dir in "${auto_detected_dirs[@]}"; do |
| 71 | + IFS="%" |
| 72 | + if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${dir}${IFS} ]]; then |
| 73 | + warning_msg "Selected folder already exists in config" |
| 74 | + unset IFS |
| 75 | + else |
| 76 | + unset IFS |
| 77 | + CONFIG_FOLDER_LIST+=("${dir}") |
| 78 | + success_msg "Added ${dir}" |
| 79 | + fi |
| 80 | + done |
| 81 | + break |
| 82 | + ;; |
| 83 | + *) |
| 84 | + ### Invalid input |
| 85 | + deny_action |
| 86 | + ;; |
| 87 | + esac |
| 88 | + done && input="" |
| 89 | +} |
| 90 | + |
| 91 | +add_additional_dirs() { |
| 92 | + ### Add additional directories to config folder list |
| 93 | + |
| 94 | + local input |
| 95 | + |
| 96 | + ### Loop until user input is valid |
| 97 | + while true; do |
| 98 | + ### Prompt user for input |
| 99 | + input="" |
| 100 | + read -r -p "$(echo -e "${PURPLE}Do you want to add additional directories? [y|n] ${NC}")" -i "n" -e input |
| 101 | + ### Validate user input |
| 102 | + case ${input} in |
| 103 | + y | Y) |
| 104 | + ### Add directory to list |
| 105 | + input="" |
| 106 | + |
| 107 | + ### Prompt user for input |
| 108 | + read -r -p "$(echo -e "${PURPLE}Enter the path of config folder: ${NC}")" input |
| 109 | + ### Expand user input to full paths |
| 110 | + #! Only if the user didn't input a full path |
| 111 | + if ! echo "${input}" | grep -q "^/"; then |
| 112 | + ### Check if ´~´ was input and replace it with ${HOME} |
| 113 | + if echo "${input}" | grep -q "^~"; then |
| 114 | + input=${input/\~/${HOME}} |
| 115 | + else |
| 116 | + ### Relative path detected |
| 117 | + #! Prepend ${HOME} in the hope that the user set up klipper as a standard installation |
| 118 | + warning_msg "Relative path detected. Assuming relative to ${HOME}" |
| 119 | + input="${HOME}/${input}" |
| 120 | + fi |
| 121 | + fi |
| 122 | + |
| 123 | + ### Append input to the config folder list |
| 124 | + IFS="%" |
| 125 | + if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${input}${IFS} ]]; then |
| 126 | + warning_msg "Selected folder already exists in config" |
| 127 | + unset IFS |
| 128 | + else |
| 129 | + unset IFS |
| 130 | + CONFIG_FOLDER_LIST+=("${input}") |
| 131 | + success_msg "Added ${input}" |
| 132 | + fi |
| 133 | + |
| 134 | + ### Reset input to prevent duplicate entries |
| 135 | + input="" |
| 136 | + ;; |
| 137 | + n | N) |
| 138 | + ### Stop adding direcories |
| 139 | + break |
| 140 | + ;; |
| 141 | + *) |
| 142 | + ### Invalid input |
| 143 | + deny_action |
| 144 | + ;; |
| 145 | + esac |
| 146 | + done && input="" |
| 147 | +} |
| 148 | + |
| 149 | +remove_config_folder() { |
| 150 | + ### Removes config folders from list |
| 151 | + |
| 152 | + local input |
| 153 | + local del_dir |
| 154 | + |
| 155 | + ### Loop until user input is valid |
| 156 | + while true; do |
| 157 | + ### Print all current folders |
| 158 | + info_msg "The following folders are currently in config:" |
| 159 | + for i in "${!CONFIG_FOLDER_LIST[@]}"; do |
| 160 | + echo -e "$((i + 1))) ${CONFIG_FOLDER_LIST[${i}]}" |
| 161 | + done |
| 162 | + ### Prompt user for input |
| 163 | + echo -e "${PURPLE}'stop' to stop removing folders${NC}" |
| 164 | + read -r -p "$(echo -e "${PURPLE}Which folder do you want to remove? ${NC}")" input |
| 165 | + ### Validate user input |
| 166 | + case ${input} in |
| 167 | + [0-9]*) |
| 168 | + if [[ ${input} -le ${#auto_detected_dirs[@]} ]]; then |
| 169 | + ### Loop over all folders |
| 170 | + #! Skip input |
| 171 | + for i in "${!CONFIG_FOLDER_LIST[@]}"; do |
| 172 | + if [[ ${i} -ne $((input - 1)) ]]; then |
| 173 | + tmp_array+=("${CONFIG_FOLDER_LIST[${i}]}") |
| 174 | + else |
| 175 | + del_dir="${CONFIG_FOLDER_LIST[${i}]}" |
| 176 | + fi |
| 177 | + done |
| 178 | + CONFIG_FOLDER_LIST=("${tmp_array[@]}") |
| 179 | + success_msg "Removed ${del_dir} from backed up folders" |
| 180 | + else |
| 181 | + ### Invalid input |
| 182 | + deny_action |
| 183 | + fi |
| 184 | + ;; |
| 185 | + stop) |
| 186 | + break |
| 187 | + ;; |
| 188 | + *) |
| 189 | + ### Invalid input |
| 190 | + deny_action |
| 191 | + ;; |
| 192 | + esac |
| 193 | + done && input="" |
| 194 | +} |
| 195 | + |
| 196 | +config_folders() { |
| 197 | + ### Configure folders to be backed up |
| 198 | + |
| 199 | + local input |
| 200 | + |
| 201 | + ### Loop until user input is valid |
| 202 | + while true; do |
| 203 | + ### Prompt user for input |
| 204 | + read -r -p "$(echo -e "${PURPLE}Do you want to add or remove folders from the config? ${NC}")" -i "add" -e input |
| 205 | + ### Validate user input |
| 206 | + case ${input} in |
| 207 | + add) |
| 208 | + add_config_folder |
| 209 | + add_additional_dirs |
| 210 | + break |
| 211 | + ;; |
| 212 | + remove) |
| 213 | + remove_config_folder |
| 214 | + break |
| 215 | + ;; |
| 216 | + *) |
| 217 | + ### Invalid input |
| 218 | + deny_action |
| 219 | + ;; |
| 220 | + esac |
| 221 | + done && input="" |
| 222 | +} |
0 commit comments