diff --git a/scripts/gcode_shell_command.sh b/scripts/gcode_shell_command.sh new file mode 100644 index 0000000..f8d88b2 --- /dev/null +++ b/scripts/gcode_shell_command.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +#=======================================================================# +# Copyright (C) 2020 - 2022 Dominik Willner # +# # +# This file is part of KIAUH - Klipper Installation And Update Helper # +# https://github.com/th33xitus/kiauh # +# # +# This file may be distributed under the terms of the GNU GPLv3 license # +#=======================================================================# + +set -e + +#=================================================# +#======== INSTALL GCODE_SHELL_COMMAND.PY =========# +#=================================================# + +function setup_gcode_shell_command(){ + top_border + echo -e "| You are about to install the G-Code Shell Command |" + echo -e "| extension. Please make sure to read the instructions |" + echo -e "| before you continue and remember that potential risks |" + echo -e "| can be involved after installing this extension! |" + blank_line + echo -e "| ${red}You accept that you are doing this on your own risk!${white} |" + bottom_border + while true; do + read -p "${cyan}###### Do you want to continue? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + if [ ! -d "${KLIPPER_DIR}/klippy/extras" ]; then + print_error "Folder ~/klipper/klippy/extras not found!\n Klipper not installed yet?" + return + fi + + status_msg "Installing gcode shell command extension ..." + if [ ! -f "${KLIPPER_DIR}/klippy/extras/gcode_shell_command.py" ]; then + install_gcode_shell_command + else + echo; warn_msg "File 'gcode_shell_command.py' already exists in the destination location!" + while true; do + read -p "${cyan}###### Do you want to overwrite it? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + rm -f "${KLIPPER_DIR}/klippy/extras/gcode_shell_command.py" + install_gcode_shell_command + break;; + N|n|No|no) + select_msg "No" + break;; + *) + error_msg "Invalid Input!";; + esac + done + fi + return;; + N|n|No|no) + select_msg "No" + return;; + *) + error_msg "Invalid Input!";; + esac + done +} + +function install_gcode_shell_command(){ + do_action_service "stop" "klipper" + status_msg "Copy 'gcode_shell_command.py' to '${KLIPPER_DIR}/klippy/extras' ..." + if cp "${SRCDIR}/kiauh/resources/gcode_shell_command.py" "${KLIPPER_DIR}/klippy/extras"; then + ok_msg "Done!" + else + error_msg "Cannot copy file to target destination...Exiting!" + return 1 + fi + while true; do + echo + read -p "${cyan}###### Create an example shell command? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + create_example_shell_command + break;; + N|n|No|no) + select_msg "No" + break;; + esac + done + do_action_service "restart" "klipper" + print_confirm "Shell command extension installed!" + return +} + +function create_example_shell_command() { + ### create a backup of the config folder + backup_klipper_config_dir + + local printer_cfgs + printer_cfgs=$(find "$(get_klipper_cfg_dir)" -type f -name "printer.cfg") + for cfg in ${printer_cfgs}; do + path=$(echo "${cfg}" | rev | cut -d"/" -f2- | rev) + if [ ! -f "${path}/shell_command.cfg" ]; then + status_msg "Copy shell_command.cfg to ${path} ..." + cp "${SRCDIR}/kiauh/resources/shell_command.cfg" "${path}" + ok_msg "${path}/shell_command.cfg created!" + ### write the include to the very first line of the printer.cfg + sed -i "1 i [include shell_command.cfg]" "${cfg}" + fi + done +} \ No newline at end of file diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 22e41f7..8e28231 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -400,97 +400,6 @@ function dependency_check(){ fi } -function setup_gcode_shell_command(){ - echo - top_border - echo -e "| You are about to install the G-Code Shell Command |" - echo -e "| extension. Please make sure to read the instructions |" - echo -e "| before you continue and remember that potential risks |" - echo -e "| can be involved after installing this extension! |" - blank_line - echo -e "| ${red}You accept that you are doing this on your own risk!${white} |" - bottom_border - while true; do - read -p "${cyan}###### Do you want to continue? (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - if [ -d "${KLIPPER_DIR}/klippy/extras" ]; then - status_msg "Installing gcode shell command extension ..." - if [ -f "${KLIPPER_DIR}/klippy/extras/gcode_shell_command.py" ]; then - warn_msg "There is already a file named 'gcode_shell_command.py'\nin the destination location!" - while true; do - read -p "${cyan}###### Do you want to overwrite it? (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - rm -f "${KLIPPER_DIR}/klippy/extras/gcode_shell_command.py" - install_gcode_shell_command - break;; - N|n|No|no) - break;; - esac - done - else - install_gcode_shell_command - fi - else - ERROR_MSG="Folder ~/klipper/klippy/extras not found!" - fi - break;; - N|n|No|no) - break;; - *) - print_unkown_cmd - print_msg && clear_msg;; - esac - done -} - -function install_gcode_shell_command(){ - do_action_service "stop" "klipper" - status_msg "Copy 'gcode_shell_command.py' to '${KLIPPER_DIR}/klippy/extras' ..." - cp "${SRCDIR}/kiauh/resources/gcode_shell_command.py" "${KLIPPER_DIR}/klippy/extras" - while true; do - echo - read -p "${cyan}###### Do you want to create the example shell command? (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - status_msg "Copy shell_command.cfg ..." - ### create a backup of the config folder - backup_klipper_config_dir - - ### handle single printer.cfg - if [ -f "${klipper_cfg_loc}/printer.cfg" ] && [ ! -f "${klipper_cfg_loc}/shell_command.cfg" ]; then - ### copy shell_command.cfg to config location - cp "${SRCDIR}/kiauh/resources/shell_command.cfg" "${klipper_cfg_loc}" - ok_msg "${klipper_cfg_loc}/shell_command.cfg created!" - - ### write the include to the very first line of the printer.cfg - sed -i "1 i [include shell_command.cfg]" "${klipper_cfg_loc}/printer.cfg" - fi - - ### handle multi printer.cfg - if ls "${klipper_cfg_loc}"/printer_* 2>/dev/null 1>&2; then - for config in $(find ${klipper_cfg_loc}/printer_*/printer.cfg); do - path=$(echo "${config}" | rev | cut -d"/" -f2- | rev) - if [ ! -f "${path}/shell_command.cfg" ]; then - ### copy shell_command.cfg to config location - cp "${SRCDIR}/kiauh/resources/shell_command.cfg" "${path}" - ok_msg "${path}/shell_command.cfg created!" - - ### write the include to the very first line of the printer.cfg - sed -i "1 i [include shell_command.cfg]" "${path}/printer.cfg" - fi - done - fi - break;; - N|n|No|no) - break;; - esac - done - ok_msg "Shell command extension installed!" - do_action_service "restart" "klipper" -} - function system_check_webui(){ ### check system for an installed and enabled octoprint service if sudo systemctl list-unit-files | grep -E "octoprint.*" | grep "enabled" &>/dev/null; then